From 6a55bb23073a4642923db94f56d574261b0c53c8 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 28 May 2013 19:21:51 +0000 Subject: [PATCH] Add an overridable MatchCallback::onEndOfTranslationUnit() function. Differential Revision: http://llvm-reviews.chandlerc.com/D745 llvm-svn: 182798 --- .../clang/ASTMatchers/ASTMatchFinder.h | 5 +++++ clang/lib/ASTMatchers/ASTMatchFinder.cpp | 10 +++++++++ .../unittests/ASTMatchers/ASTMatchersTest.cpp | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h index be58afffd1bf..18a0cf5ac921 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h +++ b/clang/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/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp index eccc9cd4d379..378453d85108 100644 --- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp +++ b/clang/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/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 8dd3b70d210f..86b54acdebc9 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/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