diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 13d0ba2d0e23..15621b772563 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/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/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp index 59f01c848fec..6089b45e42e0 100644 --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/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/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index 05b07c81a9f2..5d09700e6e0e 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/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]")))));