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:
IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)30   IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
31       : IndexCtx(std::move(IndexCtx)) {}
32 
MacroExpands(const Token & MacroNameTok,const MacroDefinition & MD,SourceRange Range,const MacroArgs * Args)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 
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)39   void MacroDefined(const Token &MacroNameTok,
40                     const MacroDirective *MD) override {
41     IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
42                                  MacroNameTok.getLocation(),
43                                  *MD->getMacroInfo());
44   }
45 
MacroUndefined(const Token & MacroNameTok,const MacroDefinition & MD,const MacroDirective * Undef)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:
IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,const IndexingOptions & Opts,std::shared_ptr<Preprocessor> PP,std::function<bool (const Decl *)> ShouldSkipFunctionBody)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:
Initialize(ASTContext & Context)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 
HandleTopLevelDecl(DeclGroupRef DG)83   bool HandleTopLevelDecl(DeclGroupRef DG) override {
84     return IndexCtx->indexDeclGroupRef(DG);
85   }
86 
HandleInterestingDecl(DeclGroupRef DG)87   void HandleInterestingDecl(DeclGroupRef DG) override {
88     // Ignore deserialized decls.
89   }
90 
HandleTopLevelDeclInObjCContainer(DeclGroupRef DG)91   void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
92     IndexCtx->indexDeclGroupRef(DG);
93   }
94 
HandleTranslationUnit(ASTContext & Ctx)95   void HandleTranslationUnit(ASTContext &Ctx) override {
96     DataConsumer->finish();
97   }
98 
shouldSkipFunctionBody(Decl * D)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:
IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,const IndexingOptions & Opts)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:
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)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 
createIndexingASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,const IndexingOptions & Opts,std::shared_ptr<Preprocessor> PP,std::function<bool (const Decl *)> ShouldSkipFunctionBody)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 
createIndexingASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,const IndexingOptions & Opts,std::shared_ptr<Preprocessor> PP)134 std::unique_ptr<ASTConsumer> clang::index::createIndexingASTConsumer(
135     std::shared_ptr<IndexDataConsumer> DataConsumer,
136     const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP) {
137   std::function<bool(const Decl *)> ShouldSkipFunctionBody = [](const Decl *) {
138     return false;
139   };
140   if (Opts.ShouldTraverseDecl)
141     ShouldSkipFunctionBody =
142         [ShouldTraverseDecl(Opts.ShouldTraverseDecl)](const Decl *D) {
143           return !ShouldTraverseDecl(D);
144         };
145   return createIndexingASTConsumer(std::move(DataConsumer), Opts, std::move(PP),
146                                    std::move(ShouldSkipFunctionBody));
147 }
148 
149 std::unique_ptr<FrontendAction>
createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,const IndexingOptions & Opts)150 index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
151                             const IndexingOptions &Opts) {
152   assert(DataConsumer != nullptr);
153   return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
154 }
155 
topLevelDeclVisitor(void * context,const Decl * D)156 static bool topLevelDeclVisitor(void *context, const Decl *D) {
157   IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
158   return IndexCtx.indexTopLevelDecl(D);
159 }
160 
indexTranslationUnit(ASTUnit & Unit,IndexingContext & IndexCtx)161 static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
162   Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor);
163 }
164 
indexPreprocessorMacros(const Preprocessor & PP,IndexDataConsumer & DataConsumer)165 static void indexPreprocessorMacros(const Preprocessor &PP,
166                                     IndexDataConsumer &DataConsumer) {
167   for (const auto &M : PP.macros())
168     if (MacroDirective *MD = M.second.getLatest()) {
169       auto *MI = MD->getMacroInfo();
170       // When using modules, it may happen that we find #undef of a macro that
171       // was defined in another module. In such case, MI may be nullptr, since
172       // we only look for macro definitions in the current TU. In that case,
173       // there is nothing to index.
174       if (!MI)
175         continue;
176 
177       DataConsumer.handleMacroOccurrence(
178           M.first, MD->getMacroInfo(),
179           static_cast<unsigned>(index::SymbolRole::Definition),
180           MD->getLocation());
181     }
182 }
183 
indexASTUnit(ASTUnit & Unit,IndexDataConsumer & DataConsumer,IndexingOptions Opts)184 void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer,
185                          IndexingOptions Opts) {
186   IndexingContext IndexCtx(Opts, DataConsumer);
187   IndexCtx.setASTContext(Unit.getASTContext());
188   DataConsumer.initialize(Unit.getASTContext());
189   DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
190 
191   if (Opts.IndexMacrosInPreprocessor)
192     indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer);
193   indexTranslationUnit(Unit, IndexCtx);
194   DataConsumer.finish();
195 }
196 
indexTopLevelDecls(ASTContext & Ctx,Preprocessor & PP,ArrayRef<const Decl * > Decls,IndexDataConsumer & DataConsumer,IndexingOptions Opts)197 void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP,
198                                ArrayRef<const Decl *> Decls,
199                                IndexDataConsumer &DataConsumer,
200                                IndexingOptions Opts) {
201   IndexingContext IndexCtx(Opts, DataConsumer);
202   IndexCtx.setASTContext(Ctx);
203 
204   DataConsumer.initialize(Ctx);
205 
206   if (Opts.IndexMacrosInPreprocessor)
207     indexPreprocessorMacros(PP, DataConsumer);
208 
209   for (const Decl *D : Decls)
210     IndexCtx.indexTopLevelDecl(D);
211   DataConsumer.finish();
212 }
213 
214 std::unique_ptr<PPCallbacks>
indexMacrosCallback(IndexDataConsumer & Consumer,IndexingOptions Opts)215 index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) {
216   return std::make_unique<IndexPPCallbacks>(
217       std::make_shared<IndexingContext>(Opts, Consumer));
218 }
219 
indexModuleFile(serialization::ModuleFile & Mod,ASTReader & Reader,IndexDataConsumer & DataConsumer,IndexingOptions Opts)220 void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader,
221                             IndexDataConsumer &DataConsumer,
222                             IndexingOptions Opts) {
223   ASTContext &Ctx = Reader.getContext();
224   IndexingContext IndexCtx(Opts, DataConsumer);
225   IndexCtx.setASTContext(Ctx);
226   DataConsumer.initialize(Ctx);
227 
228   if (Opts.IndexMacrosInPreprocessor)
229     indexPreprocessorMacros(Reader.getPreprocessor(), DataConsumer);
230 
231   for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
232     IndexCtx.indexTopLevelDecl(D);
233   }
234   DataConsumer.finish();
235 }
236