clang-tools  8.0.0svn
Query.h
Go to the documentation of this file.
1 //===--- Query.h - clang-query ----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_QUERY_QUERY_H
12 
13 #include "QuerySession.h"
14 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
15 #include "llvm/ADT/IntrusiveRefCntPtr.h"
16 #include "llvm/ADT/Optional.h"
17 #include <string>
18 
19 namespace clang {
20 namespace query {
21 
23 
24 enum QueryKind {
35 };
36 
37 class QuerySession;
38 
39 struct Query : llvm::RefCountedBase<Query> {
40  Query(QueryKind Kind) : Kind(Kind) {}
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  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 {
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 {
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 {
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 
136  T Value;
137 };
138 
139 // Implements the exclusive 'set output dump|diag|print' options.
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 
154 };
155 
156 // Implements the non-exclusive 'set output dump|diag|print' options.
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 
167  bool Value;
168 };
169 
173 
174  static bool classof(const Query *Q) { return Q->Kind == QK_EnableOutputKind; }
175 };
176 
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
std::string Name
Definition: Query.h:106
Query(QueryKind Kind)
Definition: Query.h:40
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:143
EnableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:171
static bool classof(const Query *Q)
Definition: Query.h:68
const QueryKind Kind
Definition: Query.h:48
No-op query (i.e. a blank line).
Definition: Query.h:64
SetNonExclusiveOutputQuery(QueryKind Kind, bool QuerySession::*Var, bool Value)
Definition: Query.h:158
SetExclusiveOutputQuery(bool QuerySession::*Var)
Definition: Query.h:141
static bool classof(const Query *Q)
Definition: Query.h:60
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:124
MatchQuery(StringRef Source, const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:89
static bool classof(const Query *Q)
Definition: Query.h:76
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:131
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:161
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:54
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:55
static bool classof(const Query *Q)
Definition: Query.h:84
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:51
Query for "match MATCHER".
Definition: Query.h:88
static constexpr llvm::StringLiteral Name
static bool classof(const Query *Q)
Definition: Query.h:98
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:102
static bool classof(const Query *Q)
Definition: Query.h:174
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static bool classof(const Query *Q)
Definition: Query.h:181
bool QuerySession::* Var
Definition: Query.h:153
DisableOutputQuery(bool QuerySession::*Var)
Definition: Query.h:178
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:94
Query for "quit".
Definition: Query.h:80
StringRef Source
Definition: Query.h:96
std::string ErrStr
Definition: Query.h:58
Query for "set VAR VALUE".
Definition: Query.h:123
static bool classof(const Query *Q)
Definition: Query.h:151
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:126
static bool classof(const Query *Q)
Definition: Query.h:109
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:107
Query for "help".
Definition: Query.h:72
virtual ~Query()
Definition: Query.cpp:23
T QuerySession::* Var
Definition: Query.h:135