1 1.6 rillig /* $NetBSD: d_alignof.c,v 1.6 2022/05/12 20:49:21 rillig Exp $ */ 2 1.2 rillig # 3 "d_alignof.c" 3 1.2 rillig 4 1.3 rillig /* https://gcc.gnu.org/onlinedocs/gcc/Alignment.html */ 5 1.3 rillig 6 1.3 rillig unsigned long 7 1.3 rillig leading_and_trailing_alignof_type(void) 8 1.1 jruoho { 9 1.1 jruoho return __alignof__(short); 10 1.1 jruoho } 11 1.3 rillig 12 1.3 rillig unsigned long 13 1.3 rillig leading_alignof_type(void) 14 1.3 rillig { 15 1.3 rillig return __alignof(short); 16 1.3 rillig } 17 1.3 rillig 18 1.3 rillig unsigned long 19 1.3 rillig plain_alignof_type(void) 20 1.3 rillig { 21 1.3 rillig /* The plain word 'alignof' is not recognized by GCC. */ 22 1.4 rillig /* expect+2: error: function 'alignof' implicitly declared to return int [215] */ 23 1.4 rillig /* expect+1: error: syntax error 'short' [249] */ 24 1.3 rillig return alignof(short); 25 1.3 rillig } 26 1.4 rillig /* expect-1: warning: function plain_alignof_type falls off bottom without returning value [217] */ 27 1.3 rillig 28 1.3 rillig unsigned long 29 1.3 rillig leading_and_trailing_alignof_expr(void) 30 1.3 rillig { 31 1.3 rillig return __alignof__ 3; 32 1.3 rillig } 33 1.3 rillig 34 1.3 rillig unsigned long 35 1.3 rillig leading_alignof_expr(void) 36 1.3 rillig { 37 1.3 rillig return __alignof 3; 38 1.3 rillig } 39 1.3 rillig 40 1.3 rillig unsigned long 41 1.3 rillig plain_alignof_expr(void) 42 1.3 rillig { 43 1.3 rillig /* The plain word 'alignof' is not recognized by GCC. */ 44 1.4 rillig /* expect+2: error: 'alignof' undefined [99] */ 45 1.3 rillig /* expect+1: error: syntax error '3' [249] */ 46 1.3 rillig return alignof 3; 47 1.3 rillig } 48 1.3 rillig /* expect-1: warning: function plain_alignof_expr falls off bottom without returning value [217] */ 49 1.6 rillig 50 1.6 rillig 51 1.6 rillig /* 52 1.6 rillig * As with 'sizeof', the keyword '__alignof__' doesn't require parentheses 53 1.6 rillig * when followed by an expression. This allows for the seemingly strange 54 1.6 rillig * '->' after the parentheses, which in fact is perfectly fine. 55 1.6 rillig * 56 1.6 rillig * The NetBSD style guide says "We parenthesize sizeof expressions", even 57 1.6 rillig * though it is misleading in edge cases like this. The GCC manual says that 58 1.6 rillig * '__alignof__' and 'sizeof' are syntactically the same, therefore the same 59 1.6 rillig * reasoning applies to '__alignof__'. 60 1.6 rillig */ 61 1.6 rillig unsigned long 62 1.6 rillig alignof_pointer_to_member(void) 63 1.6 rillig { 64 1.6 rillig struct s { 65 1.6 rillig unsigned long member; 66 1.6 rillig } var = { 0 }, *ptr = &var; 67 1.6 rillig 68 1.6 rillig /* FIXME: the syntax error is wrong, this is perfectly valid */ 69 1.6 rillig /* expect+1: error: syntax error '->' [249] */ 70 1.6 rillig return __alignof__(ptr)->member + ptr->member; 71 1.6 rillig } 72 1.6 rillig /* expect-1: warning: function alignof_pointer_to_member falls off bottom without returning value [217] */ 73