Index: llvm/test/tools/llvm-readobj/basic.test =================================================================== --- llvm/test/tools/llvm-readobj/basic.test +++ llvm/test/tools/llvm-readobj/basic.test @@ -1,11 +1,11 @@ # Test case where input file does not exit. RUN: not llvm-readobj %t.blah 2>&1 | FileCheck --check-prefix=ENOENT %s -ENOENT: {{.*}}.blah: {{[Nn]}}o such file or directory +ENOENT: '{{.*}}.blah': {{[Nn]}}o such file or directory # Test case where input file is too small to be a recognised object file. RUN: touch %t.empty RUN: not llvm-readobj %t.empty 2>&1 | FileCheck --check-prefix=EMPTY %s -EMPTY: {{.*}}.empty: The file was not recognized as a valid object file +EMPTY: '{{.*}}.empty': The file was not recognized as a valid object file # Test that unrecognised files in archives are ignored. RUN: rm -f %t.a Index: llvm/test/tools/llvm-readobj/codeview-merging-cycle.test =================================================================== --- llvm/test/tools/llvm-readobj/codeview-merging-cycle.test +++ llvm/test/tools/llvm-readobj/codeview-merging-cycle.test @@ -1,6 +1,6 @@ ; RUN: not llvm-readobj -codeview-merged-types %S/Inputs/codeview-cycle.obj 2>&1 | FileCheck %s -; CHECK: Error{{.*}} Input type graph contains cycles +; CHECK: error{{.*}} Input type graph contains cycles ; To reproduce codeview-cycle.obj: ; $ cat codeview-cycle.asm Index: llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test =================================================================== --- llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test +++ llvm/test/tools/llvm-readobj/elf-dynamic-malformed.test @@ -3,7 +3,7 @@ # RUN: not llvm-readobj --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE # RUN: not llvm-readelf --dynamic-table %t.bad-size 2>&1 | FileCheck %s --check-prefix ERR-SIZE -# ERR-SIZE: Error reading file: Invalid entity size. +# ERR-SIZE: {{.+}} Invalid entity size --- !ELF FileHeader: @@ -69,7 +69,7 @@ # RUN: not llvm-readobj --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING # RUN: not llvm-readelf --dynamic-table %t.bad-string 2>&1 | FileCheck %s --check-prefix BAD-STRING -# BAD-STRING: Error reading file: Invalid dynamic string table reference. +# BAD-STRING: Invalid dynamic string table reference --- !ELF FileHeader: Index: llvm/test/tools/llvm-readobj/elf-malformed-pt-dynamic.test =================================================================== --- llvm/test/tools/llvm-readobj/elf-malformed-pt-dynamic.test +++ llvm/test/tools/llvm-readobj/elf-malformed-pt-dynamic.test @@ -21,7 +21,7 @@ # RUN: %python -c "with open(r'%t.truncated2', 'r+') as f: f.truncate(0xFFF)" # RUN: not llvm-readobj %t.truncated2 --dynamic-table 2>&1 | FileCheck %s -# CHECK: Error reading file: Invalid data was encountered while parsing the file. +# CHECK: error: Invalid data was encountered while parsing the file --- !ELF FileHeader: Index: llvm/test/tools/llvm-readobj/elf-packed-relocs-error1.s =================================================================== --- llvm/test/tools/llvm-readobj/elf-packed-relocs-error1.s +++ llvm/test/tools/llvm-readobj/elf-packed-relocs-error1.s @@ -1,7 +1,7 @@ // REQUIRES: x86-registered-target // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s -// CHECK: Error reading file: invalid packed relocation header +// CHECK: error: invalid packed relocation header .section .rela.dyn, "a", @0x60000001 .ascii "APS9" Index: llvm/test/tools/llvm-readobj/elf-packed-relocs-error2.s =================================================================== --- llvm/test/tools/llvm-readobj/elf-packed-relocs-error2.s +++ llvm/test/tools/llvm-readobj/elf-packed-relocs-error2.s @@ -1,7 +1,7 @@ // REQUIRES: x86-registered-target // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s -// CHECK: Error reading file: malformed sleb128, extends past end +// CHECK: error: malformed sleb128, extends past end .section .rela.dyn, "a", @0x60000001 .ascii "APS2" Index: llvm/test/tools/llvm-readobj/elf-packed-relocs-error3.s =================================================================== --- llvm/test/tools/llvm-readobj/elf-packed-relocs-error3.s +++ llvm/test/tools/llvm-readobj/elf-packed-relocs-error3.s @@ -1,7 +1,7 @@ // REQUIRES: x86-registered-target // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s -// CHECK: Error reading file: malformed sleb128, extends past end +// CHECK: error: malformed sleb128, extends past end .section .rela.dyn, "a", @0x60000001 .ascii "APS2" Index: llvm/test/tools/llvm-readobj/elf-packed-relocs-error4.s =================================================================== --- llvm/test/tools/llvm-readobj/elf-packed-relocs-error4.s +++ llvm/test/tools/llvm-readobj/elf-packed-relocs-error4.s @@ -1,7 +1,7 @@ // REQUIRES: x86-registered-target // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s -// CHECK: Error reading file: malformed sleb128, extends past end +// CHECK: error: malformed sleb128, extends past end .section .rela.dyn, "a", @0x60000001 .ascii "APS2" Index: llvm/test/tools/llvm-readobj/elf-packed-relocs-error5.s =================================================================== --- llvm/test/tools/llvm-readobj/elf-packed-relocs-error5.s +++ llvm/test/tools/llvm-readobj/elf-packed-relocs-error5.s @@ -1,7 +1,7 @@ // REQUIRES: x86-registered-target // RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | not llvm-readobj -relocations - 2>&1 | FileCheck %s -// CHECK: Error reading file: relocation group unexpectedly large +// CHECK: error: relocation group unexpectedly large .section .rela.dyn, "a", @0x60000001 .ascii "APS2" Index: llvm/test/tools/llvm-readobj/mips-got.test =================================================================== --- llvm/test/tools/llvm-readobj/mips-got.test +++ llvm/test/tools/llvm-readobj/mips-got.test @@ -30,7 +30,7 @@ RUN: --elf-output-style=GNU | \ RUN: FileCheck %s -check-prefix GNU-GOT-STATIC -GOT-OBJ: Error reading file: Cannot find .got section. +GOT-OBJ: error: Cannot find .got section GOT-EXE: Primary GOT { GOT-EXE-NEXT: Canonical gp value: 0x418880 @@ -386,7 +386,7 @@ GOT-STATIC-NEXT: ] GOT-STATIC-NEXT: } -GNU-GOT-OBJ: Error reading file: Cannot find .got section. +GNU-GOT-OBJ: error: Cannot find .got section GNU-GOT-EXE: Primary GOT: GNU-GOT-EXE-NEXT: Canonical gp value: 00418880 Index: llvm/test/tools/llvm-readobj/string-dump.test =================================================================== --- llvm/test/tools/llvm-readobj/string-dump.test +++ llvm/test/tools/llvm-readobj/string-dump.test @@ -19,13 +19,14 @@ # CHECK-NEXT: [ 3] null{{$}} # CHECK-NOT: {{.}} -# RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR -# RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: not llvm-readobj --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1 +# RUN: not llvm-readobj --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2 -# RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR -# RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR +# RUN: not llvm-readelf --string-dump=does_not_exist %t 2>&1 | FileCheck %s --check-prefix=ERR1 +# RUN: not llvm-readelf --string-dump=42 %t 2>&1 | FileCheck %s --check-prefix=ERR2 -# ERR: Error reading file: invalid section reference. +# ERR1: error: could not find section 'does_not_exist' +# ERR2: error: could not find section '42' --- !ELF FileHeader: Index: llvm/test/tools/llvm-readobj/thin-archive-paths.test =================================================================== --- llvm/test/tools/llvm-readobj/thin-archive-paths.test +++ llvm/test/tools/llvm-readobj/thin-archive-paths.test @@ -16,7 +16,7 @@ # RUN: rm a/b/1.o # RUN: not llvm-readobj --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1 # RUN: not llvm-readelf --file-headers a/relative.a 2>&1 | FileCheck %s --check-prefix=ERR1 -# ERR1: Error reading file: a/relative.a: 'b/1.o': {{[Nn]}}o such file or directory +# ERR1: error: 'a/relative.a': 'b/1.o': {{[Nn]}}o such file or directory # RUN: mkdir -p %t/c # RUN: cp %p/Inputs/trivial.obj.elf-x86-64 a/b/1.o @@ -30,4 +30,4 @@ # RUN: rm a/b/1.o # RUN: not llvm-readobj --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t # RUN: not llvm-readelf --file-headers %t/c/absolute.a 2>&1 | FileCheck %s --check-prefix=ERR2 -DDIR=%t -# ERR2: Error reading file: [[DIR]]/c/absolute.a: '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory +# ERR2: error: '[[DIR]]/c/absolute.a': '[[DIR]]/a/b/1.o': {{[Nn]}}o such file or directory Index: llvm/test/tools/llvm-readobj/thin-archive.test =================================================================== --- llvm/test/tools/llvm-readobj/thin-archive.test +++ llvm/test/tools/llvm-readobj/thin-archive.test @@ -67,4 +67,4 @@ # MISSING: File: {{.*}}1.o # MISSING: SectionHeaderCount: 10 # MISSING-NOT: File: {{.*}}3.o -# ERR: Error reading file: {{.*}}.a: '{{.*}}2.o': {{[Nn]}}o such file or directory +# ERR: error: '{{.*}}.a': '{{.*}}2.o': {{[Nn]}}o such file or directory Index: llvm/test/tools/llvm-readobj/wasm-invalid.test =================================================================== --- llvm/test/tools/llvm-readobj/wasm-invalid.test +++ llvm/test/tools/llvm-readobj/wasm-invalid.test @@ -4,4 +4,4 @@ FileHeader: Version: 0x0000000c -# CHECK: Error reading file: : Bad version number +# CHECK: error: '': Bad version number Index: llvm/tools/llvm-readobj/ObjDumper.cpp =================================================================== --- llvm/tools/llvm-readobj/ObjDumper.cpp +++ llvm/tools/llvm-readobj/ObjDumper.cpp @@ -16,6 +16,7 @@ #include "llvm-readobj.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Error.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" @@ -54,8 +55,9 @@ SecIndex++; } - return make_error("invalid section reference", - object::object_error::parse_failed); + return make_error( + formatv("could not find section '{0}'", SecName), + object::object_error::parse_failed); } void ObjDumper::printSectionAsString(const object::ObjectFile *Obj, Index: llvm/tools/llvm-readobj/llvm-readobj.cpp =================================================================== --- llvm/tools/llvm-readobj/llvm-readobj.cpp +++ llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -39,6 +39,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/WithColor.h" using namespace llvm; using namespace llvm::object; @@ -367,16 +368,20 @@ namespace llvm { LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { - errs() << "\nError reading file: " << Msg << ".\n"; + errs() << "\n"; + WithColor::error(errs()) << Msg; + errs() << "\n"; errs().flush(); exit(1); } -void error(Error EC) { - if (!EC) +void error(Error Err) { + if (!Err) return; - handleAllErrors(std::move(EC), - [&](const ErrorInfoBase &EI) { reportError(EI.message()); }); + handleAllErrors(std::move(Err), + [](const ErrorInfoBase &EI) { + reportError(EI.message()); + }); } void error(std::error_code EC) { @@ -391,22 +396,14 @@ } // namespace llvm -static void reportError(StringRef Input, std::error_code EC) { +static void reportError(StringRef Input, Error Err) { if (Input == "-") Input = ""; - - reportError(Twine(Input) + ": " + EC.message()); + error(createFileError(Input, std::move(Err))); } -static void reportError(StringRef Input, Error Err) { - if (Input == "-") - Input = ""; - std::string ErrMsg; - { - raw_string_ostream ErrStream(ErrMsg); - logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": "); - } - reportError(ErrMsg); +static void reportError(StringRef Input, std::error_code EC) { + reportError(Input, errorCodeToError(EC)); } static bool isMipsArch(unsigned Arch) {