clang-tools  7.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 "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 {
32 };
33 
34 class QuerySession;
35 
36 struct Query : llvm::RefCountedBase<Query> {
37  Query(QueryKind Kind) : Kind(Kind) {}
38  virtual ~Query();
39 
40  /// Perform the query on \p QS and print output to \p OS.
41  ///
42  /// \return false if an error occurs, otherwise return true.
43  virtual bool run(llvm::raw_ostream &OS, QuerySession &QS) const = 0;
44 
45  const QueryKind Kind;
46 };
47 
48 typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
49 
50 /// Any query which resulted in a parse error. The error message is in ErrStr.
51 struct InvalidQuery : Query {
52  InvalidQuery(const Twine &ErrStr) : Query(QK_Invalid), ErrStr(ErrStr.str()) {}
53  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
54 
55  std::string ErrStr;
56 
57  static bool classof(const Query *Q) { return Q->Kind == QK_Invalid; }
58 };
59 
60 /// No-op query (i.e. a blank line).
61 struct NoOpQuery : Query {
63  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
64 
65  static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; }
66 };
67 
68 /// Query for "help".
69 struct HelpQuery : Query {
71  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
72 
73  static bool classof(const Query *Q) { return Q->Kind == QK_Help; }
74 };
75 
76 /// Query for "quit".
77 struct QuitQuery : Query {
79  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
80 
81  static bool classof(const Query *Q) { return Q->Kind == QK_Quit; }
82 };
83 
84 /// Query for "match MATCHER".
85 struct MatchQuery : Query {
86  MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
87  : Query(QK_Match), Matcher(Matcher) {}
88  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
89 
90  ast_matchers::dynamic::DynTypedMatcher Matcher;
91 
92  static bool classof(const Query *Q) { return Q->Kind == QK_Match; }
93 };
94 
95 struct LetQuery : Query {
96  LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
97  : Query(QK_Let), Name(Name), Value(Value) {}
98  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override;
99 
100  std::string Name;
101  ast_matchers::dynamic::VariantValue Value;
102 
103  static bool classof(const Query *Q) { return Q->Kind == QK_Let; }
104 };
105 
106 template <typename T> struct SetQueryKind {};
107 
108 template <> struct SetQueryKind<bool> {
109  static const QueryKind value = QK_SetBool;
110 };
111 
112 template <> struct SetQueryKind<OutputKind> {
113  static const QueryKind value = QK_SetOutputKind;
114 };
115 
116 /// Query for "set VAR VALUE".
117 template <typename T> struct SetQuery : Query {
118  SetQuery(T QuerySession::*Var, T Value)
119  : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {}
120  bool run(llvm::raw_ostream &OS, QuerySession &QS) const override {
121  QS.*Var = Value;
122  return true;
123  }
124 
125  static bool classof(const Query *Q) {
126  return Q->Kind == SetQueryKind<T>::value;
127  }
128 
130  T Value;
131 };
132 
133 } // namespace query
134 } // namespace clang
135 
136 #endif
std::string Name
Definition: Query.h:100
llvm::StringRef Name
Query(QueryKind Kind)
Definition: Query.h:37
static bool classof(const Query *Q)
Definition: Query.h:65
const QueryKind Kind
Definition: Query.h:45
No-op query (i.e. a blank line).
Definition: Query.h:61
static bool classof(const Query *Q)
Definition: Query.h:57
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:118
static bool classof(const Query *Q)
Definition: Query.h:73
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:125
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:51
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:52
static bool classof(const Query *Q)
Definition: Query.h:81
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:48
Query for "match MATCHER".
Definition: Query.h:85
static bool classof(const Query *Q)
Definition: Query.h:92
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:96
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:90
Query for "quit".
Definition: Query.h:77
std::string ErrStr
Definition: Query.h:55
Query for "set VAR VALUE".
Definition: Query.h:117
bool run(llvm::raw_ostream &OS, QuerySession &QS) const override
Perform the query on QS and print output to OS.
Definition: Query.h:120
static bool classof(const Query *Q)
Definition: Query.h:103
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:101
Query for "help".
Definition: Query.h:69
virtual ~Query()
Definition: Query.cpp:23
T QuerySession::* Var
Definition: Query.h:129
MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:86