[cfe-commits] r47429 - in /cfe/trunk: AST/Expr.cpp CodeGen/CGExprScalar.cpp test/CodeGen/exprs.c

Chris Lattner sabre at nondot.org
Wed Feb 20 21:45:29 PST 2008


Author: lattner
Date: Wed Feb 20 23:45:29 2008
New Revision: 47429

URL: http://llvm.org/viewvc/llvm-project?rev=47429&view=rev
Log:
implement codegen support for sizeof(void), fixing PR2080.

Modified:
    cfe/trunk/AST/Expr.cpp
    cfe/trunk/CodeGen/CGExprScalar.cpp
    cfe/trunk/test/CodeGen/exprs.c

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

==============================================================================
--- cfe/trunk/AST/Expr.cpp (original)
+++ cfe/trunk/AST/Expr.cpp Wed Feb 20 23:45:29 2008
@@ -580,7 +580,8 @@
   case SizeOfAlignOfTypeExprClass: {
     const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
     // alignof always evaluates to a constant.
-    if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
+    if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
+        !Exp->getArgumentType()->isConstantSizeType()) {
       if (Loc) *Loc = Exp->getOperatorLoc();
       return false;
     }
@@ -721,17 +722,23 @@
       return true;  // FIXME: this is wrong.
     case UnaryOperator::SizeOf:
     case UnaryOperator::AlignOf:
+      // Return the result in the right width.
+      Result.zextOrTrunc(
+                         static_cast<uint32_t>(Ctx.getTypeSize(getType(),
+                                                       Exp->getOperatorLoc())));
+        
+      // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
+      if (Exp->getSubExpr()->getType()->isVoidType()) {
+        Result = 1;
+        break;
+      }
+        
       // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
       if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
         if (Loc) *Loc = Exp->getOperatorLoc();
         return false;
       }
       
-      // Return the result in the right width.
-      Result.zextOrTrunc(
-        static_cast<uint32_t>(Ctx.getTypeSize(getType(),
-                                              Exp->getOperatorLoc())));
-
       // Get information about the size or align.
       if (Exp->getSubExpr()->getType()->isFunctionType()) {
         // GCC extension: sizeof(function) = 1.
@@ -771,16 +778,23 @@
   }
   case SizeOfAlignOfTypeExprClass: {
     const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
-    // alignof always evaluates to a constant.
+    
+    // Return the result in the right width.
+    Result.zextOrTrunc(
+      static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
+    
+    // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
+    if (Exp->getArgumentType()->isVoidType()) {
+      Result = 1;
+      break;
+    }
+    
+    // alignof always evaluates to a constant, sizeof does if arg is not VLA.
     if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
       if (Loc) *Loc = Exp->getOperatorLoc();
       return false;
     }
 
-    // Return the result in the right width.
-    Result.zextOrTrunc(
-      static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
-    
     // Get information about the size or align.
     if (Exp->getArgumentType()->isFunctionType()) {
       // GCC extension: sizeof(function) = 1.

Modified: cfe/trunk/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprScalar.cpp?rev=47429&r1=47428&r2=47429&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/CodeGen/CGExprScalar.cpp Wed Feb 20 23:45:29 2008
@@ -607,6 +607,15 @@
 /// an integer (RetType).
 Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, 
                                           QualType RetType,bool isSizeOf){
+  assert(RetType->isIntegerType() && "Result type must be an integer!");
+  uint32_t ResultWidth = 
+    static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType, 
+                                                       SourceLocation()));
+
+  // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
+  if (TypeToSize->isVoidType())
+    return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1));
+  
   /// FIXME: This doesn't handle VLAs yet!
   std::pair<uint64_t, unsigned> Info =
     CGF.getContext().getTypeInfo(TypeToSize, SourceLocation());
@@ -614,10 +623,6 @@
   uint64_t Val = isSizeOf ? Info.first : Info.second;
   Val /= 8;  // Return size in bytes, not bits.
   
-  assert(RetType->isIntegerType() && "Result type must be an integer!");
-  
-  uint32_t ResultWidth = static_cast<uint32_t>(
-    CGF.getContext().getTypeSize(RetType, SourceLocation()));
   return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
 }
 

Modified: cfe/trunk/test/CodeGen/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/exprs.c?rev=47429&r1=47428&r2=47429&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/exprs.c (original)
+++ cfe/trunk/test/CodeGen/exprs.c Wed Feb 20 23:45:29 2008
@@ -23,3 +23,14 @@
   bp -= (short)1;
 }
 
+// PR2080 - sizeof void
+int t1 = sizeof(void);
+int t2 = __alignof__(void);
+void test4() {
+  t1 = sizeof(void);
+  t2 = __alignof__(void);
+  
+  t1 = sizeof(test4());
+  t2 = __alignof__(test4());
+}
+





More information about the cfe-commits mailing list