Line data Source code
1 : /* Selftest support for the analyzer.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "analyzer/common.h"
22 :
23 : #include "stringpool.h"
24 : #include "stor-layout.h"
25 :
26 : #include "analyzer/analyzer-selftests.h"
27 :
28 : #if CHECKING_P
29 :
30 : namespace ana {
31 :
32 : namespace selftest {
33 :
34 : /* Build a VAR_DECL named NAME of type TYPE, simulating a file-level
35 : static variable. */
36 :
37 : tree
38 368 : build_global_decl (const char *name, tree type)
39 : {
40 368 : tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
41 : get_identifier (name), type);
42 368 : TREE_STATIC (decl) = 1;
43 368 : return decl;
44 : }
45 :
46 : tree
47 32 : make_string_constant (const char *str)
48 : {
49 32 : tree string_literal = build_string_literal (strlen (str) + 1, str);
50 32 : gcc_assert (TREE_CODE (string_literal) == ADDR_EXPR);
51 32 : tree array_ref = TREE_OPERAND (string_literal, 0);
52 32 : gcc_assert (TREE_CODE (array_ref) == ARRAY_REF);
53 32 : tree string_cst = TREE_OPERAND (array_ref, 0);
54 32 : gcc_assert (TREE_CODE (string_cst) == STRING_CST);
55 32 : return string_cst;
56 : }
57 :
58 : /* Helper function for selftests. Create a struct or union type named NAME,
59 : with the fields given by the FIELD_DECLS in FIELDS.
60 : If IS_STRUCT is true create a RECORD_TYPE (aka a struct), otherwise
61 : create a UNION_TYPE. */
62 :
63 : tree
64 32 : make_test_compound_type (const char *name, bool is_struct,
65 : const std::vector<tree> fields)
66 : {
67 32 : tree t = make_node (is_struct ? RECORD_TYPE : UNION_TYPE);
68 32 : TYPE_NAME (t) = get_identifier (name);
69 32 : TYPE_SIZE (t) = 0;
70 :
71 32 : tree fieldlist = NULL_TREE;
72 92 : for (auto field : fields)
73 : {
74 60 : gcc_assert (TREE_CODE (field) == FIELD_DECL);
75 60 : DECL_CONTEXT (field) = t;
76 60 : fieldlist = chainon (field, fieldlist);
77 : }
78 32 : fieldlist = nreverse (fieldlist);
79 32 : TYPE_FIELDS (t) = fieldlist;
80 :
81 32 : layout_type (t);
82 32 : return t;
83 : }
84 :
85 : /* Selftest fixture for creating the type "struct coord {int x; int y; };". */
86 :
87 24 : coord_test::coord_test ()
88 : {
89 24 : std::vector<tree> fields;
90 24 : m_x_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
91 : get_identifier ("x"), integer_type_node);
92 24 : fields.push_back (m_x_field);
93 24 : m_y_field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
94 : get_identifier ("y"), integer_type_node);
95 24 : fields.push_back (m_y_field);
96 24 : m_coord_type = make_test_compound_type ("coord", true, std::move (fields));
97 24 : }
98 :
99 : /* Run all analyzer-specific selftests. */
100 :
101 : void
102 4 : run_analyzer_selftests ()
103 : {
104 : #if ENABLE_ANALYZER
105 4 : analyzer_access_diagram_cc_tests ();
106 4 : analyzer_complexity_cc_tests ();
107 4 : analyzer_concrete_ranges_cc_tests ();
108 4 : analyzer_constraint_manager_cc_tests ();
109 4 : analyzer_function_set_cc_tests ();
110 4 : analyzer_integer_cc_tests ();
111 4 : analyzer_integer_with_unit_cc_tests ();
112 4 : analyzer_program_point_cc_tests ();
113 4 : analyzer_program_state_cc_tests ();
114 4 : analyzer_region_model_manager_cc_tests ();
115 4 : analyzer_region_model_cc_tests ();
116 4 : analyzer_sm_file_cc_tests ();
117 4 : analyzer_sm_signal_cc_tests ();
118 4 : analyzer_store_cc_tests ();
119 4 : analyzer_string_scan_cc_tests ();
120 4 : analyzer_symbolic_bit_contents_cc_tests ();
121 4 : analyzer_symbolic_bit_vectors_cc_tests ();
122 4 : analyzer_symbolic_memory_ranges_cc_tests ();
123 4 : analyzer_symbolic_nodes_cc_tests ();
124 4 : analyzer_symbolic_pointers_cc_tests ();
125 4 : analyzer_symbolic_ranges_cc_tests ();
126 4 : analyzer_symbolic_scalars_cc_tests ();
127 : #endif /* #if ENABLE_ANALYZER */
128 4 : }
129 :
130 : } /* end of namespace selftest. */
131 :
132 : } // namespace ana
133 :
134 : #endif /* #if CHECKING_P */
|