clang-tools  3.8.0
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 "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 
21 enum OutputKind {
25 };
26 
27 enum QueryKind {
36 };
37 
38 class QuerySession;
39 
40 struct Query : llvm::RefCountedBase<Query> {
41  Query(QueryKind Kind) : Kind(Kind) {}
42  virtual ~Query();
43 
44  /// Perform the query on \p QS and print output to \p OS.
45  ///
46  /// \return false if an error occurs, otherwise return true.
47  virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
48 
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(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
91  : Query(QK_Match), Matcher(Matcher) {}
92  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
93 
94  ast_matchers::dynamic::DynTypedMatcher Matcher;
95 
96  static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
97 };
98 
99 struct LetQuery : Query {
100  LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
101  : Query(QK_Let), Name(Name), Value(Value) {}
102  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
103 
104  std::string Name;
105  ast_matchers::dynamic::VariantValue Value;
106 
107  static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
108 };
109 
110 template <typename T> struct SetQueryKind {};
111 
112 template <> struct SetQueryKind<bool> {
113  static const QueryKind value = QK_SetBool;
114 };
115 
116 template <> struct SetQueryKind<OutputKind> {
117  static const QueryKind value = QK_SetOutputKind;
118 };
119 
120 /// Query for "set VAR VALUE".
121 template <typename T> struct SetQuery : Query {
123  : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
124  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
125  QS.*Var = Value;
126  return true;
127  }
128 
129  static bool classof(const Query *Q) {
130  return Q->Kind == SetQueryKind<T>::value;
131  }
132 
134  T Value;
135 };
136 
137 } // namespace query
138 } // namespace clang
139 
140 #endif
std::string Name
Definition: Query.h:104
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:135
Query(QueryKind Kind)
Definition: Query.h:41
static bool classof(const Query *Q)
Definition: Query.h:69
const QueryKind Kind
Definition: Query.h:49
No-op query (i.e. a blank line).
Definition: Query.h:65
static bool classof(const Query *Q)
Definition: Query.h:61
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:122
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:30
static bool classof(const Query *Q)
Definition: Query.h:77
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:129
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:53
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:34
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:55
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:56
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:25
static bool classof(const Query *Q)
Definition: Query.h:85
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:52
Query for "match MATCHER".
Definition: Query.h:89
static bool classof(const Query *Q)
Definition: Query.h:96
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:100
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.cpp:70
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:94
Query for "quit".
Definition: Query.h:81
std::string ErrStr
Definition: Query.h:59
Query for "set VAR VALUE".
Definition: Query.h:121
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:124
static bool classof(const Query *Q)
Definition: Query.h:107
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:105
Query for "help".
Definition: Query.h:73
virtual ~Query()
Definition: Query.cpp:23
T QuerySession::* Var
Definition: Query.h:133
MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:90