[cfe-commits] r46634 - in /cfe/trunk: Sema/SemaExpr.cpp test/Sema/expr-address-of.c

Anders Carlsson andersca at mac.com
Thu Jan 31 23:15:58 PST 2008


Author: andersca
Date: Fri Feb  1 01:15:58 2008
New Revision: 46634

URL: http://llvm.org/viewvc/llvm-project?rev=46634&view=rev
Log:
It is allowed to get the address of an array subscript, even if the array has the register qualifier, if the array is really a pointer.

Modified:
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/expr-address-of.c

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Fri Feb  1 01:15:58 2008
@@ -1616,11 +1616,11 @@
   return resType;
 }
 
-/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
+/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
 /// This routine allows us to typecheck complex/recursive expressions
 /// where the declaration is needed for type checking. Here are some
 /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
-static Decl *getPrimaryDeclaration(Expr *e) {
+static ValueDecl *getPrimaryDecl(Expr *e) {
   switch (e->getStmtClass()) {
   case Stmt::DeclRefExprClass:
     return cast<DeclRefExpr>(e)->getDecl();
@@ -1629,17 +1629,23 @@
     // &X->f is always ok, even if X is declared register.
     if (cast<MemberExpr>(e)->isArrow())
       return 0;
-    return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
-  case Stmt::ArraySubscriptExprClass:
-    // &X[4] and &4[X] is invalid if X is invalid.
-    return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
+    return getPrimaryDecl(cast<MemberExpr>(e)->getBase());
+  case Stmt::ArraySubscriptExprClass: {
+    // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
+  
+    ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(e)->getBase());
+    if (VD->getType()->isPointerType())
+      return 0;
+    else
+      return VD;
+  }
   case Stmt::UnaryOperatorClass:
-    return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
+    return getPrimaryDecl(cast<UnaryOperator>(e)->getSubExpr());
   case Stmt::ParenExprClass:
-    return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
+    return getPrimaryDecl(cast<ParenExpr>(e)->getSubExpr());
   case Stmt::ImplicitCastExprClass:
     // &X[4] when X is an array, has an implicit cast from array to pointer.
-    return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
+    return getPrimaryDecl(cast<ImplicitCastExpr>(e)->getSubExpr());
   default:
     return 0;
   }
@@ -1662,7 +1668,7 @@
     // Technically, there should be a check for array subscript
     // expressions here, but the result of one is always an lvalue anyway.
   }
-  Decl *dcl = getPrimaryDeclaration(op);
+  ValueDecl *dcl = getPrimaryDecl(op);
   Expr::isLvalueResult lval = op->isLvalue();
   
   if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1

Modified: cfe/trunk/test/Sema/expr-address-of.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/expr-address-of.c?rev=46634&r1=46633&r2=46634&view=diff

==============================================================================
--- cfe/trunk/test/Sema/expr-address-of.c (original)
+++ cfe/trunk/test/Sema/expr-address-of.c Fri Feb  1 01:15:58 2008
@@ -10,6 +10,11 @@
 void foo() {
   register int x[10];
   &x[10];              // expected-error {{address of register variable requested}}
+    
+  register int *y;
+  
+  int *x2 = &y; // expected-error {{address of register variable requested}}
+  int *x3 = &y[10];
 }
 
 





More information about the cfe-commits mailing list