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:

My question is:

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.