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