Home | History | Annotate | Line # | Download | only in lint1
expr_sizeof.c revision 1.1
      1  1.1  rillig /*	$NetBSD: expr_sizeof.c,v 1.1 2023/01/15 00:53:19 rillig Exp $	*/
      2  1.1  rillig # 3 "expr_sizeof.c"
      3  1.1  rillig 
      4  1.1  rillig /*
      5  1.1  rillig  * C99 6.5.3.4 "The sizeof operator"
      6  1.1  rillig  * C11 6.5.3.4 "The sizeof operator"
      7  1.1  rillig  */
      8  1.1  rillig 
      9  1.1  rillig /*
     10  1.1  rillig  * A sizeof expression can either take a type name or an expression.
     11  1.1  rillig  */
     12  1.1  rillig void sink(unsigned long);
     13  1.1  rillig 
     14  1.1  rillig struct {
     15  1.1  rillig 	int member;
     16  1.1  rillig } s, *ps;
     17  1.1  rillig 
     18  1.1  rillig /*
     19  1.1  rillig  * In a sizeof expression taking a type name, the type name must be enclosed
     20  1.1  rillig  * in parentheses.
     21  1.1  rillig  */
     22  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     23  1.1  rillig typedef int sizeof_int[-(int)sizeof(int)];
     24  1.1  rillig 
     25  1.1  rillig /*
     26  1.1  rillig  * In a sizeof expression taking an expression, the expression may or may not
     27  1.1  rillig  * be enclosed in parentheses, like any other expression.
     28  1.1  rillig  */
     29  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     30  1.1  rillig typedef int sizeof_paren_zero[-(int)sizeof(0)];
     31  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     32  1.1  rillig typedef int sizeof_zero[-(int)sizeof 0];
     33  1.1  rillig 
     34  1.1  rillig /*
     35  1.1  rillig  * Even though 's' is not a constant expression, 'sizeof s' is.
     36  1.1  rillig  */
     37  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     38  1.1  rillig typedef int sizeof_global_var[-(int)sizeof s];
     39  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     40  1.1  rillig typedef int sizeof_paren_global_var[-(int)sizeof(s)];
     41  1.1  rillig 
     42  1.1  rillig /*
     43  1.1  rillig  * Even though 'sizeof(s)' may look like a function call expression, the
     44  1.1  rillig  * parentheses around 's' are ordinary parentheses and do not influence the
     45  1.1  rillig  * associativity.  Therefore, the '.' following the '(s)' takes precedence
     46  1.1  rillig  * over the 'sizeof'.  Same for the '->' following the '(ps)'.
     47  1.1  rillig  */
     48  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     49  1.1  rillig typedef int sizeof_paren_global_struct_member[-(int)sizeof(s).member];
     50  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     51  1.1  rillig typedef int sizeof_paren_global_ptr_struct_member[-(int)sizeof(ps)->member];
     52