clang-tools  14.0.0git
Headers.h
Go to the documentation of this file.
1 //===--- Headers.h - Include headers -----------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
11 
12 #include "Protocol.h"
13 #include "SourceCode.h"
14 #include "index/Symbol.h"
15 #include "support/Logger.h"
16 #include "support/Path.h"
17 #include "clang/Basic/FileEntry.h"
18 #include "clang/Basic/TokenKinds.h"
19 #include "clang/Format/Format.h"
20 #include "clang/Frontend/CompilerInstance.h"
21 #include "clang/Lex/HeaderSearch.h"
22 #include "clang/Lex/PPCallbacks.h"
23 #include "clang/Tooling/Inclusions/HeaderIncludes.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSet.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/FileSystem/UniqueID.h"
30 #include "llvm/Support/VirtualFileSystem.h"
31 #include <string>
32 
33 namespace clang {
34 namespace clangd {
35 
36 /// Returns true if \p Include is literal include like "path" or <path>.
37 bool isLiteralInclude(llvm::StringRef Include);
38 
39 /// Represents a header file to be #include'd.
40 struct HeaderFile {
41  std::string File;
42  /// If this is true, `File` is a literal string quoted with <> or "" that
43  /// can be #included directly; otherwise, `File` is an absolute file path.
44  bool Verbatim;
45 
46  bool valid() const;
47 };
48 
49 /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
50 /// include.
51 llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header,
52  llvm::StringRef HintPath);
53 
54 // Returns include headers for \p Sym sorted by popularity. If two headers are
55 // equally popular, prefer the shorter one.
56 llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym);
57 
58 // An #include directive that we found in the main file.
59 struct Inclusion {
60  tok::PPKeywordKind Directive; // Directive used for inclusion, e.g. import
61  std::string Written; // Inclusion name as written e.g. <vector>.
62  Path Resolved; // Resolved path of included file. Empty if not resolved.
63  unsigned HashOffset = 0; // Byte offset from start of file to #.
64  int HashLine = 0; // Line number containing the directive, 0-indexed.
65  SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
66  llvm::Optional<unsigned> HeaderID;
67 };
68 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &);
69 bool operator==(const Inclusion &LHS, const Inclusion &RHS);
70 
71 // Contains information about one file in the build graph and its direct
72 // dependencies. Doesn't own the strings it references (IncludeGraph is
73 // self-contained).
75  enum class SourceFlag : uint8_t {
76  None = 0,
77  // Whether current file is a main file rather than a header.
78  IsTU = 1 << 0,
79  // Whether current file had any uncompilable errors during indexing.
80  HadErrors = 1 << 1,
81  };
82 
84  llvm::StringRef URI;
86  std::vector<llvm::StringRef> DirectIncludes;
87 };
88 // FileURI and FileInclusions are references to keys of the map containing
89 // them.
90 // Important: The graph generated by those callbacks might contain cycles, self
91 // edges and multi edges.
92 using IncludeGraph = llvm::StringMap<IncludeGraphNode>;
93 
96  return static_cast<IncludeGraphNode::SourceFlag>(static_cast<uint8_t>(A) |
97  static_cast<uint8_t>(B));
98 }
99 
102  return static_cast<uint8_t>(A) & static_cast<uint8_t>(B);
103 }
104 
107  return A = A | B;
108 }
109 
110 // Information captured about the inclusion graph in a translation unit.
111 // This includes detailed information about the direct #includes, and summary
112 // information about all transitive includes.
113 //
114 // It should be built incrementally with collectIncludeStructureCallback().
115 // When we build the preamble, we capture and store its include structure along
116 // with the preamble data. When we use the preamble, we can copy its
117 // IncludeStructure and use another collectIncludeStructureCallback() to fill
118 // in any non-preamble inclusions.
120 public:
122  // Reserve HeaderID = 0 for the main file.
123  RealPathNames.emplace_back();
124  }
125 
126  // Returns a PPCallback that visits all inclusions in the main file and
127  // populates the structure.
128  std::unique_ptr<PPCallbacks> collect(const CompilerInstance &CI);
129 
130  // HeaderID identifies file in the include graph. It corresponds to a
131  // FileEntry rather than a FileID, but stays stable across preamble & main
132  // file builds.
133  enum class HeaderID : unsigned {};
134 
135  llvm::Optional<HeaderID> getID(const FileEntry *Entry) const;
136  HeaderID getOrCreateID(const FileEntry *Entry);
137 
138  StringRef getRealPath(HeaderID ID) const {
139  assert(static_cast<unsigned>(ID) <= RealPathNames.size());
140  return RealPathNames[static_cast<unsigned>(ID)];
141  }
142 
144  return !NonSelfContained.contains(ID);
145  }
146 
147  // Return all transitively reachable files.
148  llvm::ArrayRef<std::string> allHeaders() const { return RealPathNames; }
149 
150  // Return all transitively reachable files, and their minimum include depth.
151  // All transitive includes (absolute paths), with their minimum include depth.
152  // Root --> 0, #included file --> 1, etc.
153  // Root is the ID of the header being visited first.
154  llvm::DenseMap<HeaderID, unsigned>
156 
157  // Maps HeaderID to the ids of the files included from it.
158  llvm::DenseMap<HeaderID, SmallVector<HeaderID>> IncludeChildren;
159 
160  std::vector<Inclusion> MainFileIncludes;
161 
162  // We reserve HeaderID(0) for the main file and will manually check for that
163  // in getID and getOrCreateID because the UniqueID is not stable when the
164  // content of the main file changes.
165  static const HeaderID MainFileID = HeaderID(0u);
166 
167  class RecordHeaders;
168 
169 private:
170  // MainFileEntry will be used to check if the queried file is the main file
171  // or not.
172  const FileEntry *MainFileEntry = nullptr;
173 
174  std::vector<std::string> RealPathNames; // In HeaderID order.
175  // FileEntry::UniqueID is mapped to the internal representation (HeaderID).
176  // Identifying files in a way that persists from preamble build to subsequent
177  // builds is surprisingly hard. FileID is unavailable in InclusionDirective(),
178  // and RealPathName and UniqueID are not preserved in
179  // the preamble.
180  llvm::DenseMap<llvm::sys::fs::UniqueID, HeaderID> UIDToIndex;
181  // Contains HeaderIDs of all non self-contained entries in the
182  // IncludeStructure.
183  llvm::DenseSet<HeaderID> NonSelfContained;
184 };
185 
186 // Calculates insertion edit for including a new header in a file.
188 public:
189  // If \p HeaderSearchInfo is nullptr (e.g. when compile command is
190  // infeasible), this will only try to insert verbatim headers, and
191  // include path of non-verbatim header will not be shortened.
192  IncludeInserter(StringRef FileName, StringRef Code,
193  const format::FormatStyle &Style, StringRef BuildDir,
194  HeaderSearch *HeaderSearchInfo)
195  : FileName(FileName), Code(Code), BuildDir(BuildDir),
196  HeaderSearchInfo(HeaderSearchInfo),
197  Inserter(FileName, Code, Style.IncludeStyle) {}
198 
199  void addExisting(const Inclusion &Inc);
200 
201  /// Checks whether to add an #include of the header into \p File.
202  /// An #include will not be added if:
203  /// - Either \p DeclaringHeader or \p InsertedHeader is already (directly)
204  /// in \p Inclusions (including those included via different paths).
205  /// - \p DeclaringHeader or \p InsertedHeader is the same as \p File.
206  ///
207  /// \param DeclaringHeader is path of the original header corresponding to \p
208  /// InsertedHeader e.g. the header that declares a symbol.
209  /// \param InsertedHeader The preferred header to be inserted. This could be
210  /// the same as DeclaringHeader but must be provided.
211  bool shouldInsertInclude(PathRef DeclaringHeader,
212  const HeaderFile &InsertedHeader) const;
213 
214  /// Determines the preferred way to #include a file, taking into account the
215  /// search path. Usually this will prefer a shorter representation like
216  /// 'Foo/Bar.h' over a longer one like 'Baz/include/Foo/Bar.h'.
217  ///
218  /// \param InsertedHeader The preferred header to be inserted.
219  ///
220  /// \param IncludingFile is the absolute path of the file that InsertedHeader
221  /// will be inserted.
222  ///
223  /// \return A quoted "path" or <path> to be included, or None if it couldn't
224  /// be shortened.
225  llvm::Optional<std::string>
226  calculateIncludePath(const HeaderFile &InsertedHeader,
227  llvm::StringRef IncludingFile) const;
228 
229  /// Calculates an edit that inserts \p VerbatimHeader into code. If the header
230  /// is already included, this returns None.
231  llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) const;
232 
233 private:
234  StringRef FileName;
235  StringRef Code;
236  StringRef BuildDir;
237  HeaderSearch *HeaderSearchInfo = nullptr;
238  llvm::StringSet<> IncludedHeaders; // Both written and resolved.
239  tooling::HeaderIncludes Inserter; // Computers insertion replacement.
240 };
241 
242 } // namespace clangd
243 } // namespace clang
244 
245 namespace llvm {
246 
247 // Support HeaderIDs as DenseMap keys.
248 template <> struct DenseMapInfo<clang::clangd::IncludeStructure::HeaderID> {
250  return static_cast<clang::clangd::IncludeStructure::HeaderID>(
251  DenseMapInfo<unsigned>::getEmptyKey());
252  }
253 
255  return static_cast<clang::clangd::IncludeStructure::HeaderID>(
256  DenseMapInfo<unsigned>::getTombstoneKey());
257  }
258 
259  static unsigned
261  return hash_value(static_cast<unsigned>(Tag));
262  }
263 
266  return LHS == RHS;
267  }
268 };
269 
270 } // namespace llvm
271 
272 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
clang::clangd::HeaderFile
Represents a header file to be #include'd.
Definition: Headers.h:40
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::Inclusion::HeaderID
llvm::Optional< unsigned > HeaderID
Definition: Headers.h:66
clang::clangd::IncludeInserter::IncludeInserter
IncludeInserter(StringRef FileName, StringRef Code, const format::FormatStyle &Style, StringRef BuildDir, HeaderSearch *HeaderSearchInfo)
Definition: Headers.h:192
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
clang::clangd::IncludeStructure::getRealPath
StringRef getRealPath(HeaderID ID) const
Definition: Headers.h:138
Path.h
FormatStyle
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))
clang::clangd::IncludeStructure::getID
llvm::Optional< HeaderID > getID(const FileEntry *Entry) const
Definition: Headers.cpp:169
clang::clangd::IncludeGraphNode::Digest
FileDigest Digest
Definition: Headers.h:85
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:456
Root
ASTNode Root
Definition: DumpAST.cpp:332
clang::clangd::HeaderFile::valid
bool valid() const
Definition: Headers.cpp:120
llvm::DenseMapInfo< clang::clangd::IncludeStructure::HeaderID >::getTombstoneKey
static clang::clangd::IncludeStructure::HeaderID getTombstoneKey()
Definition: Headers.h:254
clang::clangd::IncludeGraphNode::SourceFlag::IsTU
@ IsTU
clang::clangd::IncludeStructure::HeaderID
HeaderID
Definition: Headers.h:133
clang::clangd::IncludeInserter::calculateIncludePath
llvm::Optional< std::string > calculateIncludePath(const HeaderFile &InsertedHeader, llvm::StringRef IncludingFile) const
Determines the preferred way to #include a file, taking into account the search path.
Definition: Headers.cpp:250
clang::clangd::IncludeGraphNode::SourceFlag::None
@ None
clang::clangd::IncludeStructure::IncludeStructure
IncludeStructure()
Definition: Headers.h:121
clang::clangd::IncludeStructure::MainFileIncludes
std::vector< Inclusion > MainFileIncludes
Definition: Headers.h:160
clang::clangd::operator&
DeclRelationSet operator&(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:214
clang::clangd::IncludeInserter::shouldInsertInclude
bool shouldInsertInclude(PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const
Checks whether to add an #include of the header into File.
Definition: Headers.cpp:236
Protocol.h
clang::clangd::IncludeStructure
Definition: Headers.h:119
clang::clangd::getRankedIncludes
llvm::SmallVector< llvm::StringRef, 1 > getRankedIncludes(const Symbol &Sym)
Definition: Headers.cpp:145
ns1::ns2::A
@ A
Definition: CategoricalFeature.h:3
Code
std::string Code
Definition: FindTargetTests.cpp:67
clang::clangd::hash_value
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:49
clang::clangd::Inclusion::Directive
tok::PPKeywordKind Directive
Definition: Headers.h:60
clang::clangd::IncludeStructure::RecordHeaders
Definition: Headers.cpp:26
clang::clangd::Inclusion::HashLine
int HashLine
Definition: Headers.h:64
clang::clangd::operator|=
IncludeGraphNode::SourceFlag & operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B)
Definition: Headers.h:106
clang::clangd::Inclusion::Written
std::string Written
Definition: Headers.h:61
Logger.h
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::IncludeGraphNode::SourceFlag
SourceFlag
Definition: Headers.h:75
clang::clangd::IncludeGraphNode::DirectIncludes
std::vector< llvm::StringRef > DirectIncludes
Definition: Headers.h:86
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::clangd::IncludeGraphNode::Flags
SourceFlag Flags
Definition: Headers.h:83
clang::clangd::toHeaderFile
llvm::Expected< HeaderFile > toHeaderFile(llvm::StringRef Header, llvm::StringRef HintPath)
Creates a HeaderFile from Header which can be either a URI or a literal include.
Definition: Headers.cpp:125
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2155
llvm::DenseMapInfo< clang::clangd::IncludeStructure::HeaderID >::getHashValue
static unsigned getHashValue(const clang::clangd::IncludeStructure::HeaderID &Tag)
Definition: Headers.h:260
Symbol.h
Entry
Definition: Modularize.cpp:428
SourceCode.h
clang::clangd::IncludeStructure::includeDepth
llvm::DenseMap< HeaderID, unsigned > includeDepth(HeaderID Root=MainFileID) const
Definition: Headers.cpp:201
clang::clangd::IncludeStructure::collect
std::unique_ptr< PPCallbacks > collect(const CompilerInstance &CI)
Definition: Headers.cpp:161
clang::clangd::Inclusion
Definition: Headers.h:59
clang::clangd::isLiteralInclude
bool isLiteralInclude(llvm::StringRef Include)
Returns true if Include is literal include like "path" or <path>.
Definition: Headers.cpp:116
clang::clangd::IncludeGraphNode::SourceFlag::HadErrors
@ HadErrors
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::IncludeInserter::addExisting
void addExisting(const Inclusion &Inc)
Definition: Headers.cpp:228
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::Inclusion::Resolved
Path Resolved
Definition: Headers.h:62
clang::clangd::IncludeGraphNode::URI
llvm::StringRef URI
Definition: Headers.h:84
clang::clangd::IncludeStructure::MainFileID
static const HeaderID MainFileID
Definition: Headers.h:165
clang::clangd::IncludeInserter::insert
llvm::Optional< TextEdit > insert(llvm::StringRef VerbatimHeader) const
Calculates an edit that inserts VerbatimHeader into code.
Definition: Headers.cpp:280
clang::clangd::Inclusion::FileKind
SrcMgr::CharacteristicKind FileKind
Definition: Headers.h:65
clang::clangd::FileDigest
std::array< uint8_t, 8 > FileDigest
Definition: SourceCode.h:41
clang::clangd::operator|
DeclRelationSet operator|(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:211
clang::clangd::IncludeStructure::allHeaders
llvm::ArrayRef< std::string > allHeaders() const
Definition: Headers.h:148
llvm::DenseMapInfo< clang::clangd::IncludeStructure::HeaderID >::getEmptyKey
static clang::clangd::IncludeStructure::HeaderID getEmptyKey()
Definition: Headers.h:249
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::IncludeGraphNode
Definition: Headers.h:74
clang::clangd::IncludeStructure::getOrCreateID
HeaderID getOrCreateID(const FileEntry *Entry)
Definition: Headers.cpp:181
clang::clangd::Inclusion::HashOffset
unsigned HashOffset
Definition: Headers.h:63
clang::clangd::IncludeGraph
llvm::StringMap< IncludeGraphNode > IncludeGraph
Definition: Headers.h:92
clang::clangd::operator==
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:294
llvm::DenseMapInfo< clang::clangd::IncludeStructure::HeaderID >::isEqual
static bool isEqual(const clang::clangd::IncludeStructure::HeaderID &LHS, const clang::clangd::IncludeStructure::HeaderID &RHS)
Definition: Headers.h:264
ns1::ns2::B
@ B
Definition: CategoricalFeature.h:3
clang::clangd::HeaderFile::File
std::string File
Definition: Headers.h:41
Tag
HTMLTag Tag
Definition: HTMLGenerator.cpp:90
clang::clangd::IncludeStructure::isSelfContained
bool isSelfContained(HeaderID ID) const
Definition: Headers.h:143
clang::clangd::IncludeInserter
Definition: Headers.h:187
clang::clangd::IncludeStructure::IncludeChildren
llvm::DenseMap< HeaderID, SmallVector< HeaderID > > IncludeChildren
Definition: Headers.h:158
clang::clangd::HeaderFile::Verbatim
bool Verbatim
If this is true, File is a literal string quoted with <> or "" that can be #included directly; otherw...
Definition: Headers.h:44