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