Skip to content

Commit 3d48f20

Browse files
jurahulPriyanshu3820
authored andcommitted
[NFC][TableGen] Remove close member from various CodeGenHelpers (llvm#167904)
Always rely on local scopes to enforce the lifetime of these helper objects and by extension where the "closing" of various C++ code constructs happens.
1 parent 3fe9ded commit 3d48f20

File tree

3 files changed

+106
-126
lines changed

3 files changed

+106
-126
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,17 @@ class IfDefEmitter {
3333
OS << "#undef " << Name << "\n";
3434
OS << "\n";
3535
}
36-
~IfDefEmitter() { close(); }
37-
38-
// Explicit function to close the ifdef scopes.
39-
void close() {
40-
if (Closed)
41-
return;
42-
36+
~IfDefEmitter() {
4337
OS << "\n";
4438
if (LateUndef)
4539
OS << "#undef " << Name << "\n";
4640
OS << "#endif // " << Name << "\n\n";
47-
Closed = true;
4841
}
4942

5043
private:
5144
std::string Name;
5245
raw_ostream &OS;
5346
bool LateUndef;
54-
bool Closed = false;
5547
};
5648

5749
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
@@ -62,20 +54,11 @@ class IncludeGuardEmitter {
6254
OS << "#ifndef " << Name << "\n"
6355
<< "#define " << Name << "\n\n";
6456
}
65-
~IncludeGuardEmitter() { close(); }
66-
67-
// Explicit function to close the ifdef scopes.
68-
void close() {
69-
if (Closed)
70-
return;
71-
OS << "\n#endif // " << Name << "\n\n";
72-
Closed = true;
73-
}
57+
~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
7458

7559
private:
7660
std::string Name;
7761
raw_ostream &OS;
78-
bool Closed = false;
7962
};
8063

8164
// Simple RAII helper for emitting namespace scope. Name can be a single
@@ -89,15 +72,9 @@ class NamespaceEmitter {
8972
OS << "namespace " << Name << " {\n\n";
9073
}
9174

92-
~NamespaceEmitter() { close(); }
93-
94-
// Explicit function to close the namespace scopes.
95-
void close() {
96-
if (Closed)
97-
return;
75+
~NamespaceEmitter() {
9876
if (!Name.empty())
9977
OS << "\n} // namespace " << Name << "\n";
100-
Closed = true;
10178
}
10279

10380
private:
@@ -114,7 +91,6 @@ class NamespaceEmitter {
11491
}
11592
std::string Name;
11693
raw_ostream &OS;
117-
bool Closed = false;
11894
};
11995

12096
} // end namespace llvm

llvm/utils/TableGen/Basic/DirectiveEmitter.cpp

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -276,80 +276,81 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
276276
OS << "#include <utility>\n"; // for std::pair
277277
OS << "\n";
278278
NamespaceEmitter LlvmNS(OS, "llvm");
279-
NamespaceEmitter DirLangNS(OS, DirLang.getCppNamespace());
280-
281-
if (DirLang.hasEnableBitmaskEnumInNamespace())
282-
OS << "LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n\n";
283-
284-
// Emit Directive associations
285-
std::vector<const Record *> Associations;
286-
copy_if(DirLang.getAssociations(), std::back_inserter(Associations),
287-
// Skip the "special" value
288-
[](const Record *Def) { return Def->getName() != "AS_FromLeaves"; });
289-
generateEnumClass(Associations, OS, "Association",
290-
/*Prefix=*/"", /*ExportEnums=*/false);
279+
{
280+
NamespaceEmitter DirLangNS(OS, DirLang.getCppNamespace());
281+
282+
if (DirLang.hasEnableBitmaskEnumInNamespace())
283+
OS << "LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n\n";
284+
285+
// Emit Directive associations
286+
std::vector<const Record *> Associations;
287+
copy_if(
288+
DirLang.getAssociations(), std::back_inserter(Associations),
289+
// Skip the "special" value
290+
[](const Record *Def) { return Def->getName() != "AS_FromLeaves"; });
291+
generateEnumClass(Associations, OS, "Association",
292+
/*Prefix=*/"", /*ExportEnums=*/false);
291293

292-
generateEnumClass(DirLang.getCategories(), OS, "Category", /*Prefix=*/"",
293-
/*ExportEnums=*/false);
294+
generateEnumClass(DirLang.getCategories(), OS, "Category", /*Prefix=*/"",
295+
/*ExportEnums=*/false);
294296

295-
generateEnumBitmask(DirLang.getSourceLanguages(), OS, "SourceLanguage",
296-
/*Prefix=*/"", /*ExportEnums=*/false);
297+
generateEnumBitmask(DirLang.getSourceLanguages(), OS, "SourceLanguage",
298+
/*Prefix=*/"", /*ExportEnums=*/false);
297299

298-
// Emit Directive enumeration
299-
generateEnumClass(DirLang.getDirectives(), OS, "Directive",
300-
DirLang.getDirectivePrefix(),
301-
DirLang.hasMakeEnumAvailableInNamespace());
300+
// Emit Directive enumeration
301+
generateEnumClass(DirLang.getDirectives(), OS, "Directive",
302+
DirLang.getDirectivePrefix(),
303+
DirLang.hasMakeEnumAvailableInNamespace());
302304

303-
// Emit Clause enumeration
304-
generateEnumClass(DirLang.getClauses(), OS, "Clause",
305-
DirLang.getClausePrefix(),
306-
DirLang.hasMakeEnumAvailableInNamespace());
305+
// Emit Clause enumeration
306+
generateEnumClass(DirLang.getClauses(), OS, "Clause",
307+
DirLang.getClausePrefix(),
308+
DirLang.hasMakeEnumAvailableInNamespace());
307309

308-
// Emit ClauseVals enumeration
309-
std::string EnumHelperFuncs;
310-
generateClauseEnumVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs);
310+
// Emit ClauseVals enumeration
311+
std::string EnumHelperFuncs;
312+
generateClauseEnumVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs);
311313

