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

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 5 08:22:40 PST 2024


================
@@ -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 HumanString;
+  llvm::raw_string_ostream HumanStringStream(HumanString);
+  for (const auto &[UnitSize, UnitSign] : Units) {
+    if (Number >= UnitSize) {
+      HumanStringStream << llvm::format(
+          "%.2f%c", Number / static_cast<double>(UnitSize), UnitSign);
+      break;
----------------
ilya-biryukov wrote:

Suggestion: maybe reduce the amount of the code a little by using `return` instead of `break`?
That would also make the extra variable unnecessary.

```cpp
for (... : Units) {
  if (Number >= UnitSize)
    return llvm::formatv(...);
}
return std::to_string(Number);
```

https://github.com/llvm/llvm-project/pull/114999


More information about the cfe-commits mailing list