From 35abbf166d4ad13e4a89095307bb6e4b2e96e0b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= Date: Sun, 13 Mar 2022 21:25:11 +0100 Subject: [PATCH] [clang-format] Fix crash on asm block with label Fixes https://github.com/llvm/llvm-project/issues/54349 Differential Revision: https://reviews.llvm.org/D121559 --- clang/lib/Format/TokenAnnotator.cpp | 4 +++- clang/unittests/Format/TokenAnnotatorTest.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 8f433d7..6cf3681 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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()) diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 05cf000..dd868be 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -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 -- 2.7.4