Home | History | Annotate | Line # | Download | only in lint1
d_alignof.c revision 1.9
      1 /*	$NetBSD: d_alignof.c,v 1.9 2023/06/30 09:21:52 rillig Exp $	*/
      2 # 3 "d_alignof.c"
      3 
      4 /* https://gcc.gnu.org/onlinedocs/gcc/Alignment.html */
      5 
      6 unsigned long
      7 leading_and_trailing_alignof_type(void)
      8 {
      9 	return __alignof__(short);
     10 }
     11 
     12 unsigned long
     13 leading_alignof_type(void)
     14 {
     15 	return __alignof(short);
     16 }
     17 
     18 unsigned long
     19 plain_alignof_type(void)
     20 {
     21 	/* The plain word 'alignof' is not recognized by GCC. */
     22 	/* expect+2: error: function 'alignof' implicitly declared to return int [215] */
     23 	/* expect+1: error: syntax error 'short' [249] */
     24 	return alignof(short);
     25 }
     26 /* expect-1: warning: function 'plain_alignof_type' falls off bottom without returning value [217] */
     27 
     28 unsigned long
     29 leading_and_trailing_alignof_expr(void)
     30 {
     31 	return __alignof__ 3;
     32 }
     33 
     34 unsigned long
     35 leading_alignof_expr(void)
     36 {
     37 	return __alignof 3;
     38 }
     39 
     40 unsigned long
     41 plain_alignof_expr(void)
     42 {
     43 	/* The plain word 'alignof' is not recognized by GCC. */
     44 	/* expect+2: error: 'alignof' undefined [99] */
     45 	/* expect+1: error: syntax error '3' [249] */
     46 	return alignof 3;
     47 }
     48 /* expect-1: warning: function 'plain_alignof_expr' falls off bottom without returning value [217] */
     49 
     50 
     51 /*
     52  * As with 'sizeof', the keyword '__alignof__' doesn't require parentheses
     53  * when followed by an expression.  This allows for the seemingly strange
     54  * '->' after the parentheses, which in fact is perfectly fine.
     55  *
     56  * The NetBSD style guide says "We parenthesize sizeof expressions", even
     57  * though it is misleading in edge cases like this.  The GCC manual says that
     58  * '__alignof__' and 'sizeof' are syntactically the same, therefore the same
     59  * reasoning applies to '__alignof__'.
     60  */
     61 unsigned long
     62 alignof_pointer_to_member(void)
     63 {
     64 	struct s {
     65 		unsigned long member;
     66 	} var = { 0 }, *ptr = &var;
     67 
     68 	return __alignof__(ptr)->member + ptr->member;
     69 }
     70 
     71 void
     72 alignof_variants(void)
     73 {
     74 	/* expect+1: error: negative array dimension (-4) [20] */
     75 	typedef int array_int[-(int)__alignof(int[3])];
     76 
     77 	/* expect+1: error: negative array dimension (-8) [20] */
     78 	typedef int array_double[-(int)__alignof(double[3])];
     79 
     80 	/* expect+1: error: cannot take size/alignment of function type 'function(int) returning int' [144] */
     81 	typedef int func[-(int)__alignof(int(int))];
     82 
     83 	struct int_double {
     84 		int i;
     85 		double d;
     86 	};
     87 	/* expect+1: error: negative array dimension (-8) [20] */
     88 	typedef int struct_int_double[-(int)__alignof(struct int_double)];
     89 
     90 	struct chars {
     91 		char name[20];
     92 	};
     93 	/* expect+1: error: negative array dimension (-1) [20] */
     94 	typedef int struct_chars[-(int)__alignof(struct chars)];
     95 
     96 	/* expect+1: warning: struct 'incomplete_struct' never defined [233] */
     97 	struct incomplete_struct;
     98 	/* expect+1: error: cannot take size/alignment of incomplete type [143] */
     99 	typedef int incomplete_struct[-(int)__alignof(struct incomplete_struct)];
    100 
    101 	/* expect+1: warning: union 'incomplete_union' never defined [234] */
    102 	union incomplete_union;
    103 	/* expect+1: error: cannot take size/alignment of incomplete type [143] */
    104 	typedef int incomplete_union[-(int)__alignof(union incomplete_union)];
    105 
    106 	/* expect+1: warning: enum 'incomplete_enum' never defined [235] */
    107 	enum incomplete_enum;
    108 	/* expect+1: error: negative array dimension (-4) [20] */
    109 	typedef int incomplete_enum[-(int)__alignof(enum incomplete_enum)];
    110 
    111 	struct bit_fields {
    112 		_Bool bit_field:1;
    113 	};
    114 	/*
    115 	 * FIXME: This is not an attempt to initialize the typedef, it's the
    116 	 * initialization of a nested expression.
    117 	 */
    118 	/* expect+2: error: cannot initialize typedef '00000000_tmp' [25] */
    119 	/* expect+1: error: cannot take size/alignment of bit-field [145] */
    120 	typedef int bit_field[-(int)__alignof((struct bit_fields){0}.bit_field)];
    121 
    122 	/* expect+1: error: cannot take size/alignment of void [146] */
    123 	typedef int plain_void[-(int)__alignof(void)];
    124 }
    125