Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 30 additions & 3 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,28 @@ LLVM_DUMP_METHOD void SourceManager::dump() const {
}
}

static std::string NumberToHumanString(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'}}};

std::string human_string;
llvm::raw_string_ostream human_string_stream(human_string);
for (const auto &[UnitSize, UnitSign] : Units) {
if (number >= UnitSize) {
human_string_stream << llvm::format(
"%.2f%c", number / static_cast<double>(UnitSize), UnitSign);
break;
}
}
if (human_string.empty()) {
human_string_stream << number;
}
return human_string;
}

void SourceManager::noteSLocAddressSpaceUsage(
DiagnosticsEngine &Diag, std::optional<unsigned> MaxNotes) const {
struct Info {
Expand Down Expand Up @@ -2296,22 +2318,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 << NumberToHumanString(LocalUsage) << LoadedUsage
<< NumberToHumanString(LoadedUsage) << (LocalUsage + LoadedUsage)
<< NumberToHumanString(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);
<< NumberToHumanString(FileInfo.DirectSize)
<< (FileInfo.TotalSize - FileInfo.DirectSize)
<< NumberToHumanString(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
<< NumberToHumanString(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