Skip to content

Commit cf7b2cc

Browse files
committed
[NFC][TableGen] Add InclusionGuardEmitter to emit header inclusion guards
Add a RAII class `InclusionGuardEmitter` which is similar to `IfDefEmitter` but emits header inclusion guards, and adopt it it DirectiveEmitter.
1 parent dc365b2 commit cf7b2cc

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ class IfDefEmitter {
3434
raw_ostream &OS;
3535
};
3636

37+
// Simple RAII helper for emitting header inclusion guard (i.e,
38+
// ifndef-define-endif).
39+
class InclusionGuardEmitter {
40+
public:
41+
InclusionGuardEmitter(raw_ostream &OS, StringRef Name)
42+
: Name(Name.str()), OS(OS) {
43+
OS << "#ifndef " << Name << "\n"
44+
<< "#define " << Name << "\n\n";
45+
}
46+
~InclusionGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
47+
48+
private:
49+
std::string Name;
50+
raw_ostream &OS;
51+
};
52+
3753
// Simple RAII helper for emitting namespace scope. Name can be a single
3854
// namespace (empty for anonymous namespace) or nested namespace.
3955
class NamespaceEmitter {

llvm/test/TableGen/directive1.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
177177
// CHECK-NEXT: static constexpr bool is_iterable = true;
178178
// CHECK-NEXT: };
179179
// CHECK-NEXT: } // namespace llvm
180+
// CHECK-EMPTY:
180181
// CHECK-NEXT: #endif // LLVM_Tdl_INC
181182

182183

llvm/test/TableGen/directive2.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
150150
// CHECK-NEXT: static constexpr bool is_iterable = true;
151151
// CHECK-NEXT: };
152152
// CHECK-NEXT: } // namespace llvm
153+
// CHECK-EMPTY:
153154
// CHECK-NEXT: #endif // LLVM_Tdl_INC
154155

155156
// IMPL: #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS

llvm/utils/TableGen/Basic/DirectiveEmitter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,9 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
266266
return;
267267

268268
StringRef Lang = DirLang.getName();
269+
InclusionGuardEmitter IncGuard(OS, (Twine("LLVM_") + Lang + "_INC").str());
269270

270-
OS << "#ifndef LLVM_" << Lang << "_INC\n";
271-
OS << "#define LLVM_" << Lang << "_INC\n";
272-
OS << "\n#include \"llvm/ADT/ArrayRef.h\"\n";
271+
OS << "#include \"llvm/ADT/ArrayRef.h\"\n";
273272

274273
if (DirLang.hasEnableBitmaskEnumInNamespace())
275274
OS << "#include \"llvm/ADT/BitmaskEnum.h\"\n";
@@ -370,7 +369,6 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
370369
OS << "};\n";
371370
}
372371
LlvmNS.close();
373-
OS << "#endif // LLVM_" << Lang << "_INC\n";
374372
}
375373

376374
// Given a list of spellings (for a given clause/directive), order them

0 commit comments

Comments
 (0)