1 /* A state machine for use in DejaGnu tests, to check that
2 pattern-matching works as expected.
4 Copyright (C) 2019-2020 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)
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/>. */
25 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "tree-pretty-print.h"
31 #include "diagnostic-path.h"
32 #include "diagnostic-metadata.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"
46 /* A state machine for use in DejaGnu tests, to check that
47 pattern-matching works as expected. */
49 class pattern_test_state_machine
: public state_machine
52 pattern_test_state_machine (logger
*logger
);
54 bool inherited_state_p () const FINAL OVERRIDE
{ return false; }
56 bool on_stmt (sm_context
*sm_ctxt
,
57 const supernode
*node
,
58 const gimple
*stmt
) const FINAL OVERRIDE
;
60 void on_condition (sm_context
*sm_ctxt
,
61 const supernode
*node
,
65 tree rhs
) const FINAL OVERRIDE
;
67 bool can_purge_p (state_t s
) const FINAL OVERRIDE
;
73 class pattern_match
: public pending_diagnostic_subclass
<pattern_match
>
76 pattern_match (tree lhs
, enum tree_code op
, tree rhs
)
77 : m_lhs (lhs
), m_op (op
), m_rhs (rhs
) {}
79 const char *get_kind () const FINAL OVERRIDE
{ return "pattern_match"; }
81 bool operator== (const pattern_match
&other
) const
83 return (same_tree_p (m_lhs
, other
.m_lhs
)
85 && same_tree_p (m_rhs
, other
.m_rhs
));
88 bool emit (rich_location
*rich_loc
) FINAL OVERRIDE
90 return warning_at (rich_loc
, 0, "pattern match on %<%E %s %E%>",
91 m_lhs
, op_symbol_code (m_op
), m_rhs
);
100 pattern_test_state_machine::pattern_test_state_machine (logger
*logger
)
101 : state_machine ("pattern-test", logger
)
103 m_start
= add_state ("start");
107 pattern_test_state_machine::on_stmt (sm_context
*sm_ctxt ATTRIBUTE_UNUSED
,
108 const supernode
*node ATTRIBUTE_UNUSED
,
109 const gimple
*stmt ATTRIBUTE_UNUSED
) const
114 /* Implementation of state_machine::on_condition vfunc for
115 pattern_test_state_machine.
117 Queue a pattern_match diagnostic for any comparison against a
121 pattern_test_state_machine::on_condition (sm_context
*sm_ctxt
,
122 const supernode
*node
,
131 if (!CONSTANT_CLASS_P (rhs
))
134 pending_diagnostic
*diag
= new pattern_match (lhs
, op
, rhs
);
135 sm_ctxt
->warn_for_state (node
, stmt
, lhs
, m_start
, diag
);
139 pattern_test_state_machine::can_purge_p (state_t s ATTRIBUTE_UNUSED
) const
144 } // anonymous namespace
146 /* Internal interface to this file. */
149 make_pattern_test_state_machine (logger
*logger
)
151 return new pattern_test_state_machine (logger
);
156 #endif /* #if ENABLE_ANALYZER */