-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang Author: Remy Farley (one-d-wide) ChangesAllow 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:
CC: @AaronBallman Full diff: https://github.com/llvm/llvm-project/pull/148018.diff 2 Files Affected:
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,
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I think we need to think about this a little bit; my concern is that folks often use clang-query to write queries they'll eventually be using in clang-tidy checks. However, the tidy checks are using the C++ DSL which won't support trailing commas (and I don't think could be made to support them). So while it might make things easier in some cases, it may be a surprising difference between the tools. WDYT?
If we go forward with this, we should add a release note to clang-tools-extra/docs/ReleaseNotes.rst
. We don't currently have any docs for clang-query, but if we had those docs, we should probably note the difference between the C++ DSL and clang-query in them. (That's not something you have to worry about for your patch though.)
5992431
to
b35f9eb
Compare
I don't think this is much of an issue. Once you attempt to interpret a query with trailing comma in it as C++ code, a compiler (or even an LSP) will usually emit a (mostly) unambiguous error, which should be trivial to correct. And that's it. Existing queries, including ones ported back from C++, are unaffected. I see this feature as only helpful during the development of a query, smoothing the experience by, for example, allowing to conveniently comment query arguments out (which also done in a way incompatible with C++ btw). In which case getting an error is rather annoying, since matcher arguments act more like parameters in a C++ initializer list (where trailing comma is actually allowed), rather than positional arguments in a function. Updated release notes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think I'm okay with this. It's a bit different from C++, but the difference seems sufficiently useful and the amount of pain with switching between the two DSLs seems pretty minimal.
I did have a request for some additional test coverage though.
b35f9eb
to
7e8d7da
Compare
It looks like precommit CI found a relevant failure on Windows:
I think the use of |
7e8d7da
to
22e6282
Compare
@one-d-wide Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
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:
CC: @AaronBallman