Skip to content

Commit 07b326b

Browse files
committed
[clang] Format bitfield width diagnostics with thousands-separators
1 parent 6f16a8b commit 07b326b

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6400,7 +6400,7 @@ def err_incorrect_number_of_vector_initializers : Error<
64006400
// Used by C++ which allows bit-fields that are wider than the type.
64016401
def warn_bitfield_width_exceeds_type_width: Warning<
64026402
"width of bit-field %0 (%1 bits) exceeds the width of its type; value will "
6403-
"be truncated to %2 bit%s2">, InGroup<BitFieldWidth>;
6403+
"be truncated to %2 bits">, InGroup<BitFieldWidth>;
64046404
def err_bitfield_too_wide : Error<
64056405
"%select{bit-field %1|anonymous bit-field}0 is too wide (%2 bits)">;
64066406
def warn_bitfield_too_small_for_enum : Warning<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18307,16 +18307,22 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
1830718307
if (Value.isSigned() && Value.isNegative()) {
1830818308
if (FieldName)
1830918309
return Diag(FieldLoc, diag::err_bitfield_has_negative_width)
18310-
<< FieldName << toString(Value, 10);
18310+
<< FieldName
18311+
<< toString(Value, 10, Value.isSigned(),
18312+
/*formatAsCLiteral=*/false, /*UpperCase=*/true,
18313+
/*InsertSeparators=*/true);
1831118314
return Diag(FieldLoc, diag::err_anon_bitfield_has_negative_width)
18312-
<< toString(Value, 10);
18315+
<< toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,
18316+
/*UpperCase=*/true, /*InsertSeparators=*/true);
1831318317
}
1831418318

1831518319
// The size of the bit-field must not exceed our maximum permitted object
1831618320
// size.
1831718321
if (Value.getActiveBits() > ConstantArrayType::getMaxSizeBits(Context)) {
1831818322
return Diag(FieldLoc, diag::err_bitfield_too_wide)
18319-
<< !FieldName << FieldName << toString(Value, 10);
18323+
<< !FieldName << FieldName
18324+
<< toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,
18325+
/*UpperCase=*/true, /*InsertSeparators=*/true);
1832018326
}
1832118327

1832218328
if (!FieldTy->isDependentType()) {
@@ -18335,17 +18341,26 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
1833518341
unsigned DiagWidth =
1833618342
CStdConstraintViolation ? TypeWidth : TypeStorageSize;
1833718343
return Diag(FieldLoc, diag::err_bitfield_width_exceeds_type_width)
18338-
<< (bool)FieldName << FieldName << toString(Value, 10)
18344+
<< (bool)FieldName << FieldName
18345+
<< toString(Value, 10, Value.isSigned(),
18346+
/*formatAsCLiteral=*/false, /*UpperCase=*/true,
18347+
/*InsertSeparators=*/true)
1833918348
<< !CStdConstraintViolation << DiagWidth;
1834018349
}
1834118350

1834218351
// Warn on types where the user might conceivably expect to get all
1834318352
// specified bits as value bits: that's all integral types other than
1834418353
// 'bool'.
1834518354
if (BitfieldIsOverwide && !FieldTy->isBooleanType() && FieldName) {
18355+
llvm::APInt TypeWidthAP(sizeof(TypeWidth) * 8, TypeWidth,
18356+
/*IsSigned=*/false);
1834618357
Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_width)
18347-
<< FieldName << toString(Value, 10)
18348-
<< (unsigned)TypeWidth;
18358+
<< FieldName
18359+
<< toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false,
18360+
/*UpperCase=*/true, /*InsertSeparators=*/true)
18361+
<< toString(TypeWidthAP, 10, /*Signed=*/false,
18362+
/*formatAsCLiteral=*/false, /*UpperCase=*/true,
18363+
/*InsertSeparators=*/true);
1834918364
}
1835018365
}
1835118366

clang/test/SemaCXX/bitfield-layout.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ CHECK_SIZE(Test4, 8);
3535
CHECK_ALIGN(Test4, 8);
3636

3737
struct Test5 {
38-
char c : 0x100000001; // expected-warning {{width of bit-field 'c' (4294967297 bits) exceeds the width of its type; value will be truncated to 8 bits}}
38+
char c : 0x100000001; // expected-warning {{width of bit-field 'c' (4'294'967'297 bits) exceeds the width of its type; value will be truncated to 8 bits}}
3939
};
4040
// Size and align don't really matter here, just make sure we don't crash.
4141
CHECK_SIZE(Test5, 1);
4242
CHECK_ALIGN(Test5, 1);
4343

4444
struct Test6 {
45-
char c : (unsigned __int128)0xffffffffffffffff + 2; // expected-error {{bit-field 'c' is too wide (18446744073709551617 bits)}}
45+
char c : (unsigned __int128)0xffffffffffffffff + 2; // expected-error {{bit-field 'c' is too wide (18'446'744'073'709'551'617 bits)}}
4646
};
4747
// Size and align don't really matter here, just make sure we don't crash.
4848
CHECK_SIZE(Test6, 1);

0 commit comments

Comments
 (0)