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