Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -1048,6 +1048,18 @@ Decl, UnresolvedUsingTypenameDecl> unresolvedUsingTypenameDecl; +/// \brief Matches parenthesis used in expressions +// +// Example matches (foo() + 1), it does not match parenthesis +// in function declaration neither the parenthesis in function call. +// \code +// int foo() { return 1; } +// int a = (foo() + 1); +// \endcode +const internal::VariadicDynCastAllOfMatcher< + Stmt, + ParenExpr> parenExpr; + /// \brief Matches constructor call expressions (including implicit ones). /// /// Example matches string(ptr, n) and ptr within arguments of f Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -326,6 +326,7 @@ REGISTER_MATCHER(on); REGISTER_MATCHER(onImplicitObjectArgument); REGISTER_MATCHER(parameterCountIs); + REGISTER_MATCHER(parenExpr); REGISTER_MATCHER(parenType); REGISTER_MATCHER(parmVarDecl); REGISTER_MATCHER(pointee); Index: unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3563,6 +3563,13 @@ varDecl(isExceptionVariable()))); } +TEST(ParenExpression, SimpleCases) { + EXPECT_TRUE(matches("int i = (3);", parenExpr())); + EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr())); + EXPECT_FALSE(matches("int i = 3;", parenExpr())); + EXPECT_FALSE(matches("int foo() { return 1; }; int a = foo();", parenExpr())); +} + TEST(HasConditionVariableStatement, DoesNotMatchCondition) { EXPECT_TRUE(notMatches( "void x() { if(true) {} }", Index: unittests/ASTMatchers/Dynamic/RegistryTest.cpp =================================================================== --- unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -506,6 +506,12 @@ EXPECT_FALSE(matches("struct X {};", Value)); } +TEST_F(RegistryTest, ParenExpr) { + Matcher Value = constructMatcher("parenExpr").getTypedMatcher(); + EXPECT_TRUE(matches("int i = (1);", Value)); + EXPECT_FALSE(matches("int i = 1;", Value)); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers