clang-tools 20.0.0git
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 <string>
16
17namespace clang {
18namespace query {
19
21
35};
36
37class QuerySession;
38
39struct Query : llvm::RefCountedBase<Query> {
41 virtual ~Query();
42
43 /// Perform the query on \p QS and print output to \p OS.
44 ///
45 /// \return false if an error occurs, otherwise return true.
46 virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
47
50};
51
52typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
53
54/// Any query which resulted in a parse error. The error message is in ErrStr.
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).
65struct NoOpQuery : Query {
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".
73struct HelpQuery : Query {
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".
81struct QuitQuery : Query {
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".
89struct MatchQuery : Query {
90 MatchQuery(StringRef Source,
91 const ast_matchers::dynamic::DynTypedMatcher &Matcher)
93 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
94
95 ast_matchers::dynamic::DynTypedMatcher Matcher;
96
97 StringRef Source;
98
99 static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
100};
101
102struct LetQuery : Query {
103 LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
104 : Query(QK_Let), Name(Name), Value(Value) {}
105 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
106
107 std::string Name;
108 ast_matchers::dynamic::VariantValue Value;
109
110 static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
111};
112
113template <typename T> struct SetQueryKind {};
114
115template <> struct SetQueryKind<bool> {
116 static const QueryKind value = QK_SetBool;
117};
118
119template <> struct SetQueryKind<OutputKind> {
120 static const QueryKind value = QK_SetOutputKind;
121};
122
123template <> struct SetQueryKind<TraversalKind> {
124 static const QueryKind value = QK_SetTraversalKind;
125};
126
127/// Query for "set VAR VALUE".
128template <typename T> struct SetQuery : Query {
130 : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
131 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
132 QS.*Var = Value;
133 return true;
134 }
135
136 static bool classof(const Query *Q) {
137 return Q->Kind == SetQueryKind<T>::value;
138 }
139
142};
143
144// Implements the exclusive 'set output dump|diag|print' options.
148 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
149 QS.DiagOutput = false;
150 QS.DetailedASTOutput = false;
151 QS.PrintOutput = false;
152 QS.*Var = true;
153 return true;
154 }
155
156 static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
157
159};
160
161// Implements the non-exclusive 'set output dump|diag|print' options.
164 bool Value)
165 : Query(Kind), Var(Var), Value(Value) {}
166 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
167 QS.*Var = Value;
168 return true;
169 }
170
172 bool Value;
173};
174
178
179 static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
180};
181
185
186 static bool classof(const Query *Q) {
187 return Q->Kind == QK_DisableOutputKind;
188 }
189};
190
191struct FileQuery : Query {
192 FileQuery(StringRef File, StringRef Prefix = StringRef())
193 : Query(QK_File), File(File),
194 Prefix(!Prefix.empty() ? std::optional<std::string>(Prefix)
195 : std::nullopt) {}
196
197 bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
198
199 static bool classof(const Query *Q) { return Q->Kind == QK_File; }
200
201private:
202 std::string File;
203 std::optional<std::string> Prefix;
204};
205
206} // namespace query
207} // namespace clang
208
209#endif
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
Represents the state for a particular clang-query session.
Definition: QuerySession.h:24
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:52
@ OK_Diag
Definition: Query.h:20
@ OK_Print
Definition: Query.h:20
@ OK_DetailedAST
Definition: Query.h:20
@ QK_Help
Definition: Query.h:25
@ QK_Invalid
Definition: Query.h:23
@ QK_DisableOutputKind
Definition: Query.h:32
@ QK_NoOp
Definition: Query.h:24
@ QK_EnableOutputKind
Definition: Query.h:31
@ QK_Let
Definition: Query.h:26
@ QK_Match
Definition: Query.h:27
@ QK_Quit
Definition: Query.h:33
@ QK_File
Definition: Query.h:34
@ QK_SetOutputKind
Definition: Query.h:29
@ QK_SetTraversalKind
Definition: Query.h:30
@ QK_SetBool
Definition: Query.h:28
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
-clang-tidy
DisableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:183
static bool classof(const Query *Q)
Definition: Query.h:186
EnableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:176
static bool classof(const Query *Q)
Definition: Query.h:179
FileQuery(StringRef File, StringRef Prefix=StringRef())
Definition: Query.h:192
static bool classof(const Query *Q)
Definition: Query.h:199
Query for "help".
Definition: Query.h:73
static bool classof(const Query *Q)
Definition: Query.h:77
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:55
static bool classof(const Query *Q)
Definition: Query.h:61
std::string ErrStr
Definition: Query.h:59
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:56
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:103
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:108
std::string Name
Definition: Query.h:107
static bool classof(const Query *Q)
Definition: Query.h:110
Query for "match MATCHER".
Definition: Query.h:89
MatchQuery(StringRef Source, const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:90
StringRef Source
Definition: Query.h:97
static bool classof(const Query *Q)
Definition: Query.h:99
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:95
No-op query (i.e. a blank line).
Definition: Query.h:65
static bool classof(const Query *Q)
Definition: Query.h:69
virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const =0
Perform the query on QS and print output to OS.
virtual ~Query()
Definition: Query.cpp:25
StringRef RemainingContent
Definition: Query.h:48
Query(QueryKind Kind)
Definition: Query.h:40
const QueryKind Kind
Definition: Query.h:49
Query for "quit".
Definition: Query.h:81
static bool classof(const Query *Q)
Definition: Query.h:85
SetExclusiveOutputQuery(bool QuerySession::*Var)
Definition: Query.h:146
static bool classof(const Query *Q)
Definition: Query.h:156
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:148
bool QuerySession::* Var
Definition: Query.h:158
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var, bool Value)
Definition: Query.h:163
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:166
Query for "set VAR VALUE".
Definition: Query.h:128
T QuerySession::* Var
Definition: Query.h:140
static bool classof(const Query *Q)
Definition: Query.h:136
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:131
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:129