[llvm] a29a9a3 - [yaml2obj] Refactor command line parsing

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 01:12:52 PST 2020


Author: Fangrui Song
Date: 2020-02-06T01:11:58-08:00
New Revision: a29a9a34f49fe674b259ef06a5044684401233a9

URL: https://github.com/llvm/llvm-project/commit/a29a9a34f49fe674b259ef06a5044684401233a9
DIFF: https://github.com/llvm/llvm-project/commit/a29a9a34f49fe674b259ef06a5044684401233a9.diff

LOG: [yaml2obj] Refactor command line parsing

* Hide unrelated options.
* Add "OVERVIEW: " to yaml2obj -h/--help.
* Place options under a yaml2obj category.
* Disallow -docnum. Currently -docnum is the only yaml2obj specific long option that is affected.
* Specify `cl::init("-")` and `cl::Prefix` for OutputFilename. The
  latter allows `-ofile`

Reviewed By: grimar, jhenderson

Differential Revision: https://reviews.llvm.org/D73982

Added: 
    llvm/test/tools/yaml2obj/help.test
    llvm/test/tools/yaml2obj/output-file.yaml

Modified: 
    llvm/tools/yaml2obj/yaml2obj.cpp

Removed: 
    llvm/test/tools/yaml2obj/invalid-output-file.yaml


################################################################################
diff  --git a/llvm/test/tools/yaml2obj/help.test b/llvm/test/tools/yaml2obj/help.test
new file mode 100644
index 000000000000..2e82c1278c43
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/help.test
@@ -0,0 +1,11 @@
+## Show that help text is printed correctly when requested.
+
+# RUN: yaml2obj -h | FileCheck %s --check-prefixes=CHECK,CATEG --implicit-check-not=Options:
+# RUN: yaml2obj --help | FileCheck %s --check-prefixes=CHECK,CATEG --implicit-check-not=Options:
+# RUN: yaml2obj --help-list | FileCheck %s --implicit-check-not=Options:
+
+# CHECK: OVERVIEW: Create an object file from a YAML description
+# CHECK: USAGE: yaml2obj{{(.exe)?}} [options] <input file>{{$}}
+# CHECK: OPTIONS:
+# CATEG: Generic Options:
+# CATEG: yaml2obj Options:

diff  --git a/llvm/test/tools/yaml2obj/invalid-output-file.yaml b/llvm/test/tools/yaml2obj/invalid-output-file.yaml
deleted file mode 100644
index 7b76fe1c1a0e..000000000000
--- a/llvm/test/tools/yaml2obj/invalid-output-file.yaml
+++ /dev/null
@@ -1,4 +0,0 @@
-# RUN: not yaml2obj -o %p/path/does/not/exist 2>&1 | FileCheck %s
-
-## Don't check the OS-dependent message "No such file or directory".
-# CHECK: yaml2obj: error: failed to open '{{.*}}/path/does/not/exist': {{.*}}

diff  --git a/llvm/test/tools/yaml2obj/output-file.yaml b/llvm/test/tools/yaml2obj/output-file.yaml
new file mode 100644
index 000000000000..e900291fc4c0
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/output-file.yaml
@@ -0,0 +1,19 @@
+## Test that -o sets the output file name.
+
+# RUN: rm -f %t
+# RUN: yaml2obj %s -o %t
+# RUN: ls %t
+# RUN: rm -f %t
+# RUN: yaml2obj %s -o%t
+# RUN: ls %t
+
+# RUN: not yaml2obj -o %p/path/does/not/exist 2>&1 | FileCheck %s
+
+# CHECK: yaml2obj: error: failed to open '{{.*}}/path/does/not/exist': {{[Nn]}}o such file or directory
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_NONE

diff  --git a/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp
index bf8081b8357f..dd92e01f479a 100644
--- a/llvm/tools/yaml2obj/yaml2obj.cpp
+++ b/llvm/tools/yaml2obj/yaml2obj.cpp
@@ -28,22 +28,28 @@
 
 using namespace llvm;
 
-static cl::opt<std::string>
-  Input(cl::Positional, cl::desc("<input>"), cl::init("-"));
+namespace {
+cl::OptionCategory Cat("yaml2obj Options");
 
-static cl::opt<unsigned>
+cl::opt<std::string> Input(cl::Positional, cl::desc("<input file>"),
+                           cl::init("-"), cl::cat(Cat));
+
+cl::opt<unsigned>
     DocNum("docnum", cl::init(1),
-           cl::desc("Read specified document from input (default = 1)"));
+           cl::desc("Read specified document from input (default = 1)"),
+           cl::cat(Cat));
 
-static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
-                                           cl::value_desc("filename"));
+cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
+                                    cl::value_desc("filename"), cl::init("-"),
+                                    cl::Prefix, cl::cat(Cat));
+} // namespace
 
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
-  cl::ParseCommandLineOptions(argc, argv);
-
-  if (OutputFilename.empty())
-    OutputFilename = "-";
+  cl::HideUnrelatedOptions(Cat);
+  cl::ParseCommandLineOptions(
+      argc, argv, "Create an object file from a YAML description", nullptr,
+      nullptr, /*LongOptionsUseDoubleDash=*/true);
 
   auto ErrHandler = [](const Twine &Msg) {
     WithColor::error(errs(), "yaml2obj") << Msg << "\n";


        


More information about the llvm-commits mailing list