Home | History | Annotate | Line # | Download | only in lint1
decl.c revision 1.14
      1 /*	$NetBSD: decl.c,v 1.14 2022/04/24 19:21:01 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 
     90 /*
     91  * An __attribute__ at the beginning of a declaration may become ambiguous
     92  * since a GCC fallthrough statement starts with __attribute__ as well.
     93  */
     94 void
     95 unused_local_variable(void)
     96 {
     97 	__attribute__((unused)) _Bool unused_var;
     98 
     99 	__attribute__((unused))
    100 	__attribute__((unused)) _Bool unused_twice;
    101 }
    102 
    103 int
    104 declaration_without_type_specifier(void)
    105 {
    106 	const i = 3;
    107 	/* expect-1: error: old style declaration; add 'int' [1] */
    108 	return i;
    109 }
    110 
    111 /* TODO: add quotes around %s */
    112 /* expect+2: warning: static function unused unused [236] */
    113 static void
    114 unused(void)
    115 {
    116 }
    117 
    118 /*
    119  * The attribute 'used' does not influence static functions, it only
    120  * applies to function parameters.
    121  */
    122 /* LINTED */
    123 static void
    124 unused_linted(void)
    125 {
    126 }
    127 
    128 /* covers 'type_qualifier_list: type_qualifier_list type_qualifier' */
    129 int *const volatile cover_type_qualifier_list;
    130 
    131 _Bool bool;
    132 char plain_char;
    133 signed char signed_char;
    134 unsigned char unsigned_char;
    135 short signed_short;
    136 unsigned short unsigned_short;
    137 int signed_int;
    138 unsigned int unsigned_int;
    139 long signed_long;
    140 unsigned long unsigned_long;
    141 struct {
    142 	int member;
    143 } unnamed_struct;
    144 
    145 /*
    146  * Before decl.c 1.201 from 2021-07-15, lint crashed with an internal error
    147  * in end_type.
    148  */
    149 unsigned long sizes =
    150     sizeof(const typeof(bool)) +
    151     sizeof(const typeof(plain_char)) +
    152     sizeof(const typeof(signed_char)) +
    153     sizeof(const typeof(unsigned_char)) +
    154     sizeof(const typeof(signed_short)) +
    155     sizeof(const typeof(unsigned_short)) +
    156     sizeof(const typeof(signed_int)) +
    157     sizeof(const typeof(unsigned_int)) +
    158     sizeof(const typeof(signed_long)) +
    159     sizeof(const typeof(unsigned_long)) +
    160     sizeof(const typeof(unnamed_struct));
    161 
    162 /* expect+1: syntax error 'int' [249] */
    163 thread int thread_int;
    164 __thread int thread_int;
    165 /* expect+1: syntax error 'int' [249] */
    166 __thread__ int thread_int;
    167 
    168 /* expect+2: warning: static function cover_func_declarator unused [236] */
    169 static
    170 cover_func_declarator(void)
    171 {
    172 }
    173 
    174 /*
    175  * Before decl.c 1.268 from 2022-04-03, lint ran into an assertion failure for
    176  * "elsz > 0" in 'length'.
    177  */
    178 /* expect+2: error: syntax error 'goto' [249] */
    179 /* expect+1: warning: empty array declaration: void_array_error [190] */
    180 void void_array_error[] goto;
    181