1*4c3eb207Smrg /* Sets of function names.
2*4c3eb207Smrg Copyright (C) 2019-2020 Free Software Foundation, Inc.
3*4c3eb207Smrg Contributed by David Malcolm <dmalcolm@redhat.com>.
4*4c3eb207Smrg
5*4c3eb207Smrg This file is part of GCC.
6*4c3eb207Smrg
7*4c3eb207Smrg GCC is free software; you can redistribute it and/or modify it
8*4c3eb207Smrg under the terms of the GNU General Public License as published by
9*4c3eb207Smrg the Free Software Foundation; either version 3, or (at your option)
10*4c3eb207Smrg any later version.
11*4c3eb207Smrg
12*4c3eb207Smrg GCC is distributed in the hope that it will be useful, but
13*4c3eb207Smrg WITHOUT ANY WARRANTY; without even the implied warranty of
14*4c3eb207Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*4c3eb207Smrg General Public License for more details.
16*4c3eb207Smrg
17*4c3eb207Smrg You should have received a copy of the GNU General Public License
18*4c3eb207Smrg along with GCC; see the file COPYING3. If not see
19*4c3eb207Smrg <http://www.gnu.org/licenses/>. */
20*4c3eb207Smrg
21*4c3eb207Smrg #include "config.h"
22*4c3eb207Smrg #include "system.h"
23*4c3eb207Smrg #include "coretypes.h"
24*4c3eb207Smrg #include "tree.h"
25*4c3eb207Smrg #include "selftest.h"
26*4c3eb207Smrg #include "analyzer/function-set.h"
27*4c3eb207Smrg
28*4c3eb207Smrg #if ENABLE_ANALYZER
29*4c3eb207Smrg
30*4c3eb207Smrg namespace ana {
31*4c3eb207Smrg
32*4c3eb207Smrg /* Return true if NAME is within this set. */
33*4c3eb207Smrg
34*4c3eb207Smrg bool
contains_name_p(const char * name) const35*4c3eb207Smrg function_set::contains_name_p (const char *name) const
36*4c3eb207Smrg {
37*4c3eb207Smrg /* Binary search. */
38*4c3eb207Smrg int min = 0;
39*4c3eb207Smrg int max = m_count - 1;
40*4c3eb207Smrg while (true)
41*4c3eb207Smrg {
42*4c3eb207Smrg if (min > max)
43*4c3eb207Smrg return false;
44*4c3eb207Smrg int midpoint = (min + max) / 2;
45*4c3eb207Smrg gcc_assert ((size_t)midpoint < m_count);
46*4c3eb207Smrg int cmp = strcmp (name, m_names[midpoint]);
47*4c3eb207Smrg if (cmp == 0)
48*4c3eb207Smrg return true;
49*4c3eb207Smrg else if (cmp < 0)
50*4c3eb207Smrg max = midpoint - 1;
51*4c3eb207Smrg else
52*4c3eb207Smrg min = midpoint + 1;
53*4c3eb207Smrg }
54*4c3eb207Smrg }
55*4c3eb207Smrg
56*4c3eb207Smrg /* Return true if FNDECL is within this set. */
57*4c3eb207Smrg
58*4c3eb207Smrg bool
contains_decl_p(tree fndecl) const59*4c3eb207Smrg function_set::contains_decl_p (tree fndecl) const
60*4c3eb207Smrg {
61*4c3eb207Smrg gcc_assert (fndecl && DECL_P (fndecl));
62*4c3eb207Smrg if (!maybe_special_function_p (fndecl))
63*4c3eb207Smrg return false;
64*4c3eb207Smrg return contains_name_p (IDENTIFIER_POINTER (DECL_NAME (fndecl)));
65*4c3eb207Smrg }
66*4c3eb207Smrg
67*4c3eb207Smrg /* Assert that the list of names is in sorted order. */
68*4c3eb207Smrg
69*4c3eb207Smrg void
assert_sorted() const70*4c3eb207Smrg function_set::assert_sorted () const
71*4c3eb207Smrg {
72*4c3eb207Smrg #if CHECKING_P
73*4c3eb207Smrg for (size_t idx = 1; idx < m_count; idx++)
74*4c3eb207Smrg gcc_assert (strcmp (m_names[idx - 1], m_names[idx]) < 0);
75*4c3eb207Smrg #endif /* #if CHECKING_P */
76*4c3eb207Smrg }
77*4c3eb207Smrg
78*4c3eb207Smrg /* Assert that contains_p is true for all members of the set. */
79*4c3eb207Smrg
80*4c3eb207Smrg void
assert_sane() const81*4c3eb207Smrg function_set::assert_sane () const
82*4c3eb207Smrg {
83*4c3eb207Smrg #if CHECKING_P
84*4c3eb207Smrg for (size_t i = 0; i < m_count; i++)
85*4c3eb207Smrg gcc_assert (contains_name_p (m_names[i]));
86*4c3eb207Smrg #endif /* #if CHECKING_P */
87*4c3eb207Smrg }
88*4c3eb207Smrg
89*4c3eb207Smrg #if CHECKING_P
90*4c3eb207Smrg
91*4c3eb207Smrg namespace selftest {
92*4c3eb207Smrg
93*4c3eb207Smrg /* Verify that an empty function_set works as expected. */
94*4c3eb207Smrg
95*4c3eb207Smrg static void
test_empty()96*4c3eb207Smrg test_empty ()
97*4c3eb207Smrg {
98*4c3eb207Smrg function_set fs (NULL, 0);
99*4c3eb207Smrg fs.assert_sorted ();
100*4c3eb207Smrg fs.assert_sane ();
101*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p (""));
102*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("haystack"));
103*4c3eb207Smrg }
104*4c3eb207Smrg
105*4c3eb207Smrg /* Verify that a function_set with an odd number of elements works as
106*4c3eb207Smrg expected. */
107*4c3eb207Smrg
108*4c3eb207Smrg static void
test_odd()109*4c3eb207Smrg test_odd ()
110*4c3eb207Smrg {
111*4c3eb207Smrg static const char * const names[3] = {"alpha", "beta", "gamma"};
112*4c3eb207Smrg function_set fs (names, 3);
113*4c3eb207Smrg fs.assert_sorted ();
114*4c3eb207Smrg fs.assert_sane ();
115*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p (""));
116*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("haystack"));
117*4c3eb207Smrg }
118*4c3eb207Smrg
119*4c3eb207Smrg /* Verify that a function_set with an even number of elements works as
120*4c3eb207Smrg expected. */
121*4c3eb207Smrg
122*4c3eb207Smrg static void
test_even()123*4c3eb207Smrg test_even ()
124*4c3eb207Smrg {
125*4c3eb207Smrg static const char * const names[3] = {"alpha", "beta"};
126*4c3eb207Smrg function_set fs (names, 2);
127*4c3eb207Smrg fs.assert_sorted ();
128*4c3eb207Smrg fs.assert_sane ();
129*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p (""));
130*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("haystack"));
131*4c3eb207Smrg }
132*4c3eb207Smrg
133*4c3eb207Smrg /* Verify that a function_set with some nontrivial stdio.h data works as
134*4c3eb207Smrg expected. */
135*4c3eb207Smrg
136*4c3eb207Smrg static void
test_stdio_example()137*4c3eb207Smrg test_stdio_example ()
138*4c3eb207Smrg {
139*4c3eb207Smrg static const char * const example[] = {
140*4c3eb207Smrg "__fbufsize",
141*4c3eb207Smrg "__flbf",
142*4c3eb207Smrg "__fpending",
143*4c3eb207Smrg "__fpurge",
144*4c3eb207Smrg "__freadable",
145*4c3eb207Smrg "__freading",
146*4c3eb207Smrg "__fsetlocking",
147*4c3eb207Smrg "__fwritable",
148*4c3eb207Smrg "__fwriting",
149*4c3eb207Smrg "clearerr_unlocked",
150*4c3eb207Smrg "feof_unlocked",
151*4c3eb207Smrg "ferror_unlocked",
152*4c3eb207Smrg "fflush_unlocked",
153*4c3eb207Smrg "fgetc_unlocked",
154*4c3eb207Smrg "fgets",
155*4c3eb207Smrg "fgets_unlocked",
156*4c3eb207Smrg "fgetwc_unlocked",
157*4c3eb207Smrg "fgetws_unlocked",
158*4c3eb207Smrg "fileno_unlocked",
159*4c3eb207Smrg "fputc_unlocked",
160*4c3eb207Smrg "fputs_unlocked",
161*4c3eb207Smrg "fputwc_unlocked",
162*4c3eb207Smrg "fputws_unlocked",
163*4c3eb207Smrg "fread_unlocked",
164*4c3eb207Smrg "fwrite_unlocked",
165*4c3eb207Smrg "getc_unlocked",
166*4c3eb207Smrg "getwc_unlocked",
167*4c3eb207Smrg "putc_unlocked"
168*4c3eb207Smrg };
169*4c3eb207Smrg const size_t count = sizeof(example) / sizeof (example[0]);
170*4c3eb207Smrg function_set fs (example, count);
171*4c3eb207Smrg fs.assert_sorted ();
172*4c3eb207Smrg fs.assert_sane ();
173*4c3eb207Smrg /* Examples of strings not present: before, after and alongside the
174*4c3eb207Smrg sorted list. */
175*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("___"));
176*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("Z"));
177*4c3eb207Smrg ASSERT_FALSE (fs.contains_name_p ("fgets_WITH_A_PREFIX"));
178*4c3eb207Smrg }
179*4c3eb207Smrg
180*4c3eb207Smrg /* Run all of the selftests within this file. */
181*4c3eb207Smrg
182*4c3eb207Smrg void
analyzer_function_set_cc_tests()183*4c3eb207Smrg analyzer_function_set_cc_tests ()
184*4c3eb207Smrg {
185*4c3eb207Smrg test_empty ();
186*4c3eb207Smrg test_odd ();
187*4c3eb207Smrg test_even ();
188*4c3eb207Smrg test_stdio_example ();
189*4c3eb207Smrg }
190*4c3eb207Smrg
191*4c3eb207Smrg } // namespace selftest
192*4c3eb207Smrg
193*4c3eb207Smrg #endif /* CHECKING_P */
194*4c3eb207Smrg
195*4c3eb207Smrg } // namespace ana
196*4c3eb207Smrg
197*4c3eb207Smrg #endif /* #if ENABLE_ANALYZER */
198