Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 1 | //===--- Headers.cpp - 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 | #include "Headers.h" |
| 11 | #include "Compiler.h" |
| 12 | #include "Logger.h" |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 13 | #include "SourceCode.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/CompilerInvocation.h" |
| 16 | #include "clang/Frontend/FrontendActions.h" |
| 17 | #include "clang/Lex/HeaderSearch.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 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace clangd { |
| 23 | namespace { |
| 24 | |
| 25 | class RecordHeaders : public PPCallbacks { |
| 26 | public: |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 27 | RecordHeaders(const SourceManager &SM, IncludeStructure *Out) |
| 28 | : SM(SM), Out(Out) {} |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 29 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 30 | // Record existing #includes - both written and resolved paths. Only #includes |
| 31 | // in the main file are collected. |
| 32 | void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 33 | StringRef FileName, bool IsAngled, |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 34 | CharSourceRange FilenameRange, const FileEntry *File, |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 35 | StringRef /*SearchPath*/, 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 { |
| 38 | if (SM.isWrittenInMainFile(HashLoc)) { |
| 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 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 68 | bool isLiteralInclude(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)) || |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 74 | (!Verbatim && sys::path::is_absolute(File)); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 77 | std::unique_ptr<PPCallbacks> |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 78 | collectIncludeStructureCallback(const SourceManager &SM, |
| 79 | IncludeStructure *Out) { |
| 80 | return llvm::make_unique<RecordHeaders>(SM, Out); |
| 81 | } |
| 82 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 83 | void IncludeStructure::recordInclude(StringRef IncludingName, |
| 84 | StringRef IncludedName, |
| 85 | StringRef IncludedRealName) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 86 | auto Child = fileIndex(IncludedName); |
| 87 | if (!IncludedRealName.empty() && RealPathNames[Child].empty()) |
| 88 | RealPathNames[Child] = IncludedRealName; |
| 89 | auto Parent = fileIndex(IncludingName); |
| 90 | IncludeChildren[Parent].push_back(Child); |
| 91 | } |
| 92 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 93 | unsigned IncludeStructure::fileIndex(StringRef Name) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 94 | auto R = NameToIndex.try_emplace(Name, RealPathNames.size()); |
| 95 | if (R.second) |
| 96 | RealPathNames.emplace_back(); |
| 97 | return R.first->getValue(); |
| 98 | } |
| 99 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 100 | StringMap<unsigned> IncludeStructure::includeDepth(StringRef Root) const { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 101 | // Include depth 0 is the main file only. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 102 | StringMap<unsigned> Result; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 103 | Result[Root] = 0; |
| 104 | std::vector<unsigned> CurrentLevel; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 105 | DenseSet<unsigned> Seen; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 106 | auto It = NameToIndex.find(Root); |
| 107 | if (It != NameToIndex.end()) { |
| 108 | CurrentLevel.push_back(It->second); |
| 109 | Seen.insert(It->second); |
| 110 | } |
| 111 | |
| 112 | // Each round of BFS traversal finds the next depth level. |
| 113 | std::vector<unsigned> PreviousLevel; |
| 114 | for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) { |
| 115 | PreviousLevel.clear(); |
| 116 | PreviousLevel.swap(CurrentLevel); |
| 117 | for (const auto &Parent : PreviousLevel) { |
| 118 | for (const auto &Child : IncludeChildren.lookup(Parent)) { |
| 119 | if (Seen.insert(Child).second) { |
| 120 | CurrentLevel.push_back(Child); |
| 121 | const auto &Name = RealPathNames[Child]; |
| 122 | // Can't include files if we don't have their real path. |
| 123 | if (!Name.empty()) |
| 124 | Result[Name] = Level; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return Result; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 132 | void IncludeInserter::addExisting(const Inclusion &Inc) { |
| 133 | IncludedHeaders.insert(Inc.Written); |
| 134 | if (!Inc.Resolved.empty()) |
| 135 | IncludedHeaders.insert(Inc.Resolved); |
| 136 | } |
| 137 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 138 | /// FIXME(ioeric): we might not want to insert an absolute include path if the |
| 139 | /// path is not shortened. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 140 | bool IncludeInserter::shouldInsertInclude( |
| 141 | const HeaderFile &DeclaringHeader, const HeaderFile &InsertedHeader) const { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 142 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 143 | if (FileName == DeclaringHeader.File || FileName == InsertedHeader.File) |
| 144 | return false; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 145 | auto Included = [&](StringRef Header) { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 146 | return IncludedHeaders.find(Header) != IncludedHeaders.end(); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 147 | }; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 148 | return !Included(DeclaringHeader.File) && !Included(InsertedHeader.File); |
| 149 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 150 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 151 | std::string |
| 152 | IncludeInserter::calculateIncludePath(const HeaderFile &DeclaringHeader, |
| 153 | const HeaderFile &InsertedHeader) const { |
| 154 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 155 | if (InsertedHeader.Verbatim) |
| 156 | return InsertedHeader.File; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 157 | bool IsSystem = false; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 158 | std::string Suggested = HeaderSearchInfo.suggestPathToFileForDiagnostics( |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 159 | InsertedHeader.File, BuildDir, &IsSystem); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 160 | if (IsSystem) |
| 161 | Suggested = "<" + Suggested + ">"; |
| 162 | else |
| 163 | Suggested = "\"" + Suggested + "\""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 164 | return Suggested; |
| 165 | } |
| 166 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 167 | Optional<TextEdit> IncludeInserter::insert(StringRef VerbatimHeader) const { |
| 168 | Optional<TextEdit> Edit = None; |
| 169 | if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"), |
| 170 | VerbatimHeader.startswith("<"))) |
| 171 | Edit = replacementToEdit(Code, *Insertion); |
| 172 | return Edit; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame^] | 175 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Inclusion &Inc) { |
| 176 | return OS << Inc.Written << " = " |
| 177 | << (Inc.Resolved.empty() ? Inc.Resolved : "[unresolved]") << " at " |
| 178 | << Inc.R; |
| 179 | } |
| 180 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 181 | } // namespace clangd |
| 182 | } // namespace clang |