clang-tools  9.0.0svn
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 {
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  const QueryKind Kind;
48 };
49 
50 typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
51 
52 /// Any query which resulted in a parse error. The error message is in ErrStr.
53 struct InvalidQuery : Query {
54  InvalidQuery(const Twine &ErrStr) : Query(QK_Invalid), ErrStr(ErrStr.str()) {}
55  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
56 
57  std::string ErrStr;
58 
59  static bool classof(const Query *Q) { return Q->Kind == QK_Invalid; }
60 };
61 
62 /// No-op query (i.e. a blank line).
63 struct NoOpQuery : Query {
65  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
66 
67  static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; }
68 };
69 
70 /// Query for "help".
71 struct HelpQuery : Query {
73  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
74 
75  static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
76 };
77 
78 /// Query for "quit".
79 struct QuitQuery : Query {
81  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
82 
83  static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
84 };
85 
86 /// Query for "match MATCHER".
87 struct MatchQuery : Query {
88  MatchQuery(StringRef Source,
89  const ast_matchers::dynamic::DynTypedMatcher &Matcher)
90  : Query(QK_Match), Matcher(Matcher), Source(Source) {}
91  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
92 
93  ast_matchers::dynamic::DynTypedMatcher Matcher;
94 
95  StringRef Source;
96 
97  static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
98 };
99 
100 struct LetQuery : Query {
101  LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
102  : Query(QK_Let), Name(Name), Value(Value) {}
103  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
104 
105  std::string Name;
106  ast_matchers::dynamic::VariantValue Value;
107 
108  static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
109 };
110 
111 template <typename T> struct SetQueryKind {};
112 
113 template <> struct SetQueryKind<bool> {
114  static const QueryKind value = QK_SetBool;
115 };
116 
117 template <> struct SetQueryKind<OutputKind> {
118  static const QueryKind value = QK_SetOutputKind;
119 };
120 
121 /// Query for "set VAR VALUE".
122 template <typename T> struct SetQuery : Query {
123  SetQuery(T QuerySession::*Var, T Value)
124  : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
125  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
126  QS.*Var = Value;
127  return true;
128  }
129 
130  static bool classof(const Query *Q) {
131  return Q->Kind == SetQueryKind<T>::value;
132  }
133 
135  T Value;
136 };
137 
138 // Implements the exclusive 'set output dump|diag|print' options.
141  : Query(QK_SetOutputKind), Var(Var) {}
142  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
143  QS.DiagOutput = false;
144  QS.DetailedASTOutput = false;
145  QS.PrintOutput = false;
146  QS.*Var = true;
147  return true;
148  }
149 
150  static bool classof(const Query *Q) { return Q->Kind == QK_SetOutputKind; }
151 
153 };
154 
155 // Implements the non-exclusive 'set output dump|diag|print' options.
158  bool Value)
159  : Query(Kind), Var(Var), Value(Value) {}
160  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
161  QS.*Var = Value;
162  return true;
163  }
164 
166  bool Value;
167 };
168 
172 
173  static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
174 };
175 
179 
180  static bool classof(const Query *Q) {
181  return Q->Kind == QK_DisableOutputKind;
182  }
183 };
184 
185 } // namespace query
186 } // namespace clang
187 
188 #endif
std::string Name
Definition: Query.h:105
Query(QueryKind Kind)
Definition: Query.h:39
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:142
EnableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:170
static bool classof(const Query *Q)
Definition: Query.h:67
const QueryKind Kind
Definition: Query.h:47
No-op query (i.e. a blank line).
Definition: Query.h:63
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var, bool Value)
Definition: Query.h:157
SetExclusiveOutputQuery(bool QuerySession::*Var)
Definition: Query.h:140
static bool classof(const Query *Q)
Definition: Query.h:59
Represents the state for a particular clang-query session.
Definition: QuerySession.h:23
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:123
MatchQuery(StringRef Source, const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:88
static bool classof(const Query *Q)
Definition: Query.h:75
virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const =0
Perform the query on QS and print output to OS.
static bool classof(const Query *Q)
Definition: Query.h:130
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:160
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:53
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:54
static bool classof(const Query *Q)
Definition: Query.h:83
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:50
Query for "match MATCHER".
Definition: Query.h:87
static constexpr llvm::StringLiteral Name
static bool classof(const Query *Q)
Definition: Query.h:97
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:101
static bool classof(const Query *Q)
Definition: Query.h:173
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool classof(const Query *Q)
Definition: Query.h:180
bool QuerySession::* Var
Definition: Query.h:152
DisableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:177
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:93
Query for "quit".
Definition: Query.h:79
StringRef Source
Definition: Query.h:95
std::string ErrStr
Definition: Query.h:57
Query for "set VAR VALUE".
Definition: Query.h:122
static bool classof(const Query *Q)
Definition: Query.h:150
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:125
static bool classof(const Query *Q)
Definition: Query.h:108
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:106
Query for "help".
Definition: Query.h:71
virtual ~Query()
Definition: Query.cpp:22
T QuerySession::* Var
Definition: Query.h:134