Skip to content

Commit 022bdc7

Browse files
committedSep 14, 2015
C11 _Bool bitfield diagnostic
Summary: Implement DR262 (for C). This patch will mainly affect bitfields of type _Bool Reviewers: fraggamuffin, rsmith Subscribers: hubert.reinterpretcast, cfe-commits Differential Revision: http://reviews.llvm.org/D10018 llvm-svn: 247618
1 parent bace032 commit 022bdc7

12 files changed

+43
-39
lines changed
 

‎clang/include/clang/Basic/DiagnosticGroups.td

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def AutoImport : DiagGroup<"auto-import">;
3232
def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">;
3333
def GNUCompoundLiteralInitializer : DiagGroup<"gnu-compound-literal-initializer">;
3434
def BitFieldConstantConversion : DiagGroup<"bitfield-constant-conversion">;
35+
def BitFieldWidth : DiagGroup<"bitfield-width">;
3536
def ConstantConversion :
3637
DiagGroup<"constant-conversion", [ BitFieldConstantConversion ] >;
3738
def LiteralConversion : DiagGroup<"literal-conversion">;

‎clang/include/clang/Basic/DiagnosticSemaKinds.td

+11-10
Original file line numberDiff line numberDiff line change
@@ -4314,20 +4314,21 @@ def err_bitfield_has_negative_width : Error<
43144314
def err_anon_bitfield_has_negative_width : Error<
43154315
"anonymous bit-field has negative width (%0)">;
43164316
def err_bitfield_has_zero_width : Error<"named bit-field %0 has zero width">;
4317-
def err_bitfield_width_exceeds_type_size : Error<
4318-
"size of bit-field %0 (%1 bits) exceeds size of its type (%2 bits)">;
4319-
def err_anon_bitfield_width_exceeds_type_size : Error<
4320-
"size of anonymous bit-field (%0 bits) exceeds size of its type (%1 bits)">;
4317+
def err_bitfield_width_exceeds_type_width : Error<
4318+
"width of bit-field %0 (%1 bits) exceeds width of its type (%2 bit%s2)">;
4319+
def err_anon_bitfield_width_exceeds_type_width : Error<
4320+
"width of anonymous bit-field (%0 bits) exceeds width of its type "
4321+
"(%1 bit%s1)">;
43214322
def err_incorrect_number_of_vector_initializers : Error<
43224323
"number of elements must be either one or match the size of the vector">;
43234324

43244325
// Used by C++ which allows bit-fields that are wider than the type.
4325-
def warn_bitfield_width_exceeds_type_size: Warning<
4326-
"size of bit-field %0 (%1 bits) exceeds the size of its type; value will be "
4327-
"truncated to %2 bits">;
4328-
def warn_anon_bitfield_width_exceeds_type_size : Warning<
4329-
"size of anonymous bit-field (%0 bits) exceeds size of its type; value will "
4330-
"be truncated to %1 bits">;
4326+
def warn_bitfield_width_exceeds_type_width: Warning<
4327+
"width of bit-field %0 (%1 bits) exceeds the width of its type; value will "
4328+
"be truncated to %2 bit%s2">, InGroup<BitFieldWidth>;
4329+
def warn_anon_bitfield_width_exceeds_type_width : Warning<
4330+
"width of anonymous bit-field (%0 bits) exceeds width of its type; value "
4331+
"will be truncated to %1 bit%s1">, InGroup<BitFieldWidth>;
43314332

43324333
def warn_missing_braces : Warning<
43334334
"suggest braces around initialization of subobject">,

‎clang/lib/Sema/SemaDecl.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -12625,26 +12625,26 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
1262512625
}
1262612626

1262712627
if (!FieldTy->isDependentType()) {
12628-
uint64_t TypeSize = Context.getTypeSize(FieldTy);
12629-
if (Value.getZExtValue() > TypeSize) {
12628+
uint64_t TypeWidth = Context.getIntWidth(FieldTy);
12629+
if (Value.ugt(TypeWidth)) {
1263012630
if (!getLangOpts().CPlusPlus || IsMsStruct ||
1263112631
Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1263212632
if (FieldName)
12633-
return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_size)
12633+
return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_width)
1263412634
<< FieldName << (unsigned)Value.getZExtValue()
12635-
<< (unsigned)TypeSize;
12635+
<< (unsigned)TypeWidth;
1263612636

12637-
return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_size)
12638-
<< (unsigned)Value.getZExtValue() << (unsigned)TypeSize;
12637+
return Diag(FieldLoc, diag::err_anon_bitfield_width_exceeds_type_width)
12638+
<< (unsigned)Value.getZExtValue() << (unsigned)TypeWidth;
1263912639
}
1264012640

