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 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(), @@ -118,20 +117,16 @@ DiagnosticsEngine::Note, "\"" + BI->first + "\" binds here", CharSourceRange::getTokenRange(R), None); } - 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; - } } } 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(&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/QuerySession.h =================================================================== --- clang-query/QuerySession.h +++ clang-query/QuerySession.h @@ -25,11 +25,16 @@ class QuerySession { public: QuerySession(llvm::ArrayRef> ASTs) - : ASTs(ASTs), OutKind(OK_Diag), BindRoot(true), PrintMatcher(false), + : ASTs(ASTs), PrintOutput(false), DiagOutput(true), + DetailedASTOutput(false), BindRoot(true), PrintMatcher(false), Terminate(false) {} llvm::ArrayRef> ASTs; - OutputKind OutKind; + + bool PrintOutput; + bool DiagOutput; + bool DetailedASTOutput; + bool BindRoot; bool PrintMatcher; bool Terminate; Index: unittests/clang-query/QueryEngineTest.cpp =================================================================== --- unittests/clang-query/QueryEngineTest.cpp +++ unittests/clang-query/QueryEngineTest.cpp @@ -95,7 +95,7 @@ Str.clear(); EXPECT_TRUE( - SetQuery(&QuerySession::OutKind, OK_Print).run(OS, S)); + SetExclusiveOutputQuery(&QuerySession::PrintOutput).run(OS, S)); EXPECT_TRUE(MatchQuery(FooMatcherString, FooMatcher).run(OS, S)); EXPECT_TRUE(OS.str().find("Binding for \"root\":\nvoid foo1()") != @@ -104,7 +104,7 @@ Str.clear(); EXPECT_TRUE( - SetQuery(&QuerySession::OutKind, OK_DetailedAST).run(OS, S)); + SetExclusiveOutputQuery(&QuerySession::DetailedASTOutput).run(OS, S)); EXPECT_TRUE(MatchQuery(FooMatcherString, FooMatcher).run(OS, S)); EXPECT_TRUE(OS.str().find("FunctionDecl") != std::string::npos); Index: unittests/clang-query/QueryParserTest.cpp =================================================================== --- unittests/clang-query/QueryParserTest.cpp +++ unittests/clang-query/QueryParserTest.cpp @@ -83,14 +83,12 @@ cast(Q)->ErrStr); Q = parse("set output dump"); - ASSERT_TRUE(isa >(Q)); - EXPECT_EQ(&QuerySession::OutKind, cast >(Q)->Var); - EXPECT_EQ(OK_DetailedAST, cast>(Q)->Value); + ASSERT_TRUE(isa(Q)); + EXPECT_EQ(&QuerySession::DetailedASTOutput, cast(Q)->Var); Q = parse("set output detailed-ast"); - ASSERT_TRUE(isa>(Q)); - EXPECT_EQ(&QuerySession::OutKind, cast>(Q)->Var); - EXPECT_EQ(OK_DetailedAST, cast>(Q)->Value); + ASSERT_TRUE(isa(Q)); + EXPECT_EQ(&QuerySession::DetailedASTOutput, cast(Q)->Var); Q = parse("set bind-root foo"); ASSERT_TRUE(isa(Q));