[clang] d69050d - [clang-format] Annotate r_paren before braced list as TT_CastRParen (#96271)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 21 20:23:23 PDT 2024


Author: Owen Pan
Date: 2024-06-21T20:23:18-07:00
New Revision: d69050d614cc3348fb639f4a1862e8b9a9ad9885

URL: https://github.com/llvm/llvm-project/commit/d69050d614cc3348fb639f4a1862e8b9a9ad9885
DIFF: https://github.com/llvm/llvm-project/commit/d69050d614cc3348fb639f4a1862e8b9a9ad9885.diff

LOG: [clang-format] Annotate r_paren before braced list as TT_CastRParen (#96271)

Fixes #96096.

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/TokenAnnotatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 63a028a6f4779..55c5ecee45e0c 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2837,6 +2837,11 @@ class AnnotatingParser {
     if (!AfterRParen->Next)
       return false;
 
+    if (AfterRParen->is(tok::l_brace) &&
+        AfterRParen->getBlockKind() == BK_BracedInit) {
+      return true;
+    }
+
     // If the next token after the parenthesis is a unary operator, assume
     // that this is cast, unless there are unexpected tokens inside the
     // parenthesis.

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 0dc506458f7c3..12c4b7fdd5ac2 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -662,6 +662,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
   EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_Unknown);
   EXPECT_TOKEN(Tokens[4], tok::amp, TT_BinaryOperator);
 
+  Tokens = annotate("return (struct foo){};");
+  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
+
   Tokens = annotate("#define FOO(bar) foo((uint64_t)&bar)");
   ASSERT_EQ(Tokens.size(), 15u) << Tokens;
   EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);


        


More information about the cfe-commits mailing list