Issues with clang-format of LLVM StringSwitch (original) (raw)
Hi all,
Clang-format seems to not format LLVM’s StringSwitchwell. Please see this LLVM PR: [NFC][TableGen][X86] Use StringSwitch to map from string → enum by jurahul · Pull Request #139929 · llvm/llvm-project
It seems several instances of StringSwitch are formatted to leave a lot of horizontal space at the start of the lines, which impacts readability (IMO). A better formatting, for example, in the first use of StringSwitch here might be (in function typeFromString):
OperandType Type =
Switch.Case("i16mem", TYPE_M)
.Case("i16imm", TYPE_IMM)
.Case("i16i8imm", TYPE_IMM)
.Case("GR16", TYPE_R16)
.Case("GR16orGR32orGR64", TYPE_R16)
.Case("i32mem", TYPE_M)
.Case("i32imm", TYPE_IMM)
or something similar that shifts all the cases to the left. But later on, in immediateEncodingFromString the formatting seems to be better with less horizontal whitespace.
So there seem to be 2 issues:
- Too much whitespace at the start of line in some cases, and
- Inconsistency in formatting of what looks like similar code patterns in different functions.
My question is:
- Can this be improved? Is this worth filing an issue?
- Can I steer clang-format with annotations to steer it in the direction I want? I know I can do clang-format off and do my own formatting but want to avoid it if I can.
Thanks,
Rahul
jurahul May 14, 2025, 5:44pm 2
Using auto in a few places helps shift it left, but that may not be always possible or desirable.
FWIW, the Rust-like way looks something like:
OperandType Type = Switch
.Case("i16mem", TYPE_M)
.Case("i16imm", TYPE_IMM)
.Case("i16i8imm", TYPE_IMM)
.Case("GR16", TYPE_R16)
.Case("GR16orGR32orGR64", TYPE_R16)
.Case("i32mem", TYPE_M)
.Case("i32imm", TYPE_IMM)
which certainly helps the indentation at least. Not sure if there’s a clang-format option that likes this formatting.