From: Daniel Jasper Date: Sun, 10 May 2015 08:42:04 +0000 (+0000) Subject: clang-format: Preserve line break before } in __asm { ... }. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9b03bacc4aa53f53d6115fcafb6c2fed7ffe9594;p=clang clang-format: Preserve line break before } in __asm { ... }. Some compilers ignore everything after a semicolon in such inline asm blocks and thus, the closing brace must not be moved to the previous line. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236946 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index b6a7dcd81b..54951abade 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -48,6 +48,7 @@ enum TokenType { TT_ImplicitStringLiteral, TT_InheritanceColon, TT_InlineASMColon, + TT_InlineASMBrace, TT_JavaAnnotation, TT_JsTypeColon, TT_JsTypeOptionalQuestion, diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 50ef0cafbd..67a66fcbd4 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -736,7 +736,8 @@ private: // recovered from an error (e.g. failure to find the matching >). if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro, TT_FunctionLBrace, TT_ImplicitStringLiteral, - TT_RegexLiteral, TT_TrailingReturnArrow)) + TT_InlineASMBrace, TT_RegexLiteral, + TT_TrailingReturnArrow)) CurrentToken->Type = TT_Unknown; CurrentToken->Role.reset(); CurrentToken->MatchingParen = nullptr; @@ -2006,6 +2007,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, Style.Language == FormatStyle::LK_Proto) // Don't put enums onto single lines in protocol buffers. return true; + if (Right.is(TT_InlineASMBrace)) + return Right.HasUnescapedNewline; if (Style.Language == FormatStyle::LK_JavaScript && Right.is(tok::r_brace) && Left.is(tok::l_brace) && !Left.Children.empty()) // Support AllowShortFunctionsOnASingleLine for JavaScript. diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 117e83802c..a85d9c7731 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -666,9 +666,11 @@ void UnwrappedLineParser::parseStructuralElement() { case tok::kw_asm: nextToken(); if (FormatTok->is(tok::l_brace)) { + FormatTok->Type = TT_InlineASMBrace; nextToken(); while (FormatTok && FormatTok->isNot(tok::eof)) { if (FormatTok->is(tok::r_brace)) { + FormatTok->Type = TT_InlineASMBrace; nextToken(); break; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 6569badd03..ce14165bc8 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2316,6 +2316,14 @@ TEST_F(FormatTest, FormatsInlineASM) { " call [edx][eax*4] // stdcall\n" " }\n" "}")); + EXPECT_EQ("_asm {\n" + " xor eax, eax;\n" + " cpuid;\n" + "}", + format("_asm {\n" + " xor eax, eax;\n" + " cpuid;\n" + "}")); verifyFormat("void function() {\n" " // comment\n" " asm(\"\");\n"