aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
authorBoaz Brickner <brickner@google.com>2024-11-06 09:45:16 +0100
committerGitHub <noreply@github.com>2024-11-06 09:45:16 +0100
commit8431494094c8732d1426763d3e1aae322fa76830 (patch)
tree95cffccefa99ae3a6482d0be3b65e796ce8b263f /clang/lib/Basic/SourceManager.cpp
parentc0a7b60fd1b244782032fefc261c4442c54c3935 (diff)
downloadllvm-8431494094c8732d1426763d3e1aae322fa76830.zip
llvm-8431494094c8732d1426763d3e1aae322fa76830.tar.gz
llvm-8431494094c8732d1426763d3e1aae322fa76830.tar.bz2
[clang] Make source locations space usage diagnostics numbers easier to read (#114999)
Instead of writing "12345678B", write "12345678B (12.34MB)".
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r--clang/lib/Basic/SourceManager.cpp34
1 files changed, 31 insertions, 3 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index 65a8a72..af60348 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<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 {
@@ -2296,7 +2319,9 @@ 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;
@@ -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);
}
}