IndexingAction.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include "clang/Index/IndexingAction.h"
  9. #include "IndexingContext.h"
  10. #include "clang/Frontend/CompilerInstance.h"
  11. #include "clang/Frontend/FrontendAction.h"
  12. #include "clang/Frontend/MultiplexConsumer.h"
  13. #include "clang/Index/IndexDataConsumer.h"
  14. #include "clang/Lex/PPCallbacks.h"
  15. #include "clang/Lex/Preprocessor.h"
  16. #include "clang/Serialization/ASTReader.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include <memory>
  19. using namespace clang;
  20. using namespace clang::index;
  21. namespace {
  22. class IndexPPCallbacks final : public PPCallbacks {
  23. std::shared_ptr<IndexingContext> IndexCtx;
  24. public:
  25. IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
  26. : IndexCtx(std::move(IndexCtx)) {}
  27. void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
  28. SourceRange Range, const MacroArgs *Args) override {
  29. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  30. Range.getBegin(), *MD.getMacroInfo());
  31. }
  32. void MacroDefined(const Token &MacroNameTok,
  33. const MacroDirective *MD) override {
  34. IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
  35. MacroNameTok.getLocation(),
  36. *MD->getMacroInfo());
  37. }
  38. void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
  39. const MacroDirective *Undef) override {
  40. if (!MD.getMacroInfo()) // Ignore noop #undef.
  41. return;
  42. IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(),
  43. MacroNameTok.getLocation(),
  44. *MD.getMacroInfo());
  45. }
  46. void Defined(const Token &MacroNameTok, const MacroDefinition &MD,
  47. SourceRange Range) override {
  48. if (!MD.getMacroInfo()) // Ignore nonexistent macro.
  49. return;
  50. // Note: this is defined(M), not #define M
  51. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  52. MacroNameTok.getLocation(),
  53. *MD.getMacroInfo());
  54. }
  55. void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
  56. const MacroDefinition &MD) override {
  57. if (!MD.getMacroInfo()) // Ignore non-existent macro.
  58. return;
  59. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  60. MacroNameTok.getLocation(),
  61. *MD.getMacroInfo());
  62. }
  63. void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
  64. const MacroDefinition &MD) override {
  65. if (!MD.getMacroInfo()) // Ignore nonexistent macro.
  66. return;
  67. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  68. MacroNameTok.getLocation(),
  69. *MD.getMacroInfo());
  70. }
  71. using PPCallbacks::Elifdef;
  72. using PPCallbacks::Elifndef;
  73. void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
  74. const MacroDefinition &MD) override {
  75. if (!MD.getMacroInfo()) // Ignore non-existent macro.
  76. return;
  77. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  78. MacroNameTok.getLocation(),
  79. *MD.getMacroInfo());
  80. }
  81. void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
  82. const MacroDefinition &MD) override {
  83. if (!MD.getMacroInfo()) // Ignore non-existent macro.
  84. return;
  85. IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
  86. MacroNameTok.getLocation(),
  87. *MD.getMacroInfo());
  88. }
  89. };
  90. class IndexASTConsumer final : public ASTConsumer {
  91. std::shared_ptr<IndexDataConsumer> DataConsumer;
  92. std::shared_ptr<IndexingContext> IndexCtx;
  93. std::shared_ptr<Preprocessor> PP;
  94. std::function<bool(const Decl *)> ShouldSkipFunctionBody;
  95. public:
  96. IndexASTConsumer(std::shared_ptr<IndexDataConsumer> DataConsumer,
  97. const IndexingOptions &Opts,
  98. std::shared_ptr<Preprocessor> PP,
  99. std::function<bool(const Decl *)> ShouldSkipFunctionBody)
  100. : DataConsumer(std::move(DataConsumer)),
  101. IndexCtx(new IndexingContext(Opts, *this->DataConsumer)),
  102. PP(std::move(PP)),
  103. ShouldSkipFunctionBody(std::move(ShouldSkipFunctionBody)) {
  104. assert(this->DataConsumer != nullptr);
  105. assert(this->PP != nullptr);
  106. }
  107. protected:
  108. void Initialize(ASTContext &Context) override {
  109. IndexCtx->setASTContext(Context);
  110. IndexCtx->getDataConsumer().initialize(Context);
  111. IndexCtx->getDataConsumer().setPreprocessor(PP);
  112. PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
  113. }
  114. bool HandleTopLevelDecl(DeclGroupRef DG) override {
  115. return IndexCtx->indexDeclGroupRef(DG);
  116. }
  117. void HandleInterestingDecl(DeclGroupRef DG) override {
  118. // Ignore deserialized decls.
  119. }
  120. void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override {
  121. IndexCtx->indexDeclGroupRef(DG);
  122. }
  123. void HandleTranslationUnit(ASTContext &Ctx) override {
  124. DataConsumer->finish();
  125. }
  126. bool shouldSkipFunctionBody(Decl *D) override {
  127. return ShouldSkipFunctionBody(D);
  128. }
  129. };
  130. class IndexAction final : public ASTFrontendAction {
  131. std::shared_ptr<IndexDataConsumer> DataConsumer;
  132. IndexingOptions Opts;
  133. public:
  134. IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
  135. const IndexingOptions &Opts)
  136. : DataConsumer(std::move(DataConsumer)), Opts(Opts) {
  137. assert(this->DataConsumer != nullptr);
  138. }
  139. protected:
  140. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  141. StringRef InFile) override {
  142. return std::make_unique<IndexASTConsumer>(
  143. DataConsumer, Opts, CI.getPreprocessorPtr(),
  144. /*ShouldSkipFunctionBody=*/[](const Decl *) { return false; });
  145. }
  146. };
  147. } // anonymous namespace
  148. std::unique_ptr<ASTConsumer> index::createIndexingASTConsumer(
  149. std::shared_ptr<IndexDataConsumer> DataConsumer,
  150. const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP,
  151. std::function<bool(const Decl *)> ShouldSkipFunctionBody) {
  152. return std::make_unique<IndexASTConsumer>(DataConsumer, Opts, PP,
  153. ShouldSkipFunctionBody);
  154. }
  155. std::unique_ptr<ASTConsumer> clang::index::createIndexingASTConsumer(
  156. std::shared_ptr<IndexDataConsumer> DataConsumer,
  157. const IndexingOptions &Opts, std::shared_ptr<Preprocessor> PP) {
  158. std::function<bool(const Decl *)> ShouldSkipFunctionBody = [](const Decl *) {
  159. return false;
  160. };
  161. if (Opts.ShouldTraverseDecl)
  162. ShouldSkipFunctionBody =
  163. [ShouldTraverseDecl(Opts.ShouldTraverseDecl)](const Decl *D) {
  164. return !ShouldTraverseDecl(D);
  165. };
  166. return createIndexingASTConsumer(std::move(DataConsumer), Opts, std::move(PP),
  167. std::move(ShouldSkipFunctionBody));
  168. }
  169. std::unique_ptr<FrontendAction>
  170. index::createIndexingAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
  171. const IndexingOptions &Opts) {
  172. assert(DataConsumer != nullptr);
  173. return std::make_unique<IndexAction>(std::move(DataConsumer), Opts);
  174. }
  175. static bool topLevelDeclVisitor(void *context, const Decl *D) {
  176. IndexingContext &IndexCtx = *static_cast<IndexingContext*>(context);
  177. return IndexCtx.indexTopLevelDecl(D);
  178. }
  179. static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IndexCtx) {
  180. Unit.visitLocalTopLevelDecls(&IndexCtx, topLevelDeclVisitor);
  181. }
  182. static void indexPreprocessorMacro(const IdentifierInfo *II,
  183. const MacroInfo *MI,
  184. MacroDirective::Kind DirectiveKind,
  185. SourceLocation Loc,
  186. IndexDataConsumer &DataConsumer) {
  187. // When using modules, it may happen that we find #undef of a macro that
  188. // was defined in another module. In such case, MI may be nullptr, since
  189. // we only look for macro definitions in the current TU. In that case,
  190. // there is nothing to index.
  191. if (!MI)
  192. return;
  193. // Skip implicit visibility change.
  194. if (DirectiveKind == MacroDirective::MD_Visibility)
  195. return;
  196. auto Role = DirectiveKind == MacroDirective::MD_Define
  197. ? SymbolRole::Definition
  198. : SymbolRole::Undefinition;
  199. DataConsumer.handleMacroOccurrence(II, MI, static_cast<unsigned>(Role), Loc);
  200. }
  201. static void indexPreprocessorMacros(Preprocessor &PP,
  202. IndexDataConsumer &DataConsumer) {
  203. for (const auto &M : PP.macros()) {
  204. for (auto *MD = M.second.getLatest(); MD; MD = MD->getPrevious()) {
  205. indexPreprocessorMacro(M.first, MD->getMacroInfo(), MD->getKind(),
  206. MD->getLocation(), DataConsumer);
  207. }
  208. }
  209. }
  210. static void indexPreprocessorModuleMacros(Preprocessor &PP,
  211. serialization::ModuleFile &Mod,
  212. IndexDataConsumer &DataConsumer) {
  213. for (const auto &M : PP.macros()) {
  214. if (M.second.getLatest() == nullptr) {
  215. for (auto *MM : PP.getLeafModuleMacros(M.first)) {
  216. auto *OwningMod = MM->getOwningModule();
  217. if (OwningMod && OwningMod->getASTFile() == Mod.File) {
  218. if (auto *MI = MM->getMacroInfo()) {
  219. indexPreprocessorMacro(M.first, MI, MacroDirective::MD_Define,
  220. MI->getDefinitionLoc(), DataConsumer);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. void index::indexASTUnit(ASTUnit &Unit, IndexDataConsumer &DataConsumer,
  228. IndexingOptions Opts) {
  229. IndexingContext IndexCtx(Opts, DataConsumer);
  230. IndexCtx.setASTContext(Unit.getASTContext());
  231. DataConsumer.initialize(Unit.getASTContext());
  232. DataConsumer.setPreprocessor(Unit.getPreprocessorPtr());
  233. if (Opts.IndexMacrosInPreprocessor)
  234. indexPreprocessorMacros(Unit.getPreprocessor(), DataConsumer);
  235. indexTranslationUnit(Unit, IndexCtx);
  236. DataConsumer.finish();
  237. }
  238. void index::indexTopLevelDecls(ASTContext &Ctx, Preprocessor &PP,
  239. ArrayRef<const Decl *> Decls,
  240. IndexDataConsumer &DataConsumer,
  241. IndexingOptions Opts) {
  242. IndexingContext IndexCtx(Opts, DataConsumer);
  243. IndexCtx.setASTContext(Ctx);
  244. DataConsumer.initialize(Ctx);
  245. if (Opts.IndexMacrosInPreprocessor)
  246. indexPreprocessorMacros(PP, DataConsumer);
  247. for (const Decl *D : Decls)
  248. IndexCtx.indexTopLevelDecl(D);
  249. DataConsumer.finish();
  250. }
  251. std::unique_ptr<PPCallbacks>
  252. index::indexMacrosCallback(IndexDataConsumer &Consumer, IndexingOptions Opts) {
  253. return std::make_unique<IndexPPCallbacks>(
  254. std::make_shared<IndexingContext>(Opts, Consumer));
  255. }
  256. void index::indexModuleFile(serialization::ModuleFile &Mod, ASTReader &Reader,
  257. IndexDataConsumer &DataConsumer,
  258. IndexingOptions Opts) {
  259. ASTContext &Ctx = Reader.getContext();
  260. IndexingContext IndexCtx(Opts, DataConsumer);
  261. IndexCtx.setASTContext(Ctx);
  262. DataConsumer.initialize(Ctx);
  263. if (Opts.IndexMacrosInPreprocessor) {
  264. indexPreprocessorModuleMacros(Reader.getPreprocessor(), Mod, DataConsumer);
  265. }
  266. for (const Decl *D : Reader.getModuleFileLevelDecls(Mod)) {
  267. IndexCtx.indexTopLevelDecl(D);
  268. }
  269. DataConsumer.finish();
  270. }