312-
// Generic function signatures
313-
OS << "// Enumeration helper functions\n";
314+
// Generic function signatures
315+
OS << "// Enumeration helper functions\n";
314316

315-
OS << "LLVM_ABI std::pair<Directive, directive::VersionRange> get" << Lang
316-
<< "DirectiveKindAndVersions(StringRef Str);\n";
317+
OS << "LLVM_ABI std::pair<Directive, directive::VersionRange> get" << Lang
318+
<< "DirectiveKindAndVersions(StringRef Str);\n";
317319

318-
OS << "inline Directive get" << Lang << "DirectiveKind(StringRef Str) {\n";
319-
OS << " return get" << Lang << "DirectiveKindAndVersions(Str).first;\n";
320-
OS << "}\n";
321-
OS << "\n";
320+
OS << "inline Directive get" << Lang << "DirectiveKind(StringRef Str) {\n";
321+
OS << " return get" << Lang << "DirectiveKindAndVersions(Str).first;\n";
322+
OS << "}\n";
323+
OS << "\n";
322324

323-
OS << "LLVM_ABI StringRef get" << Lang
324-
<< "DirectiveName(Directive D, unsigned Ver = 0);\n";
325-
OS << "\n";
325+
OS << "LLVM_ABI StringRef get" << Lang
326+
<< "DirectiveName(Directive D, unsigned Ver = 0);\n";
327+
OS << "\n";
326328

327-
OS << "LLVM_ABI std::pair<Clause, directive::VersionRange> get" << Lang
328-
<< "ClauseKindAndVersions(StringRef Str);\n";
329-
OS << "\n";
329+
OS << "LLVM_ABI std::pair<Clause, directive::VersionRange> get" << Lang
330+
<< "ClauseKindAndVersions(StringRef Str);\n";
331+
OS << "\n";
330332

331-
OS << "inline Clause get" << Lang << "ClauseKind(StringRef Str) {\n";
332-
OS << " return get" << Lang << "ClauseKindAndVersions(Str).first;\n";
333-
OS << "}\n";
334-
OS << "\n";
333+
OS << "inline Clause get" << Lang << "ClauseKind(StringRef Str) {\n";
334+
OS << " return get" << Lang << "ClauseKindAndVersions(Str).first;\n";
335+
OS << "}\n";
336+
OS << "\n";
335337

336-
OS << "LLVM_ABI StringRef get" << Lang
337-
<< "ClauseName(Clause C, unsigned Ver = 0);\n";
338-
OS << "\n";
338+
OS << "LLVM_ABI StringRef get" << Lang
339+
<< "ClauseName(Clause C, unsigned Ver = 0);\n";
340+
OS << "\n";
339341

340-
OS << "/// Return true if \\p C is a valid clause for \\p D in version \\p "
341-
<< "Version.\n";
342-
OS << "LLVM_ABI bool isAllowedClauseForDirective(Directive D, "
343-
<< "Clause C, unsigned Version);\n";
344-
OS << "\n";
345-
OS << "constexpr std::size_t getMaxLeafCount() { return "
346-
<< getMaxLeafCount(DirLang) << "; }\n";
347-
OS << "LLVM_ABI Association getDirectiveAssociation(Directive D);\n";
348-
OS << "LLVM_ABI Category getDirectiveCategory(Directive D);\n";
349-
OS << "LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);\n";
350-
OS << EnumHelperFuncs;
351-
352-
DirLangNS.close();
342+
OS << "/// Return true if \\p C is a valid clause for \\p D in version \\p "
343+
<< "Version.\n";
344+
OS << "LLVM_ABI bool isAllowedClauseForDirective(Directive D, "
345+
<< "Clause C, unsigned Version);\n";
346+
OS << "\n";
347+
OS << "constexpr std::size_t getMaxLeafCount() { return "
348+
<< getMaxLeafCount(DirLang) << "; }\n";
349+
OS << "LLVM_ABI Association getDirectiveAssociation(Directive D);\n";
350+
OS << "LLVM_ABI Category getDirectiveCategory(Directive D);\n";
351+
OS << "LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);\n";
352+
OS << EnumHelperFuncs;
353+
} // close DirLangNS
353354