1264112641
if (FieldName)
12642-
Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_size)
12642+
Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_width)
1264312643
<< FieldName << (unsigned)Value.getZExtValue()
12644-
<< (unsigned)TypeSize;
12644+
<< (unsigned)TypeWidth;
1264512645
else
12646-
Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_size)
12647-
<< (unsigned)Value.getZExtValue() << (unsigned)TypeSize;
12646+
Diag(FieldLoc, diag::warn_anon_bitfield_width_exceeds_type_width)
12647+
<< (unsigned)Value.getZExtValue() << (unsigned)TypeWidth;
1264812648
}
1264912649
}
1265012650

‎clang/test/CodeGen/bitfield-2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ unsigned long long test_5() {
237237
/***/
238238

239239
struct s6 {
240-
_Bool f0 : 2;
240+
unsigned f0 : 2;
241241
};
242242

243243
struct s6 g6 = { 0xF };

‎clang/test/CodeGenCXX/warn-padded-packed.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct S12 {
6969

7070
struct S13 { // expected-warning {{padding size of 'S13' with 6 bits to alignment boundary}}
7171
char c;
72-
bool b : 10; // expected-warning {{size of bit-field 'b' (10 bits) exceeds the size of its type}}
72+
bool b : 10; // expected-warning {{width of bit-field 'b' (10 bits) exceeds the width of its type}}
7373
};
7474

7575
// The warnings are emitted when the layout of the structs is computed, so we have to use them.

‎clang/test/Misc/warning-flags.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This test serves two purposes:
1818

1919
The list of warnings below should NEVER grow. It should gradually shrink to 0.
2020

21-
CHECK: Warnings without flags (92):
21+
CHECK: Warnings without flags (90):
2222
CHECK-NEXT: ext_excess_initializers
2323
CHECK-NEXT: ext_excess_initializers_in_char_array_initializer
2424
CHECK-NEXT: ext_expected_semi_decl_list
@@ -44,10 +44,8 @@ CHECK-NEXT: pp_pragma_once_in_main_file
4444
CHECK-NEXT: pp_pragma_sysheader_in_main_file
4545
CHECK-NEXT: w_asm_qualifier_ignored
4646
CHECK-NEXT: warn_accessor_property_type_mismatch
47-
CHECK-NEXT: warn_anon_bitfield_width_exceeds_type_size
4847
CHECK-NEXT: warn_arcmt_nsalloc_realloc
4948
CHECK-NEXT: warn_asm_label_on_auto_decl
50-
CHECK-NEXT: warn_bitfield_width_exceeds_type_size
5149
CHECK-NEXT: warn_c_kext
5250
CHECK-NEXT: warn_call_to_pure_virtual_member_function_from_ctor_dtor
5351
CHECK-NEXT: warn_call_wrong_number_of_arguments

‎clang/test/Sema/bitfield.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct a {
66
int a : -1; // expected-error{{bit-field 'a' has negative width}}
77

88
// rdar://6081627
9-
int b : 33; // expected-error{{size of bit-field 'b' (33 bits) exceeds size of its type (32 bits)}}
9+
int b : 33; // expected-error{{width of bit-field 'b' (33 bits) exceeds width of its type (32 bits)}}
1010

1111
int c : (1 + 0.25); // expected-error{{expression is not an integer constant expression}}
1212
int d : (int)(1 + 0.25);
@@ -22,9 +22,12 @@ struct a {
2222
int g : (_Bool)1;
2323

2424
// PR4017
25-
char : 10; // expected-error {{size of anonymous bit-field (10 bits) exceeds size of its type (8 bits)}}
25+
char : 10; // expected-error {{width of anonymous bit-field (10 bits) exceeds width of its type (8 bits)}}
2626
unsigned : -2; // expected-error {{anonymous bit-field has negative width (-2)}}
2727
float : 12; // expected-error {{anonymous bit-field has non-integral type 'float'}}
28+
29+
_Bool : 2; // expected-error {{width of anonymous bit-field (2 bits) exceeds width of its type (1 bit)}}
30+
_Bool h : 5; // expected-error {{width of bit-field 'h' (5 bits) exceeds width of its type (1 bit)}}
2831
};
2932

3033
struct b {unsigned x : 2;} x;

‎clang/test/SemaCXX/bitfield-layout.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
// Simple tests.
77
struct Test1 {
8-
char c : 9; // expected-warning {{size of bit-field 'c' (9 bits) exceeds the size of its type; value will be truncated to 8 bits}}
8+
char c : 9; // expected-warning {{width of bit-field 'c' (9 bits) exceeds the width of its type; value will be truncated to 8 bits}}
99
};
1010
CHECK_SIZE(Test1, 2);
1111
CHECK_ALIGN(Test1, 1);
1212

1313
struct Test2 {
14-
char c : 16; // expected-warning {{size of bit-field 'c' (16 bits) exceeds the size of its type; value will be truncated to 8 bits}}
14+
char c : 16; // expected-warning {{width of bit-field 'c' (16 bits) exceeds the width of its type; value will be truncated to 8 bits}}
1515
};
1616
CHECK_SIZE(Test2, 2);
1717
CHECK_ALIGN(Test2, 2);
1818

1919
struct Test3 {
20-
char c : 32; // expected-warning {{size of bit-field 'c' (32 bits) exceeds the size of its type; value will be truncated to 8 bits}}
20+
char c : 32; // expected-warning {{width of bit-field 'c' (32 bits) exceeds the width of its type; value will be truncated to 8 bits}}
2121
};
2222
CHECK_SIZE(Test3, 4);
2323
CHECK_ALIGN(Test3, 4);
2424

2525
struct Test4 {
26-
char c : 64; // expected-warning {{size of bit-field 'c' (64 bits) exceeds the size of its type; value will be truncated to 8 bits}}
26+
char c : 64; // expected-warning {{width of bit-field 'c' (64 bits) exceeds the width of its type; value will be truncated to 8 bits}}
2727
};
2828
CHECK_SIZE(Test4, 8);
2929
CHECK_ALIGN(Test4, 8);

‎clang/test/SemaCXX/constant-expression-cxx11.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1801,9 +1801,9 @@ namespace Bitfields {
18011801
bool b : 1;
18021802
unsigned u : 5;
18031803
int n : 5;
1804-
bool b2 : 3;
1805-
unsigned u2 : 74; // expected-warning {{exceeds the size of its type}}
1806-
int n2 : 81; // expected-warning {{exceeds the size of its type}}
1804+
bool b2 : 3; // expected-warning {{exceeds the width of its type}}
1805+
unsigned u2 : 74; // expected-warning {{exceeds the width of its type}}
1806+
int n2 : 81; // expected-warning {{exceeds the width of its type}}
18071807
};
18081808

18091809
constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}

‎clang/test/SemaCXX/constant-expression-cxx1y.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ namespace Lifetime {
872872

873873
namespace Bitfields {
874874
struct A {
875-
bool b : 3;
875+
bool b : 1;
876876
int n : 4;
877877
unsigned u : 5;
878878
};
+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// RUN: %clang_cc1 -fno-rtti -emit-llvm-only -triple i686-pc-win32 -fdump-record-layouts -fsyntax-only -mms-bitfields -verify %s 2>&1
22

33
struct A {
4-
char a : 9; // expected-error{{size of bit-field 'a' (9 bits) exceeds size of its type (8 bits)}}
5-
int b : 33; // expected-error{{size of bit-field 'b' (33 bits) exceeds size of its type (32 bits)}}
6-
bool c : 9; // expected-error{{size of bit-field 'c' (9 bits) exceeds size of its type (8 bits)}}
4+
char a : 9; // expected-error{{width of bit-field 'a' (9 bits) exceeds width of its type (8 bits)}}
5+
int b : 33; // expected-error{{width of bit-field 'b' (33 bits) exceeds width of its type (32 bits)}}
6+
bool c : 9; // expected-error{{width of bit-field 'c' (9 bits) exceeds width of its type (1 bit)}}
7+
bool d : 3; // expected-error{{width of bit-field 'd' (3 bits) exceeds width of its type (1 bit)}}
78
};
89

910
int a[sizeof(A) == 1 ? 1 : -1];

‎clang/test/SemaObjC/class-bitfield.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ @interface X
55
int a : -1; // expected-error{{bit-field 'a' has negative width}}
66

77
// rdar://6081627
8-
int b : 33; // expected-error{{size of bit-field 'b' (33 bits) exceeds size of its type (32 bits)}}
8+
int b : 33; // expected-error{{width of bit-field 'b' (33 bits) exceeds width of its type (32 bits)}}
99

1010
int c : (1 + 0.25); // expected-error{{expression is not an integer constant expression}}
1111
int d : (int)(1 + 0.25);

0 commit comments

Comments
 (0)
Please sign in to comment.