[llvm] [llvm-readobj][COFF] Add JSON Output Style (PR #95074)

Miguel A. Arroyo via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 07:35:06 PDT 2024


https://github.com/mayanez updated https://github.com/llvm/llvm-project/pull/95074

>From ea29a03aeb099d3394afe0ce0ad74e4f29074ee7 Mon Sep 17 00:00:00 2001
From: Miguel Arroyo <miguel.arroyo at rockstargames.com>
Date: Tue, 11 Jun 2024 20:22:52 -0700
Subject: [PATCH] [llvm-readobj][COFF] Add --coff-output-style flag

---
 llvm/docs/CommandGuide/llvm-readobj.rst             | 11 +++++++++++
 llvm/docs/ReleaseNotes.rst                          |  3 +++
 llvm/test/tools/llvm-readobj/COFF/output-style.test |  4 ++++
 llvm/tools/llvm-readobj/Opts.td                     |  1 +
 llvm/tools/llvm-readobj/llvm-readobj.cpp            | 12 ++++++++++++
 5 files changed, 31 insertions(+)
 create mode 100644 llvm/test/tools/llvm-readobj/COFF/output-style.test

diff --git a/llvm/docs/CommandGuide/llvm-readobj.rst b/llvm/docs/CommandGuide/llvm-readobj.rst
index 8bd29eafbbfcf..024cfcca50489 100644
--- a/llvm/docs/CommandGuide/llvm-readobj.rst
+++ b/llvm/docs/CommandGuide/llvm-readobj.rst
@@ -347,10 +347,21 @@ The following options are implemented only for the PE/COFF file format.
 
  Display the load config.
 
+.. option:: --coff-output-style=<value>
+
+ Format COFF information in the specified style. Valid options are ``LLVM``,
+ and ``JSON``. ``LLVM`` output (the default) is an expanded and
+ structured format. ``JSON`` is JSON formatted output intended for machine consumption.
+
 .. option:: --coff-resources
 
  Display the .rsrc section.
 
+.. option:: --pretty-print
+
+ When used with :option:`--coff-output-style`, JSON output will be formatted in
+ a more readable format.
+
 XCOFF SPECIFIC OPTIONS
 ----------------------
 
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 8cdb9db087c77..1ef2961aa252b 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -286,6 +286,9 @@ Changes to the LLVM tools
   now has a map for the mapped files. (`#92835
   <https://github.com/llvm/llvm-project/pull/92835>`).
 
+* llvm-readobj now supports ``--coff-output-style=<value>``. Valid options are ``LLVM``,
+  and ``JSON``. (`#95074 <https://github.com/llvm/llvm-project/pull/95074>`)
+
 Changes to LLDB
 ---------------------------------
 
diff --git a/llvm/test/tools/llvm-readobj/COFF/output-style.test b/llvm/test/tools/llvm-readobj/COFF/output-style.test
new file mode 100644
index 0000000000000..70a1d5112b05d
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/COFF/output-style.test
@@ -0,0 +1,4 @@
+## Error for an unknown output style.
+RUN: not llvm-readobj --coff-output-style=unknown 2>&1 | FileCheck %s
+
+CHECK: error: --coff-output-style value should be either 'LLVM' or 'JSON', but was 'unknown'
\ No newline at end of file
diff --git a/llvm/tools/llvm-readobj/Opts.td b/llvm/tools/llvm-readobj/Opts.td
index 7d574d875d22e..2b752b8f64a3c 100644
--- a/llvm/tools/llvm-readobj/Opts.td
+++ b/llvm/tools/llvm-readobj/Opts.td
@@ -87,6 +87,7 @@ def coff_imports : FF<"coff-imports", "Display import table">, Group<grp_coff>;
 def coff_load_config : FF<"coff-load-config", "Display load config">, Group<grp_coff>;
 def coff_resources : FF<"coff-resources", "Display .rsrc section">, Group<grp_coff>;
 def coff_tls_directory : FF<"coff-tls-directory", "Display TLS directory">, Group<grp_coff>;
+defm coff_output_style: Eq<"coff-output-style", "Specify COFF dump style: LLVM, JSON">, Group<grp_coff>;
 
 // XCOFF specific options.
 def grp_xcoff : OptionGroup<"kind">, HelpText<"OPTIONS (XCOFF specific)">;
diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp
index 9ac324cc672f0..98f0ff6b107d0 100644
--- a/llvm/tools/llvm-readobj/llvm-readobj.cpp
+++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp
@@ -309,6 +309,18 @@ static void parseOptions(const opt::InputArgList &Args) {
   opts::COFFLoadConfig = Args.hasArg(OPT_coff_load_config);
   opts::COFFResources = Args.hasArg(OPT_coff_resources);
   opts::COFFTLSDirectory = Args.hasArg(OPT_coff_tls_directory);
+  if (Arg *A = Args.getLastArg(OPT_coff_output_style_EQ)) {
+    std::string OutputStyleChoice = A->getValue();
+    opts::Output = StringSwitch<opts::OutputStyleTy>(OutputStyleChoice)
+                       .Case("LLVM", opts::OutputStyleTy::LLVM)
+                       .Case("JSON", opts::OutputStyleTy::JSON)
+                       .Default(opts::OutputStyleTy::UNKNOWN);
+    if (opts::Output == opts::OutputStyleTy::UNKNOWN) {
+      error("--coff-output-style value should be either 'LLVM' or "
+            "'JSON', but was '" +
+            OutputStyleChoice + "'");
+    }
+  }
 
   // XCOFF specific options.
   opts::XCOFFAuxiliaryHeader = Args.hasArg(OPT_auxiliary_header);



More information about the llvm-commits mailing list