[clang] [clang-format]: Annotate colons found in inline assembly (PR #92617)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Fri May 17 16:22:44 PDT 2024
https://github.com/gedare created https://github.com/llvm/llvm-project/pull/92617
Short-circuit the parsing of tok::colon to label colons found within lines starting with asm as InlineASMColon.
Fixes #92616.
>From b4a8c06b79ec10ed2f53a7410bd847ecfa9e8450 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 17 May 2024 17:18:59 -0600
Subject: [PATCH] [clang-format]: Annotate colons found in inline assembly
Short-circuit the parsing of tok::colon to label colons found
within lines starting with asm as InlineASMColon.
Fixes #92616.
---
clang/lib/Format/TokenAnnotator.cpp | 2 ++
clang/unittests/Format/TokenAnnotatorTest.cpp | 20 ++++++++++++++++---
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7c4c76a91f2c5..a83f937336565 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1358,6 +1358,8 @@ class AnnotatingParser {
Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
Tok->setType(TT_ModulePartitionColon);
+ } else if (Line.First->is(tok::kw_asm)) {
+ Tok->setType(TT_InlineASMColon);
} else if (Contexts.back().ColonIsDictLiteral || Style.isProto()) {
Tok->setType(TT_DictLiteral);
if (Style.Language == FormatStyle::LK_TextProto) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index aadfa6dc0165c..0f5f3936e0181 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1489,11 +1489,25 @@ TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
auto Tokens = annotate("__asm{\n"
"a:\n"
- "};");
- ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ ": x\n"
+ ":};");
+ ASSERT_EQ(Tokens.size(), 10u) << 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);
+ EXPECT_TOKEN(Tokens[3], tok::colon, TT_InlineASMColon);
+ EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+ EXPECT_TOKEN(Tokens[6], tok::colon, TT_InlineASMColon);
+ EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_InlineASMBrace);
+
+ Tokens = annotate("__asm__ volatile (\n"
+ "a:\n"
+ ": x\n"
+ ":);");
+ ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+ EXPECT_TOKEN(Tokens[0], tok::kw_asm, TT_Unknown);
+ EXPECT_TOKEN(Tokens[4], tok::colon, TT_InlineASMColon);
+ EXPECT_TOKEN(Tokens[5], tok::colon, TT_InlineASMColon);
+ EXPECT_TOKEN(Tokens[7], tok::colon, TT_InlineASMColon);
}
TEST_F(TokenAnnotatorTest, UnderstandsObjCBlock) {
More information about the cfe-commits
mailing list