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