]> granicus.if.org Git - clang/commitdiff
Adapts the FrontendAction convenience functions so that it can be
authorManuel Klimek <klimek@google.com>
Thu, 5 Jul 2012 18:13:01 +0000 (18:13 +0000)
committerManuel Klimek <klimek@google.com>
Thu, 5 Jul 2012 18:13:01 +0000 (18:13 +0000)
used with classes that generate ASTConsumers; this allows decoupling
the ASTConsumer generation from the Frontend library (like, for example,
the MatchFinder in the upcoming ASTMatcher patch).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159760 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/Tooling.h
lib/Tooling/Tooling.cpp
unittests/Tooling/TestVisitor.h
unittests/Tooling/ToolingTest.cpp

index 8ab2769cce96d6c920bcbbf7ab654b3a6f7f397b..0e1dd58ac9ce7f2f66c4d37ab6dd8a59b3b52f66 100644 (file)
@@ -35,6 +35,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Driver/Util.h"
+#include "clang/Frontend/FrontendAction.h"
 #include "clang/Tooling/ArgumentsAdjusters.h"
 #include "clang/Tooling/CompilationDatabase.h"
 #include <string>
@@ -74,18 +75,18 @@ template <typename T>
 FrontendActionFactory *newFrontendActionFactory();
 
 /// \brief Returns a new FrontendActionFactory for any type that provides an
-/// implementation of newFrontendAction().
+/// implementation of newASTConsumer().
 ///
-/// FactoryT must implement: FrontendAction *newFrontendAction().
+/// FactoryT must implement: ASTConsumer *newASTConsumer().
 ///
 /// Example:
-/// struct ProvidesFrontendActions {
-///   FrontendAction *newFrontendAction();
+/// struct ProvidesASTConsumers {
+///   clang::ASTConsumer *newASTConsumer();
 /// } Factory;
 /// FrontendActionFactory *FactoryAdapter =
 ///   newFrontendActionFactory(&Factory);
 template <typename FactoryT>
-FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory);
+FrontendActionFactory *newFrontendActionFactory(FactoryT *ConsumerFactory);
 
 /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
 ///
@@ -201,21 +202,34 @@ FrontendActionFactory *newFrontendActionFactory() {
 }
 
 template <typename FactoryT>
-FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory) {
+FrontendActionFactory *newFrontendActionFactory(FactoryT *ConsumerFactory) {
   class FrontendActionFactoryAdapter : public FrontendActionFactory {
   public:
-    explicit FrontendActionFactoryAdapter(FactoryT *ActionFactory)
-      : ActionFactory(ActionFactory) {}
+    explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory)
+      : ConsumerFactory(ConsumerFactory) {}
 
     virtual clang::FrontendAction *create() {
-      return ActionFactory->newFrontendAction();
+      return new ConsumerFactoryAdaptor(ConsumerFactory);
     }
 
   private:
-    FactoryT *ActionFactory;
+    class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
+    public:
+      ConsumerFactoryAdaptor(FactoryT *ConsumerFactory)
+        : ConsumerFactory(ConsumerFactory) {}
+
+      clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &,
+                                            llvm::StringRef) {
+        return ConsumerFactory->newASTConsumer();
+      }
+
+    private:
+      FactoryT *ConsumerFactory;
+    };
+    FactoryT *ConsumerFactory;
   };
 
-  return new FrontendActionFactoryAdapter(ActionFactory);
+  return new FrontendActionFactoryAdapter(ConsumerFactory);
 }
 
 } // end namespace tooling
index a449e105bf5a8027d62f0f6531945a57624748a1..abac182b215dfcfa5cfaa87dedf9e1915cd099dc 100644 (file)
@@ -19,7 +19,6 @@
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/Tool.h"
 #include "clang/Frontend/CompilerInstance.h"
-#include "clang/Frontend/FrontendAction.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "llvm/ADT/STLExtras.h"
index f73d2e0c2fff70f8ca68f73b01806369d1d74f3c..d439d81d89ef4134efa590248258d593ab0ff410 100644 (file)
@@ -56,6 +56,7 @@ protected:
     FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
 
     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
+      Visitor->Context = &Context;
       Visitor->TraverseDecl(Context.getTranslationUnitDecl());
     }
 
@@ -68,8 +69,7 @@ protected:
     TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
 
     virtual clang::ASTConsumer* CreateASTConsumer(
-        CompilerInstance& compiler, llvm::StringRef dummy) {
-      Visitor->Context = &compiler.getASTContext();
+        CompilerInstance&, llvm::StringRef dummy) {
       /// TestConsumer will be deleted by the framework calling us.
       return new FindConsumer(Visitor);
     }
index f6681f8153e841cdb6cd076305d7b5031e107567..fb3af2678c5f4e6f18b39e9f2f458f490ceafd33 100644 (file)
@@ -104,7 +104,9 @@ TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
 }
 
 struct IndependentFrontendActionCreator {
-  FrontendAction *newFrontendAction() { return new SyntaxOnlyAction; }
+  ASTConsumer *newASTConsumer() {
+    return new FindTopLevelDeclConsumer(NULL);
+  }
 };
 
 TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {