Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 13 | #include "clang/ASTMatchers/Dynamic/VariantValue.h" |
| 14 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 15 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame] | 16 | #include <string> |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 17 | |
| 18 | namespace clang { |
| 19 | namespace query { |
| 20 | |
| 21 | enum OutputKind { |
| 22 | OK_Diag, |
| 23 | OK_Print, |
| 24 | OK_Dump |
| 25 | }; |
| 26 | |
| 27 | enum QueryKind { |
| 28 | QK_Invalid, |
| 29 | QK_NoOp, |
| 30 | QK_Help, |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 31 | QK_Let, |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 32 | QK_Match, |
| 33 | QK_SetBool, |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 34 | QK_SetOutputKind, |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 +0000 | [diff] [blame^] | 35 | QK_Quit |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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()) {} |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 57 | bool run(llvm::raw_ostream &OS, QuerySession &QS) const override; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 { |
| 66 | NoOpQuery() : Query(QK_NoOp) {} |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 67 | bool run(llvm::raw_ostream &OS, QuerySession &QS) const override; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 68 | |
| 69 | static bool classof(const Query *Q) { return Q->Kind == QK_NoOp; } |
| 70 | }; |
| 71 | |
| 72 | /// Query for "help". |
| 73 | struct HelpQuery : Query { |
| 74 | HelpQuery() : Query(QK_Help) {} |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 75 | bool run(llvm::raw_ostream &OS, QuerySession &QS) const override; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 76 | |
| 77 | static bool classof(const Query *Q) { return Q->Kind == QK_Help; } |
| 78 | }; |
| 79 | |
Aaron Ballman | 5890717 | 2015-08-06 11:56:57 +0000 | [diff] [blame^] | 80 | /// Query for "quit". |
| 81 | struct QuitQuery : Query { |
| 82 | QuitQuery() : Query(QK_Quit) {} |
| 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 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 88 | /// Query for "match MATCHER". |
| 89 | struct MatchQuery : Query { |
| 90 | MatchQuery(const ast_matchers::dynamic::DynTypedMatcher &Matcher) |
| 91 | : Query(QK_Match), Matcher(Matcher) {} |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 92 | bool run(llvm::raw_ostream &OS, QuerySession &QS) const override; |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 93 | |
| 94 | ast_matchers::dynamic::DynTypedMatcher Matcher; |
| 95 | |
| 96 | static bool classof(const Query *Q) { return Q->Kind == QK_Match; } |
| 97 | }; |
| 98 | |
Samuel Benzaquen | 1f6066c | 2014-04-23 14:04:52 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 { |
| 122 | SetQuery(T QuerySession::*Var, T Value) |
| 123 | : Query(SetQueryKind<T>::value), Var(Var), Value(Value) {} |
Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 124 | bool run(llvm::raw_ostream &OS, QuerySession &QS) const override { |
Peter Collingbourne | 8b1265b | 2013-11-08 00:08:23 +0000 | [diff] [blame] | 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 | |
| 133 | T QuerySession::*Var; |
| 134 | T Value; |
| 135 | }; |
| 136 | |
| 137 | } // namespace query |
| 138 | } // namespace clang |
| 139 | |
| 140 | #endif |