[cfe-commits] r44289 - in /cfe/trunk: Sema/SemaStmt.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/asm.c

Anders Carlsson andersca at mac.com
Fri Nov 23 11:43:51 PST 2007


Author: andersca
Date: Fri Nov 23 13:43:50 2007
New Revision: 44289

URL: http://llvm.org/viewvc/llvm-project?rev=44289&view=rev
Log:
Check asm input and output expressions.

Added:
    cfe/trunk/test/Sema/asm.c
Modified:
    cfe/trunk/Sema/SemaStmt.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaStmt.cpp?rev=44289&r1=44288&r2=44289&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Fri Nov 23 13:43:50 2007
@@ -655,8 +655,40 @@
                                     ExprTy **Clobbers,
                                     SourceLocation RParenLoc) {
   Expr *E = (Expr *)AsmString;
+
+  // Check that the output exprs are valid lvalues.
+  for (unsigned i = 0; i < NumOutputs; i++) {
+    Expr *OutputExpr = (Expr *)Exprs[i];
+    Expr::isLvalueResult Result = OutputExpr->isLvalue();
+    
+    if (Result != Expr::LV_Valid) {
+      ParenExpr *PE = cast<ParenExpr>(OutputExpr);
+      
+      Diag(PE->getSubExpr()->getLocStart(), 
+           diag::err_invalid_lvalue_in_asm_output,
+           PE->getSubExpr()->getSourceRange());
+      
+      // FIXME: We currently leak memory here.
+      return true;
+    }
+  }
+  
+  // Check that the input exprs aren't of type void.
+  for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
+    Expr *InputExpr = (Expr *)Exprs[i];
     
-  // FIXME: Make sure that the expressions are valid.
+    if (InputExpr->getType()->isVoidType()) {
+      ParenExpr *PE = cast<ParenExpr>(InputExpr);
+      
+      Diag(PE->getSubExpr()->getLocStart(),
+           diag::err_invalid_type_in_asm_input,
+           PE->getType().getAsString(), 
+           PE->getSubExpr()->getSourceRange());
+      
+      // FIXME: We currently leak memory here.
+      return true;
+    }
+  }
   
   return new AsmStmt(AsmLoc,
                      NumOutputs,

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=44289&r1=44288&r2=44289&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Fri Nov 23 13:43:50 2007
@@ -789,6 +789,10 @@
      "expression result unused")
 DIAG(err_pascal_string_too_long, ERROR,
     "Pascal string is too long")
+DIAG(err_invalid_lvalue_in_asm_output, ERROR,
+    "invalid lvalue in asm output")
+DIAG(err_invalid_type_in_asm_input, ERROR,
+    "invalid type '%0' in asm input")
 
 // CHECK: printf format string errors
 DIAG(warn_printf_not_string_constant, WARNING,

Added: cfe/trunk/test/Sema/asm.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/asm.c?rev=44289&view=auto

==============================================================================
--- cfe/trunk/test/Sema/asm.c (added)
+++ cfe/trunk/test/Sema/asm.c Fri Nov 23 13:43:50 2007
@@ -0,0 +1,14 @@
+// RUN: clang %s -verify -fsyntax-only
+
+void
+f()
+{
+  int i;
+
+  asm ("foo\n" : : "a" (i + 2));
+  asm ("foo\n" : : "a" (f())); // expected-error {{invalid type 'void' in asm input}}
+  
+  asm ("foo\n" : "=a" (f())); // expected-error {{invalid lvalue in asm output}}
+  asm ("foo\n" : "=a" (i + 2)); // expected-error {{invalid lvalue in asm output}}
+  
+}





More information about the cfe-commits mailing list