[llvm] [llvm-pdbutil] Add output file option for pdb2yaml (PR #82300)

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 19:03:52 PST 2024


https://github.com/nikitalita created https://github.com/llvm/llvm-project/pull/82300

Adds `--yaml=<filename>` option to `pdb2yaml`. This is added primarily because we can't always count on having a utf-8 shell to redirect our output from, and if the shell is UTF-16 (like certain Windows shells), we'll output garbage.

>From bbf8986b19d7d3e54a99566fe2e9eb884fbf5f4f Mon Sep 17 00:00:00 2001
From: nikitalita <69168929+nikitalita at users.noreply.github.com>
Date: Thu, 2 Nov 2023 05:10:17 -0700
Subject: [PATCH] [llvm-pdbutil] Add output file option for pdb2yaml

---
 llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp |  7 +++--
 llvm/tools/llvm-pdbutil/YAMLOutputStyle.h   |  2 +-
 llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp    | 35 +++++++++++++++++++--
 3 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
index 80b76657facc7c..10700aadfabc56 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp
@@ -37,11 +37,14 @@ static bool checkModuleSubsection(opts::ModuleSubsection MS) {
                 });
 }
 
-YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
-    : File(File), Out(outs()), Obj(File.getAllocator()) {
+YAMLOutputStyle::YAMLOutputStyle(PDBFile &File, raw_ostream &OS)
+    : File(File), Out(OS), Obj(File.getAllocator()) {
   Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
 }
 
+YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
+    : YAMLOutputStyle::YAMLOutputStyle(File, llvm::outs()) {}
+
 Error YAMLOutputStyle::dump() {
   if (opts::pdb2yaml::StreamDirectory)
     opts::pdb2yaml::StreamMetadata = true;
diff --git a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
index 5d53e0b65d03c9..b8badecfca405a 100644
--- a/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
+++ b/llvm/tools/llvm-pdbutil/YAMLOutputStyle.h
@@ -21,7 +21,7 @@ namespace pdb {
 class YAMLOutputStyle : public OutputStyle {
 public:
   YAMLOutputStyle(PDBFile &File);
-
+  YAMLOutputStyle(PDBFile &File, raw_ostream &Output);
   Error dump() override;
 
 private:
diff --git a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
index fa870666fe61a3..e1336501103dfc 100644
--- a/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
+++ b/llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp
@@ -716,6 +716,10 @@ cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
 cl::list<std::string> InputFilename(cl::Positional,
                                     cl::desc("<input PDB file>"), cl::Required,
                                     cl::sub(PdbToYamlSubcommand));
+cl::opt<std::string> PdbYamlOutputFile(
+    "yaml", cl::desc("the name of the yaml file to write (default stdout)"),
+    cl::sub(PdbToYamlSubcommand));
+
 } // namespace pdb2yaml
 
 namespace merge {
@@ -893,8 +897,35 @@ static void pdb2Yaml(StringRef Path) {
   std::unique_ptr<IPDBSession> Session;
   auto &File = loadPDB(Path, Session);
 
-  auto O = std::make_unique<YAMLOutputStyle>(File);
-
+  std::unique_ptr<YAMLOutputStyle> O;
+  std::unique_ptr<raw_fd_ostream> OutputFile;
+  if (!opts::pdb2yaml::PdbYamlOutputFile.empty()) {
+    std::error_code EC;
+    if (!sys::fs::exists(opts::pdb2yaml::PdbYamlOutputFile)) {
+      auto ParentPath =
+          sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile);
+      if (!ParentPath.empty()) {
+        EC = sys::fs::create_directories(
+            sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile));
+        if (EC) {
+          errs() << "Error creating directory: "
+                 << sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile)
+                 << "\n";
+          exit(1);
+        }
+      }
+    }
+    OutputFile = std::make_unique<raw_fd_ostream>(
+        opts::pdb2yaml::PdbYamlOutputFile, EC, sys::fs::FA_Write);
+    if (EC || !OutputFile) {
+      errs() << "Error opening file for writing: "
+             << opts::pdb2yaml::PdbYamlOutputFile << "\n";
+      exit(1);
+    }
+    O = std::make_unique<YAMLOutputStyle>(File, *OutputFile);
+  } else {
+    O = std::make_unique<YAMLOutputStyle>(File);
+  }
   ExitOnErr(O->dump());
 }
 



More information about the llvm-commits mailing list