annotate clang-tools-extra/clang-query/Query.h @ 221:79ff65ed7e25

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