[llvm] [llvm-dwarfdump] Added --output-style option for controlling output f… (PR #216559)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 03:59:46 PDT 2026


https://github.com/arrowten created https://github.com/llvm/llvm-project/pull/216559

The `--output-style` flag gives the option to print in JSON format or LLVM format. This specific PR just adds the flag option to `llvm-dwarfdump` with TODO for the JSON format, which I will be implementing in the next PRs. I have not added any specific testcase for this. The option has also been updated in `llvm-dwardump --help` section for the same.

>From 6001b4cea69bf033376996a122560f8427107021 Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Sun, 16 Aug 2026 06:45:03 -0400
Subject: [PATCH] [llvm-dwarfdump] Added --output-style option for controlling
 output format

---
 llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 28 +++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 4d7fc7e3108bf..5e0db8f84fc89 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -16,6 +16,7 @@
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVectorExtras.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/DebugInfo/DIContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
@@ -135,6 +136,8 @@ enum ErrorDetailLevel {
   Unspecified
 };
 
+enum OutputStyleTy { LLVM, JSON, UNKNOWN };
+
 OptionCategory DwarfDumpCategory("Specific Options");
 static list<std::string>
     InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
@@ -334,6 +337,11 @@ static opt<bool> Verbose("verbose",
                          cat(DwarfDumpCategory));
 static alias VerboseAlias("v", desc("Alias for --verbose."), aliasopt(Verbose),
                           cat(DwarfDumpCategory), cl::NotHidden);
+static opt<std::string> OutputStyleOpt(
+    "output-style", init("LLVM"),
+    desc("Specify the format of the output (LLVM or JSON)"),
+    cat(DwarfDumpCategory));
+static OutputStyleTy OutputStyle = LLVM;
 static opt<bool>
     ShowVariableCoverage("show-variable-coverage",
                          desc("Show per-variable coverage metrics."),
@@ -775,7 +783,14 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
   // Dump the complete DWARF structure.
   auto DumpOpts = getDumpOpts(DICtx);
   DumpOpts.GetNameForDWARFReg = GetRegName;
-  DICtx.dump(OS, DumpOpts, DumpOffsets);
+  switch (OutputStyle) {
+  case LLVM:
+    DICtx.dump(OS, DumpOpts, DumpOffsets);
+    break;
+  case JSON:
+    // TODO: Implement JSON output style.
+    break;
+  }
   return true;
 }
 
@@ -889,6 +904,17 @@ int main(int argc, char **argv) {
       "pretty-print DWARF debug information in object files"
       " and debug info archives.\n");
 
+  OutputStyle = StringSwitch<OutputStyleTy>(OutputStyleOpt)
+                    .Case("LLVM", LLVM)
+                    .Case("JSON", JSON)
+                    .Default(UNKNOWN);
+  if (OutputStyle == UNKNOWN) {
+    WithColor::error(errs(), sys::path::filename(argv[0]))
+        << "--output-style value should be either 'LLVM' or 'JSON', but was '"
+        << OutputStyleOpt << "'\n";
+    return 1;
+  }
+
   // FIXME: Audit interactions between these two options and make them
   //        compatible.
   if (Diff && Verbose) {



More information about the llvm-commits mailing list