Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -1061,6 +1061,14 @@ [[NSString alloc] initWithString:@"Hello"] +
Matches parentheses used in expressions.
+
+Given
+ int foo() { return 1; }
+ int a = (foo() + 1);
+matches '(foo() + 1)'
+Matches return statements.
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -1048,6 +1048,17 @@
Decl,
UnresolvedUsingTypenameDecl> unresolvedUsingTypenameDecl;
+/// \brief Matches parentheses used in expressions.
+///
+/// Example matches (foo() + 1)
+/// \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,14 @@
varDecl(isExceptionVariable())));
}
+TEST(ParenExpression, SimpleCases) {
+ EXPECT_TRUE(matches("int i = (3);", parenExpr()));
+ EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr()));
+ EXPECT_TRUE(notMatches("int i = 3;", parenExpr()));
+ EXPECT_TRUE(notMatches("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