Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.2
      1 /*	$NetBSD: decl.c,v 1.2 2021/07/10 19:30:19 rillig Exp $	*/
      2 # 3 "decl.c"
      3 
      4 /*
      5  * Tests for declarations, especially the distinction between the
      6  * declaration-specifiers and the declarators.
      7  */
      8 
      9 /*
     10  * Even though 'const' comes after 'char' and is therefore quite close to the
     11  * first identifier, it applies to both identifiers.
     12  */
     13 void
     14 specifier_qualifier(void)
     15 {
     16 	char const a = 1, b = 2;
     17 
     18 	/* expect+1: warning: left operand of '=' must be modifiable lvalue [115] */
     19 	a = 1;
     20 	/* expect+1: warning: left operand of '=' must be modifiable lvalue [115] */
     21 	b = 2;
     22 }
     23 
     24 /*
     25  * Since 'const' comes before 'char', there is no ambiguity whether the
     26  * 'const' applies to all variables or just to the first.
     27  */
     28 void
     29 qualifier_specifier(void)
     30 {
     31 	const char a = 1, b = 2;
     32 
     33 	/* expect+1: warning: left operand of '=' must be modifiable lvalue [115] */
     34 	a = 3;
     35 	/* expect+1: warning: left operand of '=' must be modifiable lvalue [115] */
     36 	b = 5;
     37 }
     38 
     39 void
     40 declarator_with_prefix_qualifier(void)
     41 {
     42 	/* expect+1: syntax error 'const' [249] */
     43 	char a = 1, const b = 2;
     44 
     45 	a = 1;
     46 	/* expect+1: error: 'b' undefined [99] */
     47 	b = 2;
     48 }
     49 
     50 void
     51 declarator_with_postfix_qualifier(void)
     52 {
     53 	/* expect+1: syntax error 'const' [249] */
     54 	char a = 1, b const = 2;
     55 
     56 	a = 1;
     57 	b = 2;
     58 }
     59 
     60 void sink(double *);
     61 
     62 void
     63 declarators(void)
     64 {
     65 	char *pc = 0, c = 0, **ppc = 0;
     66 
     67 	/* expect+1: warning: converting 'pointer to char' to incompatible 'pointer to double' */
     68 	sink(pc);
     69 	/* expect+1: warning: illegal combination of pointer (pointer to double) and integer (char) */
     70 	sink(c);
     71 	/* expect+1: converting 'pointer to pointer to char' to incompatible 'pointer to double' */
     72 	sink(ppc);
     73 }
     74 
     75 _Bool
     76 enum_error_handling(void)
     77 {
     78 	enum {
     79 		/* expect+1: syntax error '"' [249] */
     80 		"error 1"
     81 		:		/* still the same error */
     82 		,		/* back on track */
     83 		A,
     84 		B
     85 	} x = A;
     86 
     87 	return x == B;
     88 }
     89