r223992 - Parse: Concatenated string literals should be verified in inline asm

David Majnemer david.majnemer at gmail.com
Wed Dec 10 17:00:49 PST 2014


Author: majnemer
Date: Wed Dec 10 19:00:48 2014
New Revision: 223992

URL: http://llvm.org/viewvc/llvm-project?rev=223992&view=rev
Log:
Parse: Concatenated string literals should be verified in inline asm

While we would correctly handle asm("foo") and reject asm(L"bar"), we
weren't careful to handle cases where an ascii literal could be
concatenated with a wide literal.

This fixes PR21822.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/SemaStmtAsm.cpp
    cfe/trunk/test/Parser/asm.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=223992&r1=223991&r2=223992&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Dec 10 19:00:48 2014
@@ -6143,7 +6143,6 @@ def warn_cast_qual2 : Warning<"cast from
 
 // inline asm.
 let CategoryName = "Inline Assembly Issue" in {
-  def err_asm_wide_character : Error<"wide string is invalid in 'asm'">;
   def err_asm_invalid_lvalue_in_output : Error<"invalid lvalue in asm output">;
   def err_asm_invalid_output_constraint : Error<
     "invalid output constraint '%0' in asm">;

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=223992&r1=223991&r2=223992&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Wed Dec 10 19:00:48 2014
@@ -1224,26 +1224,23 @@ void Parser::ParseKNRParamDeclarations(D
 ///         string-literal
 ///
 ExprResult Parser::ParseAsmStringLiteral() {
-  switch (Tok.getKind()) {
-    case tok::string_literal:
-      break;
-    case tok::utf8_string_literal:
-    case tok::utf16_string_literal:
-    case tok::utf32_string_literal:
-    case tok::wide_string_literal: {
-      SourceLocation L = Tok.getLocation();
+  if (!isTokenStringLiteral()) {
+    Diag(Tok, diag::err_expected_string_literal)
+      << /*Source='in...'*/0 << "'asm'";
+    return ExprError();
+  }
+
+  ExprResult AsmString(ParseStringLiteralExpression());
+  if (!AsmString.isInvalid()) {
+    const auto *SL = cast<StringLiteral>(AsmString.get());
+    if (!SL->isAscii()) {
       Diag(Tok, diag::err_asm_operand_wide_string_literal)
-        << (Tok.getKind() == tok::wide_string_literal)
-        << SourceRange(L, L);
+        << SL->isWide()
+        << SL->getSourceRange();
       return ExprError();
     }
-    default:
-      Diag(Tok, diag::err_expected_string_literal)
-        << /*Source='in...'*/0 << "'asm'";
-      return ExprError();
   }
-
-  return ParseStringLiteralExpression();
+  return AsmString;
 }
 
 /// ParseSimpleAsm

Modified: cfe/trunk/lib/Sema/SemaStmtAsm.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmtAsm.cpp?rev=223992&r1=223991&r2=223992&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmtAsm.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmtAsm.cpp Wed Dec 10 19:00:48 2014
@@ -116,15 +116,11 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceL
   SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
 
   // The parser verifies that there is a string literal here.
-  if (!AsmString->isAscii())
-    return StmtError(Diag(AsmString->getLocStart(),diag::err_asm_wide_character)
-      << AsmString->getSourceRange());
+  assert(AsmString->isAscii());
 
   for (unsigned i = 0; i != NumOutputs; i++) {
     StringLiteral *Literal = Constraints[i];
-    if (!Literal->isAscii())
-      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
-        << Literal->getSourceRange());
+    assert(Literal->isAscii());
 
     StringRef OutputName;
     if (Names[i])
@@ -172,9 +168,7 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceL
 
   for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
     StringLiteral *Literal = Constraints[i];
-    if (!Literal->isAscii())
-      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
-        << Literal->getSourceRange());
+    assert(Literal->isAscii());
 
     StringRef InputName;
     if (Names[i])
@@ -240,9 +234,7 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceL
   // Check that the clobbers are valid.
   for (unsigned i = 0; i != NumClobbers; i++) {
     StringLiteral *Literal = Clobbers[i];
-    if (!Literal->isAscii())
-      return StmtError(Diag(Literal->getLocStart(),diag::err_asm_wide_character)
-        << Literal->getSourceRange());
+    assert(Literal->isAscii());
 
     StringRef Clobber = Literal->getString();
 

Modified: cfe/trunk/test/Parser/asm.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/asm.cpp?rev=223992&r1=223991&r2=223992&view=diff
==============================================================================
--- cfe/trunk/test/Parser/asm.cpp (original)
+++ cfe/trunk/test/Parser/asm.cpp Wed Dec 10 19:00:48 2014
@@ -6,3 +6,4 @@ int foo3 asm (u8"bar3"); // expected-err
 int foo4 asm (u"bar4"); // expected-error {{cannot use unicode string literal in 'asm'}}
 int foo5 asm (U"bar5"); // expected-error {{cannot use unicode string literal in 'asm'}}
 int foo6 asm ("bar6"_x); // expected-error {{string literal with user-defined suffix cannot be used here}}
+int foo6 asm ("" L"bar7"); // expected-error {{cannot use wide string literal in 'asm'}}





More information about the cfe-commits mailing list