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 | |
| 20 | namespace clang { |
| 21 | namespace clangd { |
| 22 | namespace { |
| 23 | |
| 24 | class RecordHeaders : public PPCallbacks { |
| 25 | public: |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 26 | RecordHeaders(const SourceManager &SM, |
| 27 | std::function<void(Inclusion)> Callback) |
| 28 | : SM(SM), Callback(std::move(Callback)) {} |
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*/, |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 33 | llvm::StringRef FileName, bool IsAngled, |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 34 | CharSourceRange FilenameRange, const FileEntry *File, |
| 35 | llvm::StringRef /*SearchPath*/, |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 36 | llvm::StringRef /*RelativePath*/, |
Julie Hockett | 546943f | 2018-05-10 19:13:14 +0000 | [diff] [blame] | 37 | const Module * /*Imported*/, |
| 38 | SrcMgr::CharacteristicKind /*FileType*/) override { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 39 | // Only inclusion directives in the main file make sense. The user cannot |
| 40 | // select directives not in the main file. |
| 41 | if (HashLoc.isInvalid() || !SM.isInMainFile(HashLoc)) |
| 42 | return; |
| 43 | std::string Written = |
| 44 | (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str(); |
| 45 | std::string Resolved = (!File || File->tryGetRealPathName().empty()) |
| 46 | ? "" |
| 47 | : File->tryGetRealPathName(); |
| 48 | Callback({halfOpenToRange(SM, FilenameRange), Written, Resolved}); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | private: |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 52 | const SourceManager &SM; |
| 53 | std::function<void(Inclusion)> Callback; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // namespace |
| 57 | |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 58 | bool isLiteralInclude(llvm::StringRef Include) { |
| 59 | return Include.startswith("<") || Include.startswith("\""); |
| 60 | } |
| 61 | |
| 62 | bool HeaderFile::valid() const { |
| 63 | return (Verbatim && isLiteralInclude(File)) || |
| 64 | (!Verbatim && llvm::sys::path::is_absolute(File)); |
| 65 | } |
| 66 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 67 | std::unique_ptr<PPCallbacks> |
| 68 | collectInclusionsInMainFileCallback(const SourceManager &SM, |
| 69 | std::function<void(Inclusion)> Callback) { |
| 70 | return llvm::make_unique<RecordHeaders>(SM, std::move(Callback)); |
| 71 | } |
| 72 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 73 | /// FIXME(ioeric): we might not want to insert an absolute include path if the |
| 74 | /// path is not shortened. |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame^] | 75 | llvm::Expected<std::string> calculateIncludePath( |
| 76 | PathRef File, StringRef BuildDir, HeaderSearch &HeaderSearchInfo, |
| 77 | const std::vector<Inclusion> &Inclusions, const HeaderFile &DeclaringHeader, |
| 78 | const HeaderFile &InsertedHeader) { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 79 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
| 80 | if (File == DeclaringHeader.File || File == InsertedHeader.File) |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 81 | return ""; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 82 | llvm::StringSet<> IncludedHeaders; |
| 83 | for (const auto &Inc : Inclusions) { |
| 84 | IncludedHeaders.insert(Inc.Written); |
| 85 | if (!Inc.Resolved.empty()) |
| 86 | IncludedHeaders.insert(Inc.Resolved); |
| 87 | } |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 88 | auto Included = [&](llvm::StringRef Header) { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 89 | return IncludedHeaders.find(Header) != IncludedHeaders.end(); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 90 | }; |
| 91 | if (Included(DeclaringHeader.File) || Included(InsertedHeader.File)) |
| 92 | return ""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 93 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 94 | bool IsSystem = false; |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 95 | |
| 96 | if (InsertedHeader.Verbatim) |
| 97 | return InsertedHeader.File; |
| 98 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 99 | std::string Suggested = HeaderSearchInfo.suggestPathToFileForDiagnostics( |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame^] | 100 | InsertedHeader.File, BuildDir, &IsSystem); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 101 | if (IsSystem) |
| 102 | Suggested = "<" + Suggested + ">"; |
| 103 | else |
| 104 | Suggested = "\"" + Suggested + "\""; |
| 105 | |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 106 | log("Suggested #include for " + InsertedHeader.File + " is: " + Suggested); |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 107 | return Suggested; |
| 108 | } |
| 109 | |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame^] | 110 | Expected<Optional<TextEdit>> |
| 111 | IncludeInserter::insert(const HeaderFile &DeclaringHeader, |
| 112 | const HeaderFile &InsertedHeader) const { |
| 113 | auto Validate = [](const HeaderFile &Header) { |
| 114 | return Header.valid() |
| 115 | ? llvm::Error::success() |
| 116 | : llvm::make_error<llvm::StringError>( |
| 117 | "Invalid HeaderFile: " + Header.File + |
| 118 | " (verbatim=" + std::to_string(Header.Verbatim) + ").", |
| 119 | llvm::inconvertibleErrorCode()); |
| 120 | }; |
| 121 | if (auto Err = Validate(DeclaringHeader)) |
| 122 | return std::move(Err); |
| 123 | if (auto Err = Validate(InsertedHeader)) |
| 124 | return std::move(Err); |
| 125 | auto Include = |
| 126 | calculateIncludePath(FileName, BuildDir, HeaderSearchInfo, Inclusions, |
| 127 | DeclaringHeader, InsertedHeader); |
| 128 | if (!Include) |
| 129 | return Include.takeError(); |
| 130 | if (Include->empty()) |
| 131 | return llvm::None; |
| 132 | StringRef IncludeRef = *Include; |
| 133 | auto Insertion = |
| 134 | Inserter.insert(IncludeRef.trim("\"<>"), IncludeRef.startswith("<")); |
| 135 | if (!Insertion) |
| 136 | return llvm::None; |
| 137 | return replacementToEdit(Code, *Insertion); |
| 138 | } |
| 139 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 140 | } // namespace clangd |
| 141 | } // namespace clang |