clang-tools 10.0.0

Query.h
Go to the documentation of this file.
1 //===--- Query.h - clang-query ----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
11
12 #include "QuerySession.h"
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 { OK_Diag, OK_Print, OK_DetailedAST };
22
23 enum QueryKind {
24 QK_Invalid,
25 QK_NoOp,
26 QK_Help,
27 QK_Let,
28 QK_Match,
29 QK_SetBool,
30 QK_SetOutputKind,
31 QK_EnableOutputKind,
32 QK_DisableOutputKind,
33 QK_Quit
34 };
35
36 class QuerySession;
37
38 struct Query : llvm::RefCountedBase<Query> {
39 Query(QueryKind Kind) : Kind(Kind) {}
40 virtual ~Query();
41
42 /// Perform the query on \p QS and print output to \p OS.
43 ///
44 /// \return false if an error occurs, otherwise return true.
45 virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
46
47 StringRef RemainingContent;
48 const QueryKind Kind;
49 };
50
51 typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
52
53 /// Any query which resulted in a parse error. The error message is in ErrStr.
54 struct InvalidQuery : Query {
55 InvalidQuery(const Twine &ErrStr) : Query(QK_Invalid), ErrStr(ErrStr.str()) {}
56 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
57
58 std::string ErrStr;
59
60 static bool classof(const Query *Q) { return Q->Kind == QK_Invalid; }
61 };
62
63 /// No-op query (i.e. a blank line).
64 struct NoOpQuery : Query {
65 NoOpQuery() : Query(QK_NoOp) {}
66 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
67
68 static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; }
69 };
70
71 /// Query for "help".
72 struct HelpQuery : Query {
73 HelpQuery() : Query(QK_Help) {}
74 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
75
76 static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
77 };
78
79 /// Query for "quit".
80 struct QuitQuery : Query {
81 QuitQuery() : Query(QK_Quit) {}
82 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
83
84 static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
85 };
86
87 /// Query for "match MATCHER".
88 struct MatchQuery : Query {
89 MatchQuery(StringRef Source,
90 const ast_matchers::dynamic::DynTypedMatcher &Matcher)
91 : Query(QK_Match), Matcher(Matcher), Source(Source) {}
92 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
93
94 ast_matchers::dynamic::DynTypedMatcher Matcher;
95
96 StringRef Source;
97
98 static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
99 };
100
101 struct LetQuery : Query {
102 LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
103 : Query(QK_Let), Name(Name), Value(Value) {}
104 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
105
106 std::string Name;
107 ast_matchers::dynamic::VariantValue Value;
108
109 static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
110 };
111
112 template <typename T> struct SetQueryKind {};
113
114 template <> struct SetQueryKind<bool> {
115 static const QueryKind value = QK_SetBool;
116 };
117
118 template <> struct SetQueryKind<OutputKind> {
119 static const QueryKind value = QK_SetOutputKind;
120 };
121
122 /// Query for "set VAR VALUE".
123 template <typename T> struct SetQuery : Query {
124 SetQuery(T QuerySession::*Var, T Value)
125 : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
126 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
127 QS.*Var = Value;
128 return true;
129 }
130
131 static bool classof(const Query *Q) {
132 return Q->Kind == SetQueryKind<T>::value;
133 }
134
135 T QuerySession::*Var;
136 T Value;
137 };
138
139 // Implements the exclusive 'set output dump|diag|print' options.
140 struct SetExclusiveOutputQuery : Query {
141 SetExclusiveOutputQuery(bool QuerySession::*Var)
142 : Query(QK_SetOutputKind), Var(Var) {}
143 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
144 QS.DiagOutput = false;
145 QS.DetailedASTOutput = false;
146 QS.PrintOutput = false;
147 QS.*Var = true;
148 return true;
149 }
150
151 static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
152
153 bool QuerySession::*Var;
154 };
155
156 // Implements the non-exclusive 'set output dump|diag|print' options.
157 struct SetNonExclusiveOutputQuery : Query {
158 SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var,
159 bool Value)
160 : Query(Kind), Var(Var), Value(Value) {}
161 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
162 QS.*Var = Value;
163 return true;
164 }
165
166 bool QuerySession::*Var;
167 bool Value;
168 };
169
170 struct EnableOutputQuery : SetNonExclusiveOutputQuery {
171 EnableOutputQuery(bool QuerySession::*Var)
172 : SetNonExclusiveOutputQuery(QK_EnableOutputKind, Var, true) {}
173
174 static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
175 };
176
177 struct DisableOutputQuery : SetNonExclusiveOutputQuery {
178 DisableOutputQuery(bool QuerySession::*Var)
179 : SetNonExclusiveOutputQuery(QK_DisableOutputKind, Var, false) {}
180
181 static bool classof(const Query *Q) {
182 return Q->Kind == QK_DisableOutputKind;
183 }
184 };
185
186 } // namespace query
187 } // namespace clang
188
189 #endif
clang::query::LetQuery::Name
std::string Name
Definition: Query.h:106
clang::query::OK_DetailedAST
Definition: Query.h:21
clang::query::QK_DisableOutputKind
Definition: Query.h:32
clang::query::QK_SetBool
Definition: Query.h:29
clang::query::Query::Query
Query(QueryKind Kind)
Definition: Query.h:39
clang::query::DisableOutputQuery
Definition: Query.h:177
clang::query::SetExclusiveOutputQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:143
clang::query::EnableOutputQuery::EnableOutputQuery
EnableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:171
clang::query::NoOpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:68
clang::query::QK_Quit
Definition: Query.h:33
clang::query::Query::Kind
const QueryKind Kind
Definition: Query.h:48
clang::query::NoOpQuery
No-op query (i.e. a blank line).
Definition: Query.h:64
clang::query::SetNonExclusiveOutputQuery::SetNonExclusiveOutputQuery
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var, bool Value)
Definition: Query.h:158
clang::query::SetExclusiveOutputQuery::SetExclusiveOutputQuery
SetExclusiveOutputQuery(bool QuerySession::*Var)
Definition: Query.h:141
clang::query::InvalidQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:60
clang::query::QuerySession
Represents the state for a particular clang-query session.
Definition: QuerySession.h:23
clang::query::SetQuery::SetQuery
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:124
clang::query::QK_Match
Definition: Query.h:28
clang::query::MatchQuery::MatchQuery
MatchQuery(StringRef Source, const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:89
clang::query::OK_Diag
Definition: Query.h:21
clang::query::HelpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:76
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::SetNonExclusiveOutputQuery
Definition: Query.h:157
clang::query::SetQuery::Value
T Value
Definition: Query.h:136
clang::query::SetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:131
clang::query::SetNonExclusiveOutputQuery::run
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:161
clang::query::Query
Definition: Query.h:38
clang::query::QuerySession::DiagOutput
bool DiagOutput
Definition: QuerySession.h:33
clang::query::InvalidQuery
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:54
clang::query::InvalidQuery::InvalidQuery
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:55
clang::query::QK_SetOutputKind
Definition: Query.h:30
clang::query::SetExclusiveOutputQuery
Definition: Query.h:140
clang::query::QuitQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:84
clang::query::QK_Invalid
Definition: Query.h:24
clang::query::SetNonExclusiveOutputQuery::Value
bool Value
Definition: Query.h:167
QuerySession.h
clang::query::QueryRef
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:51
clang::query::MatchQuery
Query for "match MATCHER".
Definition: Query.h:88
clang::query::EnableOutputQuery
Definition: Query.h:170
clang::query::QuerySession::DetailedASTOutput
bool DetailedASTOutput
Definition: QuerySession.h:34
clang::query::QuitQuery::QuitQuery
QuitQuery()
Definition: Query.h:81
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::query::MatchQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:98
clang::query::OutputKind
OutputKind
Definition: Query.h:21
clang::query::QuerySession::PrintOutput
bool PrintOutput
Definition: QuerySession.h:32
clang::query::QK_EnableOutputKind
Definition: Query.h:31
clang::query::Query::RemainingContent
StringRef RemainingContent
Definition: Query.h:47
clang::query::OK_Print
Definition: Query.h:21
clang::query::LetQuery::LetQuery
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:102
clang::query::SetQueryKind
Definition: Query.h:112
clang::query::EnableOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:174
clang::query::QK_Let
Definition: Query.h:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::query::DisableOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:181
clang::query::SetExclusiveOutputQuery::Var
bool QuerySession::* Var
Definition: Query.h:153
clang::query::NoOpQuery::NoOpQuery
NoOpQuery()
Definition: Query.h:65
clang::query::DisableOutputQuery::DisableOutputQuery
DisableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:178
clang::query::MatchQuery::Matcher
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:94
clang::query::QuitQuery
Query for "quit".
Definition: Query.h:80
clang::query::HelpQuery::HelpQuery
HelpQuery()
Definition: Query.h:73
clang::query::MatchQuery::Source
StringRef Source
Definition: Query.h:96
clang::query::InvalidQuery::ErrStr
std::string ErrStr
Definition: Query.h:58
clang::query::SetQuery
Query for "set VAR VALUE".
Definition: Query.h:123
clang::query::SetExclusiveOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:151
clang::query::QueryKind
QueryKind
Definition: Query.h:23
clang::query::SetNonExclusiveOutputQuery::Var
bool QuerySession::* Var
Definition: Query.h:166
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:126
clang::query::LetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:109
clang::query::LetQuery
Definition: Query.h:101
clang::query::LetQuery::Value
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:107
clang::query::HelpQuery
Query for "help".
Definition: Query.h:72
clang::query::QK_Help
Definition: Query.h:26
clang::query::Query::~Query
virtual ~Query()
Definition: Query.cpp:23
clang::query::SetQuery::Var
T QuerySession::* Var
Definition: Query.h:135
clang::query::QK_NoOp
Definition: Query.h:25