Skip to content

Commit

Permalink
[ELF] Implement --color-diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 25, 2021
1 parent d1a47ee commit 6e290aa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
19 changes: 18 additions & 1 deletion elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ static const char helpmsg[] = R"(
Generate build ID
--no-build-id
--chroot DIR Set a given path to root directory
--color-diagnostics Ignored
--color-diagnostics=[auto,always,never]
Use colors in diagnostics
--color-diagnostics Alias for --color-diagnostics=always
--compress-debug-sections [none,zlib,zlib-gabi,zlib-gnu]
Compress .debug_* sections
--demangle Demangle C++ symbols in log messages (default)
Expand Down Expand Up @@ -376,6 +378,7 @@ void parse_nonpositional_args(Context<E> &ctx,
args = args.subspan(1);

bool version_shown = false;
ctx.arg.color_diagnostics = isatty(STDERR_FILENO);

while (!args.empty()) {
std::string_view arg;
Expand Down Expand Up @@ -528,6 +531,20 @@ void parse_nonpositional_args(Context<E> &ctx,
ctx.arg.directory = arg;
} else if (read_arg(ctx, args, arg, "chroot")) {
ctx.arg.chroot = arg;
} else if (args[0] == "-color-diagnostics=auto" ||
args[0] == "--color-diagnostics=auto") {
ctx.arg.color_diagnostics = isatty(STDERR_FILENO);
args = args.subspan(1);
} else if (args[0] == "-color-diagnostics=always" ||
args[0] == "--color-diagnostics=always") {
ctx.arg.color_diagnostics = true;
args = args.subspan(1);
} else if (args[0] == "-color-diagnostics=never" ||
args[0] == "--color-diagnostics=never") {
ctx.arg.color_diagnostics = false;
args = args.subspan(1);
} else if (read_flag(args, "color-diagnostics")) {
ctx.arg.color_diagnostics = true;
} else if (read_flag(args, "warn-common")) {
ctx.arg.warn_common = true;
} else if (read_flag(args, "no-warn-common")) {
Expand Down
1 change: 1 addition & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,7 @@ struct Context {
bool Bsymbolic = false;
bool Bsymbolic_functions = false;
bool allow_multiple_definition = false;
bool color_diagnostics = false;
bool demangle = true;
bool discard_all = false;
bool discard_locals = false;
Expand Down
1 change: 1 addition & 0 deletions macho/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ struct Context {
struct {
bool ObjC = false;
bool adhoc_codesign = true;
bool color_diagnostics = false;
bool dead_strip = true;
bool dead_strip_dylibs = false;
bool deduplicate = true;
Expand Down
19 changes: 16 additions & 3 deletions mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ template <typename C>
class Error {
public:
Error(C &ctx) : out(ctx, std::cerr) {
out << "mold: ";
if (ctx.arg.color_diagnostics)
out << "mold: \033[0;1;31merror:\033[0m ";
else
out << "mold: error: ";

ctx.has_error = true;
}

Expand All @@ -119,9 +123,18 @@ template <typename C>
class Warn {
public:
Warn(C &ctx) : out(ctx, std::cerr) {
out << "mold: ";
if (ctx.arg.fatal_warnings)
if (ctx.arg.fatal_warnings) {
if (ctx.arg.color_diagnostics)
out << "mold: \033[0;1;31merror:\033[0m ";
else
out << "mold: error: ";
ctx.has_error = true;
} else {
if (ctx.arg.color_diagnostics)
out << "mold: \033[0;1;35warning:\033[0m ";
else
out << "mold: warning: ";
}
}

template <class T> Warn &operator<<(T &&val) {
Expand Down
27 changes: 27 additions & 0 deletions test/elf/color-diagnostics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
export LANG=
set -e
cd $(dirname $0)
mold=`pwd`/../../mold
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/../../out/test/elf/$(basename -s .sh $0)
mkdir -p $t

cat <<EOF | cc -o $t/a.o -c -xc -
int foo();
int main() { foo(); }
EOF

! $mold -o $t/exe $t/a.o --color-diagnostics 2> $t/log
grep -Pq '\e' $t/log

! $mold -o $t/exe $t/a.o --color-diagnostics=always 2> $t/log
grep -Pq '\e' $t/log

! $mold -o $t/exe $t/a.o --color-diagnostics=never 2> $t/log
! grep -Pq '\e' $t/log || false

! $mold -o $t/exe $t/a.o --color-diagnostics=auto 2> $t/log
! grep -Pq '\e' $t/log || false

echo OK

0 comments on commit 6e290aa

Please sign in to comment.