Skip to content

Commit 5b67a18

Browse files
author
kakje
committed
[Needs review] Fix ASTMatchers clang unit test: add ignoringParenTmp AST matcher
Checked C binds string literals to temporaries. Add a matcher that ignores parens and temporary bindings on expressions and use the matcher in the IgnoringParens ASTMatcher test.
1 parent 15a3e92 commit 5b67a18

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,24 @@ AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
886886
return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
887887
}
888888

889+
/// Matches expressions that match InnerMatcher after parentheses and
890+
/// Checked C temporary binding expressions are stripped off.
891+
///
892+
/// Given
893+
/// \code
894+
/// char *a = "";
895+
/// \endcode
896+
/// The matcher
897+
/// varDecl(hasInitializer(ignoringParenTmp(stringLiteral())))
898+
/// would match the declaration for a.
899+
/// while
900+
/// varDecl(hasInitializer(stringLiteral()))
901+
/// would match the declaration for a only if the Checked C extension
902+
/// is disabled.
903+
AST_MATCHER_P(Expr, ignoringParenTmp, internal::Matcher<Expr>, InnerMatcher) {
904+
return InnerMatcher.matches(*Node.IgnoreParenTmp(), Finder, Builder);
905+
}
906+
889907
/// Matches expressions that match InnerMatcher after implicit casts and
890908
/// parentheses are stripped off.
891909
///

clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ TEST_P(ASTMatchersTest, IgnoringParens) {
15011501
EXPECT_TRUE(matches("const char* str = (\"my-string\");",
15021502
traverse(ast_type_traits::TK_AsIs,
15031503
implicitCastExpr(hasSourceExpression(
1504-
ignoringParens(stringLiteral()))))));
1504+
ignoringParenTmp(stringLiteral()))))));
15051505
}
15061506

15071507
TEST_P(ASTMatchersTest, QualType) {

0 commit comments

Comments
 (0)