23#include <mlir/TableGen/Attribute.h>
24#include <mlir/TableGen/GenInfo.h>
26#include <llvm/ADT/StringExtras.h>
27#include <llvm/Support/CommandLine.h>
28#include <llvm/Support/FormatVariadic.h>
29#include <llvm/TableGen/Record.h>
30#include <llvm/TableGen/TableGenBackend.h>
35using namespace mlir::tblgen;
46 EnumTestGenerator(llvm::raw_ostream &outputStream) : TestGenerator(
"Enum", outputStream) {}
48 virtual void genExtraMethod(
const ExtraMethod &method)
const override {
49 llvm_unreachable(
"Enums cannot have extra methods");
55 void genEnumUsageTest(StringRef cEnumName, StringRef firstCaseValue)
const {
56 static constexpr char fmt[] = R
"(
57// This test ensures the {1} enum compiles and links properly.
58TEST_F({0}EnumLinkTests, Enum_{1}_Usage) {{
59 // We create a variable and check that enum values can be assigned.
63 // Verify we can compare enum values
64 EXPECT_EQ(enumValue, {2});
67 os << llvm::formatv(fmt, dialectNameCapitalized, cEnumName, firstCaseValue);
73 void genWrapUnwrapTest(StringRef cEnumName, StringRef firstCaseValue)
const {
74 static constexpr char fmt[] = R
"(
75// This test ensures wrap/unwrap functions for {1} compile and link.
76TEST_F({0}EnumLinkTests, Enum_{1}_WrapUnwrap) {{
77 // We use the first enum case value for testing.
80 // Test that wrap and unwrap are inverses (at compile/link time)
81 // The actual C++ type doesn't exist in test context, so we just
82 // verify the functions exist and link.
83 {1} roundTrip = wrap(unwrap(cValue));
84 EXPECT_EQ(cValue, roundTrip);
87 os << llvm::formatv(fmt, dialectNameCapitalized, cEnumName, firstCaseValue);
92 void genCompleteRecord(
const EnumAttr &enumInfo) {
98 std::vector<EnumAttrCase> enumCases = enumInfo.getAllCases();
99 if (enumCases.empty()) {
108 std::string firstCase = enumCases[0].getSymbol().upper();
109 std::string firstCaseValue = llvm::formatv(
"{0}_{1}", cEnumName, firstCase).str();
112 this->genEnumUsageTest(cEnumName, firstCaseValue);
115 this->genWrapUnwrapTest(cEnumName, firstCaseValue);
122static bool emitEnumCAPITests(
const llvm::RecordKeeper &records, raw_ostream &os) {
124 emitSourceFileHeader(
"Enum C API Tests", os, records);
127 EnumTestGenerator generator(os);
130 generator.genTestClassPrologue();
133 for (
const auto *def : records.getAllDerivedDefinitionsIfDefined(
"EnumAttrInfo")) {
134 EnumAttr enumInfo(def);
135 generator.genCompleteRecord(enumInfo);
141static mlir::GenRegistration
142 genEnumCAPITests(
"gen-enum-capi-tests",
"Generate enum C API unit tests", &emitEnumCAPITests);
llvm::cl::opt< std::string > DialectName
llvm::cl::opt< std::string > FunctionPrefix
std::string toPascalCase(mlir::StringRef str)
Convert names separated by underscore or colon to PascalCase.
Generator for common test implementation file elements.