]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/analyzer/sm-pattern-test.cc
Correct handling of variable offset minus constant in -Warray-bounds [PR100137]
[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
99dee823 4 Copyright (C) 2019-2021 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"
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"
809192e7 34#include "json.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"
40
41#if ENABLE_ANALYZER
42
75038aa6
DM
43namespace ana {
44
757bf1df
DM
45namespace {
46
47/* A state machine for use in DejaGnu tests, to check that
48 pattern-matching works as expected. */
49
50class pattern_test_state_machine : public state_machine
51{
52public:
53 pattern_test_state_machine (logger *logger);
54
55 bool inherited_state_p () const FINAL OVERRIDE { return false; }
56
57 bool on_stmt (sm_context *sm_ctxt,
58 const supernode *node,
59 const gimple *stmt) const FINAL OVERRIDE;
60
61 void on_condition (sm_context *sm_ctxt,
62 const supernode *node,
63 const gimple *stmt,
64 tree lhs,
65 enum tree_code op,
66 tree rhs) const FINAL OVERRIDE;
67
68 bool can_purge_p (state_t s) const FINAL OVERRIDE;
757bf1df
DM
69};
70
71class pattern_match : public pending_diagnostic_subclass<pattern_match>
72{
73public:
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 {
14f9d7b9 81 return (same_tree_p (m_lhs, other.m_lhs)
757bf1df 82 && m_op == other.m_op
14f9d7b9 83 && same_tree_p (m_rhs, other.m_rhs));
757bf1df
DM
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
92private:
93 tree m_lhs;
94 enum tree_code m_op;
95 tree m_rhs;
96};
97
98pattern_test_state_machine::pattern_test_state_machine (logger *logger)
99: state_machine ("pattern-test", logger)
100{
757bf1df
DM
101}
102
103bool
104pattern_test_state_machine::on_stmt (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
105 const supernode *node ATTRIBUTE_UNUSED,
106 const gimple *stmt ATTRIBUTE_UNUSED) const
107{
108 return false;
109}
110
111/* Implementation of state_machine::on_condition vfunc for
112 pattern_test_state_machine.
113
114 Queue a pattern_match diagnostic for any comparison against a
115 constant. */
116
117void
118pattern_test_state_machine::on_condition (sm_context *sm_ctxt,
119 const supernode *node,
120 const gimple *stmt,
121 tree lhs,
122 enum tree_code op,
123 tree rhs) const
124{
125 if (stmt == NULL)
126 return;
127
128 if (!CONSTANT_CLASS_P (rhs))
129 return;
130
131 pending_diagnostic *diag = new pattern_match (lhs, op, rhs);
25ef215a 132 sm_ctxt->warn (node, stmt, lhs, diag);
757bf1df
DM
133}
134
135bool
136pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
137{
138 return true;
139}
140
141} // anonymous namespace
142
143/* Internal interface to this file. */
144
145state_machine *
146make_pattern_test_state_machine (logger *logger)
147{
148 return new pattern_test_state_machine (logger);
149}
150
75038aa6
DM
151} // namespace ana
152
757bf1df 153#endif /* #if ENABLE_ANALYZER */