Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -18878,14 +18878,24 @@ const llvm::APSInt &InitVal = ECD->getInitVal(); // Keep track of the size of positive and negative values. - if (InitVal.isUnsigned() || InitVal.isNonNegative()) - NumPositiveBits = std::max(NumPositiveBits, - (unsigned)InitVal.getActiveBits()); - else + if (InitVal.isUnsigned() || InitVal.isNonNegative()) { + // If the enumerator is zero that should still be counted as a positive + // bit since we need a bit to store the value zero + const unsigned ActiveBits = + InitVal.getActiveBits() ? InitVal.getActiveBits() : 1; + NumPositiveBits = std::max(NumPositiveBits, ActiveBits); + } else NumNegativeBits = std::max(NumNegativeBits, (unsigned)InitVal.getMinSignedBits()); } + // If we have have a empty set of enumerators we still need one bit. + // From [dcl.enum]p8 + // If the enumerator-list is empty, the values of the enumeration are as if + // the enumeration had a single enumerator with value 0 + if (!NumPositiveBits && !NumNegativeBits) + NumPositiveBits = 1; + // Figure out the type that should be used for this enum. QualType BestType; unsigned BestWidth; Index: clang/test/CodeGenCXX/pr12251.cpp =================================================================== --- clang/test/CodeGenCXX/pr12251.cpp +++ clang/test/CodeGenCXX/pr12251.cpp @@ -18,14 +18,14 @@ return *x; } // CHECK-LABEL: define{{.*}} i32 @_Z2g1P2e1 -// CHECK: ret i32 0 +// CHECK: ret i32 %0 enum e2 { e2_a = 0 }; e2 g2(e2 *x) { return *x; } // CHECK-LABEL: define{{.*}} i32 @_Z2g2P2e2 -// CHECK: ret i32 0 +// CHECK: ret i32 %0 enum e3 { e3_a = 16 }; e3 g3(e3 *x) {