IndexAction.cpp
8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//===--- IndexAction.cpp -----------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "IndexAction.h"
#include "Headers.h"
#include "index/Relation.h"
#include "index/SymbolOrigin.h"
#include "support/Logger.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Index/IndexingAction.h"
#include "clang/Index/IndexingOptions.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/STLExtras.h"
#include <functional>
#include <memory>
#include <utility>
namespace clang {
namespace clangd {
namespace {
llvm::Optional<std::string> toURI(const FileEntry *File) {
if (!File)
return llvm::None;
auto AbsolutePath = File->tryGetRealPathName();
if (AbsolutePath.empty())
return llvm::None;
return URI::create(AbsolutePath).toString();
}
// Collects the nodes and edges of include graph during indexing action.
// Important: The graph generated by those callbacks might contain cycles and
// self edges.
struct IncludeGraphCollector : public PPCallbacks {
public:
IncludeGraphCollector(const SourceManager &SM, IncludeGraph &IG)
: SM(SM), IG(IG) {}
// Populates everything except direct includes for a node, which represents
// edges in the include graph and populated in inclusion directive.
// We cannot populate the fields in InclusionDirective because it does not
// have access to the contents of the included file.
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType,
FileID PrevFID) override {
// We only need to process each file once. So we don't care about anything
// but entries.
if (Reason != FileChangeReason::EnterFile)
return;
const auto FileID = SM.getFileID(Loc);
const auto File = SM.getFileEntryForID(FileID);
auto URI = toURI(File);
if (!URI)
return;
auto I = IG.try_emplace(*URI).first;
auto &Node = I->getValue();
// Node has already been populated.
if (Node.URI.data() == I->getKeyData()) {
#ifndef NDEBUG
auto Digest = digestFile(SM, FileID);
assert(Digest && Node.Digest == *Digest &&
"Same file, different digest?");
#endif
return;
}
if (auto Digest = digestFile(SM, FileID))
Node.Digest = std::move(*Digest);
if (FileID == SM.getMainFileID())
Node.Flags |= IncludeGraphNode::SourceFlag::IsTU;
Node.URI = I->getKey();
}
// Add edges from including files to includes.
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
llvm::StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange, const FileEntry *File,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath, const Module *Imported,
SrcMgr::CharacteristicKind FileType) override {
auto IncludeURI = toURI(File);
if (!IncludeURI)
return;
auto IncludingURI = toURI(SM.getFileEntryForID(SM.getFileID(HashLoc)));
if (!IncludingURI)
return;
auto NodeForInclude = IG.try_emplace(*IncludeURI).first->getKey();
auto NodeForIncluding = IG.try_emplace(*IncludingURI);
NodeForIncluding.first->getValue().DirectIncludes.push_back(NodeForInclude);
}
// Sanity check to ensure we have already populated a skipped file.
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) override {
#ifndef NDEBUG
auto URI = toURI(&SkippedFile.getFileEntry());
if (!URI)
return;
auto I = IG.try_emplace(*URI);
assert(!I.second && "File inserted for the first time on skip.");
assert(I.first->getKeyData() == I.first->getValue().URI.data() &&
"Node have not been populated yet");
#endif
}
private:
const SourceManager &SM;
IncludeGraph &IG;
};
// Wraps the index action and reports index data after each translation unit.
class IndexAction : public ASTFrontendAction {
public:
IndexAction(std::shared_ptr<SymbolCollector> C,
std::unique_ptr<CanonicalIncludes> Includes,
const index::IndexingOptions &Opts,
std::function<void(SymbolSlab)> SymbolsCallback,
std::function<void(RefSlab)> RefsCallback,
std::function<void(RelationSlab)> RelationsCallback,
std::function<void(IncludeGraph)> IncludeGraphCallback)
: SymbolsCallback(SymbolsCallback), RefsCallback(RefsCallback),
RelationsCallback(RelationsCallback),
IncludeGraphCallback(IncludeGraphCallback), Collector(C),
Includes(std::move(Includes)), Opts(Opts),
PragmaHandler(collectIWYUHeaderMaps(this->Includes.get())) {
this->Opts.ShouldTraverseDecl = [this](const Decl *D) {
auto &SM = D->getASTContext().getSourceManager();
auto FID = SM.getFileID(SM.getExpansionLoc(D->getLocation()));
if (!FID.isValid())
return true;
return Collector->shouldIndexFile(FID);
};
}
std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
CI.getPreprocessor().addCommentHandler(PragmaHandler.get());
Includes->addSystemHeadersMapping(CI.getLangOpts());
if (IncludeGraphCallback != nullptr)
CI.getPreprocessor().addPPCallbacks(
std::make_unique<IncludeGraphCollector>(CI.getSourceManager(), IG));
return index::createIndexingASTConsumer(Collector, Opts,
CI.getPreprocessorPtr());
}
bool BeginInvocation(CompilerInstance &CI) override {
// We want all comments, not just the doxygen ones.
CI.getLangOpts().CommentOpts.ParseAllComments = true;
CI.getLangOpts().RetainCommentsFromSystemHeaders = true;
// Index the whole file even if there are warnings and -Werror is set.
// Avoids some analyses too. Set in two places as we're late to the party.
CI.getDiagnosticOpts().IgnoreWarnings = true;
CI.getDiagnostics().setIgnoreAllWarnings(true);
// Instruct the parser to ask our ASTConsumer if it should skip function
// bodies. The ASTConsumer will take care of skipping only functions inside
// the files that we have already processed.
CI.getFrontendOpts().SkipFunctionBodies = true;
return true;
}
void EndSourceFileAction() override {
SymbolsCallback(Collector->takeSymbols());
if (RefsCallback != nullptr)
RefsCallback(Collector->takeRefs());
if (RelationsCallback != nullptr)
RelationsCallback(Collector->takeRelations());
if (IncludeGraphCallback != nullptr) {
#ifndef NDEBUG
// This checks if all nodes are initialized.
for (const auto &Node : IG)
assert(Node.getKeyData() == Node.getValue().URI.data());
#endif
IncludeGraphCallback(std::move(IG));
}
}
private:
std::function<void(SymbolSlab)> SymbolsCallback;
std::function<void(RefSlab)> RefsCallback;
std::function<void(RelationSlab)> RelationsCallback;
std::function<void(IncludeGraph)> IncludeGraphCallback;
std::shared_ptr<SymbolCollector> Collector;
std::unique_ptr<CanonicalIncludes> Includes;
index::IndexingOptions Opts;
std::unique_ptr<CommentHandler> PragmaHandler;
IncludeGraph IG;
};
} // namespace
std::unique_ptr<FrontendAction> createStaticIndexingAction(
SymbolCollector::Options Opts,
std::function<void(SymbolSlab)> SymbolsCallback,
std::function<void(RefSlab)> RefsCallback,
std::function<void(RelationSlab)> RelationsCallback,
std::function<void(IncludeGraph)> IncludeGraphCallback) {
index::IndexingOptions IndexOpts;
IndexOpts.SystemSymbolFilter =
index::IndexingOptions::SystemSymbolFilterKind::All;
Opts.CollectIncludePath = true;
if (Opts.Origin == SymbolOrigin::Unknown)
Opts.Origin = SymbolOrigin::Static;
Opts.StoreAllDocumentation = false;
if (RefsCallback != nullptr) {
Opts.RefFilter = RefKind::All;
Opts.RefsInHeaders = true;
}
auto Includes = std::make_unique<CanonicalIncludes>();
Opts.Includes = Includes.get();
return std::make_unique<IndexAction>(
std::make_shared<SymbolCollector>(std::move(Opts)), std::move(Includes),
IndexOpts, SymbolsCallback, RefsCallback, RelationsCallback,
IncludeGraphCallback);
}
} // namespace clangd
} // namespace clang