[llvm] [NFC][TableGen] Add `InclusionGuardEmitter` to emit header inclusion guards (PR #163283)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 14 10:13:39 PDT 2025


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/163283

>From cf7b2cc073e91ef507237b1aa537653ed1ed2dc3 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 13 Oct 2025 15:38:07 -0700
Subject: [PATCH 1/2] [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.
---
 llvm/include/llvm/TableGen/CodeGenHelpers.h    | 16 ++++++++++++++++
 llvm/test/TableGen/directive1.td               |  1 +
 llvm/test/TableGen/directive2.td               |  1 +
 llvm/utils/TableGen/Basic/DirectiveEmitter.cpp |  6 ++----
 4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h
index 5b823db494e7a..4199fdfb2aff9 100644
--- a/llvm/include/llvm/TableGen/CodeGenHelpers.h
+++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h
@@ -34,6 +34,22 @@ class IfDefEmitter {
   raw_ostream &OS;
 };
 
+// Simple RAII helper for emitting header inclusion guard (i.e,
+// ifndef-define-endif).
+class InclusionGuardEmitter {
+public:
+  InclusionGuardEmitter(raw_ostream &OS, StringRef Name)
+      : Name(Name.str()), OS(OS) {
+    OS << "#ifndef " << Name << "\n"
+       << "#define " << Name << "\n\n";
+  }
+  ~InclusionGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
+
+private:
+  std::string Name;
+  raw_ostream &OS;
+};
+
 // Simple RAII helper for emitting namespace scope. Name can be a single
 // namespace (empty for anonymous namespace) or nested namespace.
 class NamespaceEmitter {
diff --git a/llvm/test/TableGen/directive1.td b/llvm/test/TableGen/directive1.td
index 3eda077eeabf7..475faf9254157 100644
--- a/llvm/test/TableGen/directive1.td
+++ b/llvm/test/TableGen/directive1.td
@@ -177,6 +177,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
 // CHECK-NEXT:    static constexpr bool is_iterable = true;
 // CHECK-NEXT:  };
 // CHECK-NEXT:  } // namespace llvm
+// CHECK-EMPTY:
 // CHECK-NEXT:  #endif // LLVM_Tdl_INC
 
 
diff --git a/llvm/test/TableGen/directive2.td b/llvm/test/TableGen/directive2.td
index a25197c3efd93..ccc09446b4465 100644
--- a/llvm/test/TableGen/directive2.td
+++ b/llvm/test/TableGen/directive2.td
@@ -150,6 +150,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
 // CHECK-NEXT:    static constexpr bool is_iterable = true;
 // CHECK-NEXT:  };
 // CHECK-NEXT:  } // namespace llvm
+// CHECK-EMPTY:
 // CHECK-NEXT:  #endif // LLVM_Tdl_INC
 
 // IMPL:      #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
diff --git a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
index b4d816ea211f7..decef7e4eae62 100644
--- a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
@@ -266,10 +266,9 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
     return;
 
   StringRef Lang = DirLang.getName();
+  InclusionGuardEmitter IncGuard(OS, (Twine("LLVM_") + Lang + "_INC").str());
 
-  OS << "#ifndef LLVM_" << Lang << "_INC\n";
-  OS << "#define LLVM_" << Lang << "_INC\n";
-  OS << "\n#include \"llvm/ADT/ArrayRef.h\"\n";
+  OS << "#include \"llvm/ADT/ArrayRef.h\"\n";
 
   if (DirLang.hasEnableBitmaskEnumInNamespace())
     OS << "#include \"llvm/ADT/BitmaskEnum.h\"\n";
@@ -370,7 +369,6 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
     OS << "};\n";
   }
   LlvmNS.close();
-  OS << "#endif // LLVM_" << Lang << "_INC\n";
 }
 
 // Given a list of spellings (for a given clause/directive), order them

>From 9296b2ea9575d25e45c98f95fc5399306345548e Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 14 Oct 2025 10:13:11 -0700
Subject: [PATCH 2/2] Rename to IncludeGuardEmitter

---
 llvm/include/llvm/TableGen/CodeGenHelpers.h    | 9 ++++-----
 llvm/utils/TableGen/Basic/DirectiveEmitter.cpp | 2 +-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h
index 4199fdfb2aff9..ce91f62a0e51e 100644
--- a/llvm/include/llvm/TableGen/CodeGenHelpers.h
+++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h
@@ -34,16 +34,15 @@ class IfDefEmitter {
   raw_ostream &OS;
 };
 
-// Simple RAII helper for emitting header inclusion guard (i.e,
-// ifndef-define-endif).
-class InclusionGuardEmitter {
+// Simple RAII helper for emitting header include guard (ifndef-define-endif).
+class IncludeGuardEmitter {
 public:
-  InclusionGuardEmitter(raw_ostream &OS, StringRef Name)
+  IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
       : Name(Name.str()), OS(OS) {
     OS << "#ifndef " << Name << "\n"
        << "#define " << Name << "\n\n";
   }
-  ~InclusionGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
+  ~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }
 
 private:
   std::string Name;
diff --git a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
index decef7e4eae62..3c6ff1132230b 100644
--- a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
@@ -266,7 +266,7 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
     return;
 
   StringRef Lang = DirLang.getName();
-  InclusionGuardEmitter IncGuard(OS, (Twine("LLVM_") + Lang + "_INC").str());
+  IncludeGuardEmitter IncGuard(OS, (Twine("LLVM_") + Lang + "_INC").str());
 
   OS << "#include \"llvm/ADT/ArrayRef.h\"\n";
 



More information about the llvm-commits mailing list