[PATCH] D41720: [clang-tidy] Add a -show-color flag.

Ian Tessier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 3 15:55:21 PST 2018


itessier created this revision.
itessier added a project: clang-tools-extra.
Herald added a subscriber: xazax.hun.

This change will allow enabling of colour diagnostics when not directly running within a terminal, but colour output is possible. For example, if stdout is being captured and redirected to a real terminal.


https://reviews.llvm.org/D41720

Files:
  clang-tidy/ClangTidy.cpp
  clang-tidy/ClangTidyOptions.cpp
  clang-tidy/ClangTidyOptions.h
  clang-tidy/tool/ClangTidyMain.cpp


Index: clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- clang-tidy/tool/ClangTidyMain.cpp
+++ clang-tidy/tool/ClangTidyMain.cpp
@@ -146,6 +146,12 @@
                                    cl::init("none"),
                                    cl::cat(ClangTidyCategory));
 
+static cl::opt<bool> ShowColor("show-color", cl::desc(R"(
+Show color diagnostics. If not specified,
+defaults to on when a color-capable terminal
+is detected.)"),
+                               cl::ValueOptional, cl::cat(ClangTidyCategory));
+
 static cl::opt<bool> ListChecks("list-checks", cl::desc(R"(
 List all enabled checks and exit. Use with
 -checks=* to list all available checks.
@@ -304,6 +310,7 @@
   DefaultOptions.SystemHeaders = SystemHeaders;
   DefaultOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
   DefaultOptions.FormatStyle = FormatStyle;
+  DefaultOptions.ShowColor = ShowColor;
   DefaultOptions.User = llvm::sys::Process::GetEnv("USER");
   // USERNAME is used on Windows.
   if (!DefaultOptions.User)
@@ -322,6 +329,8 @@
     OverrideOptions.AnalyzeTemporaryDtors = AnalyzeTemporaryDtors;
   if (FormatStyle.getNumOccurrences() > 0)
     OverrideOptions.FormatStyle = FormatStyle;
+  if (ShowColor.getNumOccurrences() > 0)
+    OverrideOptions.ShowColor = ShowColor;
 
   if (!Config.empty()) {
     if (llvm::ErrorOr<ClangTidyOptions> ParsedConfig =
Index: clang-tidy/ClangTidyOptions.h
===================================================================
--- clang-tidy/ClangTidyOptions.h
+++ clang-tidy/ClangTidyOptions.h
@@ -89,6 +89,9 @@
   /// See clang-format documentation for more about configuring format style.
   llvm::Optional<std::string> FormatStyle;
 
+  /// \brief Show color diagnostics.
+  llvm::Optional<bool> ShowColor;
+
   /// \brief Specifies the name or e-mail of the user running clang-tidy.
   ///
   /// This option is used, for example, to place the correct user name in TODO()
Index: clang-tidy/ClangTidyOptions.cpp
===================================================================
--- clang-tidy/ClangTidyOptions.cpp
+++ clang-tidy/ClangTidyOptions.cpp
@@ -88,6 +88,7 @@
     IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex);
     IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
     IO.mapOptional("FormatStyle", Options.FormatStyle);
+    IO.mapOptional("ShowColor", Options.ShowColor);
     IO.mapOptional("User", Options.User);
     IO.mapOptional("CheckOptions", NOpts->Options);
     IO.mapOptional("ExtraArgs", Options.ExtraArgs);
@@ -149,6 +150,7 @@
   overrideValue(Result.SystemHeaders, Other.SystemHeaders);
   overrideValue(Result.AnalyzeTemporaryDtors, Other.AnalyzeTemporaryDtors);
   overrideValue(Result.FormatStyle, Other.FormatStyle);
+  overrideValue(Result.ShowColor, Other.ShowColor);
   overrideValue(Result.User, Other.User);
   mergeVectors(Result.ExtraArgs, Other.ExtraArgs);
   mergeVectors(Result.ExtraArgsBefore, Other.ExtraArgsBefore);
Index: clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tidy/ClangTidy.cpp
+++ clang-tidy/ClangTidy.cpp
@@ -96,7 +96,11 @@
               DiagPrinter),
         SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
         TotalFixes(0), AppliedFixes(0), WarningsAsErrors(0) {
-    DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
+    if (Context.getOptions().ShowColor.hasValue()) {
+      DiagOpts->ShowColors = Context.getOptions().ShowColor.getValue();
+    } else {
+      DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
+    }
     DiagPrinter->BeginSourceFile(LangOpts);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41720.128579.patch
Type: text/x-patch
Size: 3691 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180103/eeb9d9a2/attachment.bin>


More information about the cfe-commits mailing list