Skip to content

Commit 139f726

Browse files
authored
[NFC][TableGen] Add IfGuardEmitter and adopt it in InstrInfoEmitter (#168616)
Add a RAII `IfGuardEmitter` to insert simple #if guards and adopt it in InstrInfoEmitter.
1 parent 19129ea commit 139f726

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

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

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

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -943,24 +943,23 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
943943
}
944944
}
945945

946-
OS << "#if defined(GET_INSTRINFO_MC_DESC) || "
947-
"defined(GET_INSTRINFO_CTOR_DTOR)\n";
948-
949-
OS << "namespace llvm {\n\n";
950-
951-
OS << "struct " << TargetName << "InstrTable {\n";
952-
OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
953-
OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
954-
"\"Unwanted padding between Insts and OperandInfo\");\n";
955-
OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
956-
OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
957-
"\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
958-
OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n";
959-
OS << "};\n\n";
960-
961-
OS << "} // end namespace llvm\n";
962-
OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || "
963-
"defined(GET_INSTRINFO_CTOR_DTOR)\n\n";
946+
{
947+
IfGuardEmitter IfGuard(
948+
OS,
949+
"defined(GET_INSTRINFO_MC_DESC) || defined(GET_INSTRINFO_CTOR_DTOR)");
950+
NamespaceEmitter NS(OS, "llvm");
951+
952+
OS << "struct " << TargetName << "InstrTable {\n";
953+
OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
954+
OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
955+
"\"Unwanted padding between Insts and OperandInfo\");\n";
956+
OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
957+
OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
958+
"\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
959+
OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U)
960+
<< "];\n";
961+
OS << "};";
962+
}
964963

965964
const CodeGenRegBank &RegBank = Target.getRegBank();
966965
const CodeGenHwModes &CGH = Target.getHwModes();

0 commit comments

Comments
 (0)