tomrittervg created this revision. tomrittervg requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
My first attempt was to figure out how to plumb the --use-color command line argument to the DiagnosticEngine that gets referenced in AST->getDiagnostics().getDiagnosticOptions() However, this did not turn to be fruitful. Those options get created in ToolInvocation::run() from the command line given to the *tool*. That command-line is constructed in ClangTool::run() and there's no apparent way to edit those Diagnostics from the clang-query tool easily. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D94624 Files: clang-tools-extra/clang-query/Query.cpp clang-tools-extra/clang-query/QuerySession.h 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)); @@ -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) { Index: clang-tools-extra/clang-query/QuerySession.h =================================================================== --- clang-tools-extra/clang-query/QuerySession.h +++ 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<std::unique_ptr<ASTUnit>> ASTs) + QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> 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<std::unique_ptr<ASTUnit>> ASTs; @@ -34,6 +34,8 @@ bool DiagOutput; bool DetailedASTOutput; + bool ColorOutput; + bool BindRoot; bool PrintMatcher; bool Terminate; Index: clang-tools-extra/clang-query/Query.cpp =================================================================== --- clang-tools-extra/clang-query/Query.cpp +++ 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";
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)); @@ -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) { Index: clang-tools-extra/clang-query/QuerySession.h =================================================================== --- clang-tools-extra/clang-query/QuerySession.h +++ 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<std::unique_ptr<ASTUnit>> ASTs) + QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> 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<std::unique_ptr<ASTUnit>> ASTs; @@ -34,6 +34,8 @@ bool DiagOutput; bool DetailedASTOutput; + bool ColorOutput; + bool BindRoot; bool PrintMatcher; bool Terminate; Index: clang-tools-extra/clang-query/Query.cpp =================================================================== --- clang-tools-extra/clang-query/Query.cpp +++ 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";
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits