Author: pcc Date: Thu Nov 7 16:30:36 2013 New Revision: 194223 URL: http://llvm.org/viewvc/llvm-project?rev=194223&view=rev Log: Introduce MatchFinder::matchAST.
Differential Revision: http://llvm-reviews.chandlerc.com/D2115 Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchFinder.h cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchFinder.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchFinder.h?rev=194223&r1=194222&r2=194223&view=diff ============================================================================== --- cfe/trunk/include/clang/ASTMatchers/ASTMatchFinder.h (original) +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchFinder.h Thu Nov 7 16:30:36 2013 @@ -163,6 +163,9 @@ public: ASTContext &Context); /// @} + /// \brief Finds all matches in the given AST. + void matchAST(ASTContext &Context); + /// \brief Registers a callback to notify the end of parsing. /// /// The provided closure is called after parsing is done, before the AST is Modified: cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp?rev=194223&r1=194222&r2=194223&view=diff ============================================================================== --- cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp (original) +++ cfe/trunk/lib/ASTMatchers/ASTMatchFinder.cpp Thu Nov 7 16:30:36 2013 @@ -744,25 +744,19 @@ bool MatchASTVisitor::TraverseNestedName class MatchASTConsumer : public ASTConsumer { public: - MatchASTConsumer( - std::vector<std::pair<internal::DynTypedMatcher, MatchCallback *> > * - MatcherCallbackPairs, - MatchFinder::ParsingDoneTestCallback *ParsingDone) - : Visitor(MatcherCallbackPairs), ParsingDone(ParsingDone) {} + MatchASTConsumer(MatchFinder *Finder, + MatchFinder::ParsingDoneTestCallback *ParsingDone) + : Finder(Finder), ParsingDone(ParsingDone) {} private: virtual void HandleTranslationUnit(ASTContext &Context) { if (ParsingDone != NULL) { ParsingDone->run(); } - Visitor.set_active_ast_context(&Context); - Visitor.onStartOfTranslationUnit(); - Visitor.TraverseDecl(Context.getTranslationUnitDecl()); - Visitor.onEndOfTranslationUnit(); - Visitor.set_active_ast_context(NULL); + Finder->matchAST(Context); } - MatchASTVisitor Visitor; + MatchFinder *Finder; MatchFinder::ParsingDoneTestCallback *ParsingDone; }; @@ -836,7 +830,7 @@ bool MatchFinder::addDynamicMatcher(cons } ASTConsumer *MatchFinder::newASTConsumer() { - return new internal::MatchASTConsumer(&MatcherCallbackPairs, ParsingDone); + return new internal::MatchASTConsumer(this, ParsingDone); } void MatchFinder::match(const clang::ast_type_traits::DynTypedNode &Node, @@ -846,6 +840,14 @@ void MatchFinder::match(const clang::ast Visitor.match(Node); } +void MatchFinder::matchAST(ASTContext &Context) { + internal::MatchASTVisitor Visitor(&MatcherCallbackPairs); + Visitor.set_active_ast_context(&Context); + Visitor.onStartOfTranslationUnit(); + Visitor.TraverseDecl(Context.getTranslationUnitDecl()); + Visitor.onEndOfTranslationUnit(); +} + void MatchFinder::registerTestCallbackAfterParsing( MatchFinder::ParsingDoneTestCallback *NewParsingDone) { ParsingDone = NewParsingDone; Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=194223&r1=194222&r2=194223&view=diff ============================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original) +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Thu Nov 7 16:30:36 2013 @@ -4114,6 +4114,12 @@ TEST(MatchFinder, InterceptsStartOfTrans OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); EXPECT_TRUE(VerifyCallback.Called); + + VerifyCallback.Called = false; + OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;")); + ASSERT_TRUE(AST.get()); + Finder.matchAST(AST->getASTContext()); + EXPECT_TRUE(VerifyCallback.Called); } class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback { @@ -4135,6 +4141,12 @@ TEST(MatchFinder, InterceptsEndOfTransla OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); EXPECT_TRUE(VerifyCallback.Called); + + VerifyCallback.Called = false; + OwningPtr<ASTUnit> AST(tooling::buildASTFromCode("int x;")); + ASSERT_TRUE(AST.get()); + Finder.matchAST(AST->getASTContext()); + EXPECT_TRUE(VerifyCallback.Called); } TEST(EqualsBoundNodeMatcher, QualType) { Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h?rev=194223&r1=194222&r2=194223&view=diff ============================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h (original) +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.h Thu Nov 7 16:30:36 2013 @@ -11,12 +11,14 @@ #define LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Frontend/ASTUnit.h" #include "clang/Tooling/Tooling.h" #include "gtest/gtest.h" namespace clang { namespace ast_matchers { +using clang::tooling::buildASTFromCodeWithArgs; using clang::tooling::newFrontendActionFactory; using clang::tooling::runToolOnCodeWithArgs; using clang::tooling::FrontendActionFactory; @@ -121,6 +123,21 @@ matchAndVerifyResultConditionally(const return testing::AssertionFailure() << "Verified unexpected result in \"" << Code << "\""; } + + VerifiedResult = false; + OwningPtr<ASTUnit> AST(buildASTFromCodeWithArgs(Code, Args)); + if (!AST.get()) + return testing::AssertionFailure() << "Parsing error in \"" << Code + << "\" while building AST"; + Finder.matchAST(AST->getASTContext()); + if (!VerifiedResult && ExpectResult) { + return testing::AssertionFailure() + << "Could not verify result in \"" << Code << "\" with AST"; + } else if (VerifiedResult && !ExpectResult) { + return testing::AssertionFailure() + << "Verified unexpected result in \"" << Code << "\" with AST"; + } + return testing::AssertionSuccess(); } _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
