Skip to content

Commit 8431494

Browse files
authored
[clang] Make source locations space usage diagnostics numbers easier to read (llvm#114999)
Instead of writing "12345678B", write "12345678B (12.34MB)".
1 parent c0a7b60 commit 8431494

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,14 @@ def remark_sloc_usage : Remark<
389389
"source manager location address space usage:">,
390390
InGroup<DiagGroup<"sloc-usage">>, DefaultRemark, ShowInSystemHeader;
391391
def note_total_sloc_usage : Note<
392-
"%0B in local locations, %1B in locations loaded from AST files, for a total "
393-
"of %2B (%3%% of available space)">;
392+
"%0B (%1B) in local locations, %2B (%3B) "
393+
"in locations loaded from AST files, for a total of %4B (%5B) "
394+
"(%6%% of available space)">;
394395
def note_file_sloc_usage : Note<
395-
"file entered %0 time%s0 using %1B of space"
396-
"%plural{0:|: plus %2B for macro expansions}2">;
396+
"file entered %0 time%s0 using %1B (%2B) of space"
397+
"%plural{0:|: plus %3B (%4B) for macro expansions}3">;
397398
def note_file_misc_sloc_usage : Note<
398-
"%0 additional files entered using a total of %1B of space">;
399+
"%0 additional files entered using a total of %1B (%2B) of space">;
399400

400401
// Modules
401402
def err_module_format_unhandled : Error<

clang/lib/Basic/SourceManager.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/Endian.h"
3030
#include "llvm/Support/ErrorHandling.h"
3131
#include "llvm/Support/FileSystem.h"
32+
#include "llvm/Support/FormatVariadic.h"
3233
#include "llvm/Support/MathExtras.h"
3334
#include "llvm/Support/MemoryBuffer.h"
3435
#include "llvm/Support/Path.h"
@@ -2227,6 +2228,28 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
22272228
}
22282229
}
22292230

