[llvm-branch-commits] [cfe-branch] r159675 - in /cfe/branches/tooling: include/clang/ASTMatchers/ASTMatchFinder.h include/clang/Tooling/Tooling.h lib/ASTMatchers/ASTMatchFinder.cpp lib/Tooling/Tooling.cpp tools/rename/ClangRename.cpp unittests/ASTMatchers/ASTMatchersTest.h unittests/Tooling/TestVisitor.h unittests/Tooling/ToolingTest.cpp

Manuel Klimek klimek at google.com
Tue Jul 3 12:12:33 PDT 2012


Author: klimek
Date: Tue Jul  3 14:12:33 2012
New Revision: 159675

URL: http://llvm.org/viewvc/llvm-project?rev=159675&view=rev
Log:
Decouples the AST matchers from Frontend.


Modified:
    cfe/branches/tooling/include/clang/ASTMatchers/ASTMatchFinder.h
    cfe/branches/tooling/include/clang/Tooling/Tooling.h
    cfe/branches/tooling/lib/ASTMatchers/ASTMatchFinder.cpp
    cfe/branches/tooling/lib/Tooling/Tooling.cpp
    cfe/branches/tooling/tools/rename/ClangRename.cpp
    cfe/branches/tooling/unittests/ASTMatchers/ASTMatchersTest.h
    cfe/branches/tooling/unittests/Tooling/TestVisitor.h
    cfe/branches/tooling/unittests/Tooling/ToolingTest.cpp

Modified: cfe/branches/tooling/include/clang/ASTMatchers/ASTMatchFinder.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/include/clang/ASTMatchers/ASTMatchFinder.h?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/include/clang/ASTMatchers/ASTMatchFinder.h (original)
+++ cfe/branches/tooling/include/clang/ASTMatchers/ASTMatchFinder.h Tue Jul  3 14:12:33 2012
@@ -7,7 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//  Provides a way to construct a FrontendAction that runs given matchers
+//  Provides a way to construct an ASTConsumer that runs given matchers
 //  over the AST and invokes a given callback on every match.
 //
 //  The general idea is to construct a matcher expression that describes a
@@ -33,7 +33,7 @@
 //    MatchFinder finder;
 //    finder.AddMatcher(Id("id", record(hasName("::a_namespace::AClass"))),
 //                      new HandleMatch);
-//    return Tool.Run(finder.NewFrontendActionFactory());
+//    return Tool.Run(newFrontendActionFactory(&finder));
 //  }
 //
 //===----------------------------------------------------------------------===//
@@ -45,8 +45,6 @@
 
 namespace clang {
 
-class FrontendAction;
-
 namespace ast_matchers {
 
 /// \brief A class to allow finding matches over the Clang AST.
@@ -54,7 +52,7 @@
 /// After creation, you can add multiple matchers to the MatchFinder via
 /// calls to addMatcher(...).
 ///
-/// Once all matchers are added, newFrontendAction() returns a FrontendAction
+/// Once all matchers are added, newASTConsumer() returns an ASTConsumer
 /// that will trigger the callbacks specified via addMatcher(...) when a match
 /// is found.
 ///
@@ -116,8 +114,8 @@
                   MatchCallback *Action);
   /// @}
 
-  /// \brief Creates a clang FrontendAction that finds all matches.
-  FrontendAction *newFrontendAction();
+  /// \brief Creates a clang ASTConsumer that finds all matches.
+  clang::ASTConsumer *newASTConsumer();
 
   /// \brief Registers a callback to notify the end of parsing.
   ///
@@ -135,8 +133,6 @@
 
   /// \brief Called when parsing is done.
   ParsingDoneTestCallback *ParsingDone;
-
-  friend class MatchFinderFrontendActionFactory;
 };
 
 } // end namespace ast_matchers

Modified: cfe/branches/tooling/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/include/clang/Tooling/Tooling.h?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/include/clang/Tooling/Tooling.h (original)
+++ cfe/branches/tooling/include/clang/Tooling/Tooling.h Tue Jul  3 14:12:33 2012
@@ -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 @@
 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: FrontendAction *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 @@
 }
 
 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

Modified: cfe/branches/tooling/lib/ASTMatchers/ASTMatchFinder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/lib/ASTMatchers/ASTMatchFinder.cpp?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/lib/ASTMatchers/ASTMatchFinder.cpp (original)
+++ cfe/branches/tooling/lib/ASTMatchers/ASTMatchFinder.cpp Tue Jul  3 14:12:33 2012
@@ -19,8 +19,6 @@
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/RecursiveASTVisitor.h"
-#include "clang/Frontend/CompilerInstance.h"
-#include "clang/Frontend/FrontendAction.h"
 #include <set>
 
 namespace clang {
@@ -504,27 +502,6 @@
   MatchFinder::ParsingDoneTestCallback *ParsingDone;
 };
 
