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" |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 10 | #include "Preamble.h" |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 11 | #include "SourceCode.h" |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 12 | #include "support/Logger.h" |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 13 | #include "clang/Basic/SourceLocation.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/CompilerInstance.h" |
Sam McCall | d97a341 | 2023-07-20 21:46:52 +0200 | [diff] [blame] | 16 | #include "clang/Lex/DirectoryLookup.h" |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 17 | #include "clang/Lex/HeaderSearch.h" |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 18 | #include "clang/Lex/PPCallbacks.h" |
| 19 | #include "clang/Lex/Preprocessor.h" |
Haojian Wu | dd46a08 | 2022-11-07 13:30:47 +0100 | [diff] [blame] | 20 | #include "clang/Tooling/Inclusions/HeaderAnalysis.h" |
Viktoriia Bakalova | e028c97 | 2023-02-07 16:52:51 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Kadir Cetinkaya | 1f6d984 | 2019-07-03 07:47:19 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Eric Liu | 709bde8 | 2018-02-19 18:48:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Kirill Bobyrev | 9f05b11 | 2022-04-21 16:59:59 +0200 | [diff] [blame] | 24 | #include <cstring> |
Kazu Hirata | 71f5573 | 2023-01-07 20:02:20 -0800 | [diff] [blame] | 25 | #include <optional> |
Viktoriia Bakalova | 7322f2d | 2023-07-11 13:33:53 +0000 | [diff] [blame] | 26 | #include <string> |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
| 29 | namespace clangd { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 30 | |
Haojian Wu | be39dae | 2023-03-10 12:06:32 +0100 | [diff] [blame] | 31 | class IncludeStructure::RecordHeaders : public PPCallbacks { |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 32 | public: |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 33 | RecordHeaders(const CompilerInstance &CI, IncludeStructure *Out) |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 34 | : SM(CI.getSourceManager()), Out(Out) {} |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 35 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 36 | // Record existing #includes - both written and resolved paths. Only #includes |
| 37 | // in the main file are collected. |
Kadir Cetinkaya | 6e01718 | 2020-04-15 22:00:19 +0200 | [diff] [blame] | 38 | void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 39 | llvm::StringRef FileName, bool IsAngled, |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 40 | CharSourceRange /*FilenameRange*/, |
Benjamin Kramer | 854c10f | 2022-12-20 00:15:11 +0100 | [diff] [blame] | 41 | OptionalFileEntryRef File, |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 42 | llvm::StringRef /*SearchPath*/, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 43 | llvm::StringRef /*RelativePath*/, |
Jan Svoboda | da95d92 | 2024-02-08 19:19:18 +0100 | [diff] [blame] | 44 | const clang::Module * /*SuggestedModule*/, |
| 45 | bool /*ModuleImported*/, |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 46 | SrcMgr::CharacteristicKind FileKind) override { |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 47 | auto MainFID = SM.getMainFileID(); |
| 48 | // If an include is part of the preamble patch, translate #line directives. |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 49 | if (InBuiltinFile) |
| 50 | HashLoc = translatePreamblePatchLocation(HashLoc, SM); |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 51 | |
| 52 | // Record main-file inclusions (including those mapped from the preamble |
| 53 | // patch). |
Haojian Wu | 6ae86ea | 2019-07-19 08:33:39 +0000 | [diff] [blame] | 54 | if (isInsideMainFile(HashLoc, SM)) { |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 55 | Out->MainFileIncludes.emplace_back(); |
| 56 | auto &Inc = Out->MainFileIncludes.back(); |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 57 | Inc.Written = |
| 58 | (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str(); |
Viktoriia Bakalova | 7322f2d | 2023-07-11 13:33:53 +0000 | [diff] [blame] | 59 | Inc.Resolved = std::string( |
| 60 | File ? getCanonicalPath(*File, SM.getFileManager()).value_or("") |
| 61 | : ""); |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 62 | Inc.HashOffset = SM.getFileOffset(HashLoc); |
Kadir Cetinkaya | d870016 | 2020-05-04 10:48:19 +0200 | [diff] [blame] | 63 | Inc.HashLine = |
| 64 | SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1; |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 65 | Inc.FileKind = FileKind; |
Kadir Cetinkaya | 6e01718 | 2020-04-15 22:00:19 +0200 | [diff] [blame] | 66 | Inc.Directive = IncludeTok.getIdentifierInfo()->getPPKeywordID(); |
Sam McCall | 478863e | 2021-11-17 13:27:58 +0100 | [diff] [blame] | 67 | if (File) { |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 68 | IncludeStructure::HeaderID HID = Out->getOrCreateID(*File); |
Sam McCall | 478863e | 2021-11-17 13:27:58 +0100 | [diff] [blame] | 69 | Inc.HeaderID = static_cast<unsigned>(HID); |
| 70 | if (IsAngled) |
Kirill Bobyrev | 46a6f5a | 2022-02-09 11:04:58 +0100 | [diff] [blame] | 71 | if (auto StdlibHeader = tooling::stdlib::Header::named(Inc.Written)) { |
Sam McCall | 478863e | 2021-11-17 13:27:58 +0100 | [diff] [blame] | 72 | auto &IDs = Out->StdlibHeaders[*StdlibHeader]; |
| 73 | // Few physical files for one stdlib header name, linear scan is ok. |
| 74 | if (!llvm::is_contained(IDs, HID)) |
| 75 | IDs.push_back(HID); |
| 76 | } |
| 77 | } |
Kazu Hirata | 3f156ef | 2024-10-11 09:00:39 -0700 | [diff] [blame] | 78 | Out->MainFileIncludesBySpelling[Inc.Written].push_back( |
| 79 | Out->MainFileIncludes.size() - 1); |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 80 | } |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 81 | |
| 82 | // Record include graph (not just for main-file includes) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 83 | if (File) { |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 84 | auto IncludingFileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 85 | if (!IncludingFileEntry) { |
Kazu Hirata | d5953e3 | 2023-12-13 23:26:09 -0800 | [diff] [blame] | 86 | assert(SM.getBufferName(HashLoc).starts_with("<") && |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 87 | "Expected #include location to be a file or <built-in>"); |
| 88 | // Treat as if included from the main file. |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 89 | IncludingFileEntry = SM.getFileEntryRefForID(MainFID); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 90 | } |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 91 | auto IncludingID = Out->getOrCreateID(*IncludingFileEntry), |
| 92 | IncludedID = Out->getOrCreateID(*File); |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 93 | Out->IncludeChildren[IncludingID].push_back(IncludedID); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 94 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 97 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 98 | SrcMgr::CharacteristicKind FileType, |
| 99 | FileID PrevFID) override { |
| 100 | switch (Reason) { |
| 101 | case PPCallbacks::EnterFile: |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 102 | ++Level; |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 103 | if (BuiltinFile.isInvalid() && SM.isWrittenInBuiltinFile(Loc)) { |
| 104 | BuiltinFile = SM.getFileID(Loc); |
| 105 | InBuiltinFile = true; |
| 106 | } |
| 107 | break; |
Kirill Bobyrev | cd0ca5a | 2021-11-26 14:12:35 +0100 | [diff] [blame] | 108 | case PPCallbacks::ExitFile: { |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 109 | --Level; |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 110 | if (PrevFID == BuiltinFile) |
| 111 | InBuiltinFile = false; |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 112 | break; |
Kirill Bobyrev | cd0ca5a | 2021-11-26 14:12:35 +0100 | [diff] [blame] | 113 | } |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 114 | case PPCallbacks::RenameFile: |
| 115 | case PPCallbacks::SystemHeaderPragma: |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 120 | private: |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 121 | // Keeps track of include depth for the current file. It's 1 for main file. |
| 122 | int Level = 0; |
| 123 | bool inMainFile() const { return Level == 1; } |
| 124 | |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 125 | const SourceManager &SM; |
Kadir Cetinkaya | 6d6d48a | 2020-05-06 16:20:31 +0200 | [diff] [blame] | 126 | // Set after entering the <built-in> file. |
| 127 | FileID BuiltinFile; |
| 128 | // Indicates whether <built-in> file is part of include stack. |
| 129 | bool InBuiltinFile = false; |
| 130 | |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 131 | IncludeStructure *Out; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 134 | bool isLiteralInclude(llvm::StringRef Include) { |
Kazu Hirata | d5953e3 | 2023-12-13 23:26:09 -0800 | [diff] [blame] | 135 | return Include.starts_with("<") || Include.starts_with("\""); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool HeaderFile::valid() const { |
| 139 | return (Verbatim && isLiteralInclude(File)) || |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 140 | (!Verbatim && llvm::sys::path::is_absolute(File)); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 143 | llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, |
| 144 | llvm::StringRef HintPath) { |
| 145 | if (isLiteralInclude(Header)) |
| 146 | return HeaderFile{Header.str(), /*Verbatim=*/true}; |
| 147 | auto U = URI::parse(Header); |
| 148 | if (!U) |
| 149 | return U.takeError(); |
| 150 | |
| 151 | auto IncludePath = URI::includeSpelling(*U); |
| 152 | if (!IncludePath) |
| 153 | return IncludePath.takeError(); |
| 154 | if (!IncludePath->empty()) |
| 155 | return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true}; |
| 156 | |
| 157 | auto Resolved = URI::resolve(*U, HintPath); |
| 158 | if (!Resolved) |
| 159 | return Resolved.takeError(); |
| 160 | return HeaderFile{std::move(*Resolved), /*Verbatim=*/false}; |
| 161 | } |
| 162 | |
David Goldman | 51f1ae5 | 2022-12-02 10:04:31 -0500 | [diff] [blame] | 163 | llvm::SmallVector<SymbolInclude, 1> getRankedIncludes(const Symbol &Sym) { |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 164 | auto Includes = Sym.IncludeHeaders; |
| 165 | // Sort in descending order by reference count and header length. |
| 166 | llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS, |
| 167 | const Symbol::IncludeHeaderWithReferences &RHS) { |
| 168 | if (LHS.References == RHS.References) |
| 169 | return LHS.IncludeHeader.size() < RHS.IncludeHeader.size(); |
| 170 | return LHS.References > RHS.References; |
| 171 | }); |
David Goldman | 51f1ae5 | 2022-12-02 10:04:31 -0500 | [diff] [blame] | 172 | llvm::SmallVector<SymbolInclude, 1> Headers; |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 173 | for (const auto &Include : Includes) |
David Goldman | 51f1ae5 | 2022-12-02 10:04:31 -0500 | [diff] [blame] | 174 | Headers.push_back({Include.IncludeHeader, Include.supportedDirectives()}); |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 175 | return Headers; |
| 176 | } |
| 177 | |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 178 | void IncludeStructure::collect(const CompilerInstance &CI) { |
Kirill Bobyrev | cd0ca5a | 2021-11-26 14:12:35 +0100 | [diff] [blame] | 179 | auto &SM = CI.getSourceManager(); |
Kirill Bobyrev | b06df22 | 2021-10-04 08:39:06 +0200 | [diff] [blame] | 180 | MainFileEntry = SM.getFileEntryForID(SM.getMainFileID()); |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 181 | auto Collector = std::make_unique<RecordHeaders>(CI, this); |
Kirill Bobyrev | eecfc73 | 2021-12-08 15:55:39 +0100 | [diff] [blame] | 182 | CI.getPreprocessor().addPPCallbacks(std::move(Collector)); |
Sam McCall | d97a341 | 2023-07-20 21:46:52 +0200 | [diff] [blame] | 183 | |
| 184 | // If we're reusing a preamble, don't repopulate SearchPathsCanonical. |
| 185 | // The entries will be the same, but canonicalizing to find out is expensive! |
| 186 | if (SearchPathsCanonical.empty()) { |
| 187 | for (const auto &Dir : |
| 188 | CI.getPreprocessor().getHeaderSearchInfo().search_dir_range()) { |
| 189 | if (Dir.getLookupType() == DirectoryLookup::LT_NormalDir) |
| 190 | SearchPathsCanonical.emplace_back( |
| 191 | SM.getFileManager().getCanonicalName(*Dir.getDirRef())); |
| 192 | } |
| 193 | } |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 196 | std::optional<IncludeStructure::HeaderID> |
Kirill Bobyrev | b06df22 | 2021-10-04 08:39:06 +0200 | [diff] [blame] | 197 | IncludeStructure::getID(const FileEntry *Entry) const { |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 198 | // HeaderID of the main file is always 0; |
Kirill Bobyrev | b06df22 | 2021-10-04 08:39:06 +0200 | [diff] [blame] | 199 | if (Entry == MainFileEntry) { |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 200 | return static_cast<IncludeStructure::HeaderID>(0u); |
| 201 | } |
| 202 | auto It = UIDToIndex.find(Entry->getUniqueID()); |
| 203 | if (It == UIDToIndex.end()) |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 204 | return std::nullopt; |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 205 | return It->second; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kirill Bobyrev | 40f361a | 2022-05-16 10:13:38 +0200 | [diff] [blame] | 208 | IncludeStructure::HeaderID IncludeStructure::getOrCreateID(FileEntryRef Entry) { |
Kirill Bobyrev | b06df22 | 2021-10-04 08:39:06 +0200 | [diff] [blame] | 209 | // Main file's FileEntry was not known at IncludeStructure creation time. |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 210 | if (&Entry.getFileEntry() == MainFileEntry) { |
Kirill Bobyrev | b06df22 | 2021-10-04 08:39:06 +0200 | [diff] [blame] | 211 | if (RealPathNames.front().empty()) |
| 212 | RealPathNames.front() = MainFileEntry->tryGetRealPathName().str(); |
| 213 | return MainFileID; |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 214 | } |
| 215 | auto R = UIDToIndex.try_emplace( |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 216 | Entry.getUniqueID(), |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 217 | static_cast<IncludeStructure::HeaderID>(RealPathNames.size())); |
Kirill Bobyrev | 230a6ed | 2021-09-30 11:36:34 +0200 | [diff] [blame] | 218 | if (R.second) |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 219 | RealPathNames.emplace_back(); |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 220 | IncludeStructure::HeaderID Result = R.first->getSecond(); |
| 221 | std::string &RealPathName = RealPathNames[static_cast<unsigned>(Result)]; |
| 222 | if (RealPathName.empty()) |
Jan Svoboda | d79ad2f | 2022-04-11 12:10:05 +0200 | [diff] [blame] | 223 | RealPathName = Entry.getFileEntry().tryGetRealPathName().str(); |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 224 | return Result; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 227 | llvm::DenseMap<IncludeStructure::HeaderID, unsigned> |
| 228 | IncludeStructure::includeDepth(HeaderID Root) const { |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 229 | // Include depth 0 is the main file only. |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 230 | llvm::DenseMap<HeaderID, unsigned> Result; |
| 231 | assert(static_cast<unsigned>(Root) < RealPathNames.size()); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 232 | Result[Root] = 0; |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 233 | std::vector<IncludeStructure::HeaderID> CurrentLevel; |
| 234 | CurrentLevel.push_back(Root); |
| 235 | llvm::DenseSet<IncludeStructure::HeaderID> Seen; |
| 236 | Seen.insert(Root); |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 237 | |
| 238 | // Each round of BFS traversal finds the next depth level. |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 239 | std::vector<IncludeStructure::HeaderID> PreviousLevel; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 240 | for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) { |
| 241 | PreviousLevel.clear(); |
| 242 | PreviousLevel.swap(CurrentLevel); |
| 243 | for (const auto &Parent : PreviousLevel) { |
| 244 | for (const auto &Child : IncludeChildren.lookup(Parent)) { |
| 245 | if (Seen.insert(Child).second) { |
| 246 | CurrentLevel.push_back(Child); |
Kirill Bobyrev | dea4807 | 2021-09-30 14:41:27 +0200 | [diff] [blame] | 247 | Result[Child] = Level; |
Sam McCall | 3f0243f | 2018-07-03 08:09:29 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | return Result; |
Eric Liu | 155f5a4 | 2018-05-14 12:19:16 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Viktoriia Bakalova | e028c97 | 2023-02-07 16:52:51 +0000 | [diff] [blame] | 255 | llvm::SmallVector<const Inclusion *> |
| 256 | IncludeStructure::mainFileIncludesWithSpelling(llvm::StringRef Spelling) const { |
| 257 | llvm::SmallVector<const Inclusion *> Includes; |
| 258 | for (auto Idx : MainFileIncludesBySpelling.lookup(Spelling)) |
| 259 | Includes.push_back(&MainFileIncludes[Idx]); |
| 260 | return Includes; |
| 261 | } |
| 262 | |
Eric Liu | fd9f426 | 2018-09-27 14:27:02 +0000 | [diff] [blame] | 263 | void IncludeInserter::addExisting(const Inclusion &Inc) { |
| 264 | IncludedHeaders.insert(Inc.Written); |
| 265 | if (!Inc.Resolved.empty()) |
| 266 | IncludedHeaders.insert(Inc.Resolved); |
| 267 | } |
| 268 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 269 | /// FIXME(ioeric): we might not want to insert an absolute include path if the |
| 270 | /// path is not shortened. |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 271 | bool IncludeInserter::shouldInsertInclude( |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 272 | PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const { |
| 273 | assert(InsertedHeader.valid()); |
Eric Liu | 00d99bd | 2019-04-11 09:36:36 +0000 | [diff] [blame] | 274 | if (!HeaderSearchInfo && !InsertedHeader.Verbatim) |
| 275 | return false; |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 276 | if (FileName == DeclaringHeader || FileName == InsertedHeader.File) |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 277 | return false; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 278 | auto Included = [&](llvm::StringRef Header) { |
Kazu Hirata | 15aa965 | 2023-03-14 21:30:29 -0700 | [diff] [blame] | 279 | return IncludedHeaders.contains(Header); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 280 | }; |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 281 | return !Included(DeclaringHeader) && !Included(InsertedHeader.File); |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 282 | } |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 283 | |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 284 | std::optional<std::string> |
Kadir Cetinkaya | 1f6d984 | 2019-07-03 07:47:19 +0000 | [diff] [blame] | 285 | IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader, |
| 286 | llvm::StringRef IncludingFile) const { |
Eric Liu | 417c889 | 2019-04-16 14:35:49 +0000 | [diff] [blame] | 287 | assert(InsertedHeader.valid()); |
Eric Liu | 6c8e858 | 2018-02-26 08:32:13 +0000 | [diff] [blame] | 288 | if (InsertedHeader.Verbatim) |
| 289 | return InsertedHeader.File; |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 290 | bool IsAngledByDefault = false; |
Sam McCall | b324c64 | 2019-07-08 18:07:46 +0000 | [diff] [blame] | 291 | std::string Suggested; |
| 292 | if (HeaderSearchInfo) { |
| 293 | Suggested = HeaderSearchInfo->suggestPathToFileForDiagnostics( |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 294 | InsertedHeader.File, BuildDir, IncludingFile, &IsAngledByDefault); |
Sam McCall | b324c64 | 2019-07-08 18:07:46 +0000 | [diff] [blame] | 295 | } else { |
| 296 | // Calculate include relative to including file only. |
| 297 | StringRef IncludingDir = llvm::sys::path::parent_path(IncludingFile); |
| 298 | SmallString<256> RelFile(InsertedHeader.File); |
| 299 | // Replacing with "" leaves "/RelFile" if IncludingDir doesn't end in "/". |
| 300 | llvm::sys::path::replace_path_prefix(RelFile, IncludingDir, "./"); |
| 301 | Suggested = llvm::sys::path::convert_to_slash( |
| 302 | llvm::sys::path::remove_leading_dotslash(RelFile)); |
| 303 | } |
| 304 | // FIXME: should we allow (some limited number of) "../header.h"? |
| 305 | if (llvm::sys::path::is_absolute(Suggested)) |
Kazu Hirata | 059a23c | 2022-12-03 11:54:50 -0800 | [diff] [blame] | 306 | return std::nullopt; |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 307 | bool IsAngled = false; |
| 308 | for (auto Filter : AngledHeaders) { |
| 309 | if (Filter(Suggested)) { |
| 310 | IsAngled = true; |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | bool IsQuoted = false; |
| 315 | for (auto Filter : QuotedHeaders) { |
| 316 | if (Filter(Suggested)) { |
| 317 | IsQuoted = true; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | // No filters apply, or both filters apply (a bug), use system default. |
| 322 | if (IsAngled == IsQuoted) { |
| 323 | // Probably a bug in the config regex. |
| 324 | if (IsAngled && IsQuoted) { |
| 325 | elog("Header '{0}' matches both quoted and angled regexes, default will " |
| 326 | "be used.", |
| 327 | Suggested); |
| 328 | } |
| 329 | IsAngled = IsAngledByDefault; |
| 330 | } |
David Goldman | 9fe632b | 2023-07-31 11:36:22 -0400 | [diff] [blame] | 331 | if (IsAngled) |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 332 | Suggested = "<" + Suggested + ">"; |
kleines Filmröllchen | 1f90797 | 2024-12-27 20:14:29 +0100 | [diff] [blame] | 333 | else // if (IsQuoted) |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 334 | Suggested = "\"" + Suggested + "\""; |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 335 | return Suggested; |
| 336 | } |
| 337 | |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 338 | std::optional<TextEdit> |
David Goldman | fc46d6e | 2022-12-02 10:04:51 -0500 | [diff] [blame] | 339 | IncludeInserter::insert(llvm::StringRef VerbatimHeader, |
| 340 | tooling::IncludeDirective Directive) const { |
Kazu Hirata | f71ffd3 | 2023-01-07 20:19:42 -0800 | [diff] [blame] | 341 | std::optional<TextEdit> Edit; |
David Goldman | fc46d6e | 2022-12-02 10:04:51 -0500 | [diff] [blame] | 342 | if (auto Insertion = |
| 343 | Inserter.insert(VerbatimHeader.trim("\"<>"), |
Kazu Hirata | d5953e3 | 2023-12-13 23:26:09 -0800 | [diff] [blame] | 344 | VerbatimHeader.starts_with("<"), Directive)) |
Eric Liu | 8f3678d | 2018-06-15 13:34:18 +0000 | [diff] [blame] | 345 | Edit = replacementToEdit(Code, *Insertion); |
| 346 | return Edit; |
Eric Liu | 63f419a | 2018-05-15 15:29:32 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 349 | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Inclusion &Inc) { |
| 350 | return OS << Inc.Written << " = " |
Kadir Cetinkaya | d870016 | 2020-05-04 10:48:19 +0200 | [diff] [blame] | 351 | << (!Inc.Resolved.empty() ? Inc.Resolved : "[unresolved]") |
| 352 | << " at line" << Inc.HashLine; |
Sam McCall | 991e316 | 2018-11-20 10:58:48 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 355 | bool operator==(const Inclusion &LHS, const Inclusion &RHS) { |
| 356 | return std::tie(LHS.Directive, LHS.FileKind, LHS.HashOffset, LHS.HashLine, |
| 357 | LHS.Resolved, LHS.Written) == |
| 358 | std::tie(RHS.Directive, RHS.FileKind, RHS.HashOffset, RHS.HashLine, |
| 359 | RHS.Resolved, RHS.Written); |
| 360 | } |
Sam McCall | 478863e | 2021-11-17 13:27:58 +0100 | [diff] [blame] | 361 | |
Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 362 | } // namespace clangd |
| 363 | } // namespace clang |