| 1 |
/* Test of <stddef.h> substitute. |
| 2 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Eric Blake <[email protected]>, 2009. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
/* Check that appropriate types are defined. */ |
| 24 |
wchar_t a = 'c'; |
| 25 |
ptrdiff_t b = 1; |
| 26 |
size_t c = 2; |
| 27 |
max_align_t mat; |
| 28 |
|
| 29 |
/* Check that NULL can be passed through varargs as a pointer type, |
| 30 |
per POSIX 2008. */ |
| 31 |
static_assert (sizeof NULL == sizeof (void *)); |
| 32 |
|
| 33 |
/* Check that offsetof produces integer constants with correct type. */ |
| 34 |
struct d |
| 35 |
{ |
| 36 |
char e; |
| 37 |
char f; |
| 38 |
}; |
| 39 |
/* Solaris 10 has a bug where offsetof is under-parenthesized, and |
| 40 |
cannot be used as an arbitrary expression. However, since it is |
| 41 |
unlikely to bite real code, we ignore that short-coming. */ |
| 42 |
/* static_assert (sizeof offsetof (struct d, e) == sizeof (size_t)); */ |
| 43 |
static_assert (sizeof (offsetof (struct d, e)) == sizeof (size_t)); |
| 44 |
static_assert (offsetof (struct d, f) == 1); |
| 45 |
|
| 46 |
/* Check max_align_t's alignment. */ |
| 47 |
static_assert (alignof (double) <= alignof (max_align_t)); |
| 48 |
static_assert (alignof (int) <= alignof (max_align_t)); |
| 49 |
static_assert (alignof (long double) <= alignof (max_align_t)); |
| 50 |
static_assert (alignof (long int) <= alignof (max_align_t)); |
| 51 |
static_assert (alignof (ptrdiff_t) <= alignof (max_align_t)); |
| 52 |
static_assert (alignof (size_t) <= alignof (max_align_t)); |
| 53 |
static_assert (alignof (wchar_t) <= alignof (max_align_t)); |
| 54 |
static_assert (alignof (struct d) <= alignof (max_align_t)); |
| 55 |
#if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__ |
| 56 |
static_assert (__alignof__ (double) <= __alignof__ (max_align_t)); |
| 57 |
static_assert (__alignof__ (int) <= __alignof__ (max_align_t)); |
| 58 |
static_assert (__alignof__ (long double) <= __alignof__ (max_align_t)); |
| 59 |
static_assert (__alignof__ (long int) <= __alignof__ (max_align_t)); |
| 60 |
static_assert (__alignof__ (ptrdiff_t) <= __alignof__ (max_align_t)); |
| 61 |
static_assert (__alignof__ (size_t) <= __alignof__ (max_align_t)); |
| 62 |
static_assert (__alignof__ (wchar_t) <= __alignof__ (max_align_t)); |
| 63 |
static_assert (__alignof__ (struct d) <= __alignof__ (max_align_t)); |
| 64 |
#endif |
| 65 |
|
| 66 |
int test_unreachable_optimization (int x); |
| 67 |
_Noreturn void test_unreachable_noreturn (void); |
| 68 |
|
| 69 |
int |
| 70 |
test_unreachable_optimization (int x) |
| 71 |
{ |
| 72 |
/* Check that the compiler uses 'unreachable' for optimization. |
| 73 |
This function, when compiled with optimization, should have code |
| 74 |
equivalent to |
| 75 |
return x + 3; |
| 76 |
Use 'objdump --disassemble test-stddef.o' to verify this. */ |
| 77 |
if (x < 4) |
| 78 |
unreachable (); |
| 79 |
return (x > 1 ? x + 3 : 2 * x + 10); |
| 80 |
} |
| 81 |
|
| 82 |
_Noreturn void |
| 83 |
test_unreachable_noreturn (void) |
| 84 |
{ |
| 85 |
/* Check that the compiler's data-flow analysis recognizes 'unreachable ()'. |
| 86 |
This function should not elicit a warning. */ |
| 87 |
unreachable (); |
| 88 |
} |
| 89 |
|
| 90 |
#include <limits.h> /* INT_MAX */ |
| 91 |
|
| 92 |
/* offsetof promotes to an unsigned integer if and only if sizes do |
| 93 |
not fit in int. */ |
| 94 |
static_assert ((offsetof (struct d, e) < -1) == (INT_MAX < (size_t) -1)); |
| 95 |
|
| 96 |
int |
| 97 |
main (void) |
| 98 |
{ |
| 99 |
return 0; |
| 100 |
} |