27 std::shared_ptr<IndexingContext> IndexCtx;
30 IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
31 : IndexCtx(std::move(IndexCtx)) {}
33 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
34 SourceRange Range,
const MacroArgs *Args)
override {
39 void MacroDefined(
const Token &MacroNameTok,
40 const MacroDirective *MD)
override {
46 void MacroUndefined(
const Token &MacroNameTok,
const MacroDefinition &MD,
47 const MacroDirective *Undef)
override {
55 void Defined(
const Token &MacroNameTok,
const MacroDefinition &MD,
56 SourceRange Range)
override {
64 void Ifdef(SourceLocation Loc,
const Token &MacroNameTok,
65 const MacroDefinition &MD)
override {
72 void Ifndef(SourceLocation Loc,
const Token &MacroNameTok,
73 const MacroDefinition &MD)
override {
83 void Elifdef(SourceLocation Loc,
const Token &MacroNameTok,
84 const MacroDefinition &MD)
override {
91 void Elifndef(SourceLocation Loc,
const Token &MacroNameTok,
92 const MacroDefinition &MD)
override {
102 std::shared_ptr<IndexDataConsumer> DataConsumer;
103 std::shared_ptr<IndexingContext> IndexCtx;
104 std::shared_ptr<Preprocessor> PP;
106 bool DeferIndexingToEndOfTranslationUnit;
109 IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
110 const IndexingOptions &Opts,
111 std::shared_ptr<Preprocessor> PP,
113 : DataConsumer(std::move(DataConsumer)),
114 IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
116 ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)),
117 DeferIndexingToEndOfTranslationUnit(
118 Opts.DeferIndexingToEndOfTranslationUnit) {
119 assert(this->DataConsumer !=
nullptr);
120 assert(this->PP !=
nullptr);
124 void Initialize(ASTContext &Context)
override {
125 IndexCtx->setASTContext(Context);
126 IndexCtx->getDataConsumer().initialize(Context);
127 IndexCtx->getDataConsumer().setPreprocessor(PP);
128 PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
131 bool HandleTopLevelDecl(DeclGroupRef DG)
override {
132 if (!DeferIndexingToEndOfTranslationUnit)
133 return IndexCtx->indexDeclGroupRef(DG);
137 void HandleInterestingDecl(DeclGroupRef DG)
override {
141 void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG)
override {
142 if (!DeferIndexingToEndOfTranslationUnit)
143 IndexCtx->indexDeclGroupRef(DG);
146 void HandleTranslationUnit(ASTContext &Ctx)
override {
147 if (DeferIndexingToEndOfTranslationUnit)
149 IndexCtx->indexTopLevelDecl(DG);
150 DataConsumer->finish();
153 bool shouldSkipFunctionBody(Decl *D)
override {
154 return ShouldSkipFunctionBody(D);
159 std::shared_ptr<IndexDataConsumer> DataConsumer;
160 IndexingOptions Opts;
163 IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
164 const IndexingOptions &Opts)
165 : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
166 assert(this->DataConsumer !=
nullptr);
170 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
171 StringRef InFile)
override {
172 return std::make_unique<IndexASTConsumer>(
174 [](
const Decl *) { return false; });
181 std::shared_ptr<IndexDataConsumer> DataConsumer,
184 return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
185 ShouldSkipFunctionBody);
189 std::shared_ptr<IndexDataConsumer> DataConsumer,
195 ShouldSkipFunctionBody =
197 return !ShouldTraverseDecl(D);
200 std::move(ShouldSkipFunctionBody));
203std::unique_ptr<FrontendAction>
206 assert(DataConsumer !=
nullptr);
207 return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
243 for (
const auto &M : PP.
macros()) {
244 for (
auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
246 MD->getLocation(), DataConsumer);
254 for (
const auto &M : PP.
macros()) {
255 if (M.second.getLatest() ==
nullptr) {
257 auto *OwningMod = MM->getOwningModule();
258 if (OwningMod && OwningMod->getASTFileKey() &&
259 *OwningMod->getASTFileKey() == Mod.
FileKey) {
260 if (
auto *MI = MM->getMacroInfo()) {
262 MI->getDefinitionLoc(), DataConsumer);
295 for (
const Decl *D : Decls)
300std::unique_ptr<PPCallbacks>
302 return std::make_unique<IndexPPCallbacks>(
303 std::make_shared<IndexingContext>(Opts, Consumer));
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
static void indexPreprocessorModuleMacros(Preprocessor &PP, serialization::ModuleFile &Mod, IndexDataConsumer &DataConsumer)
static void indexPreprocessorMacros(Preprocessor &PP, IndexDataConsumer &DataConsumer)
static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx)
static void indexPreprocessorMacro(const IdentifierInfo *II, const MacroInfo *MI, MacroDirective::Kind DirectiveKind, SourceLocation Loc, IndexDataConsumer &DataConsumer)
static bool topLevelDeclVisitor(void *context, const Decl *D)
Defines the PPCallbacks interface.
Defines the clang::Preprocessor interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
Abstract base class to use for AST consumer-based frontend actions.
Reads an AST files chain containing the contents of a translation unit.
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
llvm::iterator_range< ModuleDeclIterator > getModuleFileLevelDecls(ModuleFile &Mod)
Preprocessor & getPreprocessor() const
Retrieve the preprocessor.
Utility class for loading a ASTContext from an AST file.
bool visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn)
Iterate over local declarations (locally parsed if this is a parsed source file or the loaded declara...
std::shared_ptr< Preprocessor > getPreprocessorPtr() const
const Preprocessor & getPreprocessor() const
const ASTContext & getASTContext() const
std::shared_ptr< Preprocessor > getPreprocessorPtr()
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
One of these records is kept for each identifier that is lexed.
MacroInfo * getMacroInfo() const
Get the MacroInfo that should be used for this definition.
const MacroInfo * getMacroInfo() const
Encapsulates the data about a macro definition (e.g.
This interface provides a way to observe the actions of the preprocessor as it does its thing.
virtual void Elifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifndef branch is taken.
virtual void Elifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD)
Hook called whenever an #elifdef branch is taken.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
ArrayRef< ModuleMacro * > getLeafModuleMacros(const IdentifierInfo *II) const
Get the list of leaf (non-overridden) module macros for a name.
llvm::iterator_range< macro_iterator > macros(bool IncludeExternalMacros=true) const
Encodes a location in the source.
IdentifierInfo * getIdentifierInfo() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
virtual void setPreprocessor(std::shared_ptr< Preprocessor > PP)
virtual bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, SymbolRoleSet Roles, SourceLocation Loc)
virtual void initialize(ASTContext &Ctx)
void setASTContext(ASTContext &ctx)
bool indexTopLevelDecl(const Decl *D)
Information about a module that has been loaded by the ASTReader.
ModuleFileKey FileKey
The key ModuleManager used for the module file.
std::unique_ptr< PPCallbacks > indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts)
Creates a PPCallbacks that indexes macros and feeds macros to Consumer.
void indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP, ArrayRef< const Decl * > Decls, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes Decls.
void indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all top-level decls in the module.
std::unique_ptr< ASTConsumer > createIndexingASTConsumer(std::shared_ptr< IndexDataConsumer > DataConsumer, const IndexingOptions &Opts, std::shared_ptr< Preprocessor > PP)
Creates an ASTConsumer that indexes all symbols (macros and AST decls).
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).
void indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer, IndexingOptions Opts)
Recursively indexes all decls in the AST.
Top level wrappers for InstallAPI frontend operations.
int const char * function
std::function< bool(const Decl *)> ShouldTraverseDecl
bool IndexMacrosInPreprocessor