Skip to content

Commit 68dbe3b

Browse files
jurahulakadutta
authored andcommitted
[NFC][TableGen] Add IncludeGuardEmitter to emit header include guards (llvm#163283)
Add a RAII class `IncludeGuardEmitter` which is similar to `IfDefEmitter` but emits header include guards and adopt it in DirectiveEmitter.
1 parent 0c404a9 commit 68dbe3b

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

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

37+
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
38+
class IncludeGuardEmitter {
39+
public:
40+
IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
41+
: Name(Name.str()), OS(OS) {
42+
OS << "#ifndef " << Name << "\n"
43+
<< "#define " << Name << "\n\n";
44+
}
45+
~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }
46+
47+
private:
48+
std::string Name;
49+
raw_ostream &OS;
50+
};
51+
3752
// Simple RAII helper for emitting namespace scope. Name can be a single
3853
// namespace (empty for anonymous namespace) or nested namespace.
3954
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+
IncludeGuardEmitter 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)