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 | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 75 | bool IncludeInserter::shouldInsertInclude( |
| 76 | const HeaderFile &DeclaringHeader, const HeaderFile &InsertedHeader) const { |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 77 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 78 | if (FileName == DeclaringHeader.File || FileName == InsertedHeader.File) |
| 79 | return false; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 80 | llvm::StringSet<> IncludedHeaders; |
| 81 | for (const auto &Inc : Inclusions) { |
| 82 | IncludedHeaders.insert(Inc.Written); |
| 83 | if (!Inc.Resolved.empty()) |
| 84 | IncludedHeaders.insert(Inc.Resolved); |
| 85 | } |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 86 | auto Included = [&](llvm::StringRef Header) { |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 87 | return IncludedHeaders.find(Header) != IncludedHeaders.end(); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 88 | }; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 89 | return !Included(DeclaringHeader.File) && !Included(InsertedHeader.File); |
| 90 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 91 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 92 | std::string |
| 93 | IncludeInserter::calculateIncludePath(const HeaderFile &DeclaringHeader, |
| 94 | const HeaderFile &InsertedHeader) const { |
| 95 | assert(DeclaringHeader.valid() && InsertedHeader.valid()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 96 | if (InsertedHeader.Verbatim) |
| 97 | return InsertedHeader.File; |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 98 | bool IsSystem = false; |
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 + "\""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 105 | return Suggested; |
| 106 | } |
| 107 | |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 108 | Optional<TextEdit> IncludeInserter::insert(StringRef VerbatimHeader) const { |
| 109 | Optional<TextEdit> Edit = None; |
| 110 | if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"), |
| 111 | VerbatimHeader.startswith("<"))) |
| 112 | Edit = replacementToEdit(Code, *Insertion); |
| 113 | return Edit; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 116 | } // namespace clangd |
| 117 | } // namespace clang |