[cfe-commits] r70096 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/c89.c

Eli Friedman eli.friedman at gmail.com
Sat Apr 25 16:46:55 PDT 2009


Author: efriedma
Date: Sat Apr 25 18:46:54 2009
New Revision: 70096

URL: http://llvm.org/viewvc/llvm-project?rev=70096&view=rev
Log:
Fix for PR4074: allow subscripting non-lvalue arrays in C90 mode.

I wasn't originally going to use this approach, but cases like 
test/Sema/expr-comma.c make things difficult.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/c89.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=70096&r1=70095&r2=70096&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat Apr 25 18:46:54 2009
@@ -945,6 +945,8 @@
   "non-fragile ABI">;
 
 
+def ext_subscript_non_lvalue : Extension<
+  "ISO C90 does not allow subscripting non-lvalue array">;
 def err_typecheck_subscript_value : Error<
   "subscripted value is not an array, pointer, or vector">;
 def err_typecheck_subscript_not_integer : Error<

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Apr 25 18:46:54 2009
@@ -1621,13 +1621,11 @@
   } else if (const PointerType *PTy = LHSTy->getAsPointerType()) {
     BaseExpr = LHSExp;
     IndexExpr = RHSExp;
-    // FIXME: need to deal with const...
     ResultType = PTy->getPointeeType();
   } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
      // Handle the uncommon case of "123[Ptr]".
     BaseExpr = RHSExp;
     IndexExpr = LHSExp;
-    // FIXME: need to deal with const...
     ResultType = PTy->getPointeeType();
   } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
     BaseExpr = LHSExp;    // vectors: V[123]
@@ -1635,6 +1633,30 @@
 
     // FIXME: need to deal with const...
     ResultType = VTy->getElementType();
+  } else if (LHSTy->isArrayType()) {
+    // If we see an array that wasn't promoted by
+    // DefaultFunctionArrayConversion, it must be an array that
+    // wasn't promoted because of the C90 rule that doesn't
+    // allow promoting non-lvalue arrays.  Warn, then
+    // force the promotion here.
+    Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) <<
+        LHSExp->getSourceRange();
+    ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy));
+    LHSTy = LHSExp->getType();
+
+    BaseExpr = LHSExp;
+    IndexExpr = RHSExp;
+    ResultType = LHSTy->getAsPointerType()->getPointeeType();
+  } else if (RHSTy->isArrayType()) {
+    // Same as previous, except for 123[f().a] case
+    Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) <<
+        RHSExp->getSourceRange();
+    ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy));
+    RHSTy = RHSExp->getType();
+
+    BaseExpr = RHSExp;
+    IndexExpr = LHSExp;
+    ResultType = RHSTy->getAsPointerType()->getPointeeType();
   } else {
     return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value)
        << LHSExp->getSourceRange() << RHSExp->getSourceRange());

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

==============================================================================
--- cfe/trunk/test/Sema/c89.c (original)
+++ cfe/trunk/test/Sema/c89.c Sat Apr 25 18:46:54 2009
@@ -67,3 +67,14 @@
 void test12 (int x[const 4]) { /* expected-warning {{use of C99-specific array features}} */
   int Y[x[1]]; /* expected-warning {{variable length arrays are a C99 feature, accepted as an extension}} */
 }
+
+/* PR4074 */
+struct test13 {
+  int X[23];
+} test13a();
+
+void test13b() {
+  int a = test13a().X[1]; /* expected-warning {{ISO C90 does not allow subscripting non-lvalue array}} */
+  int b = 1[test13a().X]; /* expected-warning {{ISO C90 does not allow subscripting non-lvalue array}} */
+}
+





More information about the cfe-commits mailing list