Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions llvm/include/llvm/TableGen/CodeGenHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,29 @@ 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.
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
Expand Down
5 changes: 5 additions & 0 deletions mlir/test/mlir-tblgen/dialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -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