354355
// These specializations need to be in ::llvm.
355356
for (StringRef Enum : {"Association", "Category", "Directive", "Clause"}) {

mlir/tools/mlir-tblgen/EnumsGen.cpp

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -702,41 +702,45 @@ static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
702702
StringRef underlyingToSymFnName = enumInfo.getUnderlyingToSymbolFnName();
703703
auto enumerants = enumInfo.getAllCases();
704704

705-
llvm::NamespaceEmitter ns(os, cppNamespace);
706-
707-
// Emit the enum class definition
708-
emitEnumClass(enumDef, enumName, underlyingType, description, enumerants, os);
709-
710-
// Emit conversion function declarations
711-
if (llvm::all_of(enumerants, [](EnumCase enumerant) {
712-
return enumerant.getValue() >= 0;
713-
})) {
714-
os << formatv(
715-
"::std::optional<{0}> {1}({2});\n", enumName, underlyingToSymFnName,
716-
underlyingType.empty() ? std::string("unsigned") : underlyingType);
717-
}
718-
os << formatv("{2} {1}({0});\n", enumName, symToStrFnName, symToStrFnRetType);
719-
os << formatv("::std::optional<{0}> {1}(::llvm::StringRef);\n", enumName,
720-
strToSymFnName);
721-
722-
if (enumInfo.isBitEnum()) {
723-
emitOperators(enumDef, os);
724-
} else {
725-
emitMaxValueFn(enumDef, os);
726-
}
705+
{
706+
llvm::NamespaceEmitter ns(os, cppNamespace);
707+
708+
// Emit the enum class definition
709+
emitEnumClass(enumDef, enumName, underlyingType, description, enumerants,
710+
os);
711+
712+
// Emit conversion function declarations
713+
if (llvm::all_of(enumerants, [](EnumCase enumerant) {
714+
return enumerant.getValue() >= 0;
715+
})) {
716+
os << formatv(
717+
"::std::optional<{0}> {1}({2});\n", enumName, underlyingToSymFnName,
718+
underlyingType.empty() ? std::string("unsigned") : underlyingType);
719+
}
720+
os << formatv("{2} {1}({0});\n", enumName, symToStrFnName,
721+
symToStrFnRetType);
722+
os << formatv("::std::optional<{0}> {1}(::llvm::StringRef);\n", enumName,
723+
strToSymFnName);
724+
725+
if (enumInfo.isBitEnum()) {
726+
emitOperators(enumDef, os);
727+
} else {
728+
emitMaxValueFn(enumDef, os);
729+
}
727730

728-
// Generate a generic `stringifyEnum` function that forwards to the method
729-
// specified by the user.
730-
const char *const stringifyEnumStr = R"(
731+
// Generate a generic `stringifyEnum` function that forwards to the method
732+
// specified by the user.
733+
const char *const stringifyEnumStr = R"(
731734
inline {0} stringifyEnum({1} enumValue) {{
732735
return {2}(enumValue);
733736
}
734737
)";
735-
os << formatv(stringifyEnumStr, symToStrFnRetType, enumName, symToStrFnName);
738+
os << formatv(stringifyEnumStr, symToStrFnRetType, enumName,
739+
symToStrFnName);
736740

737-
// Generate a generic `symbolizeEnum` function that forwards to the method
738-
// specified by the user.
739-
const char *const symbolizeEnumStr = R"(
741+
// Generate a generic `symbolizeEnum` function that forwards to the method
742+
// specified by the user.
743+
const char *const symbolizeEnumStr = R"(
740744
template <typename EnumType>
741745
::std::optional<EnumType> symbolizeEnum(::llvm::StringRef);
742746
@@ -745,9 +749,9 @@ inline ::std::optional<{0}> symbolizeEnum<{0}>(::llvm::StringRef str) {
745749
return {1}(str);
746750
}
747751
)";
748-
os << formatv(symbolizeEnumStr, enumName, strToSymFnName);
752+
os << formatv(symbolizeEnumStr, enumName, strToSymFnName);
749753

750-
const char *const attrClassDecl = R"(
754+
const char *const attrClassDecl = R"(
751755
class {1} : public ::mlir::{2} {
752756
public:
753757
using ValueType = {0};
@@ -757,13 +761,12 @@ class {1} : public ::mlir::{2} {
757761
{0} getValue() const;
758762
};
759763
)";
760-
if (enumInfo.genSpecializedAttr()) {
761-
StringRef attrClassName = enumInfo.getSpecializedAttrClassName();
762-
StringRef baseAttrClassName = "IntegerAttr";
763-
os << formatv(attrClassDecl, enumName, attrClassName, baseAttrClassName);
764-
}
765-
766-
ns.close();
764+
if (enumInfo.genSpecializedAttr()) {
765+
StringRef attrClassName = enumInfo.getSpecializedAttrClassName();
766+
StringRef baseAttrClassName = "IntegerAttr";
767+
os << formatv(attrClassDecl, enumName, attrClassName, baseAttrClassName);
768+
}
769+
} // close `ns`.
767770

768771
// Generate a generic parser and printer for the enum.
769772
std::string qualName =

0 commit comments

Comments
 (0)