[PATCH] [yaml2obj] Add new command line option `-docnum`

Sean Silva chisophugis at gmail.com
Sun May 25 12:26:56 PDT 2014


================
Comment at: lib/Support/YAMLTraits.cpp:93-94
@@ -92,4 +92,4 @@
 
-void Input::nextDocument() {
-  ++DocIterator;
+bool Input::nextDocument() {
+  return ++DocIterator != Strm->end();
 }
----------------
This seems like it should be a separately tested bug fix for YAMLIO

================
Comment at: tools/yaml2obj/yaml2elf.cpp:492-502
@@ +491,13 @@
+
+  unsigned CurNum = 0;
+  do {
+    ELFYAML::Object Doc;
+    YIn >> Doc;
+    if (YIn.error()) {
+      errs() << "yaml2obj: Failed to parse YAML file!\n";
+      return 1;
+    }
+    if (++CurNum == DocNum)
+      return writeELF(Out, Doc);
+  } while (YIn.nextDocument());
+
----------------
Can we pull this loop up into yaml2obj.cpp and share it between yaml2coff and yaml2elf? I would prefer if all uses of the DocNum cl::opt are kept in the same file (and for there to be just one use).

The current way you are doing this also injects a false dependency on the document type (ELFYAML::Object or COFFYAML::Object) into the loop. It should be possible to skip documents without knowing their type (think Postel's Law). Essentially I'm imagining something like this Python code:

```
yaml2which = ... # yaml2coff or yaml2elf
for i, doc in enumerate(documents, 1):
    if i == docnum:
        yaml2which(doc)
        break
else:
    errs() << ...
```

http://reviews.llvm.org/D3901






More information about the llvm-commits mailing list