Skip to content

Commit

Permalink
[ASTMatchers] Existing matcher hasAnyArgument fixed
Browse files Browse the repository at this point in the history
Summary: A checker (will be uploaded after this patch) needs to check implicit casts. The checker needs matcher hasAnyArgument but it ignores implicit casts and parenthesized expressions which disables checking of implicit casts for arguments in the checker. However the documentation of the matcher contains a FIXME that this should be removed once separate matchers for ignoring implicit casts and parenthesized expressions are ready. Since these matchers were already there the fix could be executed. Only one Clang checker was affected which was also fixed (ignoreParenImpCasts added) and is separately uploaded. Third party checkers (not in the Clang repository) may be affected by this fix so the fix must be emphasized in the release notes.

Reviewers: klimek, sbenza, alexfh

Subscribers: alexfh, klimek, xazax.hun, cfe-commits

Differential Revision: http://reviews.llvm.org/D18243

llvm-svn: 264855
  • Loading branch information
Xazax-hun committed Mar 30, 2016
1 parent 058c302 commit 1b654f2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
10 changes: 0 additions & 10 deletions clang/docs/LibASTMatchersReference.html
Expand Up @@ -3636,11 +3636,6 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
matches x(1, y, 42)
with hasAnyArgument(...)
matching y

FIXME: Currently this will ignore parentheses and implicit casts on
the argument before applying the inner matcher. We'll want to remove
this to allow for greater control by the user once ignoreImplicit()
has been implemented.
</pre></td></tr>


Expand Down Expand Up @@ -3907,11 +3902,6 @@ <h2 id="traversal-matchers">AST Traversal Matchers</h2>
matches x(1, y, 42)
with hasAnyArgument(...)
matching y

FIXME: Currently this will ignore parentheses and implicit casts on
the argument before applying the inner matcher. We'll want to remove
this to allow for greater control by the user once ignoreImplicit()
has been implemented.
</pre></td></tr>


Expand Down
6 changes: 6 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -120,6 +120,12 @@ this section should help get you past the largest hurdles of upgrading.
AST Matchers
------------

- hasAnyArgument: Matcher no longer ignores parentheses and implicit casts on
the argument before applying the inner matcher. The fix was done to allow for
greater control by the user. In all existing checkers that use this matcher
all instances of code ``hasAnyArgument(<inner matcher>)`` must be changed to
``hasAnyArgument(ignoringParenImpCasts(<inner matcher>))``.

...

libclang
Expand Down
7 changes: 1 addition & 6 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Expand Up @@ -2989,18 +2989,13 @@ AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
/// matches x(1, y, 42)
/// with hasAnyArgument(...)
/// matching y
///
/// FIXME: Currently this will ignore parentheses and implicit casts on
/// the argument before applying the inner matcher. We'll want to remove
/// this to allow for greater control by the user once \c ignoreImplicit()
/// has been implemented.
AST_POLYMORPHIC_MATCHER_P(hasAnyArgument,
AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
CXXConstructExpr),
internal::Matcher<Expr>, InnerMatcher) {
for (const Expr *Arg : Node.arguments()) {
BoundNodesTreeBuilder Result(*Builder);
if (InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, &Result)) {
if (InnerMatcher.matches(*Arg, Finder, &Result)) {
*Builder = std::move(Result);
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion clang/unittests/ASTMatchers/ASTMatchersTest.cpp
Expand Up @@ -1633,10 +1633,15 @@ TEST(Matcher, Argument) {

TEST(Matcher, AnyArgument) {
StatementMatcher CallArgumentY = callExpr(
hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))));
hasAnyArgument(
ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("y")))))));
EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY));
EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY));
EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY));

StatementMatcher ImplicitCastedArgument = callExpr(
hasAnyArgument(implicitCastExpr()));
EXPECT_TRUE(matches("void x(long) { int y; x(y); }", ImplicitCastedArgument));
}

TEST(ForEachArgumentWithParam, ReportsNoFalsePositives) {
Expand Down

0 comments on commit 1b654f2

Please sign in to comment.