]> granicus.if.org Git - clang/commitdiff
Insert interception point onStartOfTranslationUnit.
authorManuel Klimek <klimek@google.com>
Fri, 2 Nov 2012 01:31:03 +0000 (01:31 +0000)
committerManuel Klimek <klimek@google.com>
Fri, 2 Nov 2012 01:31:03 +0000 (01:31 +0000)
Often users of the ASTMatchers want to add tasks that are done once per
translation unit, for example, cleaning up caches. Combined with the
interception point for the end of source file one can add to the factory
creation, this covers the cases we've seen users need.

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

include/clang/ASTMatchers/ASTMatchFinder.h
lib/ASTMatchers/ASTMatchFinder.cpp
unittests/ASTMatchers/ASTMatchersTest.cpp

index ba8e0a7ecccaf583fd97e158dd9e2d2ed6b7010a..30b4050e1c81c1960af82dfacb9f56807586ba61 100644 (file)
@@ -85,7 +85,14 @@ public:
   class MatchCallback {
   public:
     virtual ~MatchCallback();
+
+    /// \brief Called on every match by the \c MatchFinder.
     virtual void run(const MatchResult &Result) = 0;
+
+    /// \brief Called at the start of each translation unit.
+    ///
+    /// Optionally override to do per translation unit tasks.
+    virtual void onStartOfTranslationUnit() {}
   };
 
   /// \brief Called when parsing is finished. Intended for testing only.
index b081f5426eaeae2fdd7a8ae190c9a5c4279f501f..8ecb26e8c19d37c61f54c3e6ba9d9c175ffc8053 100644 (file)
@@ -315,6 +315,15 @@ public:
        ActiveASTContext(NULL) {
   }
 
+  void onStartOfTranslationUnit() {
+    for (std::vector<std::pair<const internal::DynTypedMatcher*,
+                               MatchCallback*> >::const_iterator
+             I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end();
+         I != E; ++I) {
+      I->second->onStartOfTranslationUnit();
+    }
+  }
+
   void set_active_ast_context(ASTContext *NewActiveASTContext) {
     ActiveASTContext = NewActiveASTContext;
   }
@@ -649,6 +658,7 @@ private:
       ParsingDone->run();
     }
     Visitor.set_active_ast_context(&Context);
+    Visitor.onStartOfTranslationUnit();
     Visitor.TraverseDecl(Context.getTranslationUnitDecl());
     Visitor.set_active_ast_context(NULL);
   }
index 8861881efa97edcd312532edcbda08ee500bc278..ad5469348d5dfd1fc9c25ed6be2772852b3fa634 100644 (file)
@@ -3366,5 +3366,26 @@ TEST(MatchFinder, CanMatchStatementsRecursively) {
     new VerifyRecursiveMatch<clang::Stmt>("if", declStmt())));
 }
 
+class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
+public:
+  VerifyStartOfTranslationUnit() : Called(false) {}
+  virtual void run(const MatchFinder::MatchResult &Result) {
+    EXPECT_TRUE(Called);
+  }
+  virtual void onStartOfTranslationUnit() {
+    Called = true;
+  }
+  bool Called;
+};
+
+TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
+  MatchFinder Finder;
+  VerifyStartOfTranslationUnit VerifyCallback;
+  Finder.addMatcher(decl(), &VerifyCallback);
+  OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
+  ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
+  EXPECT_TRUE(VerifyCallback.Called);
+}
+
 } // end namespace ast_matchers
 } // end namespace clang