From 1cf2b03af49f2a5bef8ae521a8a9df9c30cc17cd Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Thu, 25 Jun 2015 23:40:15 +0000 Subject: [PATCH] Make llvm-dwarfdump exit with non-zero exit code if error was occured. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240729 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/CommandGuide/llvm-dwarfdump.rst | 4 ++-- test/DebugInfo/dwarfdump-invalid.test | 6 +++--- tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 21 +++++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/docs/CommandGuide/llvm-dwarfdump.rst b/docs/CommandGuide/llvm-dwarfdump.rst index afaa0be3819..30c18adb771 100644 --- a/docs/CommandGuide/llvm-dwarfdump.rst +++ b/docs/CommandGuide/llvm-dwarfdump.rst @@ -26,5 +26,5 @@ OPTIONS EXIT STATUS ----------- -:program:`llvm-dwarfdump` returns 0. Other exit codes imply internal -program error. +:program:`llvm-dwarfdump` returns 0 if the input files were parsed and dumped +successfully. Otherwise, it returns 1. diff --git a/test/DebugInfo/dwarfdump-invalid.test b/test/DebugInfo/dwarfdump-invalid.test index da5b23e30ce..a36ad2f412e 100644 --- a/test/DebugInfo/dwarfdump-invalid.test +++ b/test/DebugInfo/dwarfdump-invalid.test @@ -1,6 +1,6 @@ ; Verify that llvm-dwarfdump doesn't crash on broken input files. -RUN: llvm-dwarfdump %p/Inputs/invalid.elf 2>&1 | FileCheck %s --check-prefix=INVALID-ELF -RUN: llvm-dwarfdump %p/Inputs/invalid.elf.2 2>&1 | FileCheck %s --check-prefix=INVALID-ELF -RUN: llvm-dwarfdump %p/Inputs/invalid.elf.3 2>&1 | FileCheck %s --check-prefix=INVALID-ELF +RUN: not llvm-dwarfdump %p/Inputs/invalid.elf 2>&1 | FileCheck %s --check-prefix=INVALID-ELF +RUN: not llvm-dwarfdump %p/Inputs/invalid.elf.2 2>&1 | FileCheck %s --check-prefix=INVALID-ELF +RUN: not llvm-dwarfdump %p/Inputs/invalid.elf.3 2>&1 | FileCheck %s --check-prefix=INVALID-ELF INVALID-ELF: Invalid data was encountered while parsing the file diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index c1cb0218ac3..db3fcf6ee7b 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -69,22 +69,27 @@ DumpType("debug-dump", cl::init(DIDT_All), clEnumValN(DIDT_StrOffsetsDwo, "str_offsets.dwo", ".debug_str_offsets.dwo"), clEnumValEnd)); +static int ReturnValue = EXIT_SUCCESS; + +static bool error(StringRef Filename, std::error_code EC) { + if (!EC) + return false; + errs() << Filename << ": " << EC.message() << "\n"; + ReturnValue = EXIT_FAILURE; + return true; +} + static void DumpInput(StringRef Filename) { ErrorOr> BuffOrErr = MemoryBuffer::getFileOrSTDIN(Filename); - - if (std::error_code EC = BuffOrErr.getError()) { - errs() << Filename << ": " << EC.message() << "\n"; + if (error(Filename, BuffOrErr.getError())) return; - } std::unique_ptr Buff = std::move(BuffOrErr.get()); ErrorOr> ObjOrErr = ObjectFile::createObjectFile(Buff->getMemBufferRef()); - if (std::error_code EC = ObjOrErr.getError()) { - errs() << Filename << ": " << EC.message() << '\n'; + if (error(Filename, ObjOrErr.getError())) return; - } ObjectFile &Obj = *ObjOrErr.get(); std::unique_ptr DICtx(new DWARFContextInMemory(Obj)); @@ -109,5 +114,5 @@ int main(int argc, char **argv) { std::for_each(InputFilenames.begin(), InputFilenames.end(), DumpInput); - return 0; + return ReturnValue; } -- 2.34.1