[cfe-commits] r44049 - in /cfe/trunk: AST/Expr.cpp include/clang/AST/Expr.h test/Sema/address-constant.c

Chris Lattner sabre at nondot.org
Tue Nov 13 10:05:45 PST 2007


Author: lattner
Date: Tue Nov 13 12:05:45 2007
New Revision: 44049

URL: http://llvm.org/viewvc/llvm-project?rev=44049&view=rev
Log:
improve handling of address of global when checking for
constants and initializers.  Patch by Sanghyeon Seo, thanks!

Added:
    cfe/trunk/test/Sema/address-constant.c
Modified:
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/include/clang/AST/Expr.h

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

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Tue Nov 13 12:05:45 2007
@@ -359,6 +359,22 @@
   return MLV_Valid;    
 }
 
+bool Expr::hasStaticStorage() const {
+  switch (getStmtClass()) {
+  default:
+    return false;
+  case DeclRefExprClass: {
+    const Decl *D = cast<DeclRefExpr>(this)->getDecl();
+    if (const VarDecl *VD = dyn_cast<VarDecl>(D))
+      return VD->hasStaticStorage();
+    return false;
+  }
+  case MemberExprClass:
+    const MemberExpr *M = cast<MemberExpr>(this);
+    return !M->isArrow() && M->getBase()->hasStaticStorage();
+  }
+}
+
 bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
   switch (getStmtClass()) {
   default:
@@ -391,11 +407,17 @@
     if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
       return true;
     if (Loc) *Loc = getLocStart();
+    if (isa<VarDecl>(D))
+      return TR->isArrayType();
     return false;
   }
   case UnaryOperatorClass: {
     const UnaryOperator *Exp = cast<UnaryOperator>(this);
     
+    // C99 6.6p9
+    if (Exp->getOpcode() == UnaryOperator::AddrOf)
+      return Exp->getSubExpr()->hasStaticStorage();
+
     // Get the operand value.  If this is sizeof/alignof, do not evalute the
     // operand.  This affects C99 6.6p3.
     if (!Exp->isSizeOfAlignOfOp() &&

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Nov 13 12:05:45 2007
@@ -105,6 +105,10 @@
   /// isConstantExpr - Return true if this expression is a valid constant expr.
   bool isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const;
   
+  /// hasStaticStorage - Return true if this expression has static storage
+  /// duration.
+  bool hasStaticStorage() const;
+
   static bool classof(const Stmt *T) { 
     return T->getStmtClass() >= firstExprConstant &&
            T->getStmtClass() <= lastExprConstant; 

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

==============================================================================
--- cfe/trunk/test/Sema/address-constant.c (added)
+++ cfe/trunk/test/Sema/address-constant.c Tue Nov 13 12:05:45 2007
@@ -0,0 +1,7 @@
+// RUN: clang -fsyntax-only -verify %s
+
+int i;
+int a[] = {0};
+struct { int i; } s;
+
+int *array[] = {&i, a, &s.i};





More information about the cfe-commits mailing list