Branch
Hash :
94dcab94
Author :
Date :
2025-10-12T13:31:49
stdcountof-h tests: Fix link error on MSVC 14 (regression 2025-06-07). * tests/test-stdcountof-h.c (a, b, c): Define as variables on MSVC.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* Test <stdcountof.h>.
Copyright 2025 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdcountof.h>
#include "macros.h"
/* Whether the compiler supports _Generic.
Test program:
int f (int x) { return _Generic (x, char *: 2, int: 3); }
*/
#if (defined __GNUC__ && __GNUC__ + (__GNUC_MINOR__ >= 9) > 4) \
|| (defined __clang__ && __clang_major__ >= 3) \
|| (defined __SUNPRO_C && __SUNPRO_C >= 0x5150)
# define HAVE__GENERIC 1
#endif
extern int integer;
extern int unbounded[];
extern int bounded[10];
extern int multidimensional[10][20];
static void
test_func (int parameter[3])
{
int local_bounded[20];
(void) local_bounded;
#ifdef _gl_verify_is_array
(void) _gl_verify_is_array (unbounded);
(void) _gl_verify_is_array (bounded);
(void) _gl_verify_is_array (multidimensional);
(void) _gl_verify_is_array ("string");
(void) _gl_verify_is_array (L"wide string");
# if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
(void) _gl_verify_is_array (u8"UTF-8 string");
(void) _gl_verify_is_array (u"UTF-16 string");
(void) _gl_verify_is_array (U"UTF-32 string");
# endif
#endif
ASSERT (countof (bounded) == 10);
ASSERT (countof (multidimensional) == 10);
ASSERT (countof (local_bounded) == 20);
ASSERT (countof ("string") == 6 + 1);
ASSERT (countof (L"wide string") == 11 + 1);
#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
ASSERT (countof (u8"UTF-8 string") == 12 + 1);
ASSERT (countof (u"UTF-16 string") == 13 + 1);
ASSERT (countof (U"UTF-32 string") == 13 + 1);
#endif
#if 0 /* These produce compilation errors. */
# ifdef _gl_verify_is_array
(void) _gl_verify_is_array (integer);
(void) _gl_verify_is_array (parameter);
# endif
ASSERT (countof (integer) >= 0);
ASSERT (countof (unbounded) >= 0);
#endif
{
extern int a, b, c;
ASSERT (countof (((int[]) { a, b, c })) == 3);
}
/* Check that countof(...) is an expression of type size_t. */
#if !defined __cplusplus && HAVE__GENERIC
ASSERT (_Generic (countof (bounded), size_t: 1, default: 0));
ASSERT (_Generic (countof (multidimensional), size_t: 1, default: 0));
ASSERT (_Generic (countof (local_bounded), size_t: 1, default: 0));
ASSERT (_Generic (countof ("string"), size_t: 1, default: 0));
ASSERT (_Generic (countof (L"wide string"), size_t: 1, default: 0));
# if __STDC_VERSION__ >= 201112L
ASSERT (_Generic (countof (u8"UTF-8 string"), size_t: 1, default: 0));
ASSERT (_Generic (countof (u"UTF-16 string"), size_t: 1, default: 0));
ASSERT (_Generic (countof (U"UTF-32 string"), size_t: 1, default: 0));
# endif
#endif
}
int
main ()
{
int x[3];
test_func (x);
return test_exit_status;
}
/* A definition of the variables a, b, c is not required by ISO C, because
these identifiers are only used as part of 'sizeof' expressions whose
results are integer expressions. See ISO C 23 ยง 6.9.1.(5). However,
MSVC 14 generates actual references to these variables. We thus need
to define these variables; otherwise we get link errors. */
#if defined _MSC_VER && !defined __clang__
int a, b, c;
#endif