Skip to content

[clang] Make source locations space usage diagnostics numbers easier to read #114999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions clang/include/clang/Basic/DiagnosticCommonKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,14 @@ def remark_sloc_usage : Remark<
"source manager location address space usage:">,
InGroup<DiagGroup<"sloc-usage">>, DefaultRemark, ShowInSystemHeader;
def note_total_sloc_usage : Note<
"%0B in local locations, %1B in locations loaded from AST files, for a total "
"of %2B (%3%% of available space)">;
"%0B (%1B) in local locations, %2B (%3B) "
"in locations loaded from AST files, for a total of %4B (%5B) "
"(%6%% of available space)">;
def note_file_sloc_usage : Note<
"file entered %0 time%s0 using %1B of space"
"%plural{0:|: plus %2B for macro expansions}2">;
"file entered %0 time%s0 using %1B (%2B) of space"
"%plural{0:|: plus %3B (%4B) for macro expansions}3">;
def note_file_misc_sloc_usage : Note<
"%0 additional files entered using a total of %1B of space">;
"%0 additional files entered using a total of %1B (%2B) of space">;

// Modules
def err_module_format_unhandled : Error<
Expand Down
34 changes: 31 additions & 3 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
Expand Down Expand Up @@ -2227,6 +2228,28 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
}
}

// 123 -> "123".
// 1234 -> "1.23k".
// 123456 -> "123.46k".
// 1234567 -> "1.23M".
// 1234567890 -> "1.23G".
// 1234567890123 -> "1.23T".
static std::string humanizeNumber(uint64_t Number) {
static constexpr std::array<std::pair<uint64_t, char>, 4> Units = {
{{1'000'000'000'000UL, 'T'},
{1'000'000'000UL, 'G'},
{1'000'000UL, 'M'},
{1'000UL, 'k'}}};

for (const auto &[UnitSize, UnitSign] : Units) {
if (Number >= UnitSize) {
return llvm::formatv("{0:F}{1}", Number / static_cast<double>(UnitSize),
UnitSign);
}
}
return std::to_string(Number);
}

void SourceManager::noteSLocAddressSpaceUsage(
DiagnosticsEngine &Diag, std::optional<unsigned> MaxNotes) const {
struct Info {
Expand Down Expand Up @@ -2296,22 +2319,27 @@ void SourceManager::noteSLocAddressSpaceUsage(
int UsagePercent = static_cast<int>(100.0 * double(LocalUsage + LoadedUsage) /
MaxLoadedOffset);
Diag.Report(SourceLocation(), diag::note_total_sloc_usage)
<< LocalUsage << LoadedUsage << (LocalUsage + LoadedUsage) << UsagePercent;
<< LocalUsage << humanizeNumber(LocalUsage) << LoadedUsage
<< humanizeNumber(LoadedUsage) << (LocalUsage + LoadedUsage)
<< humanizeNumber(LocalUsage + LoadedUsage) << UsagePercent;

// Produce notes on sloc address space usage for each file with a high usage.
uint64_t ReportedSize = 0;
for (auto &[Entry, FileInfo] :
llvm::make_range(SortedUsage.begin(), SortedEnd)) {
Diag.Report(FileInfo.Loc, diag::note_file_sloc_usage)
<< FileInfo.Inclusions << FileInfo.DirectSize
<< (FileInfo.TotalSize - FileInfo.DirectSize);
<< humanizeNumber(FileInfo.DirectSize)
<< (FileInfo.TotalSize - FileInfo.DirectSize)
<< humanizeNumber(FileInfo.TotalSize - FileInfo.DirectSize);
ReportedSize += FileInfo.TotalSize;
}

// Describe any remaining usage not reported in the per-file usage.
if (ReportedSize != CountedSize) {
Diag.Report(SourceLocation(), diag::note_file_misc_sloc_usage)
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize;
<< (SortedUsage.end() - SortedEnd) << CountedSize - ReportedSize
<< humanizeNumber(CountedSize - ReportedSize);
}
}

Expand Down
10 changes: 5 additions & 5 deletions clang/test/Lexer/SourceLocationsOverflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// CHECK-NEXT: inc1.h{{.*}}: fatal error: translation unit is too large for Clang to process: ran out of source locations
// CHECK-NEXT: #include "inc2.h"
// CHECK-NEXT: ^
// 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)
// CHECK-NEXT: {{.*}}inc2.h:1:1: note: file entered 214{{..}} times using 214{{.......}}B of space
// 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)
// CHECK-NEXT: {{.*}}inc2.h:1:1: note: file entered 214{{..}} times using 214{{.......}}B (2.15GB) of space
// CHECK-NEXT: /*.................................................................................................
// CHECK-NEXT: ^
// CHECK-NEXT: {{.*}}inc1.h:1:1: note: file entered 15 times using 39{{....}}B of space
// CHECK-NEXT: {{.*}}inc1.h:1:1: note: file entered 15 times using 39{{....}}B (396.92kB) of space
// CHECK-NEXT: #include "inc2.h"
// CHECK-NEXT: ^
// CHECK-NEXT: <built-in>:1:1: note: file entered {{.*}} times using {{.*}}B of space
// CHECK-NEXT: <built-in>:1:1: note: file entered {{.*}} times using {{.*}}B ({{.*}}B) of space
// CHECK-NEXT: # {{.*}}
// CHECK-NEXT: ^
// CHECK-NEXT: {{.*}}SourceLocationsOverflow.c:1:1: note: file entered 1 time using {{.*}}B of space
// CHECK-NEXT: {{.*}}SourceLocationsOverflow.c:1:1: note: file entered 1 time using {{.*}}B ({{.*}}B) of space
// CHECK-NEXT: // RUN: not %clang %s -S -o - 2>&1 | FileCheck %s
// CHECK-NEXT: ^
// CHECK-NEXT: 1 error generated.
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Misc/sloc-usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ bool b = EQUALS(k, k);

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