Skip to content

Commit 6f3c9f9

Browse files
committed
[clang] Make source locations space usage diagnostics numbers easier to read
Instead of write "12345678B", write "12345678B (12.34MB)".
1 parent 1e9d068 commit 6f3c9f9

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-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: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,28 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
22272227
}
22282228
}
22292229

2230+
static std::string NumberToHumanString(uint64_t number) {
2231+
static constexpr std::array<std::pair<uint64_t, char>, 4> Units = {
2232+
{{1'000'000'000'000UL, 'T'},
2233+
{1'000'000'000UL, 'G'},
2234+
{1'000'000UL, 'M'},
2235+
{1'000UL, 'k'}}};
2236+
2237+
std::string human_string;
2238+
llvm::raw_string_ostream human_string_stream(human_string);
2239+
for (const auto &[UnitSize, UnitSign] : Units) {
2240+
if (number >= UnitSize) {
2241+
human_string_stream << llvm::format(
2242+
"%.2f%c", number / static_cast<double>(UnitSize), UnitSign);
2243+
break;
2244+
}
2245+
}
2246+
if (human_string.empty()) {
2247+
human_string_stream << number;
2248+
}
2249+
return human_string;
2250+
}
2251+
22302252
void SourceManager::noteSLocAddressSpaceUsage(
22312253
DiagnosticsEngine &Diag, std::optional<unsigned> MaxNotes) const {
22322254
struct Info {
@@ -2296,22 +2318,27 @@ void SourceManager::noteSLocAddressSpaceUsage(
22962318
int UsagePercent = static_cast<int>(100.0 * double(LocalUsage + LoadedUsage) /
22972319
MaxLoadedOffset);
22982320
Diag.Report(SourceLocation(), diag::note_total_sloc_usage)
2299-
<< LocalUsage << LoadedUsage << (LocalUsage + LoadedUsage) << UsagePercent;
2321+
<< LocalUsage << NumberToHumanString(LocalUsage) << LoadedUsage
2322+
<< NumberToHumanString(LoadedUsage) << (LocalUsage + LoadedUsage)
2323+
<< NumberToHumanString(LocalUsage + LoadedUsage) << UsagePercent;
23002324

23012325
// Produce notes on sloc address space usage for each file with a high usage.
23022326
uint64_t ReportedSize = 0;
23032327
for (auto &[Entry, FileInfo] :
23042328
llvm::make_range(SortedUsage.begin(), SortedEnd)) {
23052329
Diag.Report(FileInfo.Loc, diag::note_file_sloc_usage)
23062330
<< FileInfo.Inclusions << FileInfo.DirectSize
2307-
<< (FileInfo.TotalSize - FileInfo.DirectSize);
2331+
<< NumberToHumanString(FileInfo.DirectSize)
2332+
<< (FileInfo.TotalSize - FileInfo.DirectSize)
2333+
<< NumberToHumanString(FileInfo.TotalSize - FileInfo.DirectSize);
23082334
ReportedSize += FileInfo.TotalSize;
23092335
}
23102336

23112337
// Describe any remaining usage not reported in the per-file usage.
23122338
if (ReportedSize != CountedSize) {
23132339
Diag.Report(SourceLocation(), diag::note_file_misc_sloc_usage)
2314-
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize;
2340+
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize
2341+
<< NumberToHumanString(CountedSize - ReportedSize);
23152342
}
23162343
}
23172344

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)