Index: llvm/test/tools/llvm-objcopy/tool-color.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objcopy/tool-color.test @@ -0,0 +1,8 @@ +# RUN: not llvm-objdump --disassemble doesnotexist --color 2>&1 | FileCheck %s --check-prefix=COLOR +# RUN: not llvm-objdump --disassemble doesnotexist --no-color 2>&1 | FileCheck %s --check-prefix=NOCOLOR +# RUN: not llvm-objdump --color --no-color 2>&1 | FileCheck %s --check-prefix=CONFLICT + +COLOR: error: 'doesnotexist' +NOCOLOR-NOT [0;1;31m +NOCOLOR: error: 'doesnotexist' +CONFLICT: error: --color and --no-color are mutually exclusive Index: llvm/tools/llvm-objdump/ObjdumpOpts.td =================================================================== --- llvm/tools/llvm-objdump/ObjdumpOpts.td +++ llvm/tools/llvm-objdump/ObjdumpOpts.td @@ -78,6 +78,10 @@ def disassemble_zeroes : Flag<["--"], "disassemble-zeroes">, HelpText<"Do not skip blocks of zeroes when disassembling">; +def no_color : Flag<["--"], "no-color">, + HelpText<"Disable color output">; +def color : Flag<["--"], "color">, + HelpText<"Force color output">; def : Flag<["-"], "z">, Alias, HelpText<"Alias for --disassemble-zeroes">; Index: llvm/tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- llvm/tools/llvm-objdump/llvm-objdump.cpp +++ llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -177,6 +177,8 @@ #define DEBUG_TYPE "objdump" +static bool NoColor; +static bool ForceColor; static uint64_t AdjustVMA; static bool AllHeaders; static std::string ArchName; @@ -234,6 +236,9 @@ StringSet<> objdump::FoundSectionSet; static StringRef ToolName; +static bool disableColor(const raw_ostream &OS) { return false; } +static bool forceColor(const raw_ostream &OS) { return true; } + std::unique_ptr BIDFetcher; Dumper::Dumper(const object::ObjectFile &O) : O(O) { @@ -1900,6 +1905,9 @@ formatted_raw_ostream FOS(outs()); + if (ForceColor) + FOS.enable_colors(true); + std::unordered_map AllLabels; std::unordered_map> BBAddrMapLabels; if (SymbolizeOperands) { @@ -3330,6 +3338,18 @@ else parseObjdumpOptions(InputArgs); + // We need to parse the color flags first. Parsing other properties might + // require us to print a colored error message. + NoColor = InputArgs.hasArg(OBJDUMP_no_color); + ForceColor = InputArgs.hasArg(OBJDUMP_color); + if (NoColor && ForceColor) + reportCmdLineError("--color and --no-color are mutually exclusive"); + + if (NoColor) + WithColor::setAutoDetectFunction(disableColor); + else if(ForceColor) + WithColor::setAutoDetectFunction(forceColor); + if (StartAddress >= StopAddress) reportCmdLineError("start address should be less than stop address");