1 /* Sets of function names.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
26 #include "analyzer/function-set.h"
32 /* Return true if NAME is within this set. */
35 function_set::contains_name_p (const char *name
) const
39 int max
= m_count
- 1;
44 int midpoint
= (min
+ max
) / 2;
45 gcc_assert ((size_t)midpoint
< m_count
);
46 int cmp
= strcmp (name
, m_names
[midpoint
]);
56 /* Return true if FNDECL is within this set. */
59 function_set::contains_decl_p (tree fndecl
) const
61 gcc_assert (fndecl
&& DECL_P (fndecl
));
62 if (!maybe_special_function_p (fndecl
))
64 return contains_name_p (IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
67 /* Assert that the list of names is in sorted order. */
70 function_set::assert_sorted () const
73 for (size_t idx
= 1; idx
< m_count
; idx
++)
74 gcc_assert (strcmp (m_names
[idx
- 1], m_names
[idx
]) < 0);
75 #endif /* #if CHECKING_P */
78 /* Assert that contains_p is true for all members of the set. */
81 function_set::assert_sane () const
84 for (size_t i
= 0; i
< m_count
; i
++)
85 gcc_assert (contains_name_p (m_names
[i
]));
86 #endif /* #if CHECKING_P */
93 /* Verify that an empty function_set works as expected. */
98 function_set
fs (NULL
, 0);
101 ASSERT_FALSE (fs
.contains_name_p (""));
102 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
105 /* Verify that a function_set with an odd number of elements works as
111 static const char * const names
[3] = {"alpha", "beta", "gamma"};
112 function_set
fs (names
, 3);
115 ASSERT_FALSE (fs
.contains_name_p (""));
116 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
119 /* Verify that a function_set with an even number of elements works as
125 static const char * const names
[3] = {"alpha", "beta"};
126 function_set
fs (names
, 2);
129 ASSERT_FALSE (fs
.contains_name_p (""));
130 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
133 /* Verify that a function_set with some nontrivial stdio.h data works as
137 test_stdio_example ()
139 static const char * const example
[] = {
169 const size_t count
= ARRAY_SIZE (example
);
170 function_set
fs (example
, count
);
173 /* Examples of strings not present: before, after and alongside the
175 ASSERT_FALSE (fs
.contains_name_p ("___"));
176 ASSERT_FALSE (fs
.contains_name_p ("Z"));
177 ASSERT_FALSE (fs
.contains_name_p ("fgets_WITH_A_PREFIX"));
180 /* Run all of the selftests within this file. */
183 analyzer_function_set_cc_tests ()
188 test_stdio_example ();
191 } // namespace selftest
193 #endif /* CHECKING_P */
197 #endif /* #if ENABLE_ANALYZER */