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