X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=tools%2Fyaml2obj%2Fyaml2obj.cpp;h=af4d8689067240d3a65ce3fceb41ad03ddf0fe73;hb=c2e7b3e49549cc306a36a052b12b7b0fe0fb45b6;hp=6d1107c8581240ddadeb5c2b6c970b2aa97f6cd7;hpb=5918b7a03d4d6a52e18f7c102250c9cfd6ae52dd;p=oota-llvm.git diff --git a/tools/yaml2obj/yaml2obj.cpp b/tools/yaml2obj/yaml2obj.cpp index 6d1107c8581..af4d8689067 100644 --- a/tools/yaml2obj/yaml2obj.cpp +++ b/tools/yaml2obj/yaml2obj.cpp @@ -15,14 +15,17 @@ //===----------------------------------------------------------------------===// #include "yaml2obj.h" -#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/system_error.h" +#include using namespace llvm; @@ -50,6 +53,27 @@ cl::opt Format( clEnumValN(YOF_ELF, "elf", "ELF object file format"), clEnumValEnd)); +cl::opt +DocNum("docnum", cl::init(1), + cl::desc("Read specified document from input (default = 1)")); + +static cl::opt OutputFilename("o", cl::desc("Output filename"), + cl::value_desc("filename")); + +typedef int (*ConvertFuncPtr)(yaml::Input & YIn, raw_ostream &Out); + +static int convertYAML(yaml::Input &YIn, raw_ostream &Out, + ConvertFuncPtr Convert) { + unsigned CurDocNum = 0; + do { + if (++CurDocNum == DocNum) + return Convert(YIn, Out); + } while (YIn.nextDocument()); + + errs() << "yaml2obj: Cannot find the " << DocNum + << llvm::getOrdinalSuffix(DocNum) << " document\n"; + return 1; +} int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv); @@ -57,15 +81,37 @@ int main(int argc, char **argv) { PrettyStackTraceProgram X(argc, argv); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. - OwningPtr Buf; - if (MemoryBuffer::getFileOrSTDIN(Input, Buf)) + if (OutputFilename.empty()) + OutputFilename = "-"; + + std::error_code EC; + std::unique_ptr Out( + new tool_output_file(OutputFilename, EC, sys::fs::F_None)); + if (EC) { + errs() << EC.message() << '\n'; return 1; - if (Format == YOF_COFF) { - return yaml2coff(outs(), Buf.get()); - } else if (Format == YOF_ELF) { - return yaml2elf(outs(), Buf.get()); - } else { + } + + ErrorOr> Buf = + MemoryBuffer::getFileOrSTDIN(Input); + if (!Buf) + return 1; + + ConvertFuncPtr Convert = nullptr; + if (Format == YOF_COFF) + Convert = yaml2coff; + else if (Format == YOF_ELF) + Convert = yaml2elf; + else { errs() << "Not yet implemented\n"; return 1; } + + yaml::Input YIn(Buf.get()->getBuffer()); + + int Res = convertYAML(YIn, Out->os(), Convert); + if (Res == 0) + Out->keep(); + + return Res; }