]> granicus.if.org Git - clang/commitdiff
Implement rdar://6319320: give a good diagnostic for cases where people
authorChris Lattner <sabre@nondot.org>
Mon, 17 Nov 2008 19:51:54 +0000 (19:51 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 17 Nov 2008 19:51:54 +0000 (19:51 +0000)
are trying to use the old GCC "casts as lvalue" extension.  We don't and
will hopefully never support this.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59460 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 6e67f2853f939c6cc17f1fd00be3306d14bf98f3..030a399e1d4aa177aa48bad4662a1eb576755f90 100644 (file)
@@ -99,6 +99,7 @@ public:
     MLV_IncompleteVoidType,
     MLV_DuplicateVectorComponents,
     MLV_InvalidExpression,
+    MLV_LValueCast,           // Specialized form of MLV_InvalidExpression.
     MLV_IncompleteType,
     MLV_ConstQualified,
     MLV_ArrayType,
index 692d01d8fb5906a2a8ebcd3b52de06ca3821fe2d..d1f72316c19799b879558fd8103ad05a6d07bf7f 100644 (file)
@@ -1225,6 +1225,8 @@ DIAG(err_typecheck_expression_not_modifiable_lvalue, ERROR,
      "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)")
index 29d9a11de785287cde886e09008d70affb1432e9..d2c9ea9573a3ed2a39ab613b0fbd0bbfa2289a0b 100644 (file)
@@ -495,7 +495,15 @@ Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
   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());
index a5066a8cd7f430f0b3af63f9cc8952927a17ad0b..0506b4076eff98873a8bb8c2fd1805ef67c6833e 100644 (file)
@@ -2345,6 +2345,10 @@ inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
     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());
index 62b774e26d41420c7702545aff2b209cb1fc3ed7..c4d0c2dbf8476c2f22f32531abe71d11d532089d 100644 (file)
@@ -30,7 +30,7 @@ int test1() {
 
 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;
 
index c98a7ed0c83748e6ec3f8ba7591ca784c9f08b4b..8c8adc65b5fb7158f3c09565f9bb8c19fed1e076 100644 (file)
@@ -24,3 +24,8 @@ void test4() {
       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}}
+}
+