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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | /* $NetBSD: d_alignof.c,v 1.11 2023/06/30 16:39:17 rillig Exp $ */ # 3 "d_alignof.c" /* https://gcc.gnu.org/onlinedocs/gcc/Alignment.html */ unsigned long leading_and_trailing_alignof_type(void) { return __alignof__(short); } unsigned long leading_alignof_type(void) { return __alignof(short); } unsigned long plain_alignof_type(void) { /* The plain word 'alignof' is not recognized by GCC. */ /* expect+2: error: function 'alignof' implicitly declared to return int [215] */ /* expect+1: error: syntax error 'short' [249] */ return alignof(short); } /* expect-1: warning: function 'plain_alignof_type' falls off bottom without returning value [217] */ unsigned long leading_and_trailing_alignof_expr(void) { return __alignof__ 3; } unsigned long leading_alignof_expr(void) { return __alignof 3; } unsigned long plain_alignof_expr(void) { /* The plain word 'alignof' is not recognized by GCC. */ /* expect+2: error: 'alignof' undefined [99] */ /* expect+1: error: syntax error '3' [249] */ return alignof 3; } /* expect-1: warning: function 'plain_alignof_expr' falls off bottom without returning value [217] */ /* * As with 'sizeof', the keyword '__alignof__' doesn't require parentheses * when followed by an expression. This allows for the seemingly strange * '->' after the parentheses, which in fact is perfectly fine. * * The NetBSD style guide says "We parenthesize sizeof expressions", even * though it is misleading in edge cases like this. The GCC manual says that * '__alignof__' and 'sizeof' are syntactically the same, therefore the same * reasoning applies to '__alignof__'. */ unsigned long alignof_pointer_to_member(void) { struct s { unsigned long member; } var = { 0 }, *ptr = &var; return __alignof__(ptr)->member + ptr->member; } void alignof_variants(void) { /* expect+1: error: negative array dimension (-4) [20] */ typedef int array_int[-(int)__alignof(int[3])]; /* expect+1: error: negative array dimension (-8) [20] */ typedef int array_double[-(int)__alignof(double[3])]; /* expect+1: error: cannot take size/alignment of function type 'function(int) returning int' [144] */ typedef int func[-(int)__alignof(int(int))]; struct int_double { int i; double d; }; /* expect+1: error: negative array dimension (-8) [20] */ typedef int struct_int_double[-(int)__alignof(struct int_double)]; struct chars { char name[20]; }; /* expect+1: error: negative array dimension (-1) [20] */ typedef int struct_chars[-(int)__alignof(struct chars)]; /* expect+1: warning: struct 'incomplete_struct' never defined [233] */ struct incomplete_struct; /* expect+1: error: cannot take size/alignment of incomplete type [143] */ typedef int incomplete_struct[-(int)__alignof(struct incomplete_struct)]; /* expect+1: warning: union 'incomplete_union' never defined [234] */ union incomplete_union; /* expect+1: error: cannot take size/alignment of incomplete type [143] */ typedef int incomplete_union[-(int)__alignof(union incomplete_union)]; /* expect+1: warning: enum 'incomplete_enum' never defined [235] */ enum incomplete_enum; /* expect+1: error: cannot take size/alignment of incomplete type [143] */ typedef int incomplete_enum[-(int)__alignof(enum incomplete_enum)]; /* expect+1: error: cannot take size/alignment of incomplete type [143] */ typedef int incomplete_array[-(int)__alignof(int[])]; struct bit_fields { _Bool bit_field:1; }; /* * FIXME: This is not an attempt to initialize the typedef, it's the * initialization of a nested expression. */ /* expect+2: error: cannot initialize typedef '00000000_tmp' [25] */ /* expect+1: error: cannot take size/alignment of bit-field [145] */ typedef int bit_field_1[-(int)__alignof((struct bit_fields){0}.bit_field)]; struct bit_fields bit_fields; /* expect+1: error: cannot take size/alignment of bit-field [145] */ typedef int bit_field_2[-(int)__alignof(bit_fields.bit_field)]; /* expect+1: error: cannot take size/alignment of void [146] */ typedef int plain_void[-(int)__alignof(void)]; } |