diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -54,6 +54,7 @@ TYPE(InheritanceComma) \ TYPE(InlineASMBrace) \ TYPE(InlineASMColon) \ + TYPE(InlineASMSymbolicNameLSquare) \ TYPE(JavaAnnotation) \ TYPE(JsComputedPropertyName) \ TYPE(JsExponentiation) \ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -499,6 +499,8 @@ } else if (Left->is(TT_Unknown)) { if (StartsObjCMethodExpr) { Left->Type = TT_ObjCMethodExpr; + } else if (Style.isCpp() && InsideInlineASM) { + Left->Type = TT_InlineASMSymbolicNameLSquare; } else if (IsCpp11AttributeSpecifier) { Left->Type = TT_AttributeSquare; } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -15127,6 +15127,30 @@ guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); } +TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { + // ASM symbolic names are identifiers that must be surrounded by [] without + // space in between: + // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands + + // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. + verifyFormat(R"(// +asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); +)"); + + // A list of several ASM symbolic names. + verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); + + // ASM symbolic names in inline ASM with inputs and outputs. + verifyFormat(R"(// +asm("cmoveq %1, %2, %[result]" + : [result] "=r"(result) + : "r"(test), "r"(new), "[result]"(old)); +)"); + + // ASM symbolic names in inline ASM with no outputs. + verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); +} + TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "void f() {\n"