[PATCH] D94624: PATCH] [clang-query] Add a --use-color option to clang-query to allow forcing the behavior

Tom Ritter via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 15 11:07:15 PST 2021


tomrittervg updated this revision to Diff 317027.
tomrittervg added a comment.

Remove braces


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94624/new/

https://reviews.llvm.org/D94624

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


Index: clang-tools-extra/clang-query/tool/ClangQuery.cpp
===================================================================
--- clang-tools-extra/clang-query/tool/ClangQuery.cpp
+++ clang-tools-extra/clang-query/tool/ClangQuery.cpp
@@ -49,6 +49,14 @@
 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 @@
 
   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 Status = Tool.buildASTs(ASTs);
   int ASTStatus = 0;
Index: clang-tools-extra/clang-query/Query.cpp
===================================================================
--- clang-tools-extra/clang-query/Query.cpp
+++ clang-tools-extra/clang-query/Query.cpp
@@ -156,8 +156,7 @@
         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";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94624.317027.patch
Type: text/x-patch
Size: 2263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210115/090a0829/attachment.bin>


More information about the cfe-commits mailing list