Skip to content

Commit f9127d6

Browse files
committed
[llvm-pdbutil] Add output file option for pdb2yaml
1 parent 2e12bad commit f9127d6

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

llvm/tools/llvm-pdbutil/YAMLOutputStyle.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ static bool checkModuleSubsection(opts::ModuleSubsection MS) {
3737
});
3838
}
3939

40-
YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
41-
: File(File), Out(outs()), Obj(File.getAllocator()) {
40+
YAMLOutputStyle::YAMLOutputStyle(PDBFile &File, raw_ostream &OS)
41+
: File(File), Out(OS), Obj(File.getAllocator()) {
4242
Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
4343
}
4444

45+
YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
46+
: YAMLOutputStyle::YAMLOutputStyle(File, llvm::outs()) {}
47+
4548
Error YAMLOutputStyle::dump() {
4649
if (opts::pdb2yaml::StreamDirectory)
4750
opts::pdb2yaml::StreamMetadata = true;

llvm/tools/llvm-pdbutil/YAMLOutputStyle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace pdb {
2121
class YAMLOutputStyle : public OutputStyle {
2222
public:
2323
YAMLOutputStyle(PDBFile &File);
24-
24+
YAMLOutputStyle(PDBFile &File, raw_ostream &Output);
2525
Error dump() override;
2626

2727
private:

llvm/tools/llvm-pdbutil/llvm-pdbutil.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
718718
cl::list<std::string> InputFilename(cl::Positional,
719719
cl::desc("<input PDB file>"), cl::Required,
720720
cl::sub(PdbToYamlSubcommand));
721+
cl::opt<std::string> PdbYamlOutputFile(
722+
"yaml", cl::desc("the name of the yaml file to write (default stdout)"),
723+
cl::sub(PdbToYamlSubcommand));
724+
721725
} // namespace pdb2yaml
722726

723727
namespace merge {
@@ -897,8 +901,35 @@ static void pdb2Yaml(StringRef Path) {
897901
std::unique_ptr<IPDBSession> Session;
898902
auto &File = loadPDB(Path, Session);
899903

900-
auto O = std::make_unique<YAMLOutputStyle>(File);
901-
904+
std::unique_ptr<YAMLOutputStyle> O;
905+
std::unique_ptr<raw_fd_ostream> OutputFile;
906+
if (!opts::pdb2yaml::PdbYamlOutputFile.empty()) {
907+
std::error_code EC;
908+
if (!sys::fs::exists(opts::pdb2yaml::PdbYamlOutputFile)) {
909+
auto ParentPath =
910+
sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile);
911+
if (!ParentPath.empty()) {
912+
EC = sys::fs::create_directories(
913+
sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile));
914+
if (EC) {
915+
errs() << "Error creating directory: "
916+
<< sys::path::parent_path(opts::pdb2yaml::PdbYamlOutputFile)
917+
<< "\n";
918+
exit(1);
919+
}
920+
}
921+
}
922+
OutputFile = std::make_unique<raw_fd_ostream>(
923+
opts::pdb2yaml::PdbYamlOutputFile, EC, sys::fs::FA_Write);
924+
if (EC || !OutputFile) {
925+
errs() << "Error opening file for writing: "
926+
<< opts::pdb2yaml::PdbYamlOutputFile << "\n";
927+
exit(1);
928+
}
929+
O = std::make_unique<YAMLOutputStyle>(File, *OutputFile);
930+
} else {
931+
O = std::make_unique<YAMLOutputStyle>(File);
932+
}
902933
ExitOnErr(O->dump());
903934
}
904935

0 commit comments

Comments
 (0)