Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 1 | //===--- Headers.h - Include headers -----------------------------*- C++-*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H |
| 11 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 12 | #include "Protocol.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 13 | #include "SourceCode.h" |
Dmitri Gribenko | 08b49b5 | 2019-02-28 13:23:03 +0000 | [diff] [blame] | 14 | #include "index/Symbol.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 15 | #include "support/Path.h" |
Kadir Cetinkaya | 6e01718 | 2020-04-15 22:00:19 +0200 | [diff] [blame] | 16 | #include "clang/Basic/TokenKinds.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 17 | #include "clang/Format/Format.h" |
| 18 | #include "clang/Lex/HeaderSearch.h" |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 19 | #include "clang/Lex/PPCallbacks.h" |
Eric Liu | 528eb65 | 2018-06-04 09:04:28 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/Inclusions/HeaderIncludes.h" |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/ArrayRef.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSet.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Error.h" |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 25 | #include "llvm/Support/VirtualFileSystem.h" |
Kadir Cetinkaya | 6e01718 | 2020-04-15 22:00:19 +0200 | [diff] [blame] | 26 | #include <string> |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
| 29 | namespace clangd { |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 30 | |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 31 | /// Returns true if \p Include is literal include like "path" or <path>. |
| 32 | bool isLiteralInclude(llvm::StringRef Include); |
| 33 | |
| 34 | /// Represents a header file to be #include'd. |
| 35 | struct HeaderFile { |
| 36 | std::string File; |
| 37 | /// If this is true, `File` is a literal string quoted with <> or "" that |
| 38 | /// can be #included directly; otherwise, `File` is an absolute file path. |
| 39 | bool Verbatim; |
| 40 | |
| 41 | bool valid() const; |
| 42 | }; |
| 43 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 44 | /// Creates a `HeaderFile` from \p Header which can be either a URI or a literal |
| 45 | /// include. |
| 46 | llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, |
| 47 | llvm::StringRef HintPath); |
| 48 | |
| 49 | // Returns include headers for \p Sym sorted by popularity. If two headers are |
| 50 | // equally popular, prefer the shorter one. |
| 51 | llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym); |
| 52 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 53 | // An #include directive that we found in the main file. |
| 54 | struct Inclusion { |
Kadir Cetinkaya | 6e01718 | 2020-04-15 22:00:19 +0200 | [diff] [blame] | 55 | tok::PPKeywordKind Directive; // Directive used for inclusion, e.g. import |
| 56 | std::string Written; // Inclusion name as written e.g. <vector>. |
| 57 | Path Resolved; // Resolved path of included file. Empty if not resolved. |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 58 | unsigned HashOffset = 0; // Byte offset from start of file to #. |
Kadir Cetinkaya | d870016 | 2020-05-04 10:48:19 +0200 | [diff] [blame] | 59 | int HashLine = 0; // Line number containing the directive, 0-indexed. |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 60 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 61 | }; |
Kadir Cetinkaya | 5399552 | 2018-11-30 16:59:00 +0000 | [diff] [blame] | 62 | llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &); |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 63 | bool operator==(const Inclusion &LHS, const Inclusion &RHS); |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 64 | |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 65 | // Contains information about one file in the build grpah and its direct |
| 66 | // dependencies. Doesn't own the strings it references (IncludeGraph is |
| 67 | // self-contained). |
| 68 | struct IncludeGraphNode { |
Kadir Cetinkaya | adbb347 | 2019-07-04 09:52:04 +0000 | [diff] [blame] | 69 | enum class SourceFlag : uint8_t { |
| 70 | None = 0, |
| 71 | // Whether current file is a main file rather than a header. |
| 72 | IsTU = 1 << 0, |
| 73 | // Whether current file had any uncompilable errors during indexing. |
| 74 | HadErrors = 1 << 1, |
| 75 | }; |
| 76 | |
| 77 | SourceFlag Flags = SourceFlag::None; |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 78 | llvm::StringRef URI; |
Simon Pilgrim | 2d12d81 | 2018-12-04 14:07:29 +0000 | [diff] [blame] | 79 | FileDigest Digest{{0}}; |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 80 | std::vector<llvm::StringRef> DirectIncludes; |
| 81 | }; |
| 82 | // FileURI and FileInclusions are references to keys of the map containing |
| 83 | // them. |
Kadir Cetinkaya | 5399552 | 2018-11-30 16:59:00 +0000 | [diff] [blame] | 84 | // Important: The graph generated by those callbacks might contain cycles, self |
| 85 | // edges and multi edges. |
Kadir Cetinkaya | d08eab4 | 2018-11-27 16:08:53 +0000 | [diff] [blame] | 86 | using IncludeGraph = llvm::StringMap<IncludeGraphNode>; |
| 87 | |
Kadir Cetinkaya | adbb347 | 2019-07-04 09:52:04 +0000 | [diff] [blame] | 88 | inline IncludeGraphNode::SourceFlag operator|(IncludeGraphNode::SourceFlag A, |
| 89 | IncludeGraphNode::SourceFlag B) { |
| 90 | return static_cast<IncludeGraphNode::SourceFlag>(static_cast<uint8_t>(A) | |
| 91 | static_cast<uint8_t>(B)); |
| 92 | } |
| 93 | |
| 94 | inline bool operator&(IncludeGraphNode::SourceFlag A, |
| 95 | IncludeGraphNode::SourceFlag B) { |
| 96 | return static_cast<uint8_t>(A) & static_cast<uint8_t>(B); |
| 97 | } |
| 98 | |
| 99 | inline IncludeGraphNode::SourceFlag & |
| 100 | operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B) { |
| 101 | return A = A | B; |
| 102 | } |
| 103 | |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 104 | // Information captured about the inclusion graph in a translation unit. |
| 105 | // This includes detailed information about the direct #includes, and summary |
| 106 | // information about all transitive includes. |
| 107 | // |
| 108 | // It should be built incrementally with collectIncludeStructureCallback(). |
| 109 | // When we build the preamble, we capture and store its include structure along |
| 110 | // with the preamble data. When we use the preamble, we can copy its |
| 111 | // IncludeStructure and use another collectIncludeStructureCallback() to fill |
| 112 | // in any non-preamble inclusions. |
| 113 | class IncludeStructure { |
| 114 | public: |
| 115 | std::vector<Inclusion> MainFileIncludes; |
| 116 | |
| 117 | // Return all transitively reachable files, and their minimum include depth. |
| 118 | // All transitive includes (absolute paths), with their minimum include depth. |
| 119 | // Root --> 0, #included file --> 1, etc. |
| 120 | // Root is clang's name for a file, which may not be absolute. |
| 121 | // Usually it should be SM.getFileEntryForID(SM.getMainFileID())->getName(). |
| 122 | llvm::StringMap<unsigned> includeDepth(llvm::StringRef Root) const; |
| 123 | |
| 124 | // This updates IncludeDepth(), but not MainFileIncludes. |
| 125 | void recordInclude(llvm::StringRef IncludingName, |
| 126 | llvm::StringRef IncludedName, |
| 127 | llvm::StringRef IncludedRealName); |
| 128 | |
| 129 | private: |
| 130 | // Identifying files in a way that persists from preamble build to subsequent |
| 131 | // builds is surprisingly hard. FileID is unavailable in InclusionDirective(), |
Jan Korous | 2ee6497 | 2019-07-30 20:39:39 +0000 | [diff] [blame] | 132 | // and RealPathName and UniqueID are not preserved in the preamble. |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 133 | // We use the FileEntry::Name, which is stable, interned into a "file index". |
| 134 | // The paths we want to expose are the RealPathName, so store those too. |
| 135 | std::vector<std::string> RealPathNames; // In file index order. |
| 136 | unsigned fileIndex(llvm::StringRef Name); |
| 137 | llvm::StringMap<unsigned> NameToIndex; // Values are file indexes. |
| 138 | // Maps a file's index to that of the files it includes. |
| 139 | llvm::DenseMap<unsigned, SmallVector<unsigned, 8>> IncludeChildren; |
| 140 | }; |
| 141 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 142 | /// Returns a PPCallback that visits all inclusions in the main file. |
| 143 | std::unique_ptr<PPCallbacks> |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 144 | collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out); |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 145 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 146 | // Calculates insertion edit for including a new header in a file. |
| 147 | class IncludeInserter { |
| 148 | public: |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 149 | // If \p HeaderSearchInfo is nullptr (e.g. when compile command is |
| 150 | // infeasible), this will only try to insert verbatim headers, and |
| 151 | // include path of non-verbatim header will not be shortened. |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 152 | IncludeInserter(StringRef FileName, StringRef Code, |
| 153 | const format::FormatStyle &Style, StringRef BuildDir, |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 154 | HeaderSearch *HeaderSearchInfo) |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 155 | : FileName(FileName), Code(Code), BuildDir(BuildDir), |
| 156 | HeaderSearchInfo(HeaderSearchInfo), |
| 157 | Inserter(FileName, Code, Style.IncludeStyle) {} |
| 158 | |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 159 | void addExisting(const Inclusion &Inc); |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 160 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 161 | /// Checks whether to add an #include of the header into \p File. |
| 162 | /// An #include will not be added if: |
| 163 | /// - Either \p DeclaringHeader or \p InsertedHeader is already (directly) |
| 164 | /// in \p Inclusions (including those included via different paths). |
| 165 | /// - \p DeclaringHeader or \p InsertedHeader is the same as \p File. |
| 166 | /// |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 167 | /// \param DeclaringHeader is path of the original header corresponding to \p |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 168 | /// InsertedHeader e.g. the header that declares a symbol. |
| 169 | /// \param InsertedHeader The preferred header to be inserted. This could be |
Simon Pilgrim | cbd8c76 | 2018-06-26 17:00:43 +0000 | [diff] [blame] | 170 | /// the same as DeclaringHeader but must be provided. |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 171 | bool shouldInsertInclude(PathRef DeclaringHeader, |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 172 | const HeaderFile &InsertedHeader) const; |
| 173 | |
| 174 | /// Determines the preferred way to #include a file, taking into account the |
| 175 | /// search path. Usually this will prefer a shorter representation like |
| 176 | /// 'Foo/Bar.h' over a longer one like 'Baz/include/Foo/Bar.h'. |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 177 | /// |
Sam McCall | b324c64 | 2019-07-08 18:07:46 +0000 | [diff] [blame] | 178 | /// \param InsertedHeader The preferred header to be inserted. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 179 | /// |
Kadir Cetinkaya | 1f6d984 | 2019-07-03 07:47:19 +0000 | [diff] [blame] | 180 | /// \param IncludingFile is the absolute path of the file that InsertedHeader |
| 181 | /// will be inserted. |
| 182 | /// |
Sam McCall | b324c64 | 2019-07-08 18:07:46 +0000 | [diff] [blame] | 183 | /// \return A quoted "path" or <path> to be included, or None if it couldn't |
| 184 | /// be shortened. |
| 185 | llvm::Optional<std::string> |
| 186 | calculateIncludePath(const HeaderFile &InsertedHeader, |
| 187 | llvm::StringRef IncludingFile) const; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 188 | |
| 189 | /// Calculates an edit that inserts \p VerbatimHeader into code. If the header |
| 190 | /// is already included, this returns None. |
| 191 | llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) const; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 192 | |
| 193 | private: |
| 194 | StringRef FileName; |
| 195 | StringRef Code; |
| 196 | StringRef BuildDir; |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 197 | HeaderSearch *HeaderSearchInfo = nullptr; |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 198 | llvm::StringSet<> IncludedHeaders; // Both written and resolved. |
| 199 | tooling::HeaderIncludes Inserter; // Computers insertion replacement. |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 200 | }; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 201 | |
| 202 | } // namespace clangd |
| 203 | } // namespace clang |
| 204 | |
| 205 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H |