LoongArch: Rework bswap{hi,si,di}2 definition
[official-gcc.git] / gcc / analyzer / sm-pattern-test.cc
blobb56289ad259b477e7a2b4f88da1b5a67881e542e
1 /* A state machine for use in DejaGnu tests, to check that
2 pattern-matching works as expected.
4 Copyright (C) 2019-2024 Free Software Foundation, Inc.
5 Contributed by David Malcolm <dmalcolm@redhat.com>.
7 This file is part of GCC.
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.
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.
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/>. */
23 #include "config.h"
24 #define INCLUDE_MEMORY
25 #define INCLUDE_VECTOR
26 #include "system.h"
27 #include "coretypes.h"
28 #include "make-unique.h"
29 #include "tree.h"
30 #include "function.h"
31 #include "basic-block.h"
32 #include "gimple.h"
33 #include "tree-pretty-print.h"
34 #include "diagnostic-path.h"
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"
40 #include "analyzer/call-string.h"
41 #include "analyzer/program-point.h"
42 #include "analyzer/store.h"
43 #include "analyzer/region-model.h"
45 #if ENABLE_ANALYZER
47 namespace ana {
49 namespace {
51 /* A state machine for use in DejaGnu tests, to check that
52 pattern-matching works as expected. */
54 class pattern_test_state_machine : public state_machine
56 public:
57 pattern_test_state_machine (logger *logger);
59 bool inherited_state_p () const final override { return false; }
61 bool on_stmt (sm_context &sm_ctxt,
62 const supernode *node,
63 const gimple *stmt) const final override;
65 void on_condition (sm_context &sm_ctxt,
66 const supernode *node,
67 const gimple *stmt,
68 const svalue *lhs,
69 enum tree_code op,
70 const svalue *rhs) const final override;
72 bool can_purge_p (state_t s) const final override;
75 class pattern_match : public pending_diagnostic_subclass<pattern_match>
77 public:
78 pattern_match (tree lhs, enum tree_code op, tree rhs)
79 : m_lhs (lhs), m_op (op), m_rhs (rhs) {}
81 const char *get_kind () const final override { return "pattern_match"; }
83 bool operator== (const pattern_match &other) const
85 return (same_tree_p (m_lhs, other.m_lhs)
86 && m_op == other.m_op
87 && same_tree_p (m_rhs, other.m_rhs));
90 int get_controlling_option () const final override
92 return 0;
95 bool emit (diagnostic_emission_context &ctxt) final override
97 return ctxt.warn ("pattern match on %<%E %s %E%>",
98 m_lhs, op_symbol_code (m_op), m_rhs);
101 private:
102 tree m_lhs;
103 enum tree_code m_op;
104 tree m_rhs;
107 pattern_test_state_machine::pattern_test_state_machine (logger *logger)
108 : state_machine ("pattern-test", logger)
112 bool
113 pattern_test_state_machine::on_stmt (sm_context &sm_ctxt ATTRIBUTE_UNUSED,
114 const supernode *node ATTRIBUTE_UNUSED,
115 const gimple *stmt ATTRIBUTE_UNUSED) const
117 return false;
120 /* Implementation of state_machine::on_condition vfunc for
121 pattern_test_state_machine.
123 Queue a pattern_match diagnostic for any comparison against a
124 constant. */
126 void
127 pattern_test_state_machine::on_condition (sm_context &sm_ctxt,
128 const supernode *node,
129 const gimple *stmt,
130 const svalue *lhs,
131 enum tree_code op,
132 const svalue *rhs) const
134 if (stmt == NULL)
135 return;
137 tree rhs_cst = rhs->maybe_get_constant ();
138 if (!rhs_cst)
139 return;
141 if (tree lhs_expr = sm_ctxt.get_diagnostic_tree (lhs))
143 sm_ctxt.warn (node, stmt, lhs_expr,
144 make_unique<pattern_match> (lhs_expr, op, rhs_cst));
148 bool
149 pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
151 return true;
154 } // anonymous namespace
156 /* Internal interface to this file. */
158 state_machine *
159 make_pattern_test_state_machine (logger *logger)
161 return new pattern_test_state_machine (logger);
164 } // namespace ana
166 #endif /* #if ENABLE_ANALYZER */