llvm-cov: Only emit colour by default if the output is a tty
authorJustin Bogner <mail@justinbogner.com>
Thu, 19 Mar 2015 00:02:23 +0000 (00:02 +0000)
committerJustin Bogner <mail@justinbogner.com>
Thu, 19 Mar 2015 00:02:23 +0000 (00:02 +0000)
This replaces the -no-color flag with a -color={auto|always|never}
option, with auto as the default, which is much saner.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232693 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-cov/binary-formats.c
test/tools/llvm-cov/report.cpp
test/tools/llvm-cov/showLineExecutionCounts.cpp
test/tools/llvm-cov/showTemplateInstantiations.cpp
test/tools/llvm-cov/universal-binary.c
tools/llvm-cov/CodeCoverage.cpp

index 6ebd089fbe9a5d5acd583f03c902c0b1dbd9ac76..31c6c4c3bf4d5814a77d39aef7686df63b8acafa 100644 (file)
@@ -4,6 +4,6 @@
 int main(int argc, const char *argv[]) {}
 
 // RUN: llvm-profdata merge %S/Inputs/binary-formats.proftext -o %t.profdata
-// RUN: llvm-cov show %S/Inputs/binary-formats.macho32l -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
-// RUN: llvm-cov show %S/Inputs/binary-formats.macho64l -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
-// RUN: llvm-cov show %S/Inputs/binary-formats.macho32b -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/binary-formats.macho32l -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/binary-formats.macho64l -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/binary-formats.macho32b -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
index 4422d794381a60d6c74389337636db28b1f11152..3322e1a2c063088d034fc7d79e21a713332bdcf0 100644 (file)
@@ -1,5 +1,5 @@
-// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -no-colors -filename-equivalence 2>&1 | FileCheck %s
-// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -no-colors -filename-equivalence report.cpp 2>&1 | FileCheck -check-prefix=FILT-NEXT %s
+// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -filename-equivalence 2>&1 | FileCheck %s
+// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -filename-equivalence report.cpp 2>&1 | FileCheck -check-prefix=FILT-NEXT %s
 
 // CHECK:      Filename   Regions  Miss   Cover  Functions  Executed
 // CHECK-NEXT: ---
index 895d0f4f8628b689bceec99634f0e1e0a298ab57..d5254da788bfe53433e6721ae3c861f793cb53ce 100644 (file)
@@ -26,5 +26,5 @@ int main() {                             // CHECK:   1| [[@LINE]]|int main(
 // after coverage                   // WHOLE-FILE:    | [[@LINE]]|// after
                                     // FILTER-NOT:    | [[@LINE-1]]|// after
 
-// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck -check-prefix=CHECK -check-prefix=WHOLE-FILE %s
-// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -no-colors -filename-equivalence -name=main %s | FileCheck -check-prefix=CHECK -check-prefix=FILTER %s
+// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -filename-equivalence %s | FileCheck -check-prefix=CHECK -check-prefix=WHOLE-FILE %s
+// RUN: llvm-cov show %S/Inputs/lineExecutionCounts.covmapping -instr-profile %t.profdata -filename-equivalence -name=main %s | FileCheck -check-prefix=CHECK -check-prefix=FILTER %s
index 41bebf6bac295660a8dfc2c587fc7b33e2ce2a9a..a9be473dc91564c83b7ec0aedfd4f768bdea99ca 100644 (file)
@@ -1,5 +1,5 @@
-// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -no-colors -filename-equivalence %s | FileCheck -check-prefix=CHECK -check-prefix=ALL %s
-// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -no-colors -filename-equivalence -name=_Z4funcIbEiT_ %s | FileCheck -check-prefix=CHECK -check-prefix=FILTER %s
+// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence %s | FileCheck -check-prefix=CHECK -check-prefix=ALL %s
+// RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %S/Inputs/templateInstantiations.profdata -filename-equivalence -name=_Z4funcIbEiT_ %s | FileCheck -check-prefix=CHECK -check-prefix=FILTER %s
 
 // before coverage   // WHOLE-FILE:   | [[@LINE]]|// before
                      // FILTER-NOT:   | [[@LINE-1]]|// before
index 84e1b0a36185ce4478749cb9737fc39e2a673ec8..8ff94d4e3ad33ac65af14571a821acd4d5773645 100644 (file)
@@ -4,7 +4,7 @@
 int main(int argc, const char *argv[]) {}
 
 // RUN: llvm-profdata merge %S/Inputs/universal-binary.proftext -o %t.profdata
-// RUN: llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -no-colors -filename-equivalence %s -arch x86_64 | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -filename-equivalence %s -arch x86_64 | FileCheck %s
 
-// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -no-colors -filename-equivalence %s -arch i386 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s
+// RUN: not llvm-cov show %S/Inputs/universal-binary -instr-profile %t.profdata -filename-equivalence %s -arch i386 2>&1 | FileCheck --check-prefix=WRONG-ARCH %s
 // WRONG-ARCH: Failed to load coverage
index b4a058433116ea0558f0f0eb7784eddef21b6006..335fb0e59c17a9b487e657f4a3979ed0d6f129cb 100644 (file)
@@ -29,6 +29,7 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/Signals.h"
 #include <functional>
 #include <system_error>
@@ -293,11 +294,24 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
                "greater than the given threshold"),
       cl::cat(FilteringCategory));
 
+  enum class Colors { Auto, Always, Never };
+  cl::opt<Colors> Color(
+      "color", cl::desc("Configure color output:"),
+      cl::values(clEnumValN(Colors::Auto, "auto",
+                            "Enable color if stdout seems to support it"),
+                 clEnumValN(Colors::Always, "always", "Enable color"),
+                 clEnumValN(Colors::Never, "never", "Disable color"),
+                 clEnumValEnd));
+
   auto commandLineParser = [&, this](int argc, const char **argv) -> int {
     cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
     ViewOpts.Debug = DebugDump;
     CompareFilenamesOnly = FilenameEquivalence;
 
+    ViewOpts.Colors =
+        Color == Colors::Always ||
+        (Color == Colors::Auto && sys::Process::StandardOutHasColors());
+
     // Create the function filters
     if (!NameFilters.empty() || !NameRegexFilters.empty()) {
       auto NameFilterer = new CoverageFilters;
@@ -388,15 +402,10 @@ int CodeCoverageTool::show(int argc, const char **argv,
                                    cl::desc("Show function instantiations"),
                                    cl::cat(ViewCategory));
 
-  cl::opt<bool> NoColors("no-colors", cl::Optional,
-                         cl::desc("Don't show text colors"), cl::init(false),
-                         cl::cat(ViewCategory));
-
   auto Err = commandLineParser(argc, argv);
   if (Err)
     return Err;
 
-  ViewOpts.Colors = !NoColors;
   ViewOpts.ShowLineNumbers = true;
   ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
                            !ShowRegions || ShowBestLineRegionsCounts;
@@ -462,15 +471,10 @@ int CodeCoverageTool::show(int argc, const char **argv,
 
 int CodeCoverageTool::report(int argc, const char **argv,
                              CommandLineParserType commandLineParser) {
-  cl::opt<bool> NoColors("no-colors", cl::Optional,
-                         cl::desc("Don't show text colors"), cl::init(false));
-
   auto Err = commandLineParser(argc, argv);
   if (Err)
     return Err;
 
-  ViewOpts.Colors = !NoColors;
-
   auto Coverage = load();
   if (!Coverage)
     return 1;