OSDN Git Service

Make ListScope and DictScope re-use the same code.
authorZachary Turner <zturner@google.com>
Wed, 4 May 2016 01:46:59 +0000 (01:46 +0000)
committerZachary Turner <zturner@google.com>
Wed, 4 May 2016 01:46:59 +0000 (01:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268472 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ScopedPrinter.h

index 44a196d..f3cf573 100644 (file)
@@ -302,33 +302,26 @@ ScopedPrinter::printHex<support::ulittle16_t>(StringRef Label,
   startLine() << Label << ": " << hex(Value) << "\n";
 }
 
-struct DictScope {
-  DictScope(ScopedPrinter &W, StringRef N) : W(W) {
-    W.startLine() << N << " {\n";
+template<char Open, char Close>
+struct DelimitedScope {
+  DelimitedScope(ScopedPrinter &W, StringRef N) : W(W) {
+    W.startLine() << N;
+    if (!N.empty())
+      W.getOStream() << ' ';
+    W.getOStream() << Open << '\n';
     W.indent();
   }
 
-  ~DictScope() {
+  ~DelimitedScope() {
     W.unindent();
-    W.startLine() << "}\n";
+    W.startLine() << Close << '\n';
   }
 
   ScopedPrinter &W;
 };
 
-struct ListScope {
-  ListScope(ScopedPrinter &W, StringRef N) : W(W) {
-    W.startLine() << N << " [\n";
-    W.indent();
-  }
-
-  ~ListScope() {
-    W.unindent();
-    W.startLine() << "]\n";
-  }
-
-  ScopedPrinter &W;
-};
+using DictScope = DelimitedScope<'{', '}'>;
+using ListScope = DelimitedScope<'[', ']'>;
 
 } // namespace llvm