[PATCH] D19893: Make ListScope and DictScope support empty labels

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Tue May 3 16:21:39 PDT 2016


zturner created this revision.
zturner added a reviewer: rnk.
zturner added a subscriber: llvm-commits.

You might want to label a scope with nothing at all, for example so that you can print out a list of records as:

```
{
   Field1,
   Field2
}
{
   FieldA,
   FieldB
}
```

This wasn't possible though, because a space was always being inserted after the label name, even if the label was empty.  Rather than duplicate this ugly code in both `ListScope` and `DictScope`, I noticed that they were essentially identical except for the scope delimeters.  So I made `DelimitedScope<Open,Close>` and then turned `DictScope` and `ListScope` into template aliases

http://reviews.llvm.org/D19893

Files:
  include/llvm/Support/ScopedPrinter.h

Index: include/llvm/Support/ScopedPrinter.h
===================================================================
--- include/llvm/Support/ScopedPrinter.h
+++ include/llvm/Support/ScopedPrinter.h
@@ -304,33 +304,26 @@
   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
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19893.56077.patch
Type: text/x-patch
Size: 1142 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160503/b7602abc/attachment-0001.bin>


More information about the llvm-commits mailing list