clang-tools  14.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 "llvm/ADT/Optional.h"
16 #include <string>
17 
18 namespace clang {
19 namespace query {
20 
22 
23 enum QueryKind {
35 };
36 
37 class QuerySession;
38 
39 struct 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 
48  StringRef RemainingContent;
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 {
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 {
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 {
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(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 
102 struct 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 
113 template <typename T> struct SetQueryKind {};
114 
115 template <> struct SetQueryKind<bool> {
116  static const QueryKind value = QK_SetBool;
117 };
118 
119 template <> struct SetQueryKind<OutputKind> {
120  static const QueryKind value = QK_SetOutputKind;
121 };
122 
123 template <> struct SetQueryKind<TraversalKind> {
124  static const QueryKind value = QK_SetTraversalKind;
125 };
126 
127 /// Query for "set VAR VALUE".
128 template <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 
141  T Value;
142 };
143 
144 // Implements the exclusive 'set output dump|diag|print' options.
147  : Query(QK_SetOutputKind), Var(Var) {}
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.SrcLocOutput = false;
153  QS.*Var = true;
154  return true;
155  }
156 
157  static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
158 
160 };
161 
162 // Implements the non-exclusive 'set output dump|diag|print' options.
165  bool Value)
166  : Query(Kind), Var(Var), Value(Value) {}
167  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
168  QS.*Var = Value;
169  return true;
170  }
171 
173  bool Value;
174 };
175 
179 
180  static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
181 };
182 
186 
187  static bool classof(const Query *Q) {
188  return Q->Kind == QK_DisableOutputKind;
189  }
190 };
191 
192 } // namespace query
193 } // namespace clang
194 
195 #endif
clang::query::QueryRef
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:52
clang::query::LetQuery::Value
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:108
clang::query::QuerySession::DiagOutput
bool DiagOutput
Definition: QuerySession.h:34
clang::query::SetNonExclusiveOutputQuery::SetNonExclusiveOutputQuery
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var, bool Value)
Definition: Query.h:164
clang::query::QK_Match
@ QK_Match
Definition: Query.h:28
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:269
clang::query::OutputKind
OutputKind
Definition: Query.h:21
clang::query::QK_SetTraversalKind
@ QK_SetTraversalKind
Definition: Query.h:31
clang::query::MatchQuery::MatchQuery
MatchQuery(StringRef Source, const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:90
clang::query::InvalidQuery
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:55
clang::query::EnableOutputQuery::EnableOutputQuery
EnableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:177
clang::query::LetQuery::LetQuery
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:103
clang::query::OK_SrcLoc
@ OK_SrcLoc
Definition: Query.h:21
clang::query::InvalidQuery::InvalidQuery
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:56
clang::query::InvalidQuery::ErrStr
std::string ErrStr
Definition: Query.h:59
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:26
clang::query::OK_DetailedAST
@ OK_DetailedAST
Definition: Query.h:21
clang::query::SetExclusiveOutputQuery::SetExclusiveOutputQuery
SetExclusiveOutputQuery(bool QuerySession::*Var)
Definition: Query.h:146
clang::query::SetQuery::Value
T Value
Definition: Query.h:141
clang::query::SetNonExclusiveOutputQuery::Value
bool Value
Definition: Query.h:173
clang::query::QuerySession::SrcLocOutput
bool SrcLocOutput
Definition: QuerySession.h:36
clang::query::NoOpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:69
clang::query::QK_Help
@ QK_Help
Definition: Query.h:26
clang::query::HelpQuery::HelpQuery
HelpQuery()
Definition: Query.h:74
clang::query::SetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:136
clang::query::OK_Diag
@ OK_Diag
Definition: Query.h:21
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:77
clang::query::Query
Definition: Query.h:39
clang::query::SetQuery::SetQuery
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:129
clang::query::Query::~Query
virtual ~Query()
Definition: Query.cpp:24
clang::query::SetNonExclusiveOutputQuery
Definition: Query.h:163
clang::query::DisableOutputQuery::DisableOutputQuery
DisableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:184
clang::query::HelpQuery
Query for "help".
Definition: Query.h:73
clang::query::SetNonExclusiveOutputQuery::Var
bool QuerySession::* Var
Definition: Query.h:172
clang::query::QuitQuery
Query for "quit".
Definition: Query.h:81
clang::query::MatchQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:99
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::QK_SetOutputKind
@ QK_SetOutputKind
Definition: Query.h:30
clang::query::InvalidQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:61
clang::query::QK_DisableOutputKind
@ QK_DisableOutputKind
Definition: Query.h:33
clang::query::SetExclusiveOutputQuery::Var
bool QuerySession::* Var
Definition: Query.h:159
clang::query::QK_EnableOutputKind
@ QK_EnableOutputKind
Definition: Query.h:32
clang::query::QuerySession::PrintOutput
bool PrintOutput
Definition: QuerySession.h:33
clang::query::Query::Query
Query(QueryKind Kind)
Definition: Query.h:40
clang::query::QuitQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:85
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:31
clang::query::LetQuery::Name
std::string Name
Definition: Query.h:107
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:167
clang::query::MatchQuery
Query for "match MATCHER".
Definition: Query.h:89
clang::query::Query::RemainingContent
StringRef RemainingContent
Definition: Query.h:48
clang::query::SetExclusiveOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:157
clang::query::SetQuery::Var
T QuerySession::* Var
Definition: Query.h:140
clang::query::EnableOutputQuery
Definition: Query.h:176
clang::query::DisableOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:187
clang::query::QK_Let
@ QK_Let
Definition: Query.h:27
clang::query::QuitQuery::QuitQuery
QuitQuery()
Definition: Query.h:82
clang::query::QueryKind
QueryKind
Definition: Query.h:23
clang::query::MatchQuery::Source
StringRef Source
Definition: Query.h:97
clang::query::QK_NoOp
@ QK_NoOp
Definition: Query.h:25
clang::query::EnableOutputQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:180
clang::query::SetQuery
Query for "set VAR VALUE".
Definition: Query.h:128
clang::query::NoOpQuery::NoOpQuery
NoOpQuery()
Definition: Query.h:66
clang::query::QK_Invalid
@ QK_Invalid
Definition: Query.h:24
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:148
clang::query::SetQueryKind
Definition: Query.h:113
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:131
clang::query::LetQuery
Definition: Query.h:102
clang::query::DisableOutputQuery
Definition: Query.h:183
clang::query::NoOpQuery
No-op query (i.e. a blank line).
Definition: Query.h:65
clang::query::QK_SetBool
@ QK_SetBool
Definition: Query.h:29
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::query::HelpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:77
clang::query::QuerySession::DetailedASTOutput
bool DetailedASTOutput
Definition: QuerySession.h:35
clang::query::MatchQuery::Matcher
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:95
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:178
clang::query::SetExclusiveOutputQuery
Definition: Query.h:145
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:35
clang::query::Query::Kind
const QueryKind Kind
Definition: Query.h:49
clang::query::QK_Quit
@ QK_Quit
Definition: Query.h:34
clang::query::LetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:110
clang::query::QuerySession
Represents the state for a particular clang-query session.
Definition: QuerySession.h:24
clang::query::OK_Print
@ OK_Print
Definition: Query.h:21
QuerySession.h