From: Steven Wu Date: Tue, 12 May 2015 00:16:37 +0000 (+0000) Subject: Allow empty assembly string literal with -fno-gnu-inline-asm X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2ecef342b1f3d35908931b65ee2bbc94352bc850;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237073 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Parse/ParseStmtAsm.cpp b/lib/Parse/ParseStmtAsm.cpp index 85eff1712d..8ba9f15f0a 100644 --- a/lib/Parse/ParseStmtAsm.cpp +++ b/lib/Parse/ParseStmtAsm.cpp @@ -616,10 +616,6 @@ StmtResult Parser::ParseAsmStatement(bool &msAsm) { 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(bool &msAsm) { 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(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(); diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp index ed27a9e395..697fda9215 100644 --- a/lib/Parse/Parser.cpp +++ b/lib/Parse/Parser.cpp @@ -670,12 +670,17 @@ Parser::ParseExternalDeclaration(ParsedAttributesWithRange &attrs, 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(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"); diff --git a/test/Parser/no-gnu-inline-asm.c b/test/Parser/no-gnu-inline-asm.c index 7089fa4b7d..03c2ede948 100644 --- a/test/Parser/no-gnu-inline-asm.c +++ b/test/Parser/no-gnu-inline-asm.c @@ -5,8 +5,11 @@ asm ("INST r1, 0"); // expected-error {{GNU-style inline assembly is disabled}} 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; }