[cfe-commits] r62939 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp lib/AST/ExprConstant.cpp

Chris Lattner sabre at nondot.org
Sat Jan 24 13:53:27 PST 2009


Author: lattner
Date: Sat Jan 24 15:53:27 2009
New Revision: 62939

URL: http://llvm.org/viewvc/llvm-project?rev=62939&view=rev
Log:
add initial support for the gcc "alignof(decl) is the alignment of the decl
not the type" semantics.  This can definitely be improved, but is better than
what we had.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Sat Jan 24 15:53:27 2009
@@ -403,6 +403,11 @@
     return getTypeInfo(T).second;
   }
   
+  /// getDeclAlign - Return a conservative estimate of the alignment of the
+  /// specified decl.  Note that bitfields do not have a valid alignment, so
+  /// this method will assert on them.
+  unsigned getDeclAlign(const Decl *D);
+  
   /// getASTRecordLayout - Get or compute information about the layout of the
   /// specified record (struct/union/class), which indicates its size and field
   /// position information.

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sat Jan 24 15:53:27 2009
@@ -260,6 +260,26 @@
   }
 }
 
+/// getDeclAlign - Return a conservative estimate of the alignment of the
+/// specified decl.  Note that bitfields do not have a valid alignment, so
+/// this method will assert on them.
+unsigned ASTContext::getDeclAlign(const Decl *D) {
+  // FIXME: If attribute(align) is specified on the decl, round up to it.
+  
+  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);
+  }
+  
+  return 1;
+}
 
 /// getTypeSize - Return the size of the specified type, in bits.  This method
 /// does not work on incomplete types.

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

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sat Jan 24 15:53:27 2009
@@ -508,8 +508,8 @@
 
 private:
   bool HandleCast(CastExpr* E);
-  uint64_t GetAlignOfExpr(const Expr *E);
-  uint64_t GetAlignOfType(QualType T);
+  unsigned GetAlignOfExpr(const Expr *E);
+  unsigned GetAlignOfType(QualType T);
 };
 } // end anonymous namespace
 
@@ -844,7 +844,7 @@
   return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
 }
 
-uint64_t IntExprEvaluator::GetAlignOfType(QualType T) {
+unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
   const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
   
   // __alignof__(void) = 1 as a gcc extension.
@@ -873,8 +873,17 @@
   return Info.Ctx.getTypeAlign(Ty) / CharSize;
 }
 
-uint64_t IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
-  
+unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
+  E = E->IgnoreParens();
+
+  // alignof decl is always accepted, even if it doesn't make sense: we default
+  // to 1 in those cases. 
+  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
+    return Info.Ctx.getDeclAlign(DRE->getDecl());
+    
+  if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
+    return Info.Ctx.getDeclAlign(ME->getMemberDecl());
+
   return GetAlignOfType(E->getType());
 }
 





More information about the cfe-commits mailing list