Skip to content

[clang-query] Allow for trailing comma in matchers #148018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ Improvements to clang-doc
Improvements to clang-query
---------------------------

- Matcher queries interpreted by clang-query are now support trailing comma (,)
in matcher arguments. Note that C++ still doesn't allow this in function
arguments. So when porting a query to C++, remove all instances of trailing
comma (otherwise C++ compiler will just complain about "expected expression").

Improvements to include-cleaner
-------------------------------
- Deprecated the ``-insert`` and ``-remove`` command line options, and added
Expand Down
21 changes: 21 additions & 0 deletions clang-tools-extra/test/clang-query/trailing-comma.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
void foo(void) {}
// CHECK-OK: trailing-comma.c:1:1: note: "root" binds here
// CHECK-ERR-COMMA: Invalid token <,> found when looking for a value.

// RUN: clang-query -c "match \
// RUN: functionDecl( \
// RUN: hasName( \
// RUN: \"foo\", \
// RUN: ), \
// RUN: ) \
// RUN: " %s | FileCheck --check-prefix=CHECK-OK %s

// Same with \n tokens
// RUN: echo "match functionDecl( hasName( \"foo\" , ) , )" | sed "s/ /\n/g" >%t
// RUN: clang-query -f %t %s | FileCheck --check-prefix=CHECK-OK %s

// RUN: not clang-query -c "match functionDecl(hasName(\"foo\"),,)" %s \
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s

// RUN: not clang-query -c "match functionDecl(,)" %s \
// RUN: | FileCheck --check-prefix=CHECK-ERR-COMMA %s
10 changes: 10 additions & 0 deletions clang/lib/ASTMatchers/Dynamic/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ bool Parser::parseMatcherBuilder(MatcherCtor Ctor, const TokenInfo &NameToken,
<< CommaToken.Text;
return false;
}
// Allow for a trailing , token and possibly a new line.
Tokenizer->SkipNewlines();
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
continue;
}
}

Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
Expand Down Expand Up @@ -658,6 +663,11 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
<< CommaToken.Text;
return false;
}
// Allow for a trailing , token and possibly a new line.
Tokenizer->SkipNewlines();
if (Tokenizer->nextTokenKind() == TokenInfo::TK_CloseParen) {
continue;
}
}

Diagnostics::Context Ctx(Diagnostics::Context::MatcherArg, Error,
Expand Down