-class MatchASTAction : public clang::ASTFrontendAction {
- public:
-  explicit MatchASTAction(
-      std::vector< std::pair<const UntypedBaseMatcher*,
-                   MatchFinder::MatchCallback*> > *Triggers,
-      MatchFinder::ParsingDoneTestCallback *ParsingDone)
-      : Triggers(Triggers),
-        ParsingDone(ParsingDone) {}
-
- private:
-  clang::ASTConsumer *CreateASTConsumer(
-      clang::CompilerInstance &Compiler,
-      llvm::StringRef) {
-    return new MatchASTConsumer(Triggers, ParsingDone);
-  }
-
-  std::vector< std::pair<const UntypedBaseMatcher*,
-               MatchFinder::MatchCallback*> > *const Triggers;
-  MatchFinder::ParsingDoneTestCallback *ParsingDone;
-};
-
 } // end namespace
 } // end namespace internal
 
@@ -565,8 +542,8 @@
     new internal::TypedBaseMatcher<clang::Stmt>(NodeMatch), Action));
 }
 
-clang::FrontendAction *MatchFinder::newFrontendAction() {
-  return new internal::MatchASTAction(&Triggers, ParsingDone);
+clang::ASTConsumer *MatchFinder::newASTConsumer() {
+  return new internal::MatchASTConsumer(&Triggers, ParsingDone);
 }
 
 void MatchFinder::registerTestCallbackAfterParsing(

Modified: cfe/branches/tooling/lib/Tooling/Tooling.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/lib/Tooling/Tooling.cpp?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/lib/Tooling/Tooling.cpp (original)
+++ cfe/branches/tooling/lib/Tooling/Tooling.cpp Tue Jul  3 14:12:33 2012
@@ -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"

Modified: cfe/branches/tooling/tools/rename/ClangRename.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/tools/rename/ClangRename.cpp?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/tools/rename/ClangRename.cpp (original)
+++ cfe/branches/tooling/tools/rename/ClangRename.cpp Tue Jul  3 14:12:33 2012
@@ -48,7 +48,7 @@
 template <typename T>
 class TestVisitor : public clang::RecursiveASTVisitor<T> {
 public:
-  FrontendAction *newFrontendAction() { return new TestAction(this); }
+  clang::ASTConsumer *newASTConsumer() { return new FindConsumer(this); }
 
   bool shouldVisitTemplateInstantiations() const {
     return true;
@@ -63,27 +63,13 @@
     FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
 
     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
+      Visitor->Context = &Context;
       Visitor->TraverseDecl(Context.getTranslationUnitDecl());
     }
 
   private:
     TestVisitor *Visitor;
   };
-
-  class TestAction : public clang::ASTFrontendAction {
-  public:
-    TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
-
-    virtual clang::ASTConsumer* CreateASTConsumer(
-        clang::CompilerInstance& compiler, llvm::StringRef dummy) {
-      Visitor->Context = &compiler.getASTContext();
-      /// TestConsumer will be deleted by the framework calling us.
-      return new FindConsumer(Visitor);
-    }
-
-  private:
-    TestVisitor *Visitor;
-  };
 };
 
 struct Xaver {

Modified: cfe/branches/tooling/unittests/ASTMatchers/ASTMatchersTest.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/ASTMatchers/ASTMatchersTest.h?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/ASTMatchers/ASTMatchersTest.h (original)
+++ cfe/branches/tooling/unittests/ASTMatchers/ASTMatchersTest.h Tue Jul  3 14:12:33 2012
@@ -17,7 +17,9 @@
 namespace clang {
 namespace ast_matchers {
 
+using clang::tooling::newFrontendActionFactory;
 using clang::tooling::runToolOnCode;
+using clang::tooling::FrontendActionFactory;
 
 class BoundNodesCallback {
 public:
@@ -53,7 +55,8 @@
   bool Found = false;
   MatchFinder Finder;
   Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
-  if (!runToolOnCode(Finder.newFrontendAction(), Code)) {
+  OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
+  if (!runToolOnCode(Factory->create(), Code)) {
     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
   }
   if (!Found && ExpectMatch) {
@@ -87,7 +90,8 @@
   MatchFinder Finder;
   Finder.addMatcher(
       AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
-  if (!runToolOnCode(Finder.newFrontendAction(), Code)) {
+  OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
+  if (!runToolOnCode(Factory->create(), Code)) {
     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
   }
   if (!VerifiedResult && ExpectResult) {

Modified: cfe/branches/tooling/unittests/Tooling/TestVisitor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/Tooling/TestVisitor.h?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/Tooling/TestVisitor.h (original)
+++ cfe/branches/tooling/unittests/Tooling/TestVisitor.h Tue Jul  3 14:12:33 2012
@@ -55,6 +55,7 @@
     FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
 
     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
+      Visitor->Context = &Context;
       Visitor->TraverseDecl(Context.getTranslationUnitDecl());
     }
 
@@ -67,8 +68,7 @@
     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);
     }

Modified: cfe/branches/tooling/unittests/Tooling/ToolingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/Tooling/ToolingTest.cpp?rev=159675&r1=159674&r2=159675&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/Tooling/ToolingTest.cpp (original)
+++ cfe/branches/tooling/unittests/Tooling/ToolingTest.cpp Tue Jul  3 14:12:33 2012
@@ -104,7 +104,9 @@
 }
 
 struct IndependentFrontendActionCreator {
-  FrontendAction *newFrontendAction() { return new SyntaxOnlyAction; }
+  ASTConsumer *newASTConsumer() {
+    return new FindTopLevelDeclConsumer(NULL);
+  }
 };
 
 TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {





More information about the llvm-branch-commits mailing list