How to clang-format tablegen files?

Hi!

I have tried to format the tablegen file with the following command under llvm source tree:

clang-format-13 -i ${Target}.td

My input:

// matches vt:$rd = (op2 (op1 s64:$ptr, i32:$offset))
class TwoOpOneRetFrag<SDPatternOperator op1, SDPatternOperator op2,
                      ValueType vt>
    : PatFrag<(ops node:$x, node:$y),
              (vt (op2 (op1 node:$x, node:$y)))> {
}

After the clang-format:

// matches vt:$rd = (op2 (op1 s64:$ptr, i32:$offset))
class TwoOpOneRetFrag<SDPatternOperator op1, SDPatternOperator op2,
                      ValueType vt> : PatFrag<(ops node
                                               : $x, node
                                               : $y),
                                              (vt(op2(op1 node
                                                      : $x, node
                                                      : $y)))> {
}

Is this the expected format? Or I should use some other different command options?

Thanks :slight_smile:
CY

TableGen is not a language supported by clang-format.

Hi @jrtc27,
Thanks for your reply! When I read the source code, I see:

clang/include/clang/Format/Format.h

// Returns a string representation of ``Language``.
inline StringRef getLanguageName(FormatStyle::LanguageKind Language) {
  switch (Language) {
  case FormatStyle::LK_Cpp:
    return "C++";
...
  case FormatStyle::LK_TableGen:
    return "TableGen";

clang/lib/Format/Format.cpp

FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
  FormatStyle LLVMStyle;
  LLVMStyle.InheritsParentConfig = false;
  LLVMStyle.Language = Language;
...
  // Defaults that differ when not C++.
  if (Language == FormatStyle::LK_TableGen)
    LLVMStyle.SpacesInContainerLiterals = false;

That’s why I guess clang-format supports tablegen as well :slight_smile:
But the results looks weird to me :thinking:

Thanks,
CY

I agree this is confusing.

Looking at git blame, initial support was added in 2015 and never touched since. It is well known within the community that clang-format “doesn’t support” TableGen because it produces random output, not because it’s not recognised.

Perhaps we should just remove the TableGen type and not even pretend it works?

Thanks @rengolin!
Understood :slight_smile: