LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
CommonAttrOrTypeCAPITestGen.h
Go to the documentation of this file.
1//===- CommonAttrOrTypeCAPITestGen.h - Common test generation utilities ---===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
40//===----------------------------------------------------------------------===//
41
42#pragma once
43
44#include <mlir/TableGen/AttrOrTypeDef.h>
45#include <mlir/TableGen/Dialect.h>
46
47#include <llvm/ADT/StringExtras.h>
48#include <llvm/Support/FormatVariadic.h>
49#include <llvm/Support/raw_ostream.h>
50
51#include "CommonCAPIGen.h"
52
62std::string
63generateDummyParamsForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def, bool isType);
64
72std::string generateParamListForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def);
73
79 using TestGenerator::TestGenerator;
80
81 virtual ~AttrOrTypeTestGenerator() = default;
82
85 void setParamName(mlir::StringRef name) {
86 this->paramName = name;
88 }
89
93 virtual void
94 genGetBuilderTest(const std::string &dummyParams, const std::string &paramList) const {
95 static constexpr char fmt[] = R"(
96// This test ensures {0}{2}{3}Get links properly.
97TEST_F({2}{1}LinkTests, Get_{3}) {{
98 auto test{1} = createIndex{1}();
99
100 // We only verify the function compiles and links, wrapped in an unreachable condition
101 if ({0}{1}IsA{2}{3}(test{1})) {{
102{4}
103 (void){0}{2}{3}Get(context{5});
104 }
105}
106)";
107 assert(!className.empty() && "className must be set");
108 os << llvm::formatv(
109 fmt,
110 FunctionPrefix, // {0}
111 kind, // {1}
113 className, // {3}
114 dummyParams, // {4}
115 paramList // {5}
116 );
117 }
118
120 virtual void genParamGetterTest() const {
121 static constexpr char fmt[] = R"(
122// This test ensures {0}{2}{3}Get{5} links properly.
123TEST_F({2}{1}LinkTests, Get_{3}_{4}) {{
124 auto test{1} = createIndex{1}();
125
126 if ({0}{1}IsA{2}{3}(test{1})) {{
127 (void){0}{2}{3}Get{5}(test{1});
128 }
129}
130)";
131 assert(!className.empty() && "className must be set");
132 assert(!paramName.empty() && "paramName must be set");
133 os << llvm::formatv(
136 );
137 }
138
140 virtual void genArrayRefParamCountTest() const {
141 static constexpr char fmt[] = R"(
142// This test ensures {0}{2}{3}Get{5}Count links properly.
143TEST_F({2}{1}LinkTests, Get_{3}_{4}Count) {{
144 auto test{1} = createIndex{1}();
145
146 if ({0}{1}IsA{2}{3}(test{1})) {{
147 (void){0}{2}{3}Get{5}Count(test{1});
148 }
149}
150)";
151 assert(!className.empty() && "className must be set");
152 assert(!paramName.empty() && "paramName must be set");
153 os << llvm::formatv(
156 );
157 }
158
160 virtual void genArrayRefParamAtTest() const {
161 static constexpr char fmt[] = R"(
162// This test ensures {0}{2}{3}Get{5}At links properly.
163TEST_F({2}{1}LinkTests, Get_{3}_{4}At) {{
164 auto test{1} = createIndex{1}();
165
166 if ({0}{1}IsA{2}{3}(test{1})) {{
167 (void){0}{2}{3}Get{5}At(test{1}, 0);
168 }
169}
170)";
171 assert(!className.empty() && "className must be set");
172 assert(!paramName.empty() && "paramName must be set");
173 os << llvm::formatv(
176 );
177 }
178
179 void genCompleteRecord(const mlir::tblgen::AttrOrTypeDef def, bool isType) {
180 const mlir::tblgen::Dialect &defDialect = def.getDialect();
181
182 // Generate for the selected dialect only
183 if (defDialect.getName() != DialectName) {
184 return;
185 }
186
187 this->setDialectAndClassName(&defDialect, def.getCppClassName());
189 // Generate IsA test
190 if (GenIsA) {
191 this->genIsATest();
192 }
193
194 // Generate Get builder test
195 if (GenTypeOrAttrGet && !def.skipDefaultBuilders()) {
196 std::string dummyParams = generateDummyParamsForAttrOrTypeGet(def, isType);
197 std::string paramList = generateParamListForAttrOrTypeGet(def);
198 this->genGetBuilderTest(dummyParams, paramList);
199 }
200
201 // Generate parameter getter tests
203 for (const auto &param : def.getParameters()) {
204 this->setParamName(param.getName());
205 mlir::StringRef cppType = param.getCppType();
206 if (isArrayRefType(cppType)) {
209 } else {
210 this->genParamGetterTest();
211 }
212 }
213 }
214
215 // Generate extra class method tests
217 std::optional<mlir::StringRef> extraDecls = def.getExtraDecls();
218 if (extraDecls.has_value()) {
219 this->genExtraMethods(extraDecls.value());
220 }
221 }
222 }
223
224protected:
225 mlir::StringRef paramName;
226 std::string paramNameCapitalized;
227};
std::string generateDummyParamsForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def, bool isType)
Generate dummy parameters for Get builder (used by both Attr and Type)
std::string generateParamListForAttrOrTypeGet(const mlir::tblgen::AttrOrTypeDef &def)
Generate parameter list for Get builder call (used by both Attr and Type)
llvm::cl::opt< bool > GenTypeOrAttrParamGetters
llvm::cl::opt< bool > GenTypeOrAttrGet
llvm::cl::opt< bool > GenIsA
llvm::cl::opt< std::string > DialectName
llvm::cl::opt< std::string > FunctionPrefix
bool isArrayRefType(mlir::StringRef cppType)
Check if a C++ type is an ArrayRef type.
std::string toPascalCase(mlir::StringRef str)
Convert names separated by underscore or colon to PascalCase.
llvm::cl::opt< bool > GenExtraClassMethods
Base class for attribute and type test generators.
virtual void genArrayRefParamCountTest() const
Generate ArrayRef parameter count getter test.
virtual ~AttrOrTypeTestGenerator()=default
void setParamName(mlir::StringRef name)
Set the parameter name for code generation.
virtual void genGetBuilderTest(const std::string &dummyParams, const std::string &paramList) const
Generate Get builder test for a definition.
void genCompleteRecord(const mlir::tblgen::AttrOrTypeDef def, bool isType)
virtual void genArrayRefParamAtTest() const
Generate ArrayRef parameter element getter test.
virtual void genParamGetterTest() const
Generate parameter getter test.
virtual void setDialectAndClassName(const mlir::tblgen::Dialect *d, mlir::StringRef cppClassName)
Set the dialect and class name for code generation.
mlir::StringRef className
virtual void genExtraMethods(mlir::StringRef extraDecl) const
Generate code for extra methods from an extraClassDeclaration
std::string dialectNameCapitalized
llvm::raw_ostream & os
std::string kind
Generator for common test implementation file elements.
virtual void genIsATest() const
Generate IsA test for a class.