gribozavr created this revision.
Herald added subscribers: cfe-commits, arphaman.
Herald added a project: clang.
gribozavr added a parent revision: D66875: [Index] Marked a bunch of classes 
'final'.

Doing so removes one reason to create a custom FrontendAction.
FrontendActions are not desirable because they are difficult to compose.
ASTConsumers are much easier to compose.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66876

Files:
  clang/lib/Index/IndexingAction.cpp

Index: clang/lib/Index/IndexingAction.cpp
===================================================================
--- clang/lib/Index/IndexingAction.cpp
+++ clang/lib/Index/IndexingAction.cpp
@@ -23,7 +23,37 @@
 
 namespace {
 
-class IndexASTConsumer : public ASTConsumer {
+class IndexPPCallbacks final : public PPCallbacks {
+  std::shared_ptr<IndexingContext> IndexCtx;
+
+public:
+  IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
+      : IndexCtx(std::move(IndexCtx)) {}
+
+  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
+                    SourceRange Range, const MacroArgs *Args) override {
+    IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
+                                   Range.getBegin(), *MD.getMacroInfo());
+  }
+
+  void MacroDefined(const Token &MacroNameTok,
+                    const MacroDirective *MD) override {
+    IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
+                                 MacroNameTok.getLocation(),
+                                 *MD->getMacroInfo());
+  }
+
+  void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
+                      const MacroDirective *Undef) override {
+    if (!MD.getMacroInfo())  // Ignore noop #undef.
+      return;
+    IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(),
+                                   MacroNameTok.getLocation(),
+                                   *MD.getMacroInfo());
+  }
+};
+
+class IndexASTConsumer final : public ASTConsumer {
   std::shared_ptr<Preprocessor> PP;
   std::shared_ptr<IndexingContext> IndexCtx;
 
@@ -37,6 +67,7 @@
     IndexCtx->setASTContext(Context);
     IndexCtx->getDataConsumer().initialize(Context);
     IndexCtx->getDataConsumer().setPreprocessor(PP);
+    PP->addPPCallbacks(std::make_unique<IndexPPCallbacks>(IndexCtx));
   }
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
@@ -55,36 +86,6 @@
   }
 };
 
-class IndexPPCallbacks : public PPCallbacks {
-  std::shared_ptr<IndexingContext> IndexCtx;
-
-public:
-  IndexPPCallbacks(std::shared_ptr<IndexingContext> IndexCtx)
-      : IndexCtx(std::move(IndexCtx)) {}
-
-  void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
-                    SourceRange Range, const MacroArgs *Args) override {
-    IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(),
-                                   Range.getBegin(), *MD.getMacroInfo());
-  }
-
-  void MacroDefined(const Token &MacroNameTok,
-                    const MacroDirective *MD) override {
-    IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(),
-                                 MacroNameTok.getLocation(),
-                                 *MD->getMacroInfo());
-  }
-
-  void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD,
-                      const MacroDirective *Undef) override {
-    if (!MD.getMacroInfo())  // Ignore noop #undef.
-      return;
-    IndexCtx->handleMacroUndefined(*MacroNameTok.getIdentifierInfo(),
-                                   MacroNameTok.getLocation(),
-                                   *MD.getMacroInfo());
-  }
-};
-
 class IndexActionBase {
 protected:
   std::shared_ptr<IndexDataConsumer> DataConsumer;
@@ -101,16 +102,12 @@
                                                IndexCtx);
   }
 
-  std::unique_ptr<PPCallbacks> createIndexPPCallbacks() {
-    return std::make_unique<IndexPPCallbacks>(IndexCtx);
-  }
-
   void finish() {
     DataConsumer->finish();
   }
 };
 
-class IndexAction : public ASTFrontendAction, IndexActionBase {
+class IndexAction final : public ASTFrontendAction, IndexActionBase {
 public:
   IndexAction(std::shared_ptr<IndexDataConsumer> DataConsumer,
               IndexingOptions Opts)
@@ -122,18 +119,13 @@
     return createIndexASTConsumer(CI);
   }
 
-  bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
-    CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks());
-    return true;
-  }
-
   void EndSourceFileAction() override {
     FrontendAction::EndSourceFileAction();
     finish();
   }
 };
 
-class WrappingIndexAction : public WrapperFrontendAction, IndexActionBase {
+class WrappingIndexAction final : public WrapperFrontendAction, IndexActionBase {
   bool IndexActionFailed = false;
 
 public:
@@ -158,12 +150,6 @@
     return std::make_unique<MultiplexConsumer>(std::move(Consumers));
   }
 
-  bool BeginSourceFileAction(clang::CompilerInstance &CI) override {
-    WrapperFrontendAction::BeginSourceFileAction(CI);
-    CI.getPreprocessor().addPPCallbacks(createIndexPPCallbacks());
-    return true;
-  }
-
   void EndSourceFileAction() override {
     // Invoke wrapped action's method.
     WrapperFrontendAction::EndSourceFileAction();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to