[cfe-commits] r91070 - in /cfe/trunk: lib/AST/ASTContext.cpp lib/AST/ExprConstant.cpp lib/Sema/SemaDecl.cpp test/CodeGenCXX/enum.cpp

Eli Friedman eli.friedman at gmail.com
Thu Dec 10 14:29:29 PST 2009


Author: efriedma
Date: Thu Dec 10 16:29:29 2009
New Revision: 91070

URL: http://llvm.org/viewvc/llvm-project?rev=91070&view=rev
Log:
Clean up enum constants so that they're finally sane.  Fixes PR3173 and a
recently introduced crash.


Added:
    cfe/trunk/test/CodeGenCXX/enum.cpp
Modified:
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Dec 10 16:29:29 2009
@@ -4522,7 +4522,7 @@
     return FWIT->getWidth();
   }
   if (EnumType *ET = dyn_cast<EnumType>(T))
-    T = ET->getDecl()->getPromotionType();
+    T = ET->getDecl()->getIntegerType();
   // For builtin types, just use the standard type sizing method
   return (unsigned)getTypeSize(T);
 }

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

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Thu Dec 10 16:29:29 2009
@@ -848,16 +848,8 @@
 
 bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
   // Enums are integer constant exprs.
-  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
-    // FIXME: This is an ugly hack around the fact that enums don't set their
-    // signedness consistently; see PR3173.
-    APSInt SI = ECD->getInitVal();
-    SI.setIsUnsigned(!E->getType()->isSignedIntegerType());
-    // FIXME: This is an ugly hack around the fact that enums don't
-    // set their width (!?!) consistently; see PR3173.
-    SI.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
-    return Success(SI, E);
-  }
+  if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
+    return Success(ECD->getInitVal(), E);
 
   // In C++, const, non-volatile integers initialized with ICEs are ICEs.
   // In C, they can also be folded, although they are not ICEs.

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=91070&r1=91069&r2=91070&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Dec 10 16:29:29 2009
@@ -5694,6 +5694,7 @@
       // First value, set to zero.
       EltTy = Context.IntTy;
       EnumVal.zextOrTrunc(static_cast<uint32_t>(Context.getTypeSize(EltTy)));
+      EnumVal.setIsSigned(true);
     }
   }
 
@@ -5908,19 +5909,8 @@
     // enumerator value fits in an int, type it as an int, otherwise type it the
     // same as the enumerator decl itself.  This means that in "enum { X = 1U }"
     // that X has type 'int', not 'unsigned'.
-    if (ECD->getType() == Context.IntTy) {
-      // Make sure the init value is signed.
-      llvm::APSInt IV = ECD->getInitVal();
-      IV.setIsSigned(true);
-      ECD->setInitVal(IV);
-
-      if (getLangOptions().CPlusPlus)
-        // C++ [dcl.enum]p4: Following the closing brace of an
-        // enum-specifier, each enumerator has the type of its
-        // enumeration.
-        ECD->setType(EnumType);
-      continue;  // Already int type.
-    }
+    if (!getLangOptions().CPlusPlus && ECD->getType() == Context.IntTy)
+      continue;
 
     // Determine whether the value fits into an int.
     llvm::APSInt InitVal = ECD->getInitVal();
@@ -5935,7 +5925,7 @@
     QualType NewTy;
     unsigned NewWidth;
     bool NewSign;
-    if (FitsInInt) {
+    if (FitsInInt && !getLangOptions().CPlusPlus) {
       NewTy = Context.IntTy;
       NewWidth = IntWidth;
       NewSign = true;

Added: cfe/trunk/test/CodeGenCXX/enum.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/enum.cpp?rev=91070&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/enum.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/enum.cpp Thu Dec 10 16:29:29 2009
@@ -0,0 +1,4 @@
+// RUN: clang-cc -emit-llvm-only -verify %s
+
+enum A { a } __attribute((packed));
+int func(A x) { return x==a; }





More information about the cfe-commits mailing list