2231+
// 123 -> "123".
2232+
// 1234 -> "1.23k".
2233+
// 123456 -> "123.46k".
2234+
// 1234567 -> "1.23M".
2235+
// 1234567890 -> "1.23G".
2236+
// 1234567890123 -> "1.23T".
2237+
static std::string humanizeNumber(uint64_t Number) {
2238+
static constexpr std::array<std::pair<uint64_t, char>, 4> Units = {
2239+
{{1'000'000'000'000UL, 'T'},
2240+
{1'000'000'000UL, 'G'},
2241+
{1'000'000UL, 'M'},
2242+
{1'000UL, 'k'}}};
2243+
2244+
for (const auto &[UnitSize, UnitSign] : Units) {
2245+
if (Number >= UnitSize) {
2246+
return llvm::formatv("{0:F}{1}", Number / static_cast<double>(UnitSize),
2247+
UnitSign);
2248+
}
2249+
}
2250+
return std::to_string(Number);
2251+
}
2252+
22302253
void SourceManager::noteSLocAddressSpaceUsage(
22312254
DiagnosticsEngine &Diag, std::optional<unsigned> MaxNotes) const {
22322255
struct Info {
@@ -2296,22 +2319,27 @@ void SourceManager::noteSLocAddressSpaceUsage(
22962319
int UsagePercent = static_cast<int>(100.0 * double(LocalUsage + LoadedUsage) /
22972320
MaxLoadedOffset);
22982321
Diag.Report(SourceLocation(), diag::note_total_sloc_usage)
2299-
<< LocalUsage << LoadedUsage << (LocalUsage + LoadedUsage) << UsagePercent;
2322+
<< LocalUsage << humanizeNumber(LocalUsage) << LoadedUsage
2323+
<< humanizeNumber(LoadedUsage) << (LocalUsage + LoadedUsage)
2324+
<< humanizeNumber(LocalUsage + LoadedUsage) << UsagePercent;
23002325

23012326
// Produce notes on sloc address space usage for each file with a high usage.
23022327
uint64_t ReportedSize = 0;
23032328
for (auto &[Entry, FileInfo] :
23042329
llvm::make_range(SortedUsage.begin(), SortedEnd)) {
23052330
Diag.Report(FileInfo.Loc, diag::note_file_sloc_usage)
23062331
<< FileInfo.Inclusions << FileInfo.DirectSize
2307-
<< (FileInfo.TotalSize - FileInfo.DirectSize);
2332+
<< humanizeNumber(FileInfo.DirectSize)
2333+
<< (FileInfo.TotalSize - FileInfo.DirectSize)
2334+
<< humanizeNumber(FileInfo.TotalSize - FileInfo.DirectSize);
23082335
ReportedSize += FileInfo.TotalSize;
23092336
}
23102337

23112338
// Describe any remaining usage not reported in the per-file usage.
23122339
if (ReportedSize != CountedSize) {
23132340
Diag.Report(SourceLocation(), diag::note_file_misc_sloc_usage)
2314-
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize;
2341+
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize
2342+
<< humanizeNumber(CountedSize - ReportedSize);
23152343
}
23162344
}
23172345

clang/test/Lexer/SourceLocationsOverflow.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
// CHECK-NEXT: inc1.h{{.*}}: fatal error: translation unit is too large for Clang to process: ran out of source locations
44
// CHECK-NEXT: #include "inc2.h"
55
// CHECK-NEXT: ^
6-
// CHECK-NEXT: note: 214{{.......}}B in local locations, 0B in locations loaded from AST files, for a total of 214{{.......}}B (99% of available space)
7-
// CHECK-NEXT: {{.*}}inc2.h:1:1: note: file entered 214{{..}} times using 214{{.......}}B of space
6+
// CHECK-NEXT: note: 214{{.......}}B (2.15GB) in local locations, 0B (0B) in locations loaded from AST files, for a total of 214{{.......}}B (2.15GB) (99% of available space)
7+
// CHECK-NEXT: {{.*}}inc2.h:1:1: note: file entered 214{{..}} times using 214{{.......}}B (2.15GB) of space
88
// CHECK-NEXT: /*.................................................................................................
99
// CHECK-NEXT: ^
10-
// CHECK-NEXT: {{.*}}inc1.h:1:1: note: file entered 15 times using 39{{....}}B of space
10+
// CHECK-NEXT: {{.*}}inc1.h:1:1: note: file entered 15 times using 39{{....}}B (396.92kB) of space
1111
// CHECK-NEXT: #include "inc2.h"
1212
// CHECK-NEXT: ^
13-
// CHECK-NEXT: <built-in>:1:1: note: file entered {{.*}} times using {{.*}}B of space
13+
// CHECK-NEXT: <built-in>:1:1: note: file entered {{.*}} times using {{.*}}B ({{.*}}B) of space
1414
// CHECK-NEXT: # {{.*}}
1515
// CHECK-NEXT: ^
16-
// CHECK-NEXT: {{.*}}SourceLocationsOverflow.c:1:1: note: file entered 1 time using {{.*}}B of space
16+
// CHECK-NEXT: {{.*}}SourceLocationsOverflow.c:1:1: note: file entered 1 time using {{.*}}B ({{.*}}B) of space
1717
// CHECK-NEXT: // RUN: not %clang %s -S -o - 2>&1 | FileCheck %s
1818
// CHECK-NEXT: ^
1919
// CHECK-NEXT: 1 error generated.

clang/test/Misc/sloc-usage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ bool b = EQUALS(k, k);
99

1010
#pragma clang __debug sloc_usage // expected-remark {{address space usage}}
1111
// expected-note@* {{(0% of available space)}}
12-
// (this file) expected-note-re@1 {{file entered 1 time using {{.*}}B of space plus 51B for macro expansions}}
13-
// (included file) expected-note-re@Inputs/include.h:1 {{file entered 3 times using {{.*}}B of space{{$}}}}
12+
// (this file) expected-note-re@1 {{file entered 1 time using {{.*}}B ({{.*}}B) of space plus 51B (51B) for macro expansions}}
13+
// (included file) expected-note-re@Inputs/include.h:1 {{file entered 3 times using {{.*}}B ({{.*}}B) of space{{$}}}}
1414
// (builtins file) expected-note@* {{file entered}}

0 commit comments

Comments
 (0)