[clang] 1db8112 - [clang-format] Fix crash with ObjC Blocks

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 15 13:42:33 PDT 2022


Author: Björn Schäpers
Date: 2022-03-15T21:41:57+01:00
New Revision: 1db8112311c7f3c1d39c3b62637ac26055ad9d8f

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

LOG: [clang-format] Fix crash with ObjC Blocks

Fixes https://github.com/llvm/llvm-project/issues/54367
Fixes https://github.com/llvm/llvm-project/issues/54368

Differential Revision: https://reviews.llvm.org/D121596

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 f0a17af677016..848f69afca7c3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -783,7 +783,7 @@ class AnnotatingParser {
     OpeningBrace.ParentBracket = Contexts.back().ContextKind;
 
     if (Contexts.back().CaretFound)
-      OpeningBrace.setType(TT_ObjCBlockLBrace);
+      OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
     Contexts.back().CaretFound = false;
 
     ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index cc624d0760d12..ac0802f837173 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -644,6 +644,22 @@ TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
   EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
 }
 
+TEST_F(TokenAnnotatorTest, UnderstandsObjCBlock) {
+  auto Tokens = annotate("int (^)() = ^ ()\n"
+                         "  external_source_symbol() { //\n"
+                         "  return 1;\n"
+                         "};");
+  ASSERT_EQ(Tokens.size(), 21u) << Tokens;
+  EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ObjCBlockLParen);
+  EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_ObjCBlockLBrace);
+
+  Tokens = annotate("int *p = ^int*(){ //\n"
+                    "  return nullptr;\n"
+                    "}();");
+  ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+  EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_ObjCBlockLBrace);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang


        


More information about the cfe-commits mailing list