1 /* Sets of function names.
2 Copyright (C) 2019-2020 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"
30 /* Return true if NAME is within this set. */
33 function_set::contains_name_p (const char *name
) const
37 int max
= m_count
- 1;
42 int midpoint
= (min
+ max
) / 2;
43 gcc_assert ((size_t)midpoint
< m_count
);
44 int cmp
= strcmp (name
, m_names
[midpoint
]);
54 /* Return true if FNDECL is within this set. */
57 function_set::contains_decl_p (tree fndecl
) const
59 gcc_assert (fndecl
&& DECL_P (fndecl
));
60 return contains_name_p (IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
63 /* Assert that the list of names is in sorted order. */
66 function_set::assert_sorted () const
69 for (size_t idx
= 1; idx
< m_count
; idx
++)
70 gcc_assert (strcmp (m_names
[idx
- 1], m_names
[idx
]) < 0);
71 #endif /* #if CHECKING_P */
74 /* Assert that contains_p is true for all members of the set. */
77 function_set::assert_sane () const
80 for (size_t i
= 0; i
< m_count
; i
++)
81 gcc_assert (contains_name_p (m_names
[i
]));
82 #endif /* #if CHECKING_P */
89 /* Verify that an empty function_set works as expected. */
94 function_set
fs (NULL
, 0);
97 ASSERT_FALSE (fs
.contains_name_p (""));
98 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
101 /* Verify that a function_set with an odd number of elements works as
107 static const char * const names
[3] = {"alpha", "beta", "gamma"};
108 function_set
fs (names
, 3);
111 ASSERT_FALSE (fs
.contains_name_p (""));
112 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
115 /* Verify that a function_set with an even number of elements works as
121 static const char * const names
[3] = {"alpha", "beta"};
122 function_set
fs (names
, 2);
125 ASSERT_FALSE (fs
.contains_name_p (""));
126 ASSERT_FALSE (fs
.contains_name_p ("haystack"));
129 /* Verify that a function_set with some nontrivial stdio.h data works as
133 test_stdio_example ()
135 static const char * const example
[] = {
165 const size_t count
= sizeof(example
) / sizeof (example
[0]);
166 function_set
fs (example
, count
);
169 /* Examples of strings not present: before, after and alongside the
171 ASSERT_FALSE (fs
.contains_name_p ("___"));
172 ASSERT_FALSE (fs
.contains_name_p ("Z"));
173 ASSERT_FALSE (fs
.contains_name_p ("fgets_WITH_A_PREFIX"));
176 /* Run all of the selftests within this file. */
179 analyzer_function_set_cc_tests ()
184 test_stdio_example ();
187 } // namespace selftest
189 #endif /* CHECKING_P */
191 #endif /* #if ENABLE_ANALYZER */