2018-02-16 22:15:55 +08:00
|
|
|
//===--- Headers.h - Include headers -----------------------------*- C++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-02-16 22:15:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
|
|
|
|
|
2018-05-14 20:19:16 +08:00
|
|
|
#include "Protocol.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "SourceCode.h"
|
2019-02-28 21:23:03 +08:00
|
|
|
#include "index/Symbol.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Path.h"
|
2021-10-04 14:39:06 +08:00
|
|
|
#include "clang/Basic/FileEntry.h"
|
2020-04-16 04:00:19 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2021-11-26 21:12:35 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2021-12-08 22:55:39 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2018-06-04 17:04:28 +08:00
|
|
|
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
|
2022-02-09 18:04:58 +08:00
|
|
|
#include "clang/Tooling/Inclusions/StandardLibrary.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2021-11-26 21:12:35 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2021-09-30 20:41:27 +08:00
|
|
|
#include "llvm/Support/FileSystem/UniqueID.h"
|
2020-04-16 04:00:19 +08:00
|
|
|
#include <string>
|
2018-02-16 22:15:55 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-02-20 02:48:44 +08:00
|
|
|
|
2018-02-26 16:32:13 +08:00
|
|
|
/// Returns true if \p Include is literal include like "path" or <path>.
|
|
|
|
bool isLiteralInclude(llvm::StringRef Include);
|
|
|
|
|
2022-10-06 04:23:10 +08:00
|
|
|
/// If Text begins an Include-What-You-Use directive, returns it.
|
|
|
|
/// Given "// IWYU pragma: keep", returns "keep".
|
|
|
|
/// Input is a null-terminated char* as provided by SM.getCharacterData().
|
|
|
|
/// (This should not be StringRef as we do *not* want to scan for its length).
|
|
|
|
llvm::Optional<StringRef> parseIWYUPragma(const char *Text);
|
|
|
|
|
2018-02-26 16:32:13 +08:00
|
|
|
/// Represents a header file to be #include'd.
|
|
|
|
struct HeaderFile {
|
|
|
|
std::string File;
|
|
|
|
/// If this is true, `File` is a literal string quoted with <> or "" that
|
|
|
|
/// can be #included directly; otherwise, `File` is an absolute file path.
|
|
|
|
bool Verbatim;
|
|
|
|
|
|
|
|
bool valid() const;
|
|
|
|
};
|
|
|
|
|
2019-01-28 22:01:55 +08:00
|
|
|
/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
|
|
|
|
/// include.
|
|
|
|
llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header,
|
|
|
|
llvm::StringRef HintPath);
|
|
|
|
|
|
|
|
// Returns include headers for \p Sym sorted by popularity. If two headers are
|
|
|
|
// equally popular, prefer the shorter one.
|
|
|
|
llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym);
|
|
|
|
|
2018-05-14 20:19:16 +08:00
|
|
|
// An #include directive that we found in the main file.
|
|
|
|
struct Inclusion {
|
2020-04-16 04:00:19 +08:00
|
|
|
tok::PPKeywordKind Directive; // Directive used for inclusion, e.g. import
|
|
|
|
std::string Written; // Inclusion name as written e.g. <vector>.
|
|
|
|
Path Resolved; // Resolved path of included file. Empty if not resolved.
|
2018-11-20 18:58:48 +08:00
|
|
|
unsigned HashOffset = 0; // Byte offset from start of file to #.
|
2020-05-04 16:48:19 +08:00
|
|
|
int HashLine = 0; // Line number containing the directive, 0-indexed.
|
2018-11-20 18:58:48 +08:00
|
|
|
SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
|
2021-10-06 00:08:00 +08:00
|
|
|
llvm::Optional<unsigned> HeaderID;
|
2021-12-08 22:55:39 +08:00
|
|
|
bool BehindPragmaKeep = false; // Has IWYU pragma: keep right after.
|
2018-05-14 20:19:16 +08:00
|
|
|
};
|
2018-12-01 00:59:00 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &);
|
2020-04-23 23:44:51 +08:00
|
|
|
bool operator==(const Inclusion &LHS, const Inclusion &RHS);
|
2018-05-14 20:19:16 +08:00
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
// Contains information about one file in the build graph and its direct
|
2018-11-28 00:08:53 +08:00
|
|
|
// dependencies. Doesn't own the strings it references (IncludeGraph is
|
|
|
|
// self-contained).
|
|
|
|
struct IncludeGraphNode {
|
2019-07-04 17:52:04 +08:00
|
|
|
enum class SourceFlag : uint8_t {
|
|
|
|
None = 0,
|
|
|
|
// Whether current file is a main file rather than a header.
|
|
|
|
IsTU = 1 << 0,
|
|
|
|
// Whether current file had any uncompilable errors during indexing.
|
|
|
|
HadErrors = 1 << 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
SourceFlag Flags = SourceFlag::None;
|
2018-11-28 00:08:53 +08:00
|
|
|
llvm::StringRef URI;
|
2018-12-04 22:07:29 +08:00
|
|
|
FileDigest Digest{{0}};
|
2018-11-28 00:08:53 +08:00
|
|
|
std::vector<llvm::StringRef> DirectIncludes;
|
|
|
|
};
|
|
|
|
// FileURI and FileInclusions are references to keys of the map containing
|
|
|
|
// them.
|
2018-12-01 00:59:00 +08:00
|
|
|
// Important: The graph generated by those callbacks might contain cycles, self
|
|
|
|
// edges and multi edges.
|
2018-11-28 00:08:53 +08:00
|
|
|
using IncludeGraph = llvm::StringMap<IncludeGraphNode>;
|
|
|
|
|
2019-07-04 17:52:04 +08:00
|
|
|
inline IncludeGraphNode::SourceFlag operator|(IncludeGraphNode::SourceFlag A,
|
|
|
|
IncludeGraphNode::SourceFlag B) {
|
|
|
|
return static_cast<IncludeGraphNode::SourceFlag>(static_cast<uint8_t>(A) |
|
|
|
|
static_cast<uint8_t>(B));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator&(IncludeGraphNode::SourceFlag A,
|
|
|
|
IncludeGraphNode::SourceFlag B) {
|
|
|
|
return static_cast<uint8_t>(A) & static_cast<uint8_t>(B);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline IncludeGraphNode::SourceFlag &
|
|
|
|
operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B) {
|
|
|
|
return A = A | B;
|
|
|
|
}
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
// Information captured about the inclusion graph in a translation unit.
|
|
|
|
// This includes detailed information about the direct #includes, and summary
|
|
|
|
// information about all transitive includes.
|
|
|
|
//
|
|
|
|
// It should be built incrementally with collectIncludeStructureCallback().
|
|
|
|
// When we build the preamble, we capture and store its include structure along
|
|
|
|
// with the preamble data. When we use the preamble, we can copy its
|
|
|
|
// IncludeStructure and use another collectIncludeStructureCallback() to fill
|
|
|
|
// in any non-preamble inclusions.
|
|
|
|
class IncludeStructure {
|
|
|
|
public:
|
2021-09-30 20:41:27 +08:00
|
|
|
IncludeStructure() {
|
|
|
|
// Reserve HeaderID = 0 for the main file.
|
|
|
|
RealPathNames.emplace_back();
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:55:39 +08:00
|
|
|
// Inserts a PPCallback and CommentHandler that visits all includes in the
|
|
|
|
// main file and populates the structure. It will also scan for IWYU pragmas
|
|
|
|
// in comments.
|
|
|
|
void collect(const CompilerInstance &CI);
|
2021-10-04 14:39:06 +08:00
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
// HeaderID identifies file in the include graph. It corresponds to a
|
|
|
|
// FileEntry rather than a FileID, but stays stable across preamble & main
|
|
|
|
// file builds.
|
|
|
|
enum class HeaderID : unsigned {};
|
|
|
|
|
2021-10-04 14:39:06 +08:00
|
|
|
llvm::Optional<HeaderID> getID(const FileEntry *Entry) const;
|
2022-04-11 18:10:05 +08:00
|
|
|
HeaderID getOrCreateID(FileEntryRef Entry);
|
2021-09-30 20:41:27 +08:00
|
|
|
|
|
|
|
StringRef getRealPath(HeaderID ID) const {
|
|
|
|
assert(static_cast<unsigned>(ID) <= RealPathNames.size());
|
|
|
|
return RealPathNames[static_cast<unsigned>(ID)];
|
|
|
|
}
|
2018-07-03 16:09:29 +08:00
|
|
|
|
2021-11-26 21:12:35 +08:00
|
|
|
bool isSelfContained(HeaderID ID) const {
|
|
|
|
return !NonSelfContained.contains(ID);
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:13:38 +08:00
|
|
|
bool hasIWYUExport(HeaderID ID) const {
|
2022-05-16 16:17:40 +08:00
|
|
|
return HasIWYUExport.contains(ID);
|
2022-05-16 16:13:38 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 09:43:53 +08:00
|
|
|
// Return all transitively reachable files.
|
|
|
|
llvm::ArrayRef<std::string> allHeaders() const { return RealPathNames; }
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
// Return all transitively reachable files, and their minimum include depth.
|
|
|
|
// All transitive includes (absolute paths), with their minimum include depth.
|
|
|
|
// Root --> 0, #included file --> 1, etc.
|
2021-09-30 20:41:27 +08:00
|
|
|
// Root is the ID of the header being visited first.
|
2021-10-04 14:39:06 +08:00
|
|
|
llvm::DenseMap<HeaderID, unsigned>
|
|
|
|
includeDepth(HeaderID Root = MainFileID) const;
|
2021-09-30 14:25:31 +08:00
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
// Maps HeaderID to the ids of the files included from it.
|
|
|
|
llvm::DenseMap<HeaderID, SmallVector<HeaderID>> IncludeChildren;
|
|
|
|
|
2022-02-09 18:04:58 +08:00
|
|
|
llvm::DenseMap<tooling::stdlib::Header, llvm::SmallVector<HeaderID>>
|
|
|
|
StdlibHeaders;
|
2021-11-17 20:27:58 +08:00
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
std::vector<Inclusion> MainFileIncludes;
|
2021-09-30 15:49:27 +08:00
|
|
|
|
2021-10-04 14:39:06 +08:00
|
|
|
// We reserve HeaderID(0) for the main file and will manually check for that
|
|
|
|
// in getID and getOrCreateID because the UniqueID is not stable when the
|
|
|
|
// content of the main file changes.
|
|
|
|
static const HeaderID MainFileID = HeaderID(0u);
|
|
|
|
|
2021-11-26 21:12:35 +08:00
|
|
|
class RecordHeaders;
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
private:
|
2021-10-04 14:39:06 +08:00
|
|
|
// MainFileEntry will be used to check if the queried file is the main file
|
|
|
|
// or not.
|
|
|
|
const FileEntry *MainFileEntry = nullptr;
|
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
std::vector<std::string> RealPathNames; // In HeaderID order.
|
2021-10-04 14:39:06 +08:00
|
|
|
// FileEntry::UniqueID is mapped to the internal representation (HeaderID).
|
2018-07-03 16:09:29 +08:00
|
|
|
// Identifying files in a way that persists from preamble build to subsequent
|
2021-10-04 14:39:06 +08:00
|
|
|
// builds is surprisingly hard. FileID is unavailable in InclusionDirective(),
|
|
|
|
// and RealPathName and UniqueID are not preserved in
|
2021-09-30 20:41:27 +08:00
|
|
|
// the preamble.
|
|
|
|
llvm::DenseMap<llvm::sys::fs::UniqueID, HeaderID> UIDToIndex;
|
2021-11-26 21:12:35 +08:00
|
|
|
// Contains HeaderIDs of all non self-contained entries in the
|
|
|
|
// IncludeStructure.
|
|
|
|
llvm::DenseSet<HeaderID> NonSelfContained;
|
2022-05-16 16:13:38 +08:00
|
|
|
// Contains a set of headers that have either "IWYU pragma: export" or "IWYU
|
|
|
|
// pragma: begin_exports".
|
2022-05-16 16:17:40 +08:00
|
|
|
llvm::DenseSet<HeaderID> HasIWYUExport;
|
2018-07-03 16:09:29 +08:00
|
|
|
};
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
// Calculates insertion edit for including a new header in a file.
|
|
|
|
class IncludeInserter {
|
|
|
|
public:
|
2019-04-11 17:36:36 +08:00
|
|
|
// If \p HeaderSearchInfo is nullptr (e.g. when compile command is
|
|
|
|
// infeasible), this will only try to insert verbatim headers, and
|
|
|
|
// include path of non-verbatim header will not be shortened.
|
2018-05-15 23:29:32 +08:00
|
|
|
IncludeInserter(StringRef FileName, StringRef Code,
|
|
|
|
const format::FormatStyle &Style, StringRef BuildDir,
|
2019-04-11 17:36:36 +08:00
|
|
|
HeaderSearch *HeaderSearchInfo)
|
2018-05-15 23:29:32 +08:00
|
|
|
: FileName(FileName), Code(Code), BuildDir(BuildDir),
|
|
|
|
HeaderSearchInfo(HeaderSearchInfo),
|
|
|
|
Inserter(FileName, Code, Style.IncludeStyle) {}
|
|
|
|
|
2018-09-27 22:27:02 +08:00
|
|
|
void addExisting(const Inclusion &Inc);
|
2018-05-15 23:29:32 +08:00
|
|
|
|
2018-06-15 21:34:18 +08:00
|
|
|
/// Checks whether to add an #include of the header into \p File.
|
|
|
|
/// An #include will not be added if:
|
|
|
|
/// - Either \p DeclaringHeader or \p InsertedHeader is already (directly)
|
|
|
|
/// in \p Inclusions (including those included via different paths).
|
|
|
|
/// - \p DeclaringHeader or \p InsertedHeader is the same as \p File.
|
|
|
|
///
|
2019-04-16 22:35:49 +08:00
|
|
|
/// \param DeclaringHeader is path of the original header corresponding to \p
|
2018-06-15 21:34:18 +08:00
|
|
|
/// InsertedHeader e.g. the header that declares a symbol.
|
|
|
|
/// \param InsertedHeader The preferred header to be inserted. This could be
|
2018-06-27 01:00:43 +08:00
|
|
|
/// the same as DeclaringHeader but must be provided.
|
2019-04-16 22:35:49 +08:00
|
|
|
bool shouldInsertInclude(PathRef DeclaringHeader,
|
2018-06-15 21:34:18 +08:00
|
|
|
const HeaderFile &InsertedHeader) const;
|
|
|
|
|
|
|
|
/// Determines the preferred way to #include a file, taking into account the
|
|
|
|
/// search path. Usually this will prefer a shorter representation like
|
|
|
|
/// 'Foo/Bar.h' over a longer one like 'Baz/include/Foo/Bar.h'.
|
2018-05-15 23:29:32 +08:00
|
|
|
///
|
2019-07-09 02:07:46 +08:00
|
|
|
/// \param InsertedHeader The preferred header to be inserted.
|
2018-06-15 21:34:18 +08:00
|
|
|
///
|
2019-07-03 15:47:19 +08:00
|
|
|
/// \param IncludingFile is the absolute path of the file that InsertedHeader
|
|
|
|
/// will be inserted.
|
|
|
|
///
|
2019-07-09 02:07:46 +08:00
|
|
|
/// \return A quoted "path" or <path> to be included, or None if it couldn't
|
|
|
|
/// be shortened.
|
|
|
|
llvm::Optional<std::string>
|
|
|
|
calculateIncludePath(const HeaderFile &InsertedHeader,
|
|
|
|
llvm::StringRef IncludingFile) const;
|
2018-06-15 21:34:18 +08:00
|
|
|
|
|
|
|
/// Calculates an edit that inserts \p VerbatimHeader into code. If the header
|
|
|
|
/// is already included, this returns None.
|
|
|
|
llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) const;
|
2018-05-15 23:29:32 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
StringRef FileName;
|
|
|
|
StringRef Code;
|
|
|
|
StringRef BuildDir;
|
2019-04-11 17:36:36 +08:00
|
|
|
HeaderSearch *HeaderSearchInfo = nullptr;
|
2018-09-27 22:27:02 +08:00
|
|
|
llvm::StringSet<> IncludedHeaders; // Both written and resolved.
|
|
|
|
tooling::HeaderIncludes Inserter; // Computers insertion replacement.
|
2018-05-15 23:29:32 +08:00
|
|
|
};
|
2018-02-16 22:15:55 +08:00
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2021-09-30 20:41:27 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2021-10-04 14:39:06 +08:00
|
|
|
// Support HeaderIDs as DenseMap keys.
|
2021-09-30 20:41:27 +08:00
|
|
|
template <> struct DenseMapInfo<clang::clangd::IncludeStructure::HeaderID> {
|
|
|
|
static inline clang::clangd::IncludeStructure::HeaderID getEmptyKey() {
|
2021-11-17 20:27:58 +08:00
|
|
|
return static_cast<clang::clangd::IncludeStructure::HeaderID>(-1);
|
2021-09-30 20:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline clang::clangd::IncludeStructure::HeaderID getTombstoneKey() {
|
2021-11-17 20:27:58 +08:00
|
|
|
return static_cast<clang::clangd::IncludeStructure::HeaderID>(-2);
|
2021-09-30 20:41:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
getHashValue(const clang::clangd::IncludeStructure::HeaderID &Tag) {
|
|
|
|
return hash_value(static_cast<unsigned>(Tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(const clang::clangd::IncludeStructure::HeaderID &LHS,
|
|
|
|
const clang::clangd::IncludeStructure::HeaderID &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
|