-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[clang-format] Fix a bug in annotating CastRParen #102261
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #102102. Full diff: https://github.com/llvm/llvm-project/pull/102261.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 4ed3e9d0e8e855..e1c9f37c6e8dc2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2874,9 +2874,18 @@ class AnnotatingParser {
return false;
// Search for unexpected tokens.
- for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
+ for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous) {
+ if (Prev->is(tok::r_paren)) {
+ Prev = Prev->MatchingParen;
+ if (!Prev)
+ return false;
+ if (Prev->is(TT_FunctionTypeLParen))
+ break;
+ continue;
+ }
if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
return false;
+ }
return true;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 6b71d0d18cf7a4..cbbe550a79ebc2 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -740,6 +740,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_Unknown);
EXPECT_TOKEN(Tokens[11], tok::amp, TT_BinaryOperator);
+ Tokens = annotate("func((void (*)())&a);");
+ ASSERT_EQ(Tokens.size(), 15u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
+ EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_CastRParen);
+ EXPECT_TOKEN(Tokens[10], tok::amp, TT_UnaryOperator);
+
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
|
I'm sorry I'm not familiar with LLVM release cycle, but is it too late for this to make it into the |
It's not too late, but in general we backport bug fixes only if the bugs are labeled as |
/cherry-pick 8c7a038 |
Fixes llvm#102102. (cherry picked from commit 8c7a038)
/pull-request #102419 |
Thank you very much. |
This introduced a regression though. - for (i = 0, k = 1; i < ((PetscInt)local_n0) * partial_dim; i++, k++) {
+ for (i = 0, k = 1; i < ((PetscInt)local_n0)*partial_dim; i++, k++) { |
Nevermind, this bug (not sure if it's a bug or not) is already present in clang-format 18. See #102727. |
Fixes llvm#102102. (cherry picked from commit 8c7a038)
It appears that this introduced a regression in code like this:
After this patch, the last line gets formatted as:
|
What's your version?
|
Ohh sorry, I had started with a stale clang-format commit. This is a non-issue at HEAD. |
there still seems to be a regression when the operator is |
Fixes #102102.