@@ -38,28 +38,35 @@ class IfDefEmitter {
3838// namespace (empty for anonymous namespace) or nested namespace.
3939class NamespaceEmitter {
4040public:
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
5455private:
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
0 commit comments