steveire created this revision.
steveire added a reviewer: aaron.ballman.
Herald added a subscriber: cfe-commits.
This will make it possible to add non-exclusive mode output.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53501

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

Index: clang-query/QuerySession.h
===================================================================
--- clang-query/QuerySession.h
+++ clang-query/QuerySession.h
@@ -25,11 +25,16 @@
 class QuerySession {
 public:
   QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
-      : ASTs(ASTs), OutKind(OK_Diag), BindRoot(true), PrintMatcher(false),
+      : ASTs(ASTs), PrintOutput(true), DiagOutput(true),
+        DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
         Terminate(false) {}
 
   llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;
-  OutputKind OutKind;
+
+  bool PrintOutput;
+  bool DiagOutput;
+  bool DetailedASTOutput;
+
   bool BindRoot;
   bool PrintMatcher;
   bool Terminate;
Index: clang-query/QueryParser.cpp
===================================================================
--- clang-query/QueryParser.cpp
+++ clang-query/QueryParser.cpp
@@ -119,7 +119,17 @@
         "expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr +
         "'");
   }
-  return new SetQuery<OutputKind>(&QuerySession::OutKind, OutputKind(OutKind));
+
+  switch (OutKind) {
+  case OK_DetailedAST:
+    return new SetExclusiveOutputQuery(&QuerySession::DetailedASTOutput);
+  case OK_Diag:
+    return new SetExclusiveOutputQuery(&QuerySession::DiagOutput);
+  case OK_Print:
+    return new SetExclusiveOutputQuery(&QuerySession::PrintOutput);
+  }
+
+  llvm_unreachable("Invalid output kind");
 }
 
 QueryRef QueryParser::endQuery(QueryRef Q) {
Index: clang-query/Query.h
===================================================================
--- clang-query/Query.h
+++ clang-query/Query.h
@@ -10,9 +10,11 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
 
+#include "QuerySession.h"
 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/Optional.h"
+
 #include <string>
 
 namespace clang {
@@ -133,6 +135,23 @@
   T Value;
 };
 
+// Implements the exclusive 'set output dump|diag|print' options
+struct SetExclusiveOutputQuery : Query {
+  SetExclusiveOutputQuery(bool QuerySession::*Var)
+      : Query(QK_SetOutputKind), Var(Var) {}
+  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
+    QS.DiagOutput = false;
+    QS.DetailedASTOutput = false;
+    QS.PrintOutput = false;
+    QS.*Var = true;
+    return true;
+  }
+
+  static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
+
+  bool QuerySession::*Var;
+};
+
 } // namespace query
 } // namespace clang
 
Index: clang-query/Query.cpp
===================================================================
--- clang-query/Query.cpp
+++ clang-query/Query.cpp
@@ -107,8 +107,7 @@
 
       for (auto BI = MI->getMap().begin(), BE = MI->getMap().end(); BI != BE;
            ++BI) {
-        switch (QS.OutKind) {
-        case OK_Diag: {
+        if (QS.DiagOutput) {
           clang::SourceRange R = BI->second.getSourceRange();
           if (R.isValid()) {
             TextDiagnostic TD(OS, AST->getASTContext().getLangOpts(),
@@ -120,19 +119,18 @@
           }
           break;
         }
-        case OK_Print: {
+        if (QS.PrintOutput) {
           OS << "Binding for \"" << BI->first << "\":\n";
           BI->second.print(OS, AST->getASTContext().getPrintingPolicy());
           OS << "\n";
           break;
         }
-        case OK_DetailedAST: {
+        if (QS.DetailedASTOutput) {
           OS << "Binding for \"" << BI->first << "\":\n";
           BI->second.dump(OS, AST->getSourceManager());
           OS << "\n";
           break;
         }
-        }
       }
 
       if (MI->getMap().empty())
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to