[clang-format] Fix crash on asm block with label
authorBjörn Schäpers <bjoern@hazardy.de>
Sun, 13 Mar 2022 20:25:11 +0000 (21:25 +0100)
committerBjörn Schäpers <bjoern@hazardy.de>
Mon, 14 Mar 2022 11:44:48 +0000 (12:44 +0100)
Fixes https://github.com/llvm/llvm-project/issues/54349

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

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

index 8f433d7..6cf3681 100644 (file)
@@ -824,8 +824,10 @@ private:
               Previous->is(tok::string_literal))
             Previous->setType(TT_SelectorName);
         }
-        if (CurrentToken->is(tok::colon) || Style.isJavaScript())
+        if (CurrentToken->is(tok::colon) && OpeningBrace.is(TT_Unknown))
           OpeningBrace.setType(TT_DictLiteral);
+        else if (Style.isJavaScript())
+          OpeningBrace.overwriteFixedType(TT_DictLiteral);
       }
       if (CurrentToken->is(tok::comma)) {
         if (Style.isJavaScript())
index 05cf000..dd868be 100644 (file)
@@ -602,6 +602,16 @@ TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
           << I;
 }
 
+TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
+  auto Tokens = annotate("__asm{\n"
+                         "a:\n"
+                         "};");
+  ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+  EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+  EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_InlineASMBrace);
+  EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang