[clang] [clang-tools-extra] [clang-query] Allow for trailing comma in matchers (PR #148018)
Remy Farley via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 15 07:53:02 PDT 2025
https://github.com/one-d-wide updated https://github.com/llvm/llvm-project/pull/148018
>From 22e6282b5dbad9c76b036ef379a4c32ae85555d9 Mon Sep 17 00:00:00 2001
From: "Remy D. Farley" <one-d-wide at protonmail.com>
Date: Tue, 15 Jul 2025 14:51:31 +0000
Subject: [PATCH] [clang-query] Allow for trailing comma in matchers
---
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
.../test/clang-query/trailing-comma.c | 21 +++++++++++++++++++
clang/lib/ASTMatchers/Dynamic/Parser.cpp | 10 +++++++++
3 files changed, 36 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..4fc0cb0f24187
--- /dev/null
+++ b/clang-tools-extra/test/clang-query/trailing-comma.c
@@ -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
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,
More information about the cfe-commits
mailing list