https://github.com/one-d-wide created https://github.com/llvm/llvm-project/pull/148018
Allow AST matches in clang-query to have a trailing comma at the end of matcher arguments. Makes it nicer to work with queries that span multiple lines. So, for example, the following is possible: ```clang-query match namedDecl( isExpansionInMainFile(), anyOf( varDecl().bind("var"), functionDecl().bind("func"), # enumDecl().bind("enum"), ), ) ``` CC: @AaronBallman >From 599243135f75fe0dd629a9078f01dee3fb260cf7 Mon Sep 17 00:00:00 2001 From: "Remy D. Farley" <one-d-w...@protonmail.com> Date: Thu, 10 Jul 2025 17:46:14 +0000 Subject: [PATCH] [clang-query] Allow for trailing comma in matchers --- clang-tools-extra/test/clang-query/trailing-comma.c | 10 ++++++++++ clang/lib/ASTMatchers/Dynamic/Parser.cpp | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 clang-tools-extra/test/clang-query/trailing-comma.c 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