[cfe-commits] r59460 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Basic/DiagnosticKinds.def lib/AST/Expr.cpp lib/Sema/SemaExpr.cpp test/Sema/block-misc.c test/Sema/exprs.c

Chris Lattner sabre at nondot.org
Mon Nov 17 11:51:54 PST 2008


Author: lattner
Date: Mon Nov 17 13:51:54 2008
New Revision: 59460

URL: http://llvm.org/viewvc/llvm-project?rev=59460&view=rev
Log:
Implement rdar://6319320: give a good diagnostic for cases where people
are trying to use the old GCC "casts as lvalue" extension.  We don't and
will hopefully never support this.



Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/block-misc.c
    cfe/trunk/test/Sema/exprs.c

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=59460&r1=59459&r2=59460&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Nov 17 13:51:54 2008
@@ -99,6 +99,7 @@
     MLV_IncompleteVoidType,
     MLV_DuplicateVectorComponents,
     MLV_InvalidExpression,
+    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
     MLV_IncompleteType,
     MLV_ConstQualified,
     MLV_ArrayType,

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Nov 17 13:51:54 2008
@@ -1225,6 +1225,8 @@
      "expression is not assignable")
 DIAG(err_typecheck_incomplete_type_not_modifiable_lvalue, ERROR,
      "incomplete type '%0' is not assignable")
+DIAG(err_typecheck_lvalue_casts_not_supported, ERROR,
+     "assignment to cast is illegal, lvalue casts are not supported")
 
 DIAG(err_typecheck_duplicate_vector_components_not_mlvalue, ERROR,
      "vector is not assignable (contains duplicate components)")

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=59460&r1=59459&r2=59460&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Nov 17 13:51:54 2008
@@ -495,7 +495,15 @@
   case LV_NotObjectType: return MLV_NotObjectType;
   case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
   case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
-  case LV_InvalidExpression: return MLV_InvalidExpression;
+  case LV_InvalidExpression:
+    // If the top level is a C-style cast, and the subexpression is a valid
+    // lvalue, then this is probably a use of the old-school "cast as lvalue"
+    // GCC extension.  We don't support it, but we want to produce good
+    // diagnostics when it happens so that the user knows why.
+    if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this))
+      if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid)
+        return MLV_LValueCast;
+    return MLV_InvalidExpression;
   }
   
   QualType CT = Ctx.getCanonicalType(getType());

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=59460&r1=59459&r2=59460&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Nov 17 13:51:54 2008
@@ -2345,6 +2345,10 @@
     Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
          lhsType.getAsString(), lex->getSourceRange());
     return QualType();
+  case Expr::MLV_LValueCast:
+    Diag(loc, diag::err_typecheck_lvalue_casts_not_supported, 
+         lex->getSourceRange());
+    return QualType();
   case Expr::MLV_InvalidExpression:
     Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
          lex->getSourceRange());

Modified: cfe/trunk/test/Sema/block-misc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-misc.c?rev=59460&r1=59459&r2=59460&view=diff

==============================================================================
--- cfe/trunk/test/Sema/block-misc.c (original)
+++ cfe/trunk/test/Sema/block-misc.c Mon Nov 17 13:51:54 2008
@@ -30,7 +30,7 @@
 
 int test2(double (^S)()) {
    double (^I)(int)  = (void*) S;
-   (void*)I = (void *)S; 	// expected-error {{expression is not assignable}}
+   (void*)I = (void *)S; 	// expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
 
    void *pv = I;
 

Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=59460&r1=59459&r2=59460&view=diff

==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Mon Nov 17 13:51:54 2008
@@ -24,3 +24,8 @@
       var = -5;
 }
 
+// rdar://6319320
+void test5(int *X, float *P) {
+  (float*)X = P;   // expected-error {{assignment to cast is illegal, lvalue casts are not supported}}
+}
+





More information about the cfe-commits mailing list