From bd0e2b71110a8c3ef6c398e29d2254ec1d787a21 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 2 Nov 2012 01:31:03 +0000 Subject: [PATCH] Insert interception point onStartOfTranslationUnit. 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. llvm-svn: 167271 --- clang/include/clang/ASTMatchers/ASTMatchFinder.h | 7 +++++++ clang/lib/ASTMatchers/ASTMatchFinder.cpp | 10 ++++++++++ clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h index ba8e0a7..30b4050 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h +++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h @@ -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. diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp index b081f54..8ecb26e 100644 --- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp @@ -315,6 +315,15 @@ public: ActiveASTContext(NULL) { } + void onStartOfTranslationUnit() { + for (std::vector >::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); } diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 8861881..ad54693 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3366,5 +3366,26 @@ TEST(MatchFinder, CanMatchStatementsRecursively) { new VerifyRecursiveMatch("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 Factory(newFrontendActionFactory(&Finder)); + ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); + EXPECT_TRUE(VerifyCallback.Called); +} + } // end namespace ast_matchers } // end namespace clang -- 2.7.4