aaron.ballman created this revision.
aaron.ballman added reviewers: klimek, alexfh.
aaron.ballman added a subscriber: cfe-commits.

I would like to propose adding a clang-query command called 'quit' that 
terminates the clang-query interactive session. While Ctrl+C works well enough 
to terminate the application, it's not particularly discoverable, and it runs 
into an interesting conflict on Windows 10 where the terminal has received some 
love recently. Ctrl+C will perform a copy operation in the terminal now, 
depending on whether any text is selected or not. Muscle memory has made for 
some annoying mistakes recently for me.

I put together a patch that implements this functionality in terms of 
terminating the query session object.

Thanks!

http://reviews.llvm.org/D11761

Files:
  clang-query/Query.cpp
  clang-query/Query.h
  clang-query/QueryParser.cpp
  clang-query/QuerySession.h
  clang-query/tool/ClangQuery.cpp

Index: clang-query/tool/ClangQuery.cpp
===================================================================
--- clang-query/tool/ClangQuery.cpp
+++ clang-query/tool/ClangQuery.cpp
@@ -111,6 +111,8 @@
       QueryRef Q = QueryParser::parse(*Line, QS);
       Q->run(llvm::outs(), QS);
       llvm::outs().flush();
+      if (QS.Terminate)
+        break;
     }
   }
 
Index: clang-query/QuerySession.h
===================================================================
--- clang-query/QuerySession.h
+++ clang-query/QuerySession.h
@@ -25,11 +25,12 @@
 class QuerySession {
 public:
   QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
-      : ASTs(ASTs), OutKind(OK_Diag), BindRoot(true) {}
+      : ASTs(ASTs), OutKind(OK_Diag), BindRoot(true), Terminate(false) {}
 
   llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;
   OutputKind OutKind;
   bool BindRoot;
+  bool Terminate;
   llvm::StringMap<ast_matchers::dynamic::VariantValue> NamedValues;
 };
 
Index: clang-query/QueryParser.cpp
===================================================================
--- clang-query/QueryParser.cpp
+++ clang-query/QueryParser.cpp
@@ -142,6 +142,7 @@
   PQK_Match,
   PQK_Set,
   PQK_Unlet,
+  PQK_Quit
 };
 
 enum ParsedQueryVariable {
@@ -181,6 +182,7 @@
                               .Case("match", PQK_Match)
                               .Case("set", PQK_Set)
                               .Case("unlet", PQK_Unlet)
+                              .Case("quit", PQK_Quit)
                               .Default(PQK_Invalid);
 
   switch (QKind) {
@@ -190,6 +192,9 @@
   case PQK_Help:
     return endQuery(new HelpQuery);
 
+  case PQK_Quit:
+    return endQuery(new QuitQuery);
+
   case PQK_Let: {
     StringRef Name = lexWord();
 
Index: clang-query/Query.h
===================================================================
--- clang-query/Query.h
+++ clang-query/Query.h
@@ -32,6 +32,7 @@
   QK_Match,
   QK_SetBool,
   QK_SetOutputKind,
+  QK_Quit
 };
 
 class QuerySession;
@@ -76,6 +77,14 @@
   static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
 };
 
+/// Query for "quit".
+struct QuitQuery : Query {
+  QuitQuery() : Query(QK_Quit) {}
+  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
+
+  static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
+};
+
 /// Query for "match MATCHER".
 struct MatchQuery : Query {
   MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Index: clang-query/Query.cpp
===================================================================
--- clang-query/Query.cpp
+++ clang-query/Query.cpp
@@ -44,10 +44,17 @@
         "  set output (diag|print|dump)      "
         "Set whether to print bindings as diagnostics,\n"
         "                                    "
-        "AST pretty prints or AST dumps.\n\n";
+        "AST pretty prints or AST dumps.\n"
+        "  quit                              "
+        "Terminates the query session.\n\n";
   return true;
 }
 
+bool QuitQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
+  QS.Terminate = true;
+  return true;
+}
+
 namespace {
 
 struct CollectBoundNodes : MatchFinder::MatchCallback {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to