clang-tools 6.0.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 { OK_Diag, OK_Print, OK_Dump };
22
23 enum QueryKind {
24 QK_Invalid,
25 QK_NoOp,
26 QK_Help,
27 QK_Let,
28 QK_Match,
29 QK_SetBool,
30 QK_SetOutputKind,
31 QK_Quit
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 {
62 NoOpQuery() : Query(QK_NoOp) {}
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 {
70 HelpQuery() : Query(QK_Help) {}
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 {
78 QuitQuery() : Query(QK_Quit) {}
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
129 T QuerySession::*Var;
130 T Value;
131 };
132
133 } // namespace query
134 } // namespace clang
135
136 #endif
clang::query::LetQuery::Name
std::string Name
Definition: Query.h:100
clang::query::QK_SetBool
Definition: Query.h:29
clang::query::Query::Query
Query(QueryKind Kind)
Definition: Query.h:37
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::query::NoOpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:65
clang::query::QK_Quit
Definition: Query.h:31
clang::query::Query::Kind
const QueryKind Kind
Definition: Query.h:45
clang::query::NoOpQuery
No-op query (i.e. a blank line).
Definition: Query.h:61
clang::query::InvalidQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:57
clang::query::QuerySession
Represents the state for a particular clang-query session.
Definition: QuerySession.h:25
clang::query::SetQuery::SetQuery
SetQuery(T QuerySession::*Var, T Value)
Definition: Query.h:118
clang::query::QK_Match
Definition: Query.h:28
clang::query::OK_Diag
Definition: Query.h:21
clang::query::HelpQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:73
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::SetQuery::Value
T Value
Definition: Query.h:130
clang::query::SetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:125
clang::query::Query
Definition: Query.h:36
clang::query::InvalidQuery
Any query which resulted in a parse error. The error message is in ErrStr.
Definition: Query.h:51
clang::query::InvalidQuery::InvalidQuery
InvalidQuery(const Twine &ErrStr)
Definition: Query.h:52
clang::query::QK_SetOutputKind
Definition: Query.h:30
clang::query::QuitQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:81
clang::query::QK_Invalid
Definition: Query.h:24
clang::query::QueryRef
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:48
clang::query::MatchQuery
Query for "match MATCHER".
Definition: Query.h:85
clang::query::QuitQuery::QuitQuery
QuitQuery()
Definition: Query.h:78
clang::query::MatchQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:92
clang::query::OutputKind
OutputKind
Definition: Query.h:21
clang::query::OK_Print
Definition: Query.h:21
clang::query::LetQuery::LetQuery
LetQuery(StringRef Name, const ast_matchers::dynamic::VariantValue &Value)
Definition: Query.h:96
clang::query::SetQueryKind
Definition: Query.h:106
clang::query::QK_Let
Definition: Query.h:27
clang
Definition: AndroidTidyModule.cpp:28
clang::query::NoOpQuery::NoOpQuery
NoOpQuery()
Definition: Query.h:62
clang::query::MatchQuery::Matcher
ast_matchers::dynamic::DynTypedMatcher Matcher
Definition: Query.h:90
clang::query::QuitQuery
Query for "quit".
Definition: Query.h:77
clang::query::HelpQuery::HelpQuery
HelpQuery()
Definition: Query.h:70
clang::query::OK_Dump
Definition: Query.h:21
clang::query::InvalidQuery::ErrStr
std::string ErrStr
Definition: Query.h:55
clang::query::SetQuery
Query for "set VAR VALUE".
Definition: Query.h:117
clang::query::QueryKind
QueryKind
Definition: Query.h:23
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:120
clang::query::LetQuery::classof
static bool classof(const Query *Q)
Definition: Query.h:103
clang::query::LetQuery
Definition: Query.h:95
clang::query::LetQuery::Value
ast_matchers::dynamic::VariantValue Value
Definition: Query.h:101
clang::query::HelpQuery
Query for "help".
Definition: Query.h:69
clang::query::QK_Help
Definition: Query.h:26
clang::query::Query::~Query
virtual ~Query()
Definition: Query.cpp:23
clang::query::SetQuery::Var
T QuerySession::* Var
Definition: Query.h:129
clang::query::MatchQuery::MatchQuery
MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher)
Definition: Query.h:86
clang::query::QK_NoOp
Definition: Query.h:25