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*/, |
| 37 | SrcMgr::CharacteristicKind /*FileType*/) override { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 38 | if (SM.isInMainFile(HashLoc)) |
| 39 | Out->MainFileIncludes.push_back({ |
| 40 | halfOpenToRange(SM, FilenameRange), |
| 41 | (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str(), |
| 42 | File ? File->tryGetRealPathName() : "", |
| 43 | }); |
| 44 | if (File) { |
| 45 | auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc)); |
| 46 | if (!IncludingFileEntry) { |
| 47 | assert(SM.getBufferName(HashLoc).startswith("<") && |
| 48 | "Expected #include location to be a file or <built-in>"); |
| 49 | // Treat as if included from the main file. |
| 50 | IncludingFileEntry = SM.getFileEntryForID(SM.getMainFileID()); |
| 51 | } |
| 52 | Out->recordInclude(IncludingFileEntry->getName(), File->getName(), |
| 53 | File->tryGetRealPathName()); |
| 54 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 58 | const SourceManager &SM; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 59 | IncludeStructure *Out; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace |
| 63 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 64 | bool isLiteralInclude(StringRef Include) { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 65 | return Include.startswith("<") || Include.startswith("\""); |
| 66 | } |
| 67 | |
| 68 | bool HeaderFile::valid() const { |
| 69 | return (Verbatim && isLiteralInclude(File)) || |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 70 | (!Verbatim && sys::path::is_absolute(File)); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 73 | std::unique_ptr<PPCallbacks> |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 74 | collectIncludeStructureCallback(const SourceManager &SM, |
| 75 | IncludeStructure *Out) { |
| 76 | return llvm::make_unique<RecordHeaders>(SM, Out); |
| 77 | } |
| 78 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 79 | void IncludeStructure::recordInclude(StringRef IncludingName, |
| 80 | StringRef IncludedName, |
| 81 | StringRef IncludedRealName) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 82 | auto Child = fileIndex(IncludedName); |
| 83 | if (!IncludedRealName.empty() && RealPathNames[Child].empty()) |
| 84 | RealPathNames[Child] = IncludedRealName; |
| 85 | auto Parent = fileIndex(IncludingName); |
| 86 | IncludeChildren[Parent].push_back(Child); |
| 87 | } |
| 88 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 89 | unsigned IncludeStructure::fileIndex(StringRef Name) { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 90 | auto R = NameToIndex.try_emplace(Name, RealPathNames.size()); |
| 91 | if (R.second) |
| 92 | RealPathNames.emplace_back(); |
| 93 | return R.first->getValue(); |
| 94 | } |
| 95 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 96 | StringMap<unsigned> IncludeStructure::includeDepth(StringRef Root) const { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 97 | // Include depth 0 is the main file only. |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 98 | StringMap<unsigned> Result; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 99 | Result[Root] = 0; |
| 100 | std::vector<unsigned> CurrentLevel; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 101 | DenseSet<unsigned> Seen; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 102 | auto It = NameToIndex.find(Root); |
| 103 | if (It != NameToIndex.end()) { |
| 104 | CurrentLevel.push_back(It->second); |
| 105 | Seen.insert(It->second); |
| 106 | } |
| 107 | |
| 108 | // Each round of BFS traversal finds the next depth level. |
| 109 | std::vector<unsigned> PreviousLevel; |
| 110 | for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) { |
| 111 | PreviousLevel.clear(); |
| 112 | PreviousLevel.swap(CurrentLevel); |
| 113 | for (const auto &Parent : PreviousLevel) { |
| 114 | for (const auto &Child : IncludeChildren.lookup(Parent)) { |
| 115 | if (Seen.insert(Child).second) { |
| 116 | CurrentLevel.push_back(Child); |
| 117 | const auto &Name = RealPathNames[Child]; |
| 118 | // Can't include files if we don't have their real path. |
| 119 | if (!Name.empty()) |
| 120 | Result[Name] = Level; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | return Result; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 128 | void IncludeInserter::addExisting(const Inclusion &Inc) { |
| 129 | IncludedHeaders.insert(Inc.Written); |
| 130 | if (!Inc.Resolved.empty()) |
| 131 | IncludedHeaders.insert(Inc.Resolved); |
| 132 | } |
| 133 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 134 | /// FIXME(ioeric): we might not want to insert an absolute include path if the |
| 135 | /// path is not shortened. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 136 | bool IncludeInserter::shouldInsertInclude( |
| 137 | const HeaderFile &DeclaringHeader, const HeaderFile &InsertedHeader) const { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 138 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 139 | if (FileName == DeclaringHeader.File || FileName == InsertedHeader.File) |
| 140 | return false; |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 141 | auto Included = [&](StringRef Header) { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 142 | return IncludedHeaders.find(Header) != IncludedHeaders.end(); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 143 | }; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 144 | return !Included(DeclaringHeader.File) && !Included(InsertedHeader.File); |
| 145 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 146 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 147 | std::string |
| 148 | IncludeInserter::calculateIncludePath(const HeaderFile &DeclaringHeader, |
| 149 | const HeaderFile &InsertedHeader) const { |
| 150 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 151 | if (InsertedHeader.Verbatim) |
| 152 | return InsertedHeader.File; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 153 | bool IsSystem = false; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 154 | std::string Suggested = HeaderSearchInfo.suggestPathToFileForDiagnostics( |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 155 | InsertedHeader.File, BuildDir, &IsSystem); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 156 | if (IsSystem) |
| 157 | Suggested = "<" + Suggested + ">"; |
| 158 | else |
| 159 | Suggested = "\"" + Suggested + "\""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 160 | return Suggested; |
| 161 | } |
| 162 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 163 | Optional<TextEdit> IncludeInserter::insert(StringRef VerbatimHeader) const { |
| 164 | Optional<TextEdit> Edit = None; |
| 165 | if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"), |
| 166 | VerbatimHeader.startswith("<"))) |
| 167 | Edit = replacementToEdit(Code, *Insertion); |
| 168 | return Edit; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 171 | } // namespace clangd |
| 172 | } // namespace clang |