]> granicus.if.org Git - clang/commitdiff
clang-format: Preserve line break before } in __asm { ... }.
authorDaniel Jasper <djasper@google.com>
Sun, 10 May 2015 08:42:04 +0000 (08:42 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 10 May 2015 08:42:04 +0000 (08:42 +0000)
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

lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index b6a7dcd81b0a33319132e8582d842e7a7005496b..54951abade07236f7bffff34b19b1c243f2130a1 100644 (file)
@@ -48,6 +48,7 @@ enum TokenType {
   TT_ImplicitStringLiteral,
   TT_InheritanceColon,
   TT_InlineASMColon,
+  TT_InlineASMBrace,
   TT_JavaAnnotation,
   TT_JsTypeColon,
   TT_JsTypeOptionalQuestion,
index 50ef0cafbde3ac757969058a2046fc99d87ef3ce..67a66fcbd4cceefa019aee0b4db8faad453d0602 100644 (file)
@@ -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.
index 117e83802c877ff8eb88721547a1671498a52c2f..a85d9c7731660aa8cea4e557a318cc7e6309a83c 100644 (file)
@@ -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;
         }
index 6569badd0356e9d32d03a991b4e09af1b1174b5b..ce14165bc881e245e52e73092e4fa7c6313e6149 100644 (file)
@@ -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"