[clang] [clang-tools-extra] [clang-query] Allow for trailing comma in matchers (PR #148018)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 10 11:06:59 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Remy Farley (one-d-wide)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/148018.diff
2 Files Affected:
- (added) clang-tools-extra/test/clang-query/trailing-comma.c (+10)
- (modified) clang/lib/ASTMatchers/Dynamic/Parser.cpp (+10)
``````````diff
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,
``````````
</details>
https://github.com/llvm/llvm-project/pull/148018
More information about the cfe-commits
mailing list