Skip to content

Commit 32baec4

Browse files
authored
[clang][Diags] Automatically format AP(S)Int values with separators (#161047)
This adds an `operator<<` overload for `StreamingDiagnostic` that takes an `APInt`/`APSInt` and formats it with default options, including adding separators. This is still an opt-in mechanism since all callers that want to use this feature need to be changed from ```c++ Diag() << toString(MyInt, 10); ``` to ```c++ Diag() << MyInt; ``` This patch contains one example of a diagnostic making use of this.
1 parent 56d920d commit 32baec4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

clang/include/clang/Basic/Diagnostic.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2626
#include "llvm/ADT/SmallString.h"
2727
#include "llvm/ADT/SmallVector.h"
28+
#include "llvm/ADT/StringExtras.h"
2829
#include "llvm/ADT/iterator_range.h"
2930
#include "llvm/Support/Compiler.h"
3031
#include <cassert>
@@ -1366,6 +1367,22 @@ inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
13661367
return DB;
13671368
}
13681369

1370+
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
1371+
const llvm::APSInt &Int) {
1372+
DB.AddString(toString(Int, /*Radix=*/10, Int.isSigned(),
1373+
/*formatAsCLiteral=*/false,
1374+
/*UpperCase=*/true, /*InsertSeparators=*/true));
1375+
return DB;
1376+
}
1377+
1378+
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
1379+
const llvm::APInt &Int) {
1380+
DB.AddString(toString(Int, /*Radix=*/10, /*Signed=*/false,
1381+
/*formatAsCLiteral=*/false,
1382+
/*UpperCase=*/true, /*InsertSeparators=*/true));
1383+
return DB;
1384+
}
1385+
13691386
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
13701387
int I) {
13711388
DB.AddTaggedVal(I, DiagnosticsEngine::ak_sint);

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18909,8 +18909,7 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
1890918909
// 'bool'.
1891018910
if (BitfieldIsOverwide && !FieldTy->isBooleanType() && FieldName) {
1891118911
Diag(FieldLoc, diag::warn_bitfield_width_exceeds_type_width)
18912-
<< FieldName << toString(Value, 10)
18913-
<< (unsigned)TypeWidth;
18912+
<< FieldName << Value << (unsigned)TypeWidth;
1891418913
}
1891518914
}
1891618915

clang/test/SemaCXX/bitfield-layout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ 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);

0 commit comments

Comments
 (0)