[clang-tools-extra] r345194 - [clang-query] Refactor Output settings to booleans
Stephen Kelly via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 24 13:33:55 PDT 2018
Author: steveire
Date: Wed Oct 24 13:33:55 2018
New Revision: 345194
URL: http://llvm.org/viewvc/llvm-project?rev=345194&view=rev
Log:
[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
Modified:
clang-tools-extra/trunk/clang-query/Query.cpp
clang-tools-extra/trunk/clang-query/Query.h
clang-tools-extra/trunk/clang-query/QueryParser.cpp
clang-tools-extra/trunk/clang-query/QuerySession.h
clang-tools-extra/trunk/unittests/clang-query/QueryEngineTest.cpp
clang-tools-extra/trunk/unittests/clang-query/QueryParserTest.cpp
Modified: clang-tools-extra/trunk/clang-query/Query.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/Query.cpp?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/Query.cpp (original)
+++ clang-tools-extra/trunk/clang-query/Query.cpp Wed Oct 24 13:33:55 2018
@@ -107,8 +107,7 @@ bool MatchQuery::run(llvm::raw_ostream &
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 &
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;
- }
}
}
Modified: clang-tools-extra/trunk/clang-query/Query.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/Query.h?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/Query.h (original)
+++ clang-tools-extra/trunk/clang-query/Query.h Wed Oct 24 13:33:55 2018
@@ -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 :
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
Modified: clang-tools-extra/trunk/clang-query/QueryParser.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/QueryParser.cpp?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/QueryParser.cpp (original)
+++ clang-tools-extra/trunk/clang-query/QueryParser.cpp Wed Oct 24 13:33:55 2018
@@ -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) {
Modified: clang-tools-extra/trunk/clang-query/QuerySession.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-query/QuerySession.h?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-query/QuerySession.h (original)
+++ clang-tools-extra/trunk/clang-query/QuerySession.h Wed Oct 24 13:33:55 2018
@@ -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;
Modified: clang-tools-extra/trunk/unittests/clang-query/QueryEngineTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-query/QueryEngineTest.cpp?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-query/QueryEngineTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-query/QueryEngineTest.cpp Wed Oct 24 13:33:55 2018
@@ -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);
Modified: clang-tools-extra/trunk/unittests/clang-query/QueryParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-query/QueryParserTest.cpp?rev=345194&r1=345193&r2=345194&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-query/QueryParserTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-query/QueryParserTest.cpp Wed Oct 24 13:33:55 2018
@@ -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));
More information about the cfe-commits
mailing list