https://github.com/one-d-wide updated https://github.com/llvm/llvm-project/pull/148018
>From b35f9ebdd2e345449c2cea8df8d6731cde33dcb3 Mon Sep 17 00:00:00 2001 From: "Remy D. Farley" <one-d-w...@protonmail.com> Date: Sat, 12 Jul 2025 11:53:58 +0000 Subject: [PATCH] [clang-query] Allow for trailing comma in matchers --- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ clang-tools-extra/test/clang-query/trailing-comma.c | 10 ++++++++++ clang/lib/ASTMatchers/Dynamic/Parser.cpp | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 clang-tools-extra/test/clang-query/trailing-comma.c diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index e021d6350694e..f124f9318828f 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -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 diff --git a/clang-tools-extra/test/clang-query/trailing-comma.c b/clang-tools-extra/test/clang-query/trailing-comma.c new file mode 100644 index 0000000000000..ecdfd3b8ab6fd --- /dev/null +++ b/clang-tools-extra/test/clang-query/trailing-comma.c @@ -0,0 +1,10 @@ +// RUN: clang-query -c "match \ +// RUN: functionDecl( \ +// RUN: hasName( \ +// RUN: \"foo\", \ +// RUN: ), \ +// RUN: ) \ +// RUN: " %s -- | FileCheck %s + +// CHECK: trailing-comma.c:10:1: note: "root" binds here +void foo(void) {} diff --git a/clang/lib/ASTMatchers/Dynamic/Parser.cpp b/clang/lib/ASTMatchers/Dynamic/Parser.cpp index 8a5ac4d0f9d0c..baac797f09ec3 100644 --- a/clang/lib/ASTMatchers/Dynamic/Parser.cpp +++ b/clang/lib/ASTMatchers/Dynamic/Parser.cpp @@ -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, @@ -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, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits