[Mlir-commits] [mlir] dc365b2 - [NFC][TableGen] Emit nested namespace definitions in NamespaceEmitter (#161958)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Oct 13 14:08:32 PDT 2025
Author: Rahul Joshi
Date: 2025-10-13T14:08:27-07:00
New Revision: dc365b24172161cf98517f8d839ab5b45c9a97ed
URL: https://github.com/llvm/llvm-project/commit/dc365b24172161cf98517f8d839ab5b45c9a97ed
DIFF: https://github.com/llvm/llvm-project/commit/dc365b24172161cf98517f8d839ab5b45c9a97ed.diff
LOG: [NFC][TableGen] Emit nested namespace definitions in NamespaceEmitter (#161958)
Change NamespaceEmitter to emit nested namespace using C++17 nested
namespace definitions.
Added:
Modified:
llvm/include/llvm/TableGen/CodeGenHelpers.h
mlir/test/mlir-tblgen/dialect.td
Removed:
################################################################################
diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h
index 7dca6a051ba75..5b823db494e7a 100644
--- a/llvm/include/llvm/TableGen/CodeGenHelpers.h
+++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h
@@ -38,28 +38,35 @@ class IfDefEmitter {
// namespace (empty for anonymous namespace) or nested namespace.
class NamespaceEmitter {
public:
- NamespaceEmitter(raw_ostream &OS, StringRef Name) : OS(OS) {
- emitNamespaceStarts(Name);
+ NamespaceEmitter(raw_ostream &OS, StringRef Name)
+ : Name(trim(Name).str()), OS(OS) {
+ OS << "namespace " << this->Name << " {\n";
}
~NamespaceEmitter() { close(); }
// Explicit function to close the namespace scopes.
void close() {
- for (StringRef NS : llvm::reverse(Namespaces))
- OS << "} // namespace " << NS << "\n";
- Namespaces.clear();
+ if (!Closed)
+ OS << "} // namespace " << Name << "\n";
+ Closed = true;
}
private:
- void emitNamespaceStarts(StringRef Name) {
- llvm::SplitString(Name, Namespaces, "::");
- for (StringRef NS : Namespaces)
- OS << "namespace " << NS << " {\n";
+ // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
+ // generated namespace scope needs to use
+ //
+ // namespace mlir::toy {
+ // }
+ //
+ // and cannot use "namespace ::mlir::toy".
+ static StringRef trim(StringRef Name) {
+ Name.consume_front("::");
+ return Name;
}
-
- SmallVector<StringRef, 2> Namespaces;
+ std::string Name;
raw_ostream &OS;
+ bool Closed = false;
};
} // end namespace llvm
diff --git a/mlir/test/mlir-tblgen/dialect.td b/mlir/test/mlir-tblgen/dialect.td
index f35ce345a90a8..9b454959a1da9 100644
--- a/mlir/test/mlir-tblgen/dialect.td
+++ b/mlir/test/mlir-tblgen/dialect.td
@@ -62,9 +62,14 @@ def E_SpecialNSOp : Op<E_Dialect, "special_ns_op", []> {
// DEF: ::E::SPECIAL_NS::SpecialNSOp definitions
// DECL-LABEL: GET_OP_CLASSES
+// DECL: namespace a {
// DECL: a::SomeOp declarations
+// DECL: namespace BNS {
// DECL: BNS::SomeOp declarations
+// DECL: namespace C::CC {
// DECL: ::C::CC::SomeOp declarations
// DECL: DSomeOp declarations
+// DECL: namespace ENS {
// DECL: ENS::SomeOp declarations
+// DECL: namespace E::SPECIAL_NS {
// DECL: ::E::SPECIAL_NS::SpecialNSOp declarations
More information about the Mlir-commits
mailing list