Skip to content

Commit dc365b2

Browse files
authored
[NFC][TableGen] Emit nested namespace definitions in NamespaceEmitter (#161958)
Change NamespaceEmitter to emit nested namespace using C++17 nested namespace definitions.
1 parent b5d75b2 commit dc365b2

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,35 @@ class IfDefEmitter {
3838
// namespace (empty for anonymous namespace) or nested namespace.
3939
class NamespaceEmitter {
4040
public:
41-
NamespaceEmitter(raw_ostream &OS, StringRef Name) : OS(OS) {
42-
emitNamespaceStarts(Name);
41+
NamespaceEmitter(raw_ostream &OS, StringRef Name)
42+
: Name(trim(Name).str()), OS(OS) {
43+
OS << "namespace " << this->Name << " {\n";
4344
}
4445

4546
~NamespaceEmitter() { close(); }
4647

4748
// Explicit function to close the namespace scopes.
4849
void close() {
49-
for (StringRef NS : llvm::reverse(Namespaces))
50-
OS << "} // namespace " << NS << "\n";
51-
Namespaces.clear();
50+
if (!Closed)
51+
OS << "} // namespace " << Name << "\n";
52+
Closed = true;
5253
}
5354

5455
private:
55-
void emitNamespaceStarts(StringRef Name) {
56-
llvm::SplitString(Name, Namespaces, "::");
57-
for (StringRef NS : Namespaces)
58-
OS << "namespace " << NS << " {\n";
56+
// Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
57+
// generated namespace scope needs to use
58+
//
59+
// namespace mlir::toy {
60+
// }
61+
//
62+
// and cannot use "namespace ::mlir::toy".
63+
static StringRef trim(StringRef Name) {
64+
Name.consume_front("::");
65+
return Name;
5966
}
60-
61-
SmallVector<StringRef, 2> Namespaces;
67+
std::string Name;
6268
raw_ostream &OS;
69+
bool Closed = false;
6370
};
6471

6572
} // end namespace llvm

mlir/test/mlir-tblgen/dialect.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ def E_SpecialNSOp : Op<E_Dialect, "special_ns_op", []> {
6262
// DEF: ::E::SPECIAL_NS::SpecialNSOp definitions
6363

6464
// DECL-LABEL: GET_OP_CLASSES
65+
// DECL: namespace a {
6566
// DECL: a::SomeOp declarations
67+
// DECL: namespace BNS {
6668
// DECL: BNS::SomeOp declarations
69+
// DECL: namespace C::CC {
6770
// DECL: ::C::CC::SomeOp declarations
6871
// DECL: DSomeOp declarations
72+
// DECL: namespace ENS {
6973
// DECL: ENS::SomeOp declarations
74+
// DECL: namespace E::SPECIAL_NS {
7075
// DECL: ::E::SPECIAL_NS::SpecialNSOp declarations

0 commit comments

Comments
 (0)