]>
Commit | Line | Data |
---|---|---|
757bf1df DM |
1 | /* A state machine for use in DejaGnu tests, to check that |
2 | pattern-matching works as expected. | |
3 | ||
4 | Copyright (C) 2019-2020 Free Software Foundation, Inc. | |
5 | Contributed by David Malcolm <dmalcolm@redhat.com>. | |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3, or (at your option) | |
12 | any later version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GCC; see the file COPYING3. If not see | |
21 | <http://www.gnu.org/licenses/>. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
25 | #include "coretypes.h" | |
26 | #include "tree.h" | |
27 | #include "function.h" | |
28 | #include "basic-block.h" | |
29 | #include "gimple.h" | |
30 | #include "tree-pretty-print.h" | |
31 | #include "diagnostic-path.h" | |
32 | #include "diagnostic-metadata.h" | |
33 | #include "function.h" | |
34 | #include "analyzer/analyzer.h" | |
35 | #include "diagnostic-event-id.h" | |
36 | #include "analyzer/analyzer-logging.h" | |
37 | #include "analyzer/sm.h" | |
38 | #include "analyzer/pending-diagnostic.h" | |
39 | ||
40 | #if ENABLE_ANALYZER | |
41 | ||
42 | namespace { | |
43 | ||
44 | /* A state machine for use in DejaGnu tests, to check that | |
45 | pattern-matching works as expected. */ | |
46 | ||
47 | class pattern_test_state_machine : public state_machine | |
48 | { | |
49 | public: | |
50 | pattern_test_state_machine (logger *logger); | |
51 | ||
52 | bool inherited_state_p () const FINAL OVERRIDE { return false; } | |
53 | ||
54 | bool on_stmt (sm_context *sm_ctxt, | |
55 | const supernode *node, | |
56 | const gimple *stmt) const FINAL OVERRIDE; | |
57 | ||
58 | void on_condition (sm_context *sm_ctxt, | |
59 | const supernode *node, | |
60 | const gimple *stmt, | |
61 | tree lhs, | |
62 | enum tree_code op, | |
63 | tree rhs) const FINAL OVERRIDE; | |
64 | ||
65 | bool can_purge_p (state_t s) const FINAL OVERRIDE; | |
66 | ||
67 | private: | |
68 | state_t m_start; | |
69 | }; | |
70 | ||
71 | class pattern_match : public pending_diagnostic_subclass<pattern_match> | |
72 | { | |
73 | public: | |
74 | pattern_match (tree lhs, enum tree_code op, tree rhs) | |
75 | : m_lhs (lhs), m_op (op), m_rhs (rhs) {} | |
76 | ||
77 | const char *get_kind () const FINAL OVERRIDE { return "pattern_match"; } | |
78 | ||
79 | bool operator== (const pattern_match &other) const | |
80 | { | |
81 | return (m_lhs == other.m_lhs | |
82 | && m_op == other.m_op | |
83 | && m_rhs == other.m_rhs); | |
84 | } | |
85 | ||
86 | bool emit (rich_location *rich_loc) FINAL OVERRIDE | |
87 | { | |
88 | return warning_at (rich_loc, 0, "pattern match on %<%E %s %E%>", | |
89 | m_lhs, op_symbol_code (m_op), m_rhs); | |
90 | } | |
91 | ||
92 | private: | |
93 | tree m_lhs; | |
94 | enum tree_code m_op; | |
95 | tree m_rhs; | |
96 | }; | |
97 | ||
98 | pattern_test_state_machine::pattern_test_state_machine (logger *logger) | |
99 | : state_machine ("pattern-test", logger) | |
100 | { | |
101 | m_start = add_state ("start"); | |
102 | } | |
103 | ||
104 | bool | |
105 | pattern_test_state_machine::on_stmt (sm_context *sm_ctxt ATTRIBUTE_UNUSED, | |
106 | const supernode *node ATTRIBUTE_UNUSED, | |
107 | const gimple *stmt ATTRIBUTE_UNUSED) const | |
108 | { | |
109 | return false; | |
110 | } | |
111 | ||
112 | /* Implementation of state_machine::on_condition vfunc for | |
113 | pattern_test_state_machine. | |
114 | ||
115 | Queue a pattern_match diagnostic for any comparison against a | |
116 | constant. */ | |
117 | ||
118 | void | |
119 | pattern_test_state_machine::on_condition (sm_context *sm_ctxt, | |
120 | const supernode *node, | |
121 | const gimple *stmt, | |
122 | tree lhs, | |
123 | enum tree_code op, | |
124 | tree rhs) const | |
125 | { | |
126 | if (stmt == NULL) | |
127 | return; | |
128 | ||
129 | if (!CONSTANT_CLASS_P (rhs)) | |
130 | return; | |
131 | ||
132 | pending_diagnostic *diag = new pattern_match (lhs, op, rhs); | |
133 | sm_ctxt->warn_for_state (node, stmt, lhs, m_start, diag); | |
134 | } | |
135 | ||
136 | bool | |
137 | pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const | |
138 | { | |
139 | return true; | |
140 | } | |
141 | ||
142 | } // anonymous namespace | |
143 | ||
144 | /* Internal interface to this file. */ | |
145 | ||
146 | state_machine * | |
147 | make_pattern_test_state_machine (logger *logger) | |
148 | { | |
149 | return new pattern_test_state_machine (logger); | |
150 | } | |
151 | ||
152 | #endif /* #if ENABLE_ANALYZER */ |