clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Query.h
Go to the documentation of this file.
1 //===--- Query.h - clang-query ----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
12
13 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
14 #include "llvm/ADT/IntrusiveRefCntPtr.h"
15 #include "llvm/ADT/Optional.h"
16 #include <string>
17
18 namespace clang {
19 namespace query {
20
21 enum OutputKind {
22 OK_Diag,
23 OK_Print,
24 OK_Dump
25 };
26
27 enum QueryKind {
28 QK_Invalid,
29 QK_NoOp,
30 QK_Help,
31 QK_Let,
32 QK_Match,
33 QK_SetBool,
34 QK_SetOutputKind,
35 QK_Quit
36 };
37
38 class QuerySession;
39
40 struct Query : llvm::RefCountedBase<Query> {
41 Query(QueryKind Kind) : Kind(Kind) {}
42 virtual ~Query();
43
44 /// Perform the query on \p QS and print output to \p OS.
45 ///
46 /// \return false if an error occurs, otherwise return true.
47 virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
48
49 const QueryKind Kind;
50 };
51
52 typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
53
54 /// Any query which resulted in a parse error. The error message is in ErrStr.
55 struct InvalidQuery : Query {
56 InvalidQuery(const Twine &ErrStr) : Query(QK_Invalid), ErrStr(ErrStr.str()) {}
57 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
58
59 std::string ErrStr;
60
61 static bool classof(const Query *Q) { return Q->Kind == QK_Invalid; }
62 };
63
64 /// No-op query (i.e. a blank line).
65 struct NoOpQuery : Query {
66 NoOpQuery() : Query(QK_NoOp) {}
67 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
68
69 static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; }
70 };
71
72 /// Query for "help".
73 struct HelpQuery : Query {
74 HelpQuery() : Query(QK_Help) {}
75 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
76
77 static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
78 };
79
80 /// Query for "quit".
81 struct QuitQuery : Query {
82 QuitQuery() : Query(QK_Quit) {}
83 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
84
85 static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
86 };
87
88 /// Query for "match MATCHER".
89 struct MatchQuery : Query {
90 MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
91 : Query(QK_Match), Matcher(Matcher) {}
92 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
93
94 ast_matchers::dynamic::DynTypedMatcher Matcher;
95
96 static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
97 };
98
99 struct LetQuery : Query {
100 LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
101 : Query(QK_Let), Name(Name), Value(Value) {}
102 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
103
104 std::string Name;
105 ast_matchers::dynamic::VariantValue Value;
106
107 static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
108 };
109
110 template <typename T> struct SetQueryKind {};
111
112 template <> struct SetQueryKind<bool> {
113 static const QueryKind value = QK_SetBool;
114 };
115
116 template <> struct SetQueryKind<OutputKind> {
117 static const QueryKind value = QK_SetOutputKind;
118 };
119
120 /// Query for "set VAR VALUE".
121 template <typename T> struct SetQuery : Query {
122 SetQuery(T QuerySession::*Var, T Value)
123 : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
124 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
125 QS.*Var = Value;
126 return true;
127 }
128
129 static bool classof(const Query *Q) {
130 return Q->Kind == SetQueryKind<T>::value;
131 }
132
133 T QuerySession::*Var;
134 T Value;
135 };
136
137 } // namespace query
138 } // namespace clang
139
140 #endif
clang::query::LetQuery::Name
std::string Name
Definition: Query.h:104
clang::query::LetQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:135
clang::query::QK_SetBool
Definition: Query.h:33
clang::query::Query::Query
Query(QueryKind Kind)
Definition: Query.h:41
clang::query::NoOpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:69
clang::query::QK_Quit
Definition: Query.h:35
clang::query::Query::Kind
const QueryKind Kind
Definition: Query.h:49
clang::query::NoOpQuery
No-op query (i.e. a blank line).
Definition: Query.h:65
clang::query::InvalidQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:61
clang::query::QuerySession
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
clang::query::SetQuery::SetQuery
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:122
clang::query::QK_Match
Definition: Query.h:32
clang::query::OK_Diag
Definition: Query.h:22
clang::query::NoOpQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:30
clang::query::HelpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:77
clang::query::Query::run
virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const =0
Perform the query on QS and print output to OS.
clang::query::SetQuery::Value
T Value
Definition: Query.h:134
clang::query::SetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:129
clang::query::QuitQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:53
clang::query::HelpQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:34
clang::query::Query
Definition: Query.h:40
clang::query::InvalidQuery
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:55
clang::query::InvalidQuery::InvalidQuery
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:56
clang::query::QK_SetOutputKind
Definition: Query.h:34
clang::query::InvalidQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:25
clang::query::QuitQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:85
clang::query::QK_Invalid
Definition: Query.h:28
clang::query::QueryRef
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:52
clang::query::MatchQuery
Query for "match MATCHER".
Definition: Query.h:89
clang::query::QuitQuery::QuitQuery
QuitQuery()
Definition: Query.h:82
clang::query::MatchQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:96
clang::query::OutputKind
OutputKind
Definition: Query.h:21
clang::query::OK_Print
Definition: Query.h:23
clang::query::LetQuery::LetQuery
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:100
clang::query::SetQueryKind
Definition: Query.h:110
clang::query::QK_Let
Definition: Query.h:31
clang::query::MatchQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:70
clang::query::NoOpQuery::NoOpQuery
NoOpQuery()
Definition: Query.h:66
clang::query::MatchQuery::Matcher
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:94
clang::query::QuitQuery
Query for "quit".
Definition: Query.h:81
clang::query::HelpQuery::HelpQuery
HelpQuery()
Definition: Query.h:74
clang::query::OK_Dump
Definition: Query.h:24
clang::query::InvalidQuery::ErrStr
std::string ErrStr
Definition: Query.h:59
clang::query::SetQuery
Query for "set VAR VALUE".
Definition: Query.h:121
clang::query::QueryKind
QueryKind
Definition: Query.h:27
clang::query::SetQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:124
clang::query::LetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:107
clang::query::LetQuery
Definition: Query.h:99
clang::query::LetQuery::Value
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:105
clang::query::HelpQuery
Query for "help".
Definition: Query.h:73
clang::query::QK_Help
Definition: Query.h:30
clang::query::Query::~Query
virtual ~Query()
Definition: Query.cpp:23
clang::query::SetQuery::Var
T QuerySession::* Var
Definition: Query.h:133
clang::query::MatchQuery::MatchQuery
MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:90
clang::query::QK_NoOp
Definition: Query.h:29