From: Samuel Benzaquen Date: Wed, 2 Apr 2014 13:12:14 +0000 (+0000) Subject: Add matcher for ExprWithCleanups. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5a3d6a8eea6ed78a2a532161073903a43f9076a8;p=clang Add matcher for ExprWithCleanups. Summary: Add matcher for ExprWithCleanups. Reviewers: klimek CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D3248 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205420 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 13d0ba2d0e..15621b7725 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -662,6 +662,16 @@ const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr> memberCallExpr; +/// \brief Matches expressions that introduce cleanups to be run at the end +/// of the sub-expression's evaluation. +/// +/// Example matches std::string() +/// \code +/// const std::string str = std::string(); +/// \endcode +const internal::VariadicDynCastAllOfMatcher +exprWithCleanups; + /// \brief Matches init list expressions. /// /// Given diff --git a/lib/ASTMatchers/Dynamic/Registry.cpp b/lib/ASTMatchers/Dynamic/Registry.cpp index 59f01c848f..6089b45e42 100644 --- a/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/lib/ASTMatchers/Dynamic/Registry.cpp @@ -149,6 +149,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(equalsBoundNode); REGISTER_MATCHER(explicitCastExpr); REGISTER_MATCHER(expr); + REGISTER_MATCHER(exprWithCleanups); REGISTER_MATCHER(fieldDecl); REGISTER_MATCHER(floatLiteral); REGISTER_MATCHER(forEach); diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 05b07c81a9..5d09700e6e 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2943,6 +2943,15 @@ TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { EXPECT_TRUE(matches("void x() { int a; }", declStmt())); } +TEST(ExprWithCleanups, MatchesExprWithCleanups) { + EXPECT_TRUE(matches("struct Foo { ~Foo(); };" + "const Foo f = Foo();", + varDecl(hasInitializer(exprWithCleanups())))); + EXPECT_FALSE(matches("struct Foo { };" + "const Foo f = Foo();", + varDecl(hasInitializer(exprWithCleanups())))); +} + TEST(InitListExpression, MatchesInitListExpression) { EXPECT_TRUE(matches("int a[] = { 1, 2 };", initListExpr(hasType(asString("int [2]")))));