From 52d9a5f6028384eb956378aa984455daee1e8b43 Mon Sep 17 00:00:00 2001 From: Siddharth Bhat Date: Thu, 16 May 2019 10:39:36 +0200 Subject: [PATCH] Allow color API to enable and disable colors This is useful for front-ends which wish to selectively enable or disable coloring. Also expose these APIs from the C API. --- src/binaryen-c.cpp | 5 +++++ src/binaryen-c.h | 5 +++++ src/passes/pass.cpp | 2 +- src/support/colors.cpp | 9 +++++---- src/support/colors.h | 3 ++- src/tools/asm2wasm.cpp | 2 +- src/tools/wasm-as.cpp | 2 +- src/tools/wasm-ctor-eval.cpp | 2 +- src/tools/wasm-dis.cpp | 2 +- src/tools/wasm-emscripten-finalize.cpp | 2 +- src/tools/wasm-metadce.cpp | 2 +- src/tools/wasm-opt.cpp | 2 +- src/tools/wasm-reduce.cpp | 2 +- src/tools/wasm2js.cpp | 2 +- test/example/c-api-kitchen-sink.c | 16 ++++++++++++++++ 15 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 37d2c214244..be06cce839d 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -26,6 +26,7 @@ #include "ir/utils.h" #include "pass.h" #include "shell-interface.h" +#include "support/colors.h" #include "wasm-binary.h" #include "wasm-builder.h" #include "wasm-interpreter.h" @@ -3824,6 +3825,10 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module, return NULL; } +void BinaryenSetColorsEnabled(int enabled) { Colors::setEnabled(enabled); } + +int BinaryenAreColorsEnabled() { return Colors::isEnabled(); } + #ifdef __EMSCRIPTEN__ // Override atexit - we don't need any global ctors to actually run, and // otherwise we get clutter in the output in debug builds diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 310ca3a2ac4..820b23ccaa2 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -1221,6 +1221,11 @@ BinaryenGetFunctionTypeBySignature(BinaryenModuleRef module, BinaryenType* paramTypes, BinaryenIndex numParams); +// Enable or disable coloring for the WASM printer +void BinaryenSetColorsEnabled(int enabled); + +// Query whether color is enable for the WASM printer +int BinaryenAreColorsEnabled(); #ifdef __cplusplus } // extern "C" #endif diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index bb1a062e7f6..84914f8199d 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -428,7 +428,7 @@ static void dumpWast(Name name, Module* wasm) { fullName += std::to_string(getpid()) + '-'; #endif fullName += numstr + "-" + name.str + ".wasm"; - Colors::disable(); + Colors::setEnabled(false); ModuleWriter writer; writer.setBinary(false); // TODO: add an option for binary writer.write(*wasm, fullName); diff --git a/src/support/colors.cpp b/src/support/colors.cpp index 6d06b69fd33..d8a2c7f52d6 100644 --- a/src/support/colors.cpp +++ b/src/support/colors.cpp @@ -20,10 +20,11 @@ #include namespace { -bool colors_disabled = false; +bool colors_enabled = true; } // anonymous namespace -void Colors::disable() { colors_disabled = true; } +void Colors::setEnabled(bool enabled) { colors_enabled = enabled; } +bool Colors::isEnabled() { return colors_enabled; } #if defined(__linux__) || defined(__APPLE__) #include @@ -34,7 +35,7 @@ void Colors::outputColorCode(std::ostream& stream, const char* colorCode) { (isatty(STDOUT_FILENO) && (!getenv("COLORS") || getenv("COLORS")[0] != '0')); // implicit }(); - if (has_color && !colors_disabled) { + if (has_color && colors_enabled) { stream << colorCode; } } @@ -50,7 +51,7 @@ void Colors::outputColorCode(std::ostream& stream, const WORD& colorCode) { }(); static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); static HANDLE hStderr = GetStdHandle(STD_ERROR_HANDLE); - if (has_color && !colors_disabled) + if (has_color && colors_enabled) SetConsoleTextAttribute(&stream == &std::cout ? hStdout : hStderr, colorCode); } diff --git a/src/support/colors.h b/src/support/colors.h index 68a2519694b..f2c4460259d 100644 --- a/src/support/colors.h +++ b/src/support/colors.h @@ -20,7 +20,8 @@ #include namespace Colors { -void disable(); +void setEnabled(bool enabled); +bool isEnabled(); #if defined(__linux__) || defined(__APPLE__) void outputColorCode(std::ostream& stream, const char* colorCode); diff --git a/src/tools/asm2wasm.cpp b/src/tools/asm2wasm.cpp index b1196850bcf..0077b944896 100644 --- a/src/tools/asm2wasm.cpp +++ b/src/tools/asm2wasm.cpp @@ -53,7 +53,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add( "--mapped-globals", diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index d45f10f81d1..2203aa83c50 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -46,7 +46,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--validate", "-v", diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 6a967ece7ae..293c675d117 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -408,7 +408,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--emit-text", "-S", diff --git a/src/tools/wasm-dis.cpp b/src/tools/wasm-dis.cpp index 1dbe9765610..6c1ed15246d 100644 --- a/src/tools/wasm-dis.cpp +++ b/src/tools/wasm-dis.cpp @@ -39,7 +39,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add( "--source-map", diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index e524e473210..90183ab4875 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -59,7 +59,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [&outfile](Options*, const std::string& argument) { outfile = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--debuginfo", "-g", diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index c87da8c9a44..879919c8416 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -427,7 +427,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--emit-text", "-S", diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index f7e8b59180f..0f1ddd2560c 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -87,7 +87,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--emit-text", "-S", diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index cea415a47bd..36b9880917d 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -1113,7 +1113,7 @@ int main(int argc, const char* argv[]) { } if (!binary) { - Colors::disable(); + Colors::setEnabled(false); } std::cerr << "|wasm-reduce\n"; diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 71e7a28fd93..f8c79fae1e2 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -739,7 +739,7 @@ int main(int argc, const char* argv[]) { Options::Arguments::One, [](Options* o, const std::string& argument) { o->extra["output"] = argument; - Colors::disable(); + Colors::setEnabled(false); }) .add("--allow-asserts", "", diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 9e996a881e3..1761ab0bb3f 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -813,6 +813,21 @@ void test_tracing() { BinaryenSetAPITracing(0); } +void test_color_status() { + int i; + + // save old state + const int old_state = BinaryenAreColorsEnabled(); + + // Check that we can set the state to both {0, 1} + for(i = 0; i <= 1; i++){ + BinaryenSetColorsEnabled(i); + assert(BinaryenAreColorsEnabled() == i); + } + + BinaryenSetColorsEnabled(old_state); +} + int main() { test_types(); test_core(); @@ -822,6 +837,7 @@ int main() { test_interpret(); test_nonvalid(); test_tracing(); + test_color_status(); return 0; }