diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td index ae709e45a700a..457abea0b8147 100644 --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -389,13 +389,14 @@ def remark_sloc_usage : Remark< "source manager location address space usage:">, InGroup>, 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< diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 65a8a7253e054..af60348ef2655 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -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" @@ -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, 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(UnitSize), + UnitSign); + } + } + return std::to_string(Number); +} + void SourceManager::noteSLocAddressSpaceUsage( DiagnosticsEngine &Diag, std::optional MaxNotes) const { struct Info { @@ -2296,7 +2319,9 @@ void SourceManager::noteSLocAddressSpaceUsage( int UsagePercent = static_cast(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; @@ -2304,14 +2329,17 @@ void SourceManager::noteSLocAddressSpaceUsage( 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); } } diff --git a/clang/test/Lexer/SourceLocationsOverflow.c b/clang/test/Lexer/SourceLocationsOverflow.c index f058c09428e6e..26b0d204c49ff 100644 --- a/clang/test/Lexer/SourceLocationsOverflow.c +++ b/clang/test/Lexer/SourceLocationsOverflow.c @@ -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: :1:1: note: file entered {{.*}} times using {{.*}}B of space +// CHECK-NEXT: :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. diff --git a/clang/test/Misc/sloc-usage.cpp b/clang/test/Misc/sloc-usage.cpp index 18bd94f8b9dc3..f2c152a268ac0 100644 --- a/clang/test/Misc/sloc-usage.cpp +++ b/clang/test/Misc/sloc-usage.cpp @@ -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}}