Skip to content
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] Correctly annotate */& in if condition with braced init #109505

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 21, 2024

Fixes #109371.

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 21, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #109371.


Full diff: https://github.com/llvm/llvm-project/pull/109505.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+16-9)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f665ce2ad81eb0..f5acfaaee900a2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1165,19 +1165,26 @@ class AnnotatingParser {
 
     ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
     Contexts.back().ColonIsDictLiteral = true;
-    if (OpeningBrace.is(BK_BracedInit))
+
+    const auto *Prev = OpeningBrace.getPreviousNonComment();
+
+    if (OpeningBrace.is(BK_BracedInit)) {
       Contexts.back().IsExpression = true;
-    if (Style.isJavaScript() && OpeningBrace.Previous &&
-        OpeningBrace.Previous->is(TT_JsTypeColon)) {
-      Contexts.back().IsExpression = false;
-    }
-    if (Style.isVerilog() &&
-        (!OpeningBrace.getPreviousNonComment() ||
-         OpeningBrace.getPreviousNonComment()->isNot(Keywords.kw_apostrophe))) {
-      Contexts.back().VerilogMayBeConcatenation = true;
+      if (Prev) {
+        for (auto *Tok = Prev->Previous; Tok && Tok->isPointerOrReference();
+             Tok = Tok->Previous) {
+          Tok->setFinalizedType(TT_PointerOrReference);
+        }
+      }
     }
+
+    if (Style.isJavaScript() && Prev && Prev->is(TT_JsTypeColon))
+      Contexts.back().IsExpression = false;
+
     if (Style.isTableGen())
       Contexts.back().ColonIsDictLiteral = false;
+    else if (Style.isVerilog() && !(Prev && Prev->is(Keywords.kw_apostrophe)))
+      Contexts.back().VerilogMayBeConcatenation = true;
 
     unsigned CommaCount = 0;
     while (CurrentToken) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1884d41a5f23f5..e2c9ba01794caf 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -308,6 +308,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
   EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
 
+  Tokens = annotate("if (Foo *&foo{a})");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
+
   FormatStyle Style = getLLVMStyle();
   Style.TypeNames.push_back("MYI");
   Tokens = annotate("if (MYI *p{nullptr})", Style);

@owenca owenca merged commit 98281da into llvm:main Oct 3, 2024
10 checks passed
@owenca owenca deleted the 109371 branch October 3, 2024 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect clang-format PointerAlignment for brace-enclosed initialization within if-statement
3 participants