Issue 107872
Summary clang-format incorrectly formats TableGen
Labels clang-format
Assignees
Reporter tim-hoffman
    #### Input file
```
#ifndef DEMO_OPS
#define DEMO_OPS

include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/OpBase.td"
include "mlir/IR/RegionKindInterface.td"
include "mlir/IR/SymbolInterfaces.td"

include "Dialect/DEMO/IR/Dialect.td"
include "Dialect/DEMO/IR/Attrs.td"
include "Dialect/DEMO/IR/Types.td"

class DEMO_Op<string mnemonic, list<Trait> traits = []>
    : Op<DEMODialect, mnemonic, traits>;

//===----------------------------------------------------------------------===//
// Basic constructs
//===----------------------------------------------------------------------===//

def DEMO_StructDefOp : DEMO_Op<"struct", [
 HasParent<"mlir::ModuleOp">,
  Symbol,
  SymbolTable,
 IsolatedFromAbove,
  GraphRegionNoTerminator,
]> {
  let summary = "";
  let description = [{}];

  let arguments = (ins
 SymbolNameAttr:$sym_name,
 OptionalAttr<FlatSymbolRefArrayAttr>:$const_params
  );

  let regions = (region SizedRegion<1> : $body);

  let assemblyFormat = [{
    $sym_name (`<` $const_params^ `>`)? $body attr-dict
 }];

  let hasRegionVerifier = 1;
}

def DEMO_FieldOp : DEMO_Op<"field", [HasParent<"StructDefOp">, Symbol]> {
  let summary = "";
  let description = [{}];

  let arguments = (ins
 SymbolNameAttr:$sym_name,
 OptionalAttr<TypedArrayAttrBase<DEMO_TypeModiferAttr, "type modifiers">>:$modifiers,
      TypeAttrOf<AnyDEMOType>:$type
 );

  let assemblyFormat = [{
    $sym_name `:` ($modifiers^)? $type attr-dict
 }];
}

//===----------------------------------------------------------------------===//
// Field element operators
//===----------------------------------------------------------------------===//

class DEMO_BinaryFeltOp<string mnemonic, list<Trait> traits = []>
    : DEMO_Op<mnemonic,
              !listconcat(traits, [SameOperandsAndResultType, Pure, NoMemoryEffect])> {

  let arguments = (ins DEMO_FeltType:$lhs, DEMO_FeltType:$rhs);
  let results = (outs DEMO_FeltType:$result);
  let assemblyFormat = "$lhs `,` $rhs attr-dict";
}

#endif // DEMO_OPS
```

#### `.clang-format` file:
```
---
# Formatter for .td files
Language: TableGen
BasedOnStyle: LLVM
IndentWidth: 2
---
# Formatter for .h, .cpp, etc.
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 2
```

#### Command:
`clang-format -i include/Dialect/DEMO/IR/Ops.td`

#### Result after formatting:
```
#ifndef DEMO_OPS
#define DEMO_OPS

include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/OpBase.td"
include "mlir/IR/RegionKindInterface.td"
include "mlir/IR/SymbolInterfaces.td"

include "Dialect/DEMO/IR/Dialect.td"
include "Dialect/DEMO/IR/Attrs.td"
include "Dialect/DEMO/IR/Types.td"

class DEMO_Op<string mnemonic, list<Trait> traits = []>
    : Op<DEMODialect, mnemonic, traits>;

//===----------------------------------------------------------------------===//
// Basic constructs
//===----------------------------------------------------------------------===//

def DEMO_StructDefOp : DEMO_Op<"struct", [
 HasParent<"mlir::ModuleOp">,
  Symbol,
  SymbolTable,
 IsolatedFromAbove,
  GraphRegionNoTerminator,
]> {
  let summary = "";
  let description = [{}];

  let arguments = (ins
 SymbolNameAttr:$sym_name,
 OptionalAttr<FlatSymbolRefArrayAttr>:$const_params
  );

  let regions = (region SizedRegion<1> : $body);

  let assemblyFormat = [{
    $sym_name (`<` $const_params^ `>`)? $body attr-dict
 }];

  let hasRegionVerifier = 1;
}

def DEMO_FieldOp : DEMO_Op<"field", [HasParent<"StructDefOp">, Symbol]> {
  let summary = "";
  let description = [{}];

  let arguments = (ins
 SymbolNameAttr:$sym_name,
 OptionalAttr<TypedArrayAttrBase<DEMO_TypeModiferAttr, "type modifiers">>:$modifiers,
      TypeAttrOf<AnyDEMOType>:$type
 );

  let assemblyFormat = [{
    $sym_name `:` ($modifiers^)? $type attr-dict
 }];
}

//===----------------------------------------------------------------------===//
// Field element operators
//===---------------------------------------------------------------- -- -- -- ==
      = //

      class DEMO_BinaryFeltOp<string mnemonic, list<Trait> traits = []>
      : DEMO_Op<mnemonic, !listconcat(traits, [
                  SameOperandsAndResultType, Pure, NoMemoryEffect
                ])> {

    let arguments = (ins DEMO_FeltType : $lhs, DEMO_FeltType : $rhs);
    let results = (outs DEMO_FeltType : $result);
    let assemblyFormat = "$lhs `,` $rhs attr-dict";
  }

#endif // DEMO_OPS
```

#### Problem:
The comment block with "Field element operators" is reflowed improperly and the class declaration after it is formatted incorrectly.

#### Other attempts that did NOT resolve the issue:
1. Adding "ReflowComments: false" in the TableGen section of the `.clang-format` file
2. Adding "// clang-format off" and "// clang-format on" around the block comment. Resulted in:
```
// clang-format off
//===----------------------------------------------------------------------===//
// Field element operators
//===-------------------------------------------- -- -- -- -- -- -- -- -- -- -- -- -- -- ==
      = //
      // clang-format on

      class DEMO_BinaryFeltOp<string mnemonic, list<Trait> traits = []>
      : DEMO_Op<mnemonic, !listconcat(traits, [
                  SameOperandsAndResultType, Pure, NoMemoryEffect
 ])> {

    let arguments = (ins DEMO_FeltType : $lhs, DEMO_FeltType : $rhs);
    let results = (outs DEMO_FeltType : $result);
    let assemblyFormat = "$lhs `,` $rhs attr-dict";
 }

#endif // DEMO_OPS

```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to