Skip to content

Commit ab32e3f

Browse files
committed
[NFC][TableGen] Add IfGuardEmitter and adopt it in InstrInfoEmitter
Add a RAII `IfGuardEmitter` to insert simple #if guards and adopt it in InstrInfoEmitter.
1 parent 96e58b8 commit ab32e3f

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ class IfDefEmitter {
4646
bool LateUndef;
4747
};
4848

49+
// Simple RAII helper for emitting #if guard. It emits:
50+
// <IfGuard> <Condition>
51+
// #endif // <Condition>
52+
class IfGuardEmitter {
53+
public:
54+
IfGuardEmitter(raw_ostream &OS, StringRef Condition,
55+
StringRef IfGuard = "#if")
56+
: Condition(Condition.str()), OS(OS) {
57+
OS << IfGuard << " " << Condition << "\n\n";
58+
}
59+
60+
~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; }
61+
62+
private:
63+
std::string Condition;
64+
raw_ostream &OS;
65+
};
66+
4967
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
5068
class IncludeGuardEmitter {
5169
public:

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -911,24 +911,23 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
911911
}
912912
}
913913

914-
OS << "#if defined(GET_INSTRINFO_MC_DESC) || "
915-
"defined(GET_INSTRINFO_CTOR_DTOR)\n";
916-
917-
OS << "namespace llvm {\n\n";
918-
919-
OS << "struct " << TargetName << "InstrTable {\n";
920-
OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
921-
OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
922-
"\"Unwanted padding between Insts and OperandInfo\");\n";
923-
OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
924-
OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
925-
"\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
926-
OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n";
927-
OS << "};\n\n";
928-
929-
OS << "} // end namespace llvm\n";
930-
OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || "
931-
"defined(GET_INSTRINFO_CTOR_DTOR)\n\n";
914+
{
915+
IfGuardEmitter IfGuard(
916+
OS,
917+
"defined(GET_INSTRINFO_MC_DESC) || defined(GET_INSTRINFO_CTOR_DTOR)");
918+
NamespaceEmitter NS(OS, "llvm");
919+
920+
OS << "struct " << TargetName << "InstrTable {\n";
921+
OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
922+
OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
923+
"\"Unwanted padding between Insts and OperandInfo\");\n";
924+
OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
925+
OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
926+
"\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
927+
OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U)
928+
<< "];\n";
929+
OS << "};";
930+
}
932931

933932
const CodeGenRegBank &RegBank = Target.getRegBank();
934933
const CodeGenHwModes &CGH = Target.getHwModes();

0 commit comments

Comments
 (0)