Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 1 | //===--- Headers.cpp - 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 | #include "Headers.h" |
| 10 | #include "Compiler.h" |
| 11 | #include "Logger.h" |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 12 | #include "SourceCode.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Frontend/FrontendActions.h" |
| 16 | #include "clang/Lex/HeaderSearch.h" |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Path.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 18 | |
| 19 | namespace clang { |
| 20 | namespace clangd { |
| 21 | namespace { |
| 22 | |
| 23 | class RecordHeaders : public PPCallbacks { |
| 24 | public: |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 25 | RecordHeaders(const SourceManager &SM, IncludeStructure *Out) |
| 26 | : SM(SM), Out(Out) {} |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 27 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 28 | // Record existing #includes - both written and resolved paths. Only #includes |
| 29 | // in the main file are collected. |
| 30 | void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 31 | llvm::StringRef FileName, bool IsAngled, |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 32 | CharSourceRange FilenameRange, const FileEntry *File, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 33 | llvm::StringRef /*SearchPath*/, |
| 34 | llvm::StringRef /*RelativePath*/, |
Julie Hockett | 546943f | 2018-05-10 19:13:14 +0000 | [diff] [blame] | 35 | const Module * /*Imported*/, |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 36 | SrcMgr::CharacteristicKind FileKind) override { |
| 37 | if (SM.isWrittenInMainFile(HashLoc)) { |
| 38 | Out->MainFileIncludes.emplace_back(); |
| 39 | auto &Inc = Out->MainFileIncludes.back(); |
| 40 | Inc.R = halfOpenToRange(SM, FilenameRange); |
| 41 | Inc.Written = |
| 42 | (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str(); |
| 43 | Inc.Resolved = File ? File->tryGetRealPathName() : ""; |
| 44 | Inc.HashOffset = SM.getFileOffset(HashLoc); |
| 45 | Inc.FileKind = FileKind; |
| 46 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 47 | if (File) { |
| 48 | auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)); |
| 49 | if (!IncludingFileEntry) { |
| 50 | assert(SM.getBufferName(HashLoc).startswith("<") && |
| 51 | "Expected #include location to be a file or <built-in>"); |
| 52 | // Treat as if included from the main file. |
| 53 | IncludingFileEntry = SM.getFileEntryForID(SM.getMainFileID()); |
| 54 | } |
| 55 | Out->recordInclude(IncludingFileEntry->getName(), File->getName(), |
| 56 | File->tryGetRealPathName()); |
| 57 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 61 | const SourceManager &SM; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 62 | IncludeStructure *Out; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 67 | bool isLiteralInclude(llvm::StringRef Include) { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 68 | return Include.startswith("<") || Include.startswith("\""); |
| 69 | } |
| 70 | |
| 71 | bool HeaderFile::valid() const { |
| 72 | return (Verbatim && isLiteralInclude(File)) || |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 73 | (!Verbatim && llvm::sys::path::is_absolute(File)); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 76 | llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, |
| 77 | llvm::StringRef HintPath) { |
| 78 | if (isLiteralInclude(Header)) |
| 79 | return HeaderFile{Header.str(), /*Verbatim=*/true}; |
| 80 | auto U = URI::parse(Header); |
| 81 | if (!U) |
| 82 | return U.takeError(); |
| 83 | |
| 84 | auto IncludePath = URI::includeSpelling(*U); |
| 85 | if (!IncludePath) |
| 86 | return IncludePath.takeError(); |
| 87 | if (!IncludePath->empty()) |
| 88 | return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true}; |
| 89 | |
| 90 | auto Resolved = URI::resolve(*U, HintPath); |
| 91 | if (!Resolved) |
| 92 | return Resolved.takeError(); |
| 93 | return HeaderFile{std::move(*Resolved), /*Verbatim=*/false}; |
| 94 | } |
| 95 | |
| 96 | llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) { |
| 97 | auto Includes = Sym.IncludeHeaders; |
| 98 | // Sort in descending order by reference count and header length. |
| 99 | llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS, |
| 100 | const Symbol::IncludeHeaderWithReferences &RHS) { |
| 101 | if (LHS.References == RHS.References) |
| 102 | return LHS.IncludeHeader.size() < RHS.IncludeHeader.size(); |
| 103 | return LHS.References > RHS.References; |
| 104 | }); |
| 105 | llvm::SmallVector<llvm::StringRef, 1> Headers; |
| 106 | for (const auto &Include : Includes) |
| 107 | Headers.push_back(Include.IncludeHeader); |
| 108 | return Headers; |
| 109 | } |
| 110 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 111 | std::unique_ptr<PPCallbacks> |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 112 | collectIncludeStructureCallback(const SourceManager &SM, |
| 113 | IncludeStructure *Out) { |
| 114 | return llvm::make_unique<RecordHeaders>(SM, Out); |
| 115 | } |
| 116 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 117 | void IncludeStructure::recordInclude(llvm::StringRef IncludingName, |
| 118 | llvm::StringRef IncludedName, |
| 119 | llvm::StringRef IncludedRealName) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 120 | auto Child = fileIndex(IncludedName); |
| 121 | if (!IncludedRealName.empty() && RealPathNames[Child].empty()) |
| 122 | RealPathNames[Child] = IncludedRealName; |
| 123 | auto Parent = fileIndex(IncludingName); |
| 124 | IncludeChildren[Parent].push_back(Child); |
| 125 | } |
| 126 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 127 | unsigned IncludeStructure::fileIndex(llvm::StringRef Name) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 128 | auto R = NameToIndex.try_emplace(Name, RealPathNames.size()); |
| 129 | if (R.second) |
| 130 | RealPathNames.emplace_back(); |
| 131 | return R.first->getValue(); |
| 132 | } |
| 133 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 134 | llvm::StringMap<unsigned> |
| 135 | IncludeStructure::includeDepth(llvm::StringRef Root) const { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 136 | // Include depth 0 is the main file only. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 137 | llvm::StringMap<unsigned> Result; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 138 | Result[Root] = 0; |
| 139 | std::vector<unsigned> CurrentLevel; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 140 | llvm::DenseSet<unsigned> Seen; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 141 | auto It = NameToIndex.find(Root); |
| 142 | if (It != NameToIndex.end()) { |
| 143 | CurrentLevel.push_back(It->second); |
| 144 | Seen.insert(It->second); |
| 145 | } |
| 146 | |
| 147 | // Each round of BFS traversal finds the next depth level. |
| 148 | std::vector<unsigned> PreviousLevel; |
| 149 | for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) { |
| 150 | PreviousLevel.clear(); |
| 151 | PreviousLevel.swap(CurrentLevel); |
| 152 | for (const auto &Parent : PreviousLevel) { |
| 153 | for (const auto &Child : IncludeChildren.lookup(Parent)) { |
| 154 | if (Seen.insert(Child).second) { |
| 155 | CurrentLevel.push_back(Child); |
| 156 | const auto &Name = RealPathNames[Child]; |
| 157 | // Can't include files if we don't have their real path. |
| 158 | if (!Name.empty()) |
| 159 | Result[Name] = Level; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return Result; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 167 | void IncludeInserter::addExisting(const Inclusion &Inc) { |
| 168 | IncludedHeaders.insert(Inc.Written); |
| 169 | if (!Inc.Resolved.empty()) |
| 170 | IncludedHeaders.insert(Inc.Resolved); |
| 171 | } |
| 172 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 173 | /// FIXME(ioeric): we might not want to insert an absolute include path if the |
| 174 | /// path is not shortened. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 175 | bool IncludeInserter::shouldInsertInclude( |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame^] | 176 | PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const { |
| 177 | assert(InsertedHeader.valid()); |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 178 | if (!HeaderSearchInfo && !InsertedHeader.Verbatim) |
| 179 | return false; |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame^] | 180 | if (FileName == DeclaringHeader || FileName == InsertedHeader.File) |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 181 | return false; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 182 | auto Included = [&](llvm::StringRef Header) { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 183 | return IncludedHeaders.find(Header) != IncludedHeaders.end(); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 184 | }; |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame^] | 185 | return !Included(DeclaringHeader) && !Included(InsertedHeader.File); |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 186 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 187 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 188 | std::string |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame^] | 189 | IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader) const { |
| 190 | assert(InsertedHeader.valid()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 191 | if (InsertedHeader.Verbatim) |
| 192 | return InsertedHeader.File; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 193 | bool IsSystem = false; |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 194 | if (!HeaderSearchInfo) |
| 195 | return "\"" + InsertedHeader.File + "\""; |
| 196 | std::string Suggested = HeaderSearchInfo->suggestPathToFileForDiagnostics( |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 197 | InsertedHeader.File, BuildDir, &IsSystem); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 198 | if (IsSystem) |
| 199 | Suggested = "<" + Suggested + ">"; |
| 200 | else |
| 201 | Suggested = "\"" + Suggested + "\""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 202 | return Suggested; |
| 203 | } |
| 204 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 205 | llvm::Optional<TextEdit> |
| 206 | IncludeInserter::insert(llvm::StringRef VerbatimHeader) const { |
| 207 | llvm::Optional<TextEdit> Edit = None; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 208 | if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"), |
| 209 | VerbatimHeader.startswith("<"))) |
| 210 | Edit = replacementToEdit(Code, *Insertion); |
| 211 | return Edit; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 214 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Inclusion &Inc) { |
| 215 | return OS << Inc.Written << " = " |
| 216 | << (Inc.Resolved.empty() ? Inc.Resolved : "[unresolved]") << " at " |
| 217 | << Inc.R; |
| 218 | } |
| 219 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 220 | } // namespace clangd |
| 221 | } // namespace clang |