From: Peter Collingbourne Date: Tue, 28 May 2013 19:21:51 +0000 (+0000) Subject: Add an overridable MatchCallback::onEndOfTranslationUnit() function. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8f9e590f78b1b05c36c2a14d68c4b9f9acbb891a;p=clang Add an overridable MatchCallback::onEndOfTranslationUnit() function. Differential Revision: http://llvm-reviews.chandlerc.com/D745 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182798 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchFinder.h b/include/clang/ASTMatchers/ASTMatchFinder.h index be58afffd1..18a0cf5ac9 100644 --- a/include/clang/ASTMatchers/ASTMatchFinder.h +++ b/include/clang/ASTMatchers/ASTMatchFinder.h @@ -97,6 +97,11 @@ public: /// /// Optionally override to do per translation unit tasks. virtual void onStartOfTranslationUnit() {} + + /// \brief Called at the end of each translation unit. + /// + /// Optionally override to do per translation unit tasks. + virtual void onEndOfTranslationUnit() {} }; /// \brief Called when parsing is finished. Intended for testing only. diff --git a/lib/ASTMatchers/ASTMatchFinder.cpp b/lib/ASTMatchers/ASTMatchFinder.cpp index eccc9cd4d3..378453d851 100644 --- a/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/lib/ASTMatchers/ASTMatchFinder.cpp @@ -278,6 +278,15 @@ public: } } + void onEndOfTranslationUnit() { + for (std::vector >::const_iterator + I = MatcherCallbackPairs->begin(), E = MatcherCallbackPairs->end(); + I != E; ++I) { + I->second->onEndOfTranslationUnit(); + } + } + void set_active_ast_context(ASTContext *NewActiveASTContext) { ActiveASTContext = NewActiveASTContext; } @@ -679,6 +688,7 @@ private: Visitor.set_active_ast_context(&Context); Visitor.onStartOfTranslationUnit(); Visitor.TraverseDecl(Context.getTranslationUnitDecl()); + Visitor.onEndOfTranslationUnit(); Visitor.set_active_ast_context(NULL); } diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 8dd3b70d21..86b54acdeb 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3934,5 +3934,26 @@ TEST(MatchFinder, InterceptsStartOfTranslationUnit) { EXPECT_TRUE(VerifyCallback.Called); } +class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback { +public: + VerifyEndOfTranslationUnit() : Called(false) {} + virtual void run(const MatchFinder::MatchResult &Result) { + EXPECT_FALSE(Called); + } + virtual void onEndOfTranslationUnit() { + Called = true; + } + bool Called; +}; + +TEST(MatchFinder, InterceptsEndOfTranslationUnit) { + MatchFinder Finder; + VerifyEndOfTranslationUnit 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