From b67379c35be765995496170a1b41ecf1d952a021 Mon Sep 17 00:00:00 2001 From: Bertik23 <39457484+Bertik23@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:26:42 +0100 Subject: [PATCH] [llvm-diff] Add colorful output to diff (#131012) Adds colorful output when when possible to the diff. Adds a use to the `--color` option llvm-diff has. --- llvm/tools/llvm-diff/lib/DiffConsumer.cpp | 29 +++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/llvm/tools/llvm-diff/lib/DiffConsumer.cpp b/llvm/tools/llvm-diff/lib/DiffConsumer.cpp index b6eb71916acf..5ff9b3d76ac9 100644 --- a/llvm/tools/llvm-diff/lib/DiffConsumer.cpp +++ b/llvm/tools/llvm-diff/lib/DiffConsumer.cpp @@ -14,6 +14,8 @@ #include "llvm/IR/Instructions.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -199,20 +201,23 @@ void DiffConsumer::logd(const DiffLogBuilder &Log) { switch (Log.getLineKind(I)) { case DC_match: out << " "; - Log.getLeft(I)->print(dbgs()); dbgs() << '\n'; - //printValue(Log.getLeft(I), true); + Log.getLeft(I)->print(dbgs()); + dbgs() << '\n'; break; - case DC_left: - out << "< "; - Log.getLeft(I)->print(dbgs()); dbgs() << '\n'; - //printValue(Log.getLeft(I), true); - break; - case DC_right: - out << "> "; - Log.getRight(I)->print(dbgs()); dbgs() << '\n'; - //printValue(Log.getRight(I), false); + case DC_left: { + auto LeftColor = llvm::WithColor(out, raw_ostream::RED); + LeftColor << "< "; + Log.getLeft(I)->print(LeftColor); + LeftColor << '\n'; break; } - //out << "\n"; + case DC_right: { + auto RightColor = llvm::WithColor(out, raw_ostream::GREEN); + RightColor << "> "; + Log.getRight(I)->print(RightColor); + RightColor << '\n'; + break; + } + } } }