clang 10.0.0git

IndexingAction.cpp
Go to the documentation of this file.
1 //===- IndexingAction.cpp - Frontend index action -------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "clang/Index/IndexingAction.h"
10 #include "IndexingContext.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/Frontend/FrontendAction.h"
13 #include "clang/Frontend/MultiplexConsumer.h"
14 #include "clang/Index/IndexDataConsumer.h"
15 #include "clang/Lex/PPCallbacks.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Serialization/ASTReader.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include <memory>
20
21 using namespace clang;
22 using namespace clang::index;
23
24 namespace {
25
26 class IndexPPCallbacks final : public PPCallbacks {
27 std::shared_ptr<IndexingContext> IndexCtx;
28
29 public:
30 IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
31 : IndexCtx(std::move(IndexCtx)) {}
32
33 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
34 SourceRange Range, const MacroArgs *Args) override {
35 IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
36 Range.getBegin(), *MD.getMacroInfo());
37 }
38
39 void MacroDefined(const Token &MacroNameTok,
40 const MacroDirective *MD) override {
41 IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
42 MacroNameTok.getLocation(),
43 *MD->getMacroInfo());
44 }
45
46 void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
47 const MacroDirective *Undef) override {
48 if (!MD.getMacroInfo()) // Ignore noop #undef.
49 return;
50 IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(),
51 MacroNameTok.getLocation(),
52 *MD.getMacroInfo());
53 }
54 };
55
56 class IndexASTConsumer final : public ASTConsumer {
57 std::shared_ptr<IndexDataConsumer> DataConsumer;
58 std::shared_ptr<IndexingContext> IndexCtx;
59 std::shared_ptr<Preprocessor> PP;
60 std::function<bool(const Decl *)> ShouldSkipFunctionBody;
61
62 public:
63 IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
64 const IndexingOptions &Opts,
65 std::shared_ptr<Preprocessor> PP,
66 std::function<bool(const Decl *)> ShouldSkipFunctionBody)
67 : DataConsumer(std::move(DataConsumer)),
68 IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
69 PP(std::move(PP)),
70 ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)) {
71 assert(this->DataConsumer != nullptr);
72 assert(this->PP != nullptr);
73 }
74
75 protected:
76 void Initialize(ASTContext &Context) override {
77 IndexCtx->setASTContext(Context);
78 IndexCtx->getDataConsumer().initialize(Context);
79 IndexCtx->getDataConsumer().setPreprocessor(PP);
80 PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
81 }
82
83 bool HandleTopLevelDecl(DeclGroupRef DG) override {
84 return IndexCtx->indexDeclGroupRef(DG);
85 }
86
87 void HandleInterestingDecl(DeclGroupRef DG) override {
88 // Ignore deserialized decls.
89 }
90
91 void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
92 IndexCtx->indexDeclGroupRef(DG);
93 }
94
95 void HandleTranslationUnit(ASTContext &Ctx) override {
96 DataConsumer->finish();
97 }
98
99 bool shouldSkipFunctionBody(Decl *D) override {
100 return ShouldSkipFunctionBody(D);
101 }
102 };
103
104 class IndexAction final : public ASTFrontendAction {
105 std::shared_ptr<IndexDataConsumer> DataConsumer;
106 IndexingOptions Opts;
107
108 public:
109 IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
110 const IndexingOptions &Opts)
111 : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
112 assert(this->DataConsumer != nullptr);
113 }
114
115 protected:
116 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
117 StringRef InFile) override {
118 return std::make_unique<IndexASTConsumer>(
119 DataConsumer, Opts, CI.getPreprocessorPtr(),
120 /*ShouldSkipFunctionBody=*/[](const Decl *) { return false; });
121 }
122 };
123
124 } // anonymous namespace
125
126 std::unique_ptr<ASTConsumer> index::createIndexingASTConsumer(
127 std::shared_ptr<IndexDataConsumer> DataConsumer,
128 const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
129 std::function<bool(const Decl *)> ShouldSkipFunctionBody) {
130 return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
131 ShouldSkipFunctionBody);
132 }
133
134 std::unique_ptr<FrontendAction>
135 index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
136 const IndexingOptions &Opts) {
137 assert(DataConsumer != nullptr);
138 return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
139 }
140
141 static bool topLevelDeclVisitor(void *context, const Decl *D) {
142 IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
143 return IndexCtx.indexTopLevelDecl(D);
144 }
145
146 static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
147 Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor);
148 }
149
150 static void indexPreprocessorMacros(const Preprocessor &PP,
151 IndexDataConsumer &DataConsumer) {
152 for (const auto &M : PP.macros())
153 if (MacroDirective *MD = M.second.getLatest())
154 DataConsumer.handleMacroOccurrence(
155 M.first, MD->getMacroInfo(),
156 static_cast<unsigned>(index::SymbolRole::Definition),
157 MD->getLocation());
158 }
159
160 void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer,
161 IndexingOptions Opts) {
162 IndexingContext IndexCtx(Opts, DataConsumer);
163 IndexCtx.setASTContext(Unit.getASTContext());
164 DataConsumer.initialize(Unit.getASTContext());
165 DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
166
167 if (Opts.IndexMacrosInPreprocessor)
168 indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer);
169 indexTranslationUnit(Unit, IndexCtx);
170 DataConsumer.finish();
171 }
172
173 void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP,
174 ArrayRef<const Decl *> Decls,
175 IndexDataConsumer &DataConsumer,
176 IndexingOptions Opts) {
177 IndexingContext IndexCtx(Opts, DataConsumer);
178 IndexCtx.setASTContext(Ctx);
179
180 DataConsumer.initialize(Ctx);
181
182 if (Opts.IndexMacrosInPreprocessor)
183 indexPreprocessorMacros(PP, DataConsumer);
184
185 for (const Decl *D : Decls)
186 IndexCtx.indexTopLevelDecl(D);
187 DataConsumer.finish();
188 }
189
190 std::unique_ptr<PPCallbacks>
191 index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) {
192 return std::make_unique<IndexPPCallbacks>(
193 std::make_shared<IndexingContext>(Opts, Consumer));
194 }
195
196 void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader,
197 IndexDataConsumer &DataConsumer,
198 IndexingOptions Opts) {
199 ASTContext &Ctx = Reader.getContext();
200 IndexingContext IndexCtx(Opts, DataConsumer);
201 IndexCtx.setASTContext(Ctx);
202 DataConsumer.initialize(Ctx);
203
204 if (Opts.IndexMacrosInPreprocessor)
205 indexPreprocessorMacros(Reader.getPreprocessor(), DataConsumer);
206
207 for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
208 IndexCtx.indexTopLevelDecl(D);
209 }
210 DataConsumer.finish();
211 }
clang::index::IndexDataConsumer::setPreprocessor
virtual void setPreprocessor(std::shared_ptr< Preprocessor > PP)
Definition: IndexDataConsumer.h:39
clang::ASTConsumer
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:33
clang::ASTUnit::getPreprocessor
const Preprocessor & getPreprocessor() const
Definition: ASTUnit.h:435
indexPreprocessorMacros
static void indexPreprocessorMacros(const Preprocessor &PP, IndexDataConsumer &DataConsumer)
Definition: IndexingAction.cpp:150
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::index::IndexingOptions
Definition: IndexingOptions.h:19
clang::MacroDefinition
A description of the current definition of a macro.
Definition: MacroInfo.h:564
clang::index::IndexingContext::indexTopLevelDecl
bool indexTopLevelDecl(const Decl *D)
Definition: IndexDecl.cpp:756
ASTReader.h
IndexDataConsumer.h
clang::index::IndexingOptions::IndexMacrosInPreprocessor
bool IndexMacrosInPreprocessor
Definition: IndexingOptions.h:33
clang::index::IndexingContext::setASTContext
void setASTContext(ASTContext &ctx)
Definition: IndexingContext.h:50
clang::PPCallbacks
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:35
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::ASTUnit
Utility class for loading a ASTContext from an AST file.
Definition: ASTUnit.h:89
std
Definition: Format.h:2445
clang::index::indexASTUnit
void indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all decls in the AST.
Definition: IndexingAction.cpp:160
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::CompilerInstance::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr()
Definition: CompilerInstance.h:449
clang::index::indexModuleFile
void indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all top-level decls in the module.
Definition: IndexingAction.cpp:196
clang::ASTReader::getModuleFileLevelDecls
llvm::iterator_range< ModuleDeclIterator > getModuleFileLevelDecls(ModuleFile &Mod)
Definition: ASTReader.cpp:5872
clang::ASTReader::getContext
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
Definition: ASTReader.h:2214
clang::index::IndexDataConsumer
Definition: IndexDataConsumer.h:26
IndexingContext.h
llvm::ArrayRef
Definition: LLVM.h:31
clang::index::SymbolRole::Definition
CompilerInstance.h
clang::index::IndexingContext
Definition: IndexingContext.h:38
clang::MacroArgs
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:29
clang::Token::getLocation
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:126
Preprocessor.h
Defines the clang::Preprocessor interface.
clang::serialization::ModuleFile
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:107
clang::index::IndexDataConsumer::handleMacroOccurrence
virtual bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, SymbolRoleSet Roles, SourceLocation Loc)
Definition: IndexDataConsumer.h:49
clang::index::IndexDataConsumer::initialize
virtual void initialize(ASTContext &Ctx)
Definition: IndexDataConsumer.h:37
clang::MacroDirective
Encapsulates changes to the "macros namespace" (the location where the macro name became active...
Definition: MacroInfo.h:290
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::index::createIndexingAction
std::unique_ptr< FrontendAction > createIndexingAction(std::shared_ptr< IndexDataConsumer > DataConsumer, const IndexingOptions &Opts)
Creates a frontend action that indexes all symbols (macros and AST decls).
Definition: IndexingAction.cpp:135
clang::Token::getIdentifierInfo
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:179
clang::index
Definition: CommentToXML.h:23
clang::ASTFrontendAction
Abstract base class to use for AST consumer-based frontend actions.
Definition: FrontendAction.h:243
clang::ASTUnit::visitLocalTopLevelDecls
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
Definition: ASTUnit.cpp:2610
clang::MacroDirective::getMacroInfo
const MacroInfo * getMacroInfo() const
Definition: MacroInfo.h:390
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:340
clang::index::indexTopLevelDecls
void indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP, ArrayRef< const Decl *> Decls, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes Decls.
Definition: IndexingAction.cpp:173
clang::DeclGroupRef
Definition: DeclGroup.h:51
clang::index::createIndexingASTConsumer
std::unique_ptr< ASTConsumer > createIndexingASTConsumer(std::shared_ptr< IndexDataConsumer > DataConsumer, const IndexingOptions &Opts, std::shared_ptr< Preprocessor > PP, std::function< bool(const Decl *)> ShouldSkipFunctionBody)
Creates an ASTConsumer that indexes all symbols (macros and AST decls).
Definition: IndexingAction.cpp:126
clang::Preprocessor::macros
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
Definition: Preprocessor.h:1146
IndexingAction.h
clang::index::IndexDataConsumer::finish
virtual void finish()
Definition: IndexDataConsumer.h:66
clang::ASTUnit::getPreprocessorPtr
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
Definition: ASTUnit.h:437
clang::ASTReader::getPreprocessor
Preprocessor & getPreprocessor() const
Retrieve the preprocessor.
Definition: ASTReader.h:1658
MultiplexConsumer.h
clang::ASTUnit::getASTContext
const ASTContext & getASTContext() const
Definition: ASTUnit.h:439
PPCallbacks.h
Defines the PPCallbacks interface.
indexTranslationUnit
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx)
Definition: IndexingAction.cpp:146
clang::index::indexMacrosCallback
std::unique_ptr< PPCallbacks > indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts)
Creates a PPCallbacks that indexes macros and feeds macros to Consumer.
Definition: IndexingAction.cpp:191
topLevelDeclVisitor
static bool topLevelDeclVisitor(void *context, const Decl *D)
Definition: IndexingAction.cpp:141
FrontendAction.h
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
clang::MacroDefinition::getMacroInfo
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
Definition: MacroInfo.h:580
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:207
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:216
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128