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