diff options
author | Jonas Devlieghere <jonas@devlieghere.com> | 2018-11-11 22:12:04 +0000 |
---|---|---|
committer | Jonas Devlieghere <jonas@devlieghere.com> | 2018-11-11 22:12:04 +0000 |
commit | e787efd929ba66f5c4cbeff87207c48b0739e605 (patch) | |
tree | a99c1ba5cbb95fb299721a31802239fd9374acac | |
parent | bada60aa6b64b78a49e187c6aed78d9265d3df15 (diff) | |
download | bcm5719-llvm-e787efd929ba66f5c4cbeff87207c48b0739e605.tar.gz bcm5719-llvm-e787efd929ba66f5c4cbeff87207c48b0739e605.zip |
[llvm-objdump] Use WithColor for error reporting
Use helpers from Support/WithError.h to print errors.
llvm-svn: 346623
-rw-r--r-- | llvm/tools/llvm-objdump/COFFDump.cpp | 6 | ||||
-rw-r--r-- | llvm/tools/llvm-objdump/MachODump.cpp | 92 | ||||
-rw-r--r-- | llvm/tools/llvm-objdump/llvm-objdump.cpp | 65 |
3 files changed, 95 insertions, 68 deletions
diff --git a/llvm/tools/llvm-objdump/COFFDump.cpp b/llvm/tools/llvm-objdump/COFFDump.cpp index 3503eda42f2..a7ee49535d6 100644 --- a/llvm/tools/llvm-objdump/COFFDump.cpp +++ b/llvm/tools/llvm-objdump/COFFDump.cpp @@ -22,6 +22,7 @@ #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Format.h" #include "llvm/Support/Win64EH.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -579,8 +580,9 @@ static void printRuntimeFunctionRels(const COFFObjectFile *Obj, void llvm::printCOFFUnwindInfo(const COFFObjectFile *Obj) { if (Obj->getMachine() != COFF::IMAGE_FILE_MACHINE_AMD64) { - errs() << "Unsupported image machine type " - "(currently only AMD64 is supported).\n"; + WithColor::error(errs(), "llvm-objdump") + << "unsupported image machine type " + "(currently only AMD64 is supported).\n"; return; } diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp index 9d72d0f4b6a..6d4beb4378e 100644 --- a/llvm/tools/llvm-objdump/MachODump.cpp +++ b/llvm/tools/llvm-objdump/MachODump.cpp @@ -44,6 +44,7 @@ #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cstring> @@ -166,7 +167,7 @@ static const Target *GetTarget(const MachOObjectFile *MachOObj, if (*ThumbTarget) return TheTarget; - errs() << "llvm-objdump: error: unable to get target for '"; + WithColor::error(errs(), "llvm-objdump") << "unable to get target for '"; if (!TheTarget) errs() << TripleName; else @@ -483,7 +484,7 @@ static void PrintRType(const uint64_t cputype, const unsigned r_type) { "GOTLDP ", "GOTLDPOF", "PTRTGOT ", "TLVLDP ", "TLVLDPOF", "ADDEND ", " 11 (?) ", " 12 (?) ", " 13 (?) ", " 14 (?) ", " 15 (?) " }; - + if (r_type > 0xf){ outs() << format("%-7u", r_type) << " "; return; @@ -552,7 +553,7 @@ static void PrintRelocationEntries(const MachOObjectFile *O, bool previous_arm_half = false; bool previous_sectdiff = false; uint32_t sectdiff_r_type = 0; - + for (relocation_iterator Reloc = Begin; Reloc != End; ++Reloc) { const DataRefImpl Rel = Reloc->getRawDataRefImpl(); const MachO::any_relocation_info RE = O->getRelocation(Rel); @@ -567,7 +568,7 @@ static void PrintRelocationEntries(const MachOObjectFile *O, O->getScatteredRelocationValue(RE) : 0); const unsigned r_symbolnum = (r_scattered ? 0 : O->getPlainRelocationSymbolNum(RE)); - + if (r_scattered && cputype != MachO::CPU_TYPE_X86_64) { if (verbose) { // scattered: address @@ -578,20 +579,20 @@ static void PrintRelocationEntries(const MachOObjectFile *O, outs() << " "; else outs() << format("%08x ", (unsigned int)r_address); - + // scattered: pcrel if (r_pcrel) outs() << "True "; else outs() << "False "; - + // scattered: length PrintRLength(cputype, r_type, r_length, previous_arm_half); - + // scattered: extern & type outs() << "n/a "; PrintRType(cputype, r_type); - + // scattered: scattered & value outs() << format("True 0x%08x", (unsigned int)r_value); if (previous_sectdiff == false) { @@ -639,22 +640,22 @@ static void PrintRelocationEntries(const MachOObjectFile *O, outs() << " "; else outs() << format("%08x ", (unsigned int)r_address); - + // plain: pcrel if (r_pcrel) outs() << "True "; else outs() << "False "; - + // plain: length PrintRLength(cputype, r_type, r_length, previous_arm_half); - + if (r_extern) { // plain: extern & type & scattered outs() << "True "; PrintRType(cputype, r_type); outs() << "False "; - + // plain: symbolnum/value if (r_symbolnum > Symtab.nsyms) outs() << format("?(%d)\n", r_symbolnum); @@ -675,7 +676,7 @@ static void PrintRelocationEntries(const MachOObjectFile *O, outs() << "False "; PrintRType(cputype, r_type); outs() << "False "; - + // plain: symbolnum/value if (cputype == MachO::CPU_TYPE_ARM && r_type == llvm::MachO::ARM_RELOC_PAIR) @@ -1559,7 +1560,8 @@ static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) { if (none_of(ArchFlags, [&](const std::string &Name) { return Name == ArchFlagName; })) { - errs() << "llvm-objdump: " + Filename + ": No architecture specified.\n"; + WithColor::error(errs(), "llvm-objdump") + << Filename << ": no architecture specified.\n"; return false; } return true; @@ -1944,8 +1946,9 @@ static bool ValidateArchFlags() { ArchAll = true; } else { if (!MachOObjectFile::isValidArch(ArchFlags[i])) { - errs() << "llvm-objdump: Unknown architecture named '" + ArchFlags[i] + - "'for the -arch option\n"; + WithColor::error(errs(), "llvm-objdump") + << "unknown architecture named '" + ArchFlags[i] + + "'for the -arch option\n"; return false; } } @@ -2005,8 +2008,9 @@ void llvm::ParseInputMachO(StringRef Filename) { if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) ProcessMachO(Filename, MachOOF); else - errs() << "llvm-objdump: '" << Filename << "': " - << "Object is not a Mach-O file type.\n"; + WithColor::error(errs(), "llvm-objdump") + << Filename << "': " + << "object is not a Mach-O file type.\n"; return; } llvm_unreachable("Input object can't be invalid at this point"); @@ -2079,8 +2083,9 @@ void llvm::ParseInputMachO(MachOUniversalBinary *UB) { } } if (!ArchFound) { - errs() << "llvm-objdump: file: " + Filename + " does not contain " - << "architecture: " + ArchFlags[i] + "\n"; + WithColor::error(errs(), "llvm-objdump") + << "file: " + Filename + " does not contain " + << "architecture: " + ArchFlags[i] + "\n"; return; } } @@ -6184,8 +6189,9 @@ static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) { ScopedXarIter xi; if (!xi) { - errs() << "Can't obtain an xar iterator for xar archive " - << XarFilename << "\n"; + WithColor::error(errs(), "llvm-objdump") + << "can't obtain an xar iterator for xar archive " << XarFilename + << "\n"; return; } @@ -6193,8 +6199,9 @@ static void PrintXarFilesSummary(const char *XarFilename, xar_t xar) { for (xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)) { ScopedXarIter xp; if(!xp){ - errs() << "Can't obtain an xar iterator for xar archive " - << XarFilename << "\n"; + WithColor::error(errs(), "llvm-objdump") + << "can't obtain an xar iterator for xar archive " << XarFilename + << "\n"; return; } type = nullptr; @@ -6318,7 +6325,7 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, std::error_code XarEC = sys::fs::createTemporaryFile("llvm-objdump", "xar", FD, XarFilename); if (XarEC) { - errs() << XarEC.message() << "\n"; + WithColor::error(errs(), "llvm-objdump") << XarEC.message() << "\n"; return; } ToolOutputFile XarFile(XarFilename, FD); @@ -6331,7 +6338,8 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, ScopedXarFile xar(XarFilename.c_str(), READ); if (!xar) { - errs() << "Can't create temporary xar archive " << XarFilename << "\n"; + WithColor::error(errs(), "llvm-objdump") + << "can't create temporary xar archive " << XarFilename << "\n"; return; } @@ -6339,7 +6347,7 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, std::error_code TocEC = sys::fs::createTemporaryFile("llvm-objdump", "toc", TocFilename); if (TocEC) { - errs() << TocEC.message() << "\n"; + WithColor::error(errs(), "llvm-objdump") << TocEC.message() << "\n"; return; } xar_serialize(xar, TocFilename.c_str()); @@ -6356,7 +6364,7 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = MemoryBuffer::getFileOrSTDIN(TocFilename.c_str()); if (std::error_code EC = FileOrErr.getError()) { - errs() << EC.message() << "\n"; + WithColor::error(errs(), "llvm-objdump") << EC.message() << "\n"; return; } std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); @@ -6371,8 +6379,9 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, // TODO: Go through the xar's files. ScopedXarIter xi; if(!xi){ - errs() << "Can't obtain an xar iterator for xar archive " - << XarFilename.c_str() << "\n"; + WithColor::error(errs(), "llvm-objdump") + << "can't obtain an xar iterator for xar archive " + << XarFilename.c_str() << "\n"; return; } for(xar_file_t xf = xar_file_first(xar, xi); xf; xf = xar_file_next(xi)){ @@ -6382,8 +6391,9 @@ static void DumpBitcodeSection(MachOObjectFile *O, const char *sect, ScopedXarIter xp; if(!xp){ - errs() << "Can't obtain an xar iterator for xar archive " - << XarFilename.c_str() << "\n"; + WithColor::error(errs(), "llvm-objdump") + << "can't obtain an xar iterator for xar archive " + << XarFilename.c_str() << "\n"; return; } member_name = NULL; @@ -6860,8 +6870,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, // IP->setCommentStream(CommentStream); if (!AsmInfo || !STI || !DisAsm || !IP) { - errs() << "error: couldn't initialize disassembler for target " - << TripleName << '\n'; + WithColor::error(errs(), "llvm-objdump") + << "couldn't initialize disassembler for target " << TripleName << '\n'; return; } @@ -6902,8 +6912,9 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, } if (ThumbTarget && (!ThumbAsmInfo || !ThumbSTI || !ThumbDisAsm || !ThumbIP)) { - errs() << "error: couldn't initialize disassembler for target " - << ThumbTripleName << '\n'; + WithColor::error(errs(), "llvm-objdump") + << "couldn't initialize disassembler for target " << ThumbTripleName + << '\n'; return; } @@ -6956,7 +6967,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = MemoryBuffer::getFileOrSTDIN(DSYMFile); if (std::error_code EC = BufOrErr.getError()) { - errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; + WithColor::error(errs(), "llvm-objdump") + << Filename << ": " << EC.message() << '\n'; return; } Expected<std::unique_ptr<MachOObjectFile>> DbgObjCheck = @@ -7243,7 +7255,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, outs() << format("\t.short\t0x%04x\n", opcode); Size = 2; } else{ - errs() << "llvm-objdump: warning: invalid instruction encoding\n"; + WithColor::warning(errs(), "llvm-objdump") + << "invalid instruction encoding\n"; if (Size == 0) Size = 1; // skip illegible bytes } @@ -7290,7 +7303,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF, *(Bytes.data() + Index) & 0xff); InstSize = 1; // skip exactly one illegible byte and move on. } else { - errs() << "llvm-objdump: warning: invalid instruction encoding\n"; + WithColor::warning(errs(), "llvm-objdump") + << "invalid instruction encoding\n"; if (InstSize == 0) InstSize = 1; // skip illegible bytes } diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 827d3674229..d38ffc1a3ce 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -59,6 +59,7 @@ #include "llvm/Support/StringSaver.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cctype> @@ -337,33 +338,35 @@ SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) { void llvm::error(std::error_code EC) { if (!EC) return; - - errs() << ToolName << ": error reading file: " << EC.message() << ".\n"; + WithColor::error(errs(), ToolName) + << "reading file: " << EC.message() << ".\n"; errs().flush(); exit(1); } LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) { - errs() << ToolName << ": " << Message << ".\n"; + WithColor::error(errs(), ToolName) << Message << ".\n"; errs().flush(); exit(1); } void llvm::warn(StringRef Message) { - errs() << ToolName << ": warning: " << Message << ".\n"; + WithColor::warning(errs(), ToolName) << Message << ".\n"; errs().flush(); } LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, Twine Message) { - errs() << ToolName << ": '" << File << "': " << Message << ".\n"; + WithColor::error(errs(), ToolName) + << "'" << File << "': " << Message << ".\n"; exit(1); } LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, std::error_code EC) { assert(EC); - errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; + WithColor::error(errs(), ToolName) + << "'" << File << "': " << EC.message() << ".\n"; exit(1); } @@ -374,7 +377,7 @@ LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, raw_string_ostream OS(Buf); logAllUnhandledErrors(std::move(E), OS); OS.flush(); - errs() << ToolName << ": '" << File << "': " << Buf; + WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; exit(1); } @@ -383,7 +386,7 @@ LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, llvm::Error E, StringRef ArchitectureName) { assert(E); - errs() << ToolName << ": "; + WithColor::error(errs(), ToolName); if (ArchiveName != "") errs() << ArchiveName << "(" << FileName << ")"; else @@ -2071,8 +2074,9 @@ static void PrintUnwindInfo(const ObjectFile *o) { printMachOUnwindInfo(MachO); else { // TODO: Extract DWARF dump tool to objdump. - errs() << "This operation is only currently supported " - "for COFF and MachO object files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for COFF and MachO object files.\n"; return; } } @@ -2082,8 +2086,9 @@ void llvm::printExportsTrie(const ObjectFile *o) { if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) printMachOExportsTrie(MachO); else { - errs() << "This operation is only currently supported " - "for Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for Mach-O executable files.\n"; return; } } @@ -2093,8 +2098,9 @@ void llvm::printRebaseTable(ObjectFile *o) { if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) printMachORebaseTable(MachO); else { - errs() << "This operation is only currently supported " - "for Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for Mach-O executable files.\n"; return; } } @@ -2104,8 +2110,9 @@ void llvm::printBindTable(ObjectFile *o) { if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) printMachOBindTable(MachO); else { - errs() << "This operation is only currently supported " - "for Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for Mach-O executable files.\n"; return; } } @@ -2115,8 +2122,9 @@ void llvm::printLazyBindTable(ObjectFile *o) { if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) printMachOLazyBindTable(MachO); else { - errs() << "This operation is only currently supported " - "for Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for Mach-O executable files.\n"; return; } } @@ -2126,8 +2134,9 @@ void llvm::printWeakBindTable(ObjectFile *o) { if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) printMachOWeakBindTable(MachO); else { - errs() << "This operation is only currently supported " - "for Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for Mach-O executable files.\n"; return; } } @@ -2136,10 +2145,11 @@ void llvm::printWeakBindTable(ObjectFile *o) { /// into llvm-bcanalyzer. void llvm::printRawClangAST(const ObjectFile *Obj) { if (outs().is_displayed()) { - errs() << "The -raw-clang-ast option will dump the raw binary contents of " - "the clang ast section.\n" - "Please redirect the output to a file or another program such as " - "llvm-bcanalyzer.\n"; + WithColor::error(errs(), ToolName) + << "The -raw-clang-ast option will dump the raw binary contents of " + "the clang ast section.\n" + "Please redirect the output to a file or another program such as " + "llvm-bcanalyzer.\n"; return; } @@ -2173,8 +2183,9 @@ static void printFaultMaps(const ObjectFile *Obj) { } else if (isa<MachOObjectFile>(Obj)) { FaultMapSectionName = "__llvm_faultmaps"; } else { - errs() << "This operation is only currently supported " - "for ELF and Mach-O executable files.\n"; + WithColor::error(errs(), ToolName) + << "This operation is only currently supported " + "for ELF and Mach-O executable files.\n"; return; } @@ -2243,7 +2254,7 @@ static void printFileHeaders(const ObjectFile *o) { static void printArchiveChild(StringRef Filename, const Archive::Child &C) { Expected<sys::fs::perms> ModeOrErr = C.getAccessMode(); if (!ModeOrErr) { - errs() << "ill-formed archive entry.\n"; + WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n"; consumeError(ModeOrErr.takeError()); return; } |