]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/analyzer/sm-pattern-test.cc
analyzer: early rejection of disabled warnings [PR104955]
[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"
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"
48e8a7a6
DM
40#include "tristate.h"
41#include "selftest.h"
42#include "analyzer/call-string.h"
43#include "analyzer/program-point.h"
44#include "analyzer/store.h"
45#include "analyzer/region-model.h"
757bf1df
DM
46
47#if ENABLE_ANALYZER
48
75038aa6
DM
49namespace ana {
50
757bf1df
DM
51namespace {
52
53/* A state machine for use in DejaGnu tests, to check that
54 pattern-matching works as expected. */
55
56class pattern_test_state_machine : public state_machine
57{
58public:
59 pattern_test_state_machine (logger *logger);
60
61 bool inherited_state_p () const FINAL OVERRIDE { return false; }
62
63 bool on_stmt (sm_context *sm_ctxt,
64 const supernode *node,
65 const gimple *stmt) const FINAL OVERRIDE;
66
67 void on_condition (sm_context *sm_ctxt,
68 const supernode *node,
69 const gimple *stmt,
48e8a7a6 70 const svalue *lhs,
757bf1df 71 enum tree_code op,
48e8a7a6 72 const svalue *rhs) const FINAL OVERRIDE;
757bf1df
DM
73
74 bool can_purge_p (state_t s) const FINAL OVERRIDE;
757bf1df
DM
75};
76
77class pattern_match : public pending_diagnostic_subclass<pattern_match>
78{
79public:
80 pattern_match (tree lhs, enum tree_code op, tree rhs)
81 : m_lhs (lhs), m_op (op), m_rhs (rhs) {}
82
83 const char *get_kind () const FINAL OVERRIDE { return "pattern_match"; }
84
85 bool operator== (const pattern_match &other) const
86 {
14f9d7b9 87 return (same_tree_p (m_lhs, other.m_lhs)
757bf1df 88 && m_op == other.m_op
14f9d7b9 89 && same_tree_p (m_rhs, other.m_rhs));
757bf1df
DM
90 }
91
7fd6e36e
DM
92 int get_controlling_option () const FINAL OVERRIDE
93 {
94 return 0;
95 }
96
757bf1df
DM
97 bool emit (rich_location *rich_loc) FINAL OVERRIDE
98 {
7fd6e36e
DM
99 return warning_at (rich_loc, get_controlling_option (),
100 "pattern match on %<%E %s %E%>",
757bf1df
DM
101 m_lhs, op_symbol_code (m_op), m_rhs);
102 }
103
104private:
105 tree m_lhs;
106 enum tree_code m_op;
107 tree m_rhs;
108};
109
110pattern_test_state_machine::pattern_test_state_machine (logger *logger)
111: state_machine ("pattern-test", logger)
112{
757bf1df
DM
113}
114
115bool
116pattern_test_state_machine::on_stmt (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
117 const supernode *node ATTRIBUTE_UNUSED,
118 const gimple *stmt ATTRIBUTE_UNUSED) const
119{
120 return false;
121}
122
123/* Implementation of state_machine::on_condition vfunc for
124 pattern_test_state_machine.
125
126 Queue a pattern_match diagnostic for any comparison against a
127 constant. */
128
129void
130pattern_test_state_machine::on_condition (sm_context *sm_ctxt,
131 const supernode *node,
132 const gimple *stmt,
48e8a7a6 133 const svalue *lhs,
757bf1df 134 enum tree_code op,
48e8a7a6 135 const svalue *rhs) const
757bf1df
DM
136{
137 if (stmt == NULL)
138 return;
139
48e8a7a6
DM
140 tree rhs_cst = rhs->maybe_get_constant ();
141 if (!rhs_cst)
757bf1df
DM
142 return;
143
48e8a7a6
DM
144 if (tree lhs_expr = sm_ctxt->get_diagnostic_tree (lhs))
145 {
146 pending_diagnostic *diag = new pattern_match (lhs_expr, op, rhs_cst);
147 sm_ctxt->warn (node, stmt, lhs_expr, diag);
148 }
757bf1df
DM
149}
150
151bool
152pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED) const
153{
154 return true;
155}
156
157} // anonymous namespace
158
159/* Internal interface to this file. */
160
161state_machine *
162make_pattern_test_state_machine (logger *logger)
163{
164 return new pattern_test_state_machine (logger);
165}
166
75038aa6
DM
167} // namespace ana
168
757bf1df 169#endif /* #if ENABLE_ANALYZER */