[clang-query] Refactor Output settings to booleans

Summary: This will make it possible to add non-exclusive mode output.

Reviewers: aaron.ballman

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D53501

llvm-svn: 345194
This commit is contained in:
Stephen Kelly 2018-10-24 20:33:55 +00:00
parent 51707b21a0
commit 70d771714e
6 changed files with 45 additions and 19 deletions

View File

@ -107,8 +107,7 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
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 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
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;
}
}
}

View File

@ -10,6 +10,7 @@
#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"
@ -133,6 +134,23 @@ template <typename T> struct SetQuery : Query {
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

View File

@ -119,7 +119,17 @@ QueryRef QueryParser::parseSetOutputKind() {
"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) {

View File

@ -25,11 +25,16 @@ namespace query {
class QuerySession {
public:
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> 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<std::unique_ptr<ASTUnit>> ASTs;
OutputKind OutKind;
bool PrintOutput;
bool DiagOutput;
bool DetailedASTOutput;
bool BindRoot;
bool PrintMatcher;
bool Terminate;

View File

@ -95,7 +95,7 @@ TEST_F(QueryEngineTest, Basic) {
Str.clear();
EXPECT_TRUE(
SetQuery<OutputKind>(&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 @@ TEST_F(QueryEngineTest, Basic) {
Str.clear();
EXPECT_TRUE(
SetQuery<OutputKind>(&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);

View File

@ -83,14 +83,12 @@ TEST_F(QueryParserTest, Set) {
cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set output dump");
ASSERT_TRUE(isa<SetQuery<OutputKind> >(Q));
EXPECT_EQ(&QuerySession::OutKind, cast<SetQuery<OutputKind> >(Q)->Var);
EXPECT_EQ(OK_DetailedAST, cast<SetQuery<OutputKind>>(Q)->Value);
ASSERT_TRUE(isa<SetExclusiveOutputQuery >(Q));
EXPECT_EQ(&QuerySession::DetailedASTOutput, cast<SetExclusiveOutputQuery>(Q)->Var);
Q = parse("set output detailed-ast");
ASSERT_TRUE(isa<SetQuery<OutputKind>>(Q));
EXPECT_EQ(&QuerySession::OutKind, cast<SetQuery<OutputKind>>(Q)->Var);
EXPECT_EQ(OK_DetailedAST, cast<SetQuery<OutputKind>>(Q)->Value);
ASSERT_TRUE(isa<SetExclusiveOutputQuery>(Q));
EXPECT_EQ(&QuerySession::DetailedASTOutput, cast<SetExclusiveOutputQuery>(Q)->Var);
Q = parse("set bind-root foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));