diff --git a/clang-tools-extra/clang-query/Query.cpp b/clang-tools-extra/clang-query/Query.cpp --- a/clang-tools-extra/clang-query/Query.cpp +++ b/clang-tools-extra/clang-query/Query.cpp @@ -157,7 +157,7 @@ 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, QS.ColorOutput); Dumper.SetTraversalKind(QS.TK); Dumper.Visit(BI->second); OS << "\n"; diff --git a/clang-tools-extra/clang-query/QuerySession.h b/clang-tools-extra/clang-query/QuerySession.h --- a/clang-tools-extra/clang-query/QuerySession.h +++ b/clang-tools-extra/clang-query/QuerySession.h @@ -23,9 +23,9 @@ /// Represents the state for a particular clang-query session. class QuerySession { public: - QuerySession(llvm::ArrayRef> ASTs) + QuerySession(llvm::ArrayRef> ASTs, bool UseColor) : ASTs(ASTs), PrintOutput(false), DiagOutput(true), - DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), + DetailedASTOutput(false), ColorOutput(UseColor), BindRoot(true), PrintMatcher(false), Terminate(false), TK(ast_type_traits::TK_AsIs) {} llvm::ArrayRef> ASTs; @@ -34,6 +34,8 @@ bool DiagOutput; bool DetailedASTOutput; + bool ColorOutput; + bool BindRoot; bool PrintMatcher; bool Terminate; diff --git a/clang-tools-extra/clang-query/tool/ClangQuery.cpp b/clang-tools-extra/clang-query/tool/ClangQuery.cpp --- a/clang-tools-extra/clang-query/tool/ClangQuery.cpp +++ b/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 + 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 Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory)); @@ -124,7 +132,7 @@ assert(Status == 0 && "Unexpected status returned"); } - QuerySession QS(ASTs); + QuerySession QS(ASTs, UseColor); if (!Commands.empty()) { for (auto I = Commands.begin(), E = Commands.end(); I != E; ++I) {