[PATCH] D159224: [llvm-objdump] Add flags to force enable/disable color output
Jonas Devlieghere via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 30 14:03:59 PDT 2023
JDevlieghere updated this revision to Diff 554828.
JDevlieghere added a comment.
Move test
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D159224/new/
https://reviews.llvm.org/D159224
Files:
llvm/test/tools/llvm-objdump/tool-color.test
llvm/tools/llvm-objdump/ObjdumpOpts.td
llvm/tools/llvm-objdump/llvm-objdump.cpp
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<BuildIDFetcher> 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<uint64_t, std::string> AllLabels;
std::unordered_map<uint64_t, std::vector<std::string>> 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");
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<disassemble_zeroes>,
HelpText<"Alias for --disassemble-zeroes">;
Index: llvm/test/tools/llvm-objdump/tool-color.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objdump/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: [0;1;31merror: [0m'doesnotexist'
+NOCOLOR-NOT [0;1;31m
+NOCOLOR: error: 'doesnotexist'
+CONFLICT: error: --color and --no-color are mutually exclusive
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159224.554828.patch
Type: text/x-patch
Size: 2926 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230830/d612b03e/attachment.bin>
More information about the llvm-commits
mailing list