[clang-tools-extra] d6d36ba - Add a --use-color option to clang-query to allow forcing the behavior

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 25 05:07:40 PST 2021


Author: Tom Ritter
Date: 2021-01-25T08:06:54-05:00
New Revision: d6d36baa33e76ace11ac20c03de1097d48bd9246

URL: https://github.com/llvm/llvm-project/commit/d6d36baa33e76ace11ac20c03de1097d48bd9246
DIFF: https://github.com/llvm/llvm-project/commit/d6d36baa33e76ace11ac20c03de1097d48bd9246.diff

LOG: Add a --use-color option to clang-query to allow forcing the behavior

D62056 makes the output color if clang auto-detects a tty, but if it
does not, there is no way to force it to use colors anyway.

This patch adjusts the command-lines given to ClangTool which will
force color on or off if --use-color is specified.

Added: 
    

Modified: 
    clang-tools-extra/clang-query/Query.cpp
    clang-tools-extra/clang-query/tool/ClangQuery.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-query/Query.cpp b/clang-tools-extra/clang-query/Query.cpp
index ca2a285e9eb7..e33612a2e16d 100644
--- a/clang-tools-extra/clang-query/Query.cpp
+++ b/clang-tools-extra/clang-query/Query.cpp
@@ -156,8 +156,7 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
         if (QS.DetailedASTOutput) {
           OS << "Binding for \"" << BI->first << "\":\n";
           const ASTContext &Ctx = AST->getASTContext();
-          const SourceManager &SM = Ctx.getSourceManager();
-          ASTDumper Dumper(OS, Ctx, SM.getDiagnostics().getShowColors());
+          ASTDumper Dumper(OS, Ctx, AST->getDiagnostics().getShowColors());
           Dumper.SetTraversalKind(QS.TK);
           Dumper.Visit(BI->second);
           OS << "\n";

diff  --git a/clang-tools-extra/clang-query/tool/ClangQuery.cpp b/clang-tools-extra/clang-query/tool/ClangQuery.cpp
index 31c7f12251c9..45a355073945 100644
--- a/clang-tools-extra/clang-query/tool/ClangQuery.cpp
+++ b/clang-tools-extra/clang-query/tool/ClangQuery.cpp
@@ -49,6 +49,14 @@ using namespace llvm;
 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
 static cl::OptionCategory ClangQueryCategory("clang-query options");
 
+static cl::opt<bool>
+    UseColor("use-color",
+             cl::desc(
+                 R"(Use colors in detailed AST output. If not set, colors
+will be used if the terminal connected to
+standard output supports colors.)"),
+             cl::init(false), cl::cat(ClangQueryCategory));
+
 static cl::list<std::string> Commands("c", cl::desc("Specify command to run"),
                                       cl::value_desc("command"),
                                       cl::cat(ClangQueryCategory));
@@ -109,6 +117,19 @@ int main(int argc, const char **argv) {
 
   ClangTool Tool(OptionsParser->getCompilations(),
                  OptionsParser->getSourcePathList());
+
+  if (UseColor.getNumOccurrences() > 0) {
+    ArgumentsAdjuster colorAdjustor = [](const CommandLineArguments &Args, StringRef /*unused*/) {
+      CommandLineArguments AdjustedArgs = Args;
+      if (UseColor)
+        AdjustedArgs.push_back("-fdiagnostics-color");
+      else
+        AdjustedArgs.push_back("-fno-diagnostics-color");
+      return AdjustedArgs;
+    };
+    Tool.appendArgumentsAdjuster(colorAdjustor);
+  }
+
   std::vector<std::unique_ptr<ASTUnit>> ASTs;
   int ASTStatus = 0;
   switch (Tool.buildASTs(ASTs)) {


        


More information about the cfe-commits mailing list