llvm-project/mlir/test/mlir-tblgen/method-body-with-only-spaces.td
drazi eea1e50ac2
[mlir-tblgen] trim method body to empty with only spaces to avoid crash (#139568)
method body or default impl must be true empty. Even they contain only
spaces, ``mlir-tblgen`` considers they are non-empty and generates
invalid code lead to segment fault. It's very hard to debug.

```c++
    InterfaceMethod<
      ...
      /*methodBody=*/  [{ }],    // This must be true empty. Leaving a space here can lead to segment fault which is hard to figure out why
      /*defaultImpl=*/ [{
        ...
      }]
```

This PR trim spaces when method body or default implementation of
interface method is not empty. Now ``mlir-tblgen`` generates valid code
even when they contain only spaces.

---------

Co-authored-by: Fung Xie <ftse@nvidia.com>
Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
2025-05-13 10:03:06 +02:00

46 lines
1.6 KiB
TableGen

// RUN: mlir-tblgen --gen-type-interface-decls -I %S/../../include %s | FileCheck %s
include "mlir/IR/OpBase.td"
def TestEmptyMethodBodyTypeInterface : TypeInterface<"TestEmptyMethodBodyTypeInterface"> {
let cppNamespace = "::TestEmptyMethodBodyTypeInterface";
let description = [{
Treat method body with only spaces as empty.
}];
let methods = [
InterfaceMethod<
/*desc=*/ [{ Trim spaces of method body and default implementation }],
/*returnType=*/ "StringRef",
/*methodName=*/ "trimSpaces",
/*args=*/ (ins),
// CHECK-LABEL: StringRef detail::TestEmptyMethodBodyTypeInterfaceInterfaceTraits::Model<ConcreteType>::trimSpaces
// CHECK-SAME: {
// CHECK-NEXT: return (::llvm::cast<ConcreteType>(tablegen_opaque_val)).trimSpaces();
// CHECK-NEXT: }
/*methodBody=*/ [{ }],
/*defaultImpl=*/ [{ return StringRef(); }]
>
];
}
def TestEmptyDefaultImplTypeInterface : TypeInterface<"TestEmptyDefaultImplTypeInterface"> {
let cppNamespace = "::TestEmptyDefaultImplTypeInterface";
let description = [{
Treat default implementation with only spaces as empty.
}];
let methods = [
InterfaceMethod<
/*desc=*/ [{ Trim spaces of default implementation }],
/*returnType=*/ "StringRef",
/*methodName=*/ "trimSpaces",
/*args=*/ (ins),
/*methodBody=*/ [{ return StringRef(); }],
// COM: Don't generate default implementation
// CHECK-NOT: StringRef detail::TestEmptyDefaultImplTypeInterfaceInterfaceTraits::ExternalModel<ConcreteModel, ConcreteType>::trimSpaces
/*defaultImpl=*/ [{
}]
>
];
}