[clang] [llvm] [Support] Add scaling support in `indent` (PR #109478)
Matt Arsenault via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 20 21:49:10 PDT 2024
================
@@ -774,18 +774,27 @@ class buffer_unique_ostream : public raw_svector_ostream {
// you can use
// OS << indent(6) << "more stuff";
// which has better ergonomics (and clang-formats better as well).
+//
+// If indentation is always in increments of a fixed value, you can use Scale
+// to set that value once. So indent(1, 2) will add 2 spaces and
+// indent(1,2) + 1 will add 4 spaces.
struct indent {
- unsigned NumSpaces;
-
- explicit indent(unsigned NumSpaces) : NumSpaces(NumSpaces) {}
- void operator+=(unsigned N) { NumSpaces += N; }
- void operator-=(unsigned N) { NumSpaces -= N; }
- indent operator+(unsigned N) const { return indent(NumSpaces + N); }
- indent operator-(unsigned N) const { return indent(NumSpaces - N); }
+ // Indentation is represented as `NumIndents` steps of size `Scale` each.
+ unsigned NumIndents;
+ unsigned Scale;
+
+ explicit indent(unsigned NumIndents, unsigned Scale = 1)
+ : NumIndents(NumIndents), Scale(Scale) {}
+
+ // These arithmeric operators preserve scale.
+ void operator+=(unsigned N) { NumIndents += N; }
+ void operator-=(unsigned N) { NumIndents -= N; }
+ indent operator+(unsigned N) const { return indent(NumIndents + N, Scale); }
+ indent operator-(unsigned N) const { return indent(NumIndents - N, Scale); }
----------------
arsenm wrote:
I'm surprised there's no guard against underflow here
https://github.com/llvm/llvm-project/pull/109478
More information about the cfe-commits
mailing list