[cfe-commits] r65258 - in /cfe/trunk: lib/AST/ASTContext.cpp lib/AST/Expr.cpp test/Sema/attr-aligned.c

Eli Friedman eli.friedman at gmail.com
Sat Feb 21 18:56:27 PST 2009


Author: efriedma
Date: Sat Feb 21 20:56:25 2009
New Revision: 65258

URL: http://llvm.org/viewvc/llvm-project?rev=65258&view=rev
Log:
Improvements to ASTContext::getDeclAlignInBytes; fixes the testcase in 
PR3254 and part of PR3433.

The isICE changes are necessary to keep the computed results 
consistent with Evaluate.


Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/test/Sema/attr-aligned.c

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sat Feb 21 20:56:25 2009
@@ -264,21 +264,23 @@
 /// specified decl.  Note that bitfields do not have a valid alignment, so
 /// this method will assert on them.
 unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
-  // FIXME: If attribute(align) is specified on the decl, round up to it.
-  
+  unsigned Align = Target.getCharWidth();
+
+  if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
+    Align = std::max(Align, AA->getAlignment());
+
   if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
     QualType T = VD->getType();
     // Incomplete or function types default to 1.
-    if (T->isIncompleteType() || T->isFunctionType())
-      return 1;
-    
-    while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
-      T = cast<ArrayType>(T)->getElementType();
-    
-    return getTypeAlign(T) / Target.getCharWidth();
+    if (!T->isIncompleteType() && !T->isFunctionType()) {
+      while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
+        T = cast<ArrayType>(T)->getElementType();
+
+      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
+    }
   }
-  
-  return 1;
+
+  return Align / Target.getCharWidth();
 }
 
 /// getTypeSize - Return the size of the specified type, in bits.  This method

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

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Sat Feb 21 20:56:25 2009
@@ -994,31 +994,18 @@
   }
   case SizeOfAlignOfExprClass: {
     const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this);
-    
     QualType ArgTy = Exp->getTypeOfArgument();
-    // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
-    if (ArgTy->isVoidType()) {
-      Result = Ctx.MakeIntValue(1, getType());
-      break;
-    }
-    
-    // alignof always evaluates to a constant, sizeof does if arg is not VLA.
-    if (Exp->isSizeOf() && !ArgTy->isConstantSizeType()) {
+
+    // alignof is always an ICE; sizeof is an ICE if and only if
+    // the operand isn't a VLA
+    if (Exp->isSizeOf() && ArgTy->isVariableArrayType()) {
       if (Loc) *Loc = Exp->getOperatorLoc();
       return false;
     }
 
-    // Get information about the size or align.
-    if (ArgTy->isFunctionType()) {
-      // GCC extension: sizeof(function) = 1.
-      Result = Ctx.MakeIntValue(Exp->isSizeOf() ? 1 : 4, getType());
-    } else { 
-      unsigned CharSize = Ctx.Target.getCharWidth();
-      if (Exp->isSizeOf())
-        Result = Ctx.MakeIntValue(Ctx.getTypeSize(ArgTy)/CharSize, getType());
-      else
-        Result = Ctx.MakeIntValue(Ctx.getTypeAlign(ArgTy)/CharSize, getType());
-    }
+    // Use the Evaluate logic to calculate the value, since the
+    // calculation is non-trivial.
+    Result = EvaluateAsInt(Ctx);
     break;
   }
   case BinaryOperatorClass: {

Modified: cfe/trunk/test/Sema/attr-aligned.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-aligned.c?rev=65258&r1=65257&r2=65258&view=diff

==============================================================================
--- cfe/trunk/test/Sema/attr-aligned.c (original)
+++ cfe/trunk/test/Sema/attr-aligned.c Sat Feb 21 20:56:25 2009
@@ -1,3 +1,12 @@
-// RUN: clang -fsyntax-only -verify %s
+// RUN: clang -triple i386-apple-darwin9 -fsyntax-only -verify %s
 
 int x __attribute__((aligned(3))); // expected-error {{requested alignment is not a power of 2}}
+
+// PR3254
+short g0[3] __attribute__((aligned));
+short g0_chk[__alignof__(g0) == 16 ? 1 : -1]; 
+
+// PR3433
+double g1;
+short chk1[__alignof__(g1) == 8 ? 1 : -1]; 
+





More information about the cfe-commits mailing list