Skip to content

Commit 76a19b3

Browse files
committed
Support negative numbers in %human format
1 parent 5f5772d commit 76a19b3

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

clang/docs/InternalsManual.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ Class:
324324
Description:
325325
This is a formatter which represents the argument number in a human readable
326326
format: the value ``123`` stays ``123``, ``12345`` becomes ``12.34k``,
327-
``6666666` becomes ``6.67M``, and so on for 'G' and 'T'. Values less than
328-
``0`` are not supported.
327+
``6666666` becomes ``6.67M``, and so on for 'G' and 'T'.
329328

330329
**"objcclass" format**
331330

clang/lib/Basic/Diagnostic.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,15 +658,19 @@ static void HandleOrdinalModifier(unsigned ValNo,
658658
// 1234567 -> "1.23M".
659659
// 1234567890 -> "1.23G".
660660
// 1234567890123 -> "1.23T".
661-
static void HandleIntegerHumanModifier(unsigned ValNo,
661+
static void HandleIntegerHumanModifier(int64_t ValNo,
662662
SmallVectorImpl<char> &OutStr) {
663-
static constexpr std::array<std::pair<uint64_t, char>, 4> Units = {
664-
{{1'000'000'000'000UL, 'T'},
665-
{1'000'000'000UL, 'G'},
666-
{1'000'000UL, 'M'},
667-
{1'000UL, 'k'}}};
663+
static constexpr std::array<std::pair<int64_t, char>, 4> Units = {
664+
{{1'000'000'000'000L, 'T'},
665+
{1'000'000'000L, 'G'},
666+
{1'000'000L, 'M'},
667+
{1'000L, 'k'}}};
668668

669669
llvm::raw_svector_ostream Out(OutStr);
670+
if (ValNo < 0) {
671+
Out << "-";
672+
ValNo = -ValNo;
673+
}
670674
for (const auto &[UnitSize, UnitSign] : Units) {
671675
if (ValNo >= UnitSize) {
672676
Out << llvm::format("%0.2f%c", ValNo / static_cast<double>(UnitSize),

0 commit comments

Comments
 (0)