[PATCH] D94624: [PATCH] [clang-query] Add a --use-color option to clang-query
Tom Ritter via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 14 07:07:33 PST 2021
tomrittervg updated this revision to Diff 316652.
tomrittervg added a comment.
Actually, I think I need to be smarter than changing the default. We want to let clang auto-detect the tty and behave that way by default if the option isn't specified. Otherwise you'd get ASNI color codes when you pipe to a file.
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/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,12 @@
assert(Status == 0 && "Unexpected status returned");
}
- QuerySession QS(ASTs);
+ // If a command-line switch is not specified, let clang auto-detect a tty and
+ // behave that way
+ bool useColor = UseColor.getNumOccurrences() > 0
+ ? UseColor
+ : SM.getDiagnostics().getShowColors();
+ 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";
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94624.316652.patch
Type: text/x-patch
Size: 2990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210114/ab12dbb2/attachment.bin>
More information about the cfe-commits
mailing list