diff options
Diffstat (limited to 'llvm/tools/llvm-dis/llvm-dis.cpp')
-rw-r--r-- | llvm/tools/llvm-dis/llvm-dis.cpp | 100 |
1 files changed, 59 insertions, 41 deletions
diff --git a/llvm/tools/llvm-dis/llvm-dis.cpp b/llvm/tools/llvm-dis/llvm-dis.cpp index d66299cbf76..a0d9996da86 100644 --- a/llvm/tools/llvm-dis/llvm-dis.cpp +++ b/llvm/tools/llvm-dis/llvm-dis.cpp @@ -158,53 +158,71 @@ int main(int argc, char **argv) { std::unique_ptr<MemoryBuffer> MB = ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); - std::unique_ptr<Module> M = ExitOnErr(getLazyBitcodeModule( - *MB, Context, /*ShouldLazyLoadMetadata=*/true, SetImporting)); - if (MaterializeMetadata) - ExitOnErr(M->materializeMetadata()); - else - ExitOnErr(M->materializeAll()); - - BitcodeLTOInfo LTOInfo = ExitOnErr(getBitcodeLTOInfo(*MB)); - std::unique_ptr<ModuleSummaryIndex> Index; - if (LTOInfo.HasSummary) - Index = ExitOnErr(getModuleSummaryIndex(*MB)); - - // Just use stdout. We won't actually print anything on it. - if (DontPrint) - OutputFilename = "-"; - - if (OutputFilename.empty()) { // Unspecified output, infer it. - if (InputFilename == "-") { - OutputFilename = "-"; + + BitcodeFileContents IF = ExitOnErr(llvm::getBitcodeFileContents(*MB)); + + const size_t N = IF.Mods.size(); + + if (OutputFilename == "-" && N > 1) + errs() << "only single module bitcode files can be written to stdout\n"; + + for (size_t i = 0; i < N; ++i) { + BitcodeModule MB = IF.Mods[i]; + std::unique_ptr<Module> M = ExitOnErr(MB.getLazyModule(Context, MaterializeMetadata, + SetImporting)); + if (MaterializeMetadata) + ExitOnErr(M->materializeMetadata()); + else + ExitOnErr(M->materializeAll()); + + BitcodeLTOInfo LTOInfo = ExitOnErr(MB.getLTOInfo()); + std::unique_ptr<ModuleSummaryIndex> Index; + if (LTOInfo.HasSummary) + Index = ExitOnErr(MB.getSummary()); + + std::string FinalFilename(OutputFilename); + + // Just use stdout. We won't actually print anything on it. + if (DontPrint) + FinalFilename = "-"; + + if (OutputFilename.empty()) { // Unspecified output, infer it. + if (InputFilename == "-") { + FinalFilename = "-"; + } else { + StringRef IFN = InputFilename; + FinalFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str(); + if (N > 1) + FinalFilename += std::string(".") + std::to_string(i); + FinalFilename += ".ll"; + } } else { - StringRef IFN = InputFilename; - OutputFilename = (IFN.endswith(".bc") ? IFN.drop_back(3) : IFN).str(); - OutputFilename += ".ll"; + if (N > 1) + FinalFilename += std::string(".") + std::to_string(i); } - } - std::error_code EC; - std::unique_ptr<ToolOutputFile> Out( - new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text)); - if (EC) { - errs() << EC.message() << '\n'; - return 1; - } + std::error_code EC; + std::unique_ptr<ToolOutputFile> Out( + new ToolOutputFile(FinalFilename, EC, sys::fs::OF_Text)); + if (EC) { + errs() << EC.message() << '\n'; + return 1; + } - std::unique_ptr<AssemblyAnnotationWriter> Annotator; - if (ShowAnnotations) - Annotator.reset(new CommentWriter()); + std::unique_ptr<AssemblyAnnotationWriter> Annotator; + if (ShowAnnotations) + Annotator.reset(new CommentWriter()); - // All that llvm-dis does is write the assembly to a file. - if (!DontPrint) { - M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder); - if (Index) - Index->print(Out->os()); - } + // All that llvm-dis does is write the assembly to a file. + if (!DontPrint) { + M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder); + if (Index) + Index->print(Out->os()); + } - // Declare success. - Out->keep(); + // Declare success. + Out->keep(); + } return 0; } |