[cfe-commits] r60920 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaDecl.cpp test/Sema/bitfield.c

Chris Lattner sabre at nondot.org
Thu Dec 11 20:56:06 PST 2008


Author: lattner
Date: Thu Dec 11 22:56:04 2008
New Revision: 60920

URL: http://llvm.org/viewvc/llvm-project?rev=60920&view=rev
Log:
Implement rdar://6138816 - [sema] named bitfields cannot have 0 width

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/bitfield.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=60920&r1=60919&r2=60920&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Thu Dec 11 22:56:04 2008
@@ -1056,6 +1056,8 @@
      "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)")
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Dec 11 22:56:04 2008
@@ -2748,26 +2748,25 @@
 }
 
 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;
 }

Modified: cfe/trunk/test/Sema/bitfield.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/bitfield.c?rev=60920&r1=60919&r2=60920&view=diff

==============================================================================
--- cfe/trunk/test/Sema/bitfield.c (original)
+++ cfe/trunk/test/Sema/bitfield.c Thu Dec 11 22:56:04 2008
@@ -8,4 +8,7 @@
 
   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}}
 };





More information about the cfe-commits mailing list