r237073 - Allow empty assembly string literal with -fno-gnu-inline-asm

Steven Wu stevenwu at apple.com
Mon May 11 17:16:37 PDT 2015


Author: steven_wu
Date: Mon May 11 19:16:37 2015
New Revision: 237073

URL: http://llvm.org/viewvc/llvm-project?rev=237073&view=rev
Log:
Allow empty assembly string literal with -fno-gnu-inline-asm

Empty assembly string will not introduce assembly code in the output
binary and it is often used as a trick in the header to disable
optimizations. It doesn't conflict with the purpose of the option so it
is allowed with -fno-gnu-inline-asm flag.

Modified:
    cfe/trunk/lib/Parse/ParseStmtAsm.cpp
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/test/Parser/no-gnu-inline-asm.c

Modified: cfe/trunk/lib/Parse/ParseStmtAsm.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmtAsm.cpp?rev=237073&r1=237072&r2=237073&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmtAsm.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmtAsm.cpp Mon May 11 19:16:37 2015
@@ -616,10 +616,6 @@ StmtResult Parser::ParseAsmStatement(boo
     return ParseMicrosoftAsmStatement(AsmLoc);
   }
 
-  // Check if GNU-style inline Asm is disabled.
-  if (!getLangOpts().GNUAsm)
-    Diag(AsmLoc, diag::err_gnu_inline_asm_disabled);
-
   DeclSpec DS(AttrFactory);
   SourceLocation Loc = Tok.getLocation();
   ParseTypeQualifierListOpt(DS, AR_VendorAttributesParsed);
@@ -644,6 +640,15 @@ StmtResult Parser::ParseAsmStatement(boo
   T.consumeOpen();
 
   ExprResult AsmString(ParseAsmStringLiteral());
+
+  // Check if GNU-style InlineAsm is disabled.
+  // Error on anything other than empty string.
+  if (!(getLangOpts().GNUAsm || AsmString.isInvalid())) {
+    const auto *SL = cast<StringLiteral>(AsmString.get());
+    if (!SL->getString().trim().empty())
+      Diag(Loc, diag::err_gnu_inline_asm_disabled);
+  }
+
   if (AsmString.isInvalid()) {
     // Consume up to and including the closing paren.
     T.skipToEnd();

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=237073&r1=237072&r2=237073&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Mon May 11 19:16:37 2015
@@ -670,12 +670,17 @@ Parser::ParseExternalDeclaration(ParsedA
     SourceLocation StartLoc = Tok.getLocation();
     SourceLocation EndLoc;
 
-    // Check if GNU-style InlineAsm is disabled.
-    if (!getLangOpts().GNUAsm)
-      Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
-
     ExprResult Result(ParseSimpleAsm(&EndLoc));
 
+    // Check if GNU-style InlineAsm is disabled.
+    // Empty asm string is allowed because it will not introduce
+    // any assembly code.
+    if (!(getLangOpts().GNUAsm || Result.isInvalid())) {
+      const auto *SL = cast<StringLiteral>(Result.get());
+      if (!SL->getString().trim().empty())
+        Diag(StartLoc, diag::err_gnu_inline_asm_disabled);
+    }
+
     ExpectAndConsume(tok::semi, diag::err_expected_after,
                      "top-level asm block");
 

Modified: cfe/trunk/test/Parser/no-gnu-inline-asm.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/no-gnu-inline-asm.c?rev=237073&r1=237072&r2=237073&view=diff
==============================================================================
--- cfe/trunk/test/Parser/no-gnu-inline-asm.c (original)
+++ cfe/trunk/test/Parser/no-gnu-inline-asm.c Mon May 11 19:16:37 2015
@@ -5,8 +5,11 @@ asm ("INST r1, 0"); // expected-error {{
 void foo() __asm("__foo_func"); // AsmLabel is OK
 int foo1 asm("bar1") = 0; // OK
 
+asm(" "); // Whitespace is OK
+
 void f (void) {
   long long foo = 0, bar;
   asm volatile("INST %0, %1" : "=r"(foo) : "r"(bar)); // expected-error {{GNU-style inline assembly is disabled}}
+  asm (""); // Empty is OK
   return;
 }





More information about the cfe-commits mailing list