]> granicus.if.org Git - clang/commitdiff
Implement rdar://6138816 - [sema] named bitfields cannot have 0 width
authorChris Lattner <sabre@nondot.org>
Fri, 12 Dec 2008 04:56:04 +0000 (04:56 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 12 Dec 2008 04:56:04 +0000 (04:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60920 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaDecl.cpp
test/Sema/bitfield.c

index bba58791d1a0555703b46ff834d9ce7bcd4b279b..807d32853ddfac8527391975628f4505ee5cbba3 100644 (file)
@@ -1056,6 +1056,8 @@ DIAG(err_implicit_empty_initializer, ERROR,
      "initializer for aggregate with no elements requires explicit braces")
 DIAG(err_bitfield_has_negative_width, ERROR,
      "bit-field %0 has negative width")
+DIAG(err_bitfield_has_zero_width, ERROR,
+     "bit-field %0 has zero width")
 DIAG(err_bitfield_width_exceeds_type_size, ERROR,
      "size of bit-field %0 exceeds size of its type (%1 bits)")
 
index 43964f7ccada9636177fc2b12cbd69fa8df9e98b..29d636fabdca48ffbd5f2c9aa0c9818a1282b7d6 100644 (file)
@@ -2748,26 +2748,25 @@ static QualType TryToFixInvalidVariablyModifiedType(QualType T,
 }
 
 bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName, 
-                          QualType FieldTy, const Expr *BitWidth)
-{
+                          QualType FieldTy, const Expr *BitWidth) {
   // FIXME: 6.7.2.1p4 - verify the field type.
   
   llvm::APSInt Value;
   if (VerifyIntegerConstantExpression(BitWidth, &Value))
     return true;
 
-  if (Value.isNegative()) {
-    Diag(FieldLoc, diag::err_bitfield_has_negative_width) << FieldName;
-    return true;
-  }
+  // Zero-width bitfield is ok for anonymous field.
+  if (Value == 0 && FieldName)
+    return Diag(FieldLoc, diag::err_bitfield_has_zero_width) << FieldName;
+  
+  if (Value.isNegative())
+    return Diag(FieldLoc, diag::err_bitfield_has_negative_width) << FieldName;
 
   uint64_t TypeSize = Context.getTypeSize(FieldTy);
   // FIXME: We won't need the 0 size once we check that the field type is valid.
-  if (TypeSize && Value.getZExtValue() > TypeSize) {
-    Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size) << 
-         FieldName << (unsigned)TypeSize;
-    return true;
-  }
+  if (TypeSize && Value.getZExtValue() > TypeSize)
+    return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size)
+       << FieldName << (unsigned)TypeSize;
 
   return false;
 }
index 8d060c5238f51d2bff1d5fedf9e45f0828955490..f3b285b12ed69222be9c753f931777bf84caece3 100644 (file)
@@ -8,4 +8,7 @@ struct a {
 
   int c : (1 + 0.25); // expected-error{{expression is not an integer constant expression}}
   int d : (int)(1 + 0.25); 
+
+  // rdar://6138816
+  int e : 0;  // expected-error {{bit-field 'e' has zero width}}
 };