blob: 50c375988f7b255ce95d1997cfaa62913e7e37c2 [file] [log] [blame]
Eric Liuc5105f92018-02-16 14:15:55 +00001//===--- Headers.cpp - Include headers ---------------------------*- C++-*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Liuc5105f92018-02-16 14:15:55 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Headers.h"
10#include "Compiler.h"
Eric Liu155f5a42018-05-14 12:19:16 +000011#include "SourceCode.h"
Sam McCallad97ccf2020-04-28 17:49:17 +020012#include "support/Logger.h"
Eric Liuc5105f92018-02-16 14:15:55 +000013#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Frontend/CompilerInvocation.h"
15#include "clang/Frontend/FrontendActions.h"
16#include "clang/Lex/HeaderSearch.h"
Kadir Cetinkaya1f6d9842019-07-03 07:47:19 +000017#include "llvm/ADT/StringRef.h"
Eric Liu709bde82018-02-19 18:48:44 +000018#include "llvm/Support/Path.h"
Eric Liuc5105f92018-02-16 14:15:55 +000019
20namespace clang {
21namespace clangd {
22namespace {
23
24class RecordHeaders : public PPCallbacks {
25public:
Sam McCall3f0243f2018-07-03 08:09:29 +000026 RecordHeaders(const SourceManager &SM, IncludeStructure *Out)
27 : SM(SM), Out(Out) {}
Eric Liuc5105f92018-02-16 14:15:55 +000028
Eric Liu155f5a42018-05-14 12:19:16 +000029 // Record existing #includes - both written and resolved paths. Only #includes
30 // in the main file are collected.
Kadir Cetinkaya6e017182020-04-15 22:00:19 +020031 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000032 llvm::StringRef FileName, bool IsAngled,
Kadir Cetinkaya9b509bc2020-05-06 13:16:50 +020033 CharSourceRange FilenameRange, const FileEntry *File,
34 llvm::StringRef /*SearchPath*/,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000035 llvm::StringRef /*RelativePath*/,
Julie Hockett546943f2018-05-10 19:13:14 +000036 const Module * /*Imported*/,
Sam McCall991e3162018-11-20 10:58:48 +000037 SrcMgr::CharacteristicKind FileKind) override {
Haojian Wu6ae86ea2019-07-19 08:33:39 +000038 if (isInsideMainFile(HashLoc, SM)) {
Sam McCall991e3162018-11-20 10:58:48 +000039 Out->MainFileIncludes.emplace_back();
40 auto &Inc = Out->MainFileIncludes.back();
Sam McCall991e3162018-11-20 10:58:48 +000041 Inc.Written =
42 (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
Benjamin Krameradcd0262020-01-28 20:23:46 +010043 Inc.Resolved = std::string(File ? File->tryGetRealPathName() : "");
Sam McCall991e3162018-11-20 10:58:48 +000044 Inc.HashOffset = SM.getFileOffset(HashLoc);
Kadir Cetinkayad8700162020-05-04 10:48:19 +020045 Inc.HashLine =
46 SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
Sam McCall991e3162018-11-20 10:58:48 +000047 Inc.FileKind = FileKind;
Kadir Cetinkaya6e017182020-04-15 22:00:19 +020048 Inc.Directive = IncludeTok.getIdentifierInfo()->getPPKeywordID();
Sam McCall991e3162018-11-20 10:58:48 +000049 }
Sam McCall3f0243f2018-07-03 08:09:29 +000050 if (File) {
51 auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));
52 if (!IncludingFileEntry) {
53 assert(SM.getBufferName(HashLoc).startswith("<") &&
54 "Expected #include location to be a file or <built-in>");
55 // Treat as if included from the main file.
Kadir Cetinkaya9b509bc2020-05-06 13:16:50 +020056 IncludingFileEntry = SM.getFileEntryForID(SM.getMainFileID());
Sam McCall3f0243f2018-07-03 08:09:29 +000057 }
58 Out->recordInclude(IncludingFileEntry->getName(), File->getName(),
59 File->tryGetRealPathName());
60 }
Eric Liuc5105f92018-02-16 14:15:55 +000061 }
62
63private:
Eric Liu155f5a42018-05-14 12:19:16 +000064 const SourceManager &SM;
Sam McCall3f0243f2018-07-03 08:09:29 +000065 IncludeStructure *Out;
Eric Liuc5105f92018-02-16 14:15:55 +000066};
67
68} // namespace
69
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000070bool isLiteralInclude(llvm::StringRef Include) {
Eric Liu6c8e8582018-02-26 08:32:13 +000071 return Include.startswith("<") || Include.startswith("\"");
72}
73
74bool HeaderFile::valid() const {
75 return (Verbatim && isLiteralInclude(File)) ||
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000076 (!Verbatim && llvm::sys::path::is_absolute(File));
Eric Liu6c8e8582018-02-26 08:32:13 +000077}
78
Eric Liudd662772019-01-28 14:01:55 +000079llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header,
80 llvm::StringRef HintPath) {
81 if (isLiteralInclude(Header))
82 return HeaderFile{Header.str(), /*Verbatim=*/true};
83 auto U = URI::parse(Header);
84 if (!U)
85 return U.takeError();
86
87 auto IncludePath = URI::includeSpelling(*U);
88 if (!IncludePath)
89 return IncludePath.takeError();
90 if (!IncludePath->empty())
91 return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
92
93 auto Resolved = URI::resolve(*U, HintPath);
94 if (!Resolved)
95 return Resolved.takeError();
96 return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
97}
98
99llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) {
100 auto Includes = Sym.IncludeHeaders;
101 // Sort in descending order by reference count and header length.
102 llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS,
103 const Symbol::IncludeHeaderWithReferences &RHS) {
104 if (LHS.References == RHS.References)
105 return LHS.IncludeHeader.size() < RHS.IncludeHeader.size();
106 return LHS.References > RHS.References;
107 });
108 llvm::SmallVector<llvm::StringRef, 1> Headers;
109 for (const auto &Include : Includes)
110 Headers.push_back(Include.IncludeHeader);
111 return Headers;
112}
113
Eric Liu155f5a42018-05-14 12:19:16 +0000114std::unique_ptr<PPCallbacks>
Sam McCall3f0243f2018-07-03 08:09:29 +0000115collectIncludeStructureCallback(const SourceManager &SM,
116 IncludeStructure *Out) {
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000117 return std::make_unique<RecordHeaders>(SM, Out);
Sam McCall3f0243f2018-07-03 08:09:29 +0000118}
119
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000120void IncludeStructure::recordInclude(llvm::StringRef IncludingName,
121 llvm::StringRef IncludedName,
122 llvm::StringRef IncludedRealName) {
Sam McCall3f0243f2018-07-03 08:09:29 +0000123 auto Child = fileIndex(IncludedName);
124 if (!IncludedRealName.empty() && RealPathNames[Child].empty())
Benjamin Krameradcd0262020-01-28 20:23:46 +0100125 RealPathNames[Child] = std::string(IncludedRealName);
Sam McCall3f0243f2018-07-03 08:09:29 +0000126 auto Parent = fileIndex(IncludingName);
127 IncludeChildren[Parent].push_back(Child);
128}
129
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130unsigned IncludeStructure::fileIndex(llvm::StringRef Name) {
Sam McCall3f0243f2018-07-03 08:09:29 +0000131 auto R = NameToIndex.try_emplace(Name, RealPathNames.size());
132 if (R.second)
133 RealPathNames.emplace_back();
134 return R.first->getValue();
135}
136
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000137llvm::StringMap<unsigned>
138IncludeStructure::includeDepth(llvm::StringRef Root) const {
Sam McCall3f0243f2018-07-03 08:09:29 +0000139 // Include depth 0 is the main file only.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000140 llvm::StringMap<unsigned> Result;
Sam McCall3f0243f2018-07-03 08:09:29 +0000141 Result[Root] = 0;
142 std::vector<unsigned> CurrentLevel;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000143 llvm::DenseSet<unsigned> Seen;
Sam McCall3f0243f2018-07-03 08:09:29 +0000144 auto It = NameToIndex.find(Root);
145 if (It != NameToIndex.end()) {
146 CurrentLevel.push_back(It->second);
147 Seen.insert(It->second);
148 }
149
150 // Each round of BFS traversal finds the next depth level.
151 std::vector<unsigned> PreviousLevel;
152 for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) {
153 PreviousLevel.clear();
154 PreviousLevel.swap(CurrentLevel);
155 for (const auto &Parent : PreviousLevel) {
156 for (const auto &Child : IncludeChildren.lookup(Parent)) {
157 if (Seen.insert(Child).second) {
158 CurrentLevel.push_back(Child);
159 const auto &Name = RealPathNames[Child];
160 // Can't include files if we don't have their real path.
161 if (!Name.empty())
162 Result[Name] = Level;
163 }
164 }
165 }
166 }
167 return Result;
Eric Liu155f5a42018-05-14 12:19:16 +0000168}
169
Eric Liufd9f4262018-09-27 14:27:02 +0000170void IncludeInserter::addExisting(const Inclusion &Inc) {
171 IncludedHeaders.insert(Inc.Written);
172 if (!Inc.Resolved.empty())
173 IncludedHeaders.insert(Inc.Resolved);
174}
175
Eric Liuc5105f92018-02-16 14:15:55 +0000176/// FIXME(ioeric): we might not want to insert an absolute include path if the
177/// path is not shortened.
Eric Liu8f3678d2018-06-15 13:34:18 +0000178bool IncludeInserter::shouldInsertInclude(
Eric Liu417c8892019-04-16 14:35:49 +0000179 PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const {
180 assert(InsertedHeader.valid());
Eric Liu00d99bd2019-04-11 09:36:36 +0000181 if (!HeaderSearchInfo && !InsertedHeader.Verbatim)
182 return false;
Eric Liu417c8892019-04-16 14:35:49 +0000183 if (FileName == DeclaringHeader || FileName == InsertedHeader.File)
Eric Liu8f3678d2018-06-15 13:34:18 +0000184 return false;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000185 auto Included = [&](llvm::StringRef Header) {
Eric Liu155f5a42018-05-14 12:19:16 +0000186 return IncludedHeaders.find(Header) != IncludedHeaders.end();
Eric Liu6c8e8582018-02-26 08:32:13 +0000187 };
Eric Liu417c8892019-04-16 14:35:49 +0000188 return !Included(DeclaringHeader) && !Included(InsertedHeader.File);
Eric Liu8f3678d2018-06-15 13:34:18 +0000189}
Eric Liuc5105f92018-02-16 14:15:55 +0000190
Sam McCallb324c642019-07-08 18:07:46 +0000191llvm::Optional<std::string>
Kadir Cetinkaya1f6d9842019-07-03 07:47:19 +0000192IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
193 llvm::StringRef IncludingFile) const {
Eric Liu417c8892019-04-16 14:35:49 +0000194 assert(InsertedHeader.valid());
Eric Liu6c8e8582018-02-26 08:32:13 +0000195 if (InsertedHeader.Verbatim)
196 return InsertedHeader.File;
Eric Liu8f3678d2018-06-15 13:34:18 +0000197 bool IsSystem = false;
Sam McCallb324c642019-07-08 18:07:46 +0000198 std::string Suggested;
199 if (HeaderSearchInfo) {
200 Suggested = HeaderSearchInfo->suggestPathToFileForDiagnostics(
201 InsertedHeader.File, BuildDir, IncludingFile, &IsSystem);
202 } else {
203 // Calculate include relative to including file only.
204 StringRef IncludingDir = llvm::sys::path::parent_path(IncludingFile);
205 SmallString<256> RelFile(InsertedHeader.File);
206 // Replacing with "" leaves "/RelFile" if IncludingDir doesn't end in "/".
207 llvm::sys::path::replace_path_prefix(RelFile, IncludingDir, "./");
208 Suggested = llvm::sys::path::convert_to_slash(
209 llvm::sys::path::remove_leading_dotslash(RelFile));
210 }
211 // FIXME: should we allow (some limited number of) "../header.h"?
212 if (llvm::sys::path::is_absolute(Suggested))
213 return None;
Eric Liuc5105f92018-02-16 14:15:55 +0000214 if (IsSystem)
215 Suggested = "<" + Suggested + ">";
216 else
217 Suggested = "\"" + Suggested + "\"";
Eric Liuc5105f92018-02-16 14:15:55 +0000218 return Suggested;
219}
220
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000221llvm::Optional<TextEdit>
222IncludeInserter::insert(llvm::StringRef VerbatimHeader) const {
223 llvm::Optional<TextEdit> Edit = None;
Eric Liu8f3678d2018-06-15 13:34:18 +0000224 if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"),
225 VerbatimHeader.startswith("<")))
226 Edit = replacementToEdit(Code, *Insertion);
227 return Edit;
Eric Liu63f419a2018-05-15 15:29:32 +0000228}
229
Sam McCall991e3162018-11-20 10:58:48 +0000230llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Inclusion &Inc) {
231 return OS << Inc.Written << " = "
Kadir Cetinkayad8700162020-05-04 10:48:19 +0200232 << (!Inc.Resolved.empty() ? Inc.Resolved : "[unresolved]")
233 << " at line" << Inc.HashLine;
Sam McCall991e3162018-11-20 10:58:48 +0000234}
235
Eric Liuc5105f92018-02-16 14:15:55 +0000236} // namespace clangd
237} // namespace clang