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 @@ -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"; 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)); @@ -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> ASTs; int ASTStatus = 0; switch (Tool.buildASTs(ASTs)) {