Home | History | Annotate | Line # | Download | only in lint1
decl_arg.c revision 1.15
      1 /*	$NetBSD: decl_arg.c,v 1.15 2024/09/28 15:51:40 rillig Exp $	*/
      2 # 3 "decl_arg.c"
      3 
      4 /*
      5  * Tests for declarations of function parameters.
      6  *
      7  * See arg_declaration in cgram.y.
      8  */
      9 
     10 /* lint1-extra-flags: -X 351 */
     11 
     12 typedef double number;
     13 
     14 void no_args(void);
     15 void type_unnamed(double );
     16 void type_named(double arg);
     17 void typedef_unnamed_prototype(number);
     18 void typedef_named(number arg);
     19 
     20 void type_qualifier(const number);
     21 void type_qualifier_pointer(const number *const);
     22 
     23 /*
     24  * Just some unrealistic coverage for the grammar rule 'arg_declaration'.
     25  */
     26 /* expect+6: warning: parameter 'an_int' unused in function 'old_style' [231] */
     27 /* expect+5: warning: parameter 'a_const_int' unused in function 'old_style' [231] */
     28 /* expect+4: warning: parameter 'a_number' unused in function 'old_style' [231] */
     29 /* expect+3: warning: parameter 'a_function' unused in function 'old_style' [231] */
     30 /* expect+2: warning: parameter 'a_struct' unused in function 'old_style' [231] */
     31 extern void
     32 old_style(an_int, a_const_int, a_number, a_function, a_struct)
     33 /* expect+2: error: only 'register' is valid as storage class in parameter [9] */
     34 /* expect+1: warning: empty declaration [2] */
     35 static;
     36 /* expect+1: error: syntax error '"' [249] */
     37 static "error";
     38 /* expect+1: warning: empty declaration [2] */
     39 const;
     40 /* expect+1: error: declared parameter 'undeclared' is missing [53] */
     41 const undeclared;
     42 /* expect+2: error: declared parameter 'undeclared_initialized' is missing [53] */
     43 /* expect+1: error: cannot initialize parameter 'undeclared_initialized' [52] */
     44 const undeclared_initialized = 12345;
     45 /* expect+1: warning: empty declaration [2] */
     46 int;
     47 /* expect+1: warning: 'struct arg_struct' declared in parameter declaration list [3] */
     48 struct arg_struct { int member; };
     49 /* expect+1: error: cannot initialize parameter 'an_int' [52] */
     50 int an_int = 12345;
     51 const int a_const_int;
     52 number a_number;
     53 void (a_function) (number);
     54 /* expect+1: warning: 'struct a_struct' declared in parameter declaration list [3] */
     55 struct a_struct { int member; } a_struct;
     56 {
     57 }
     58 
     59 /*
     60  * Just some unrealistic coverage for the grammar rule
     61  * 'notype_direct_declarator'.
     62  */
     63 extern int
     64 cover_notype_direct_decl(arg)
     65 int arg;
     66 /* expect+1: error: declared parameter 'name' is missing [53] */
     67 const name;
     68 /* expect+1: error: declared parameter 'parenthesized_name' is missing [53] */
     69 const (parenthesized_name);
     70 /* expect+1: error: declared parameter 'array' is missing [53] */
     71 const array[];
     72 /* expect+1: error: declared parameter 'array_size' is missing [53] */
     73 const array_size[1+1+1];
     74 /* expect+2: error: null dimension [17] */
     75 /* expect+1: error: declared parameter 'multi_array' is missing [53] */
     76 const multi_array[][][][][][];
     77 /* expect+1: error: declared parameter 'function' is missing [53] */
     78 const function(void);
     79 /* expect+1: error: declared parameter 'prefix_attribute' is missing [53] */
     80 const __attribute__((deprecated)) prefix_attribute;
     81 /* expect+1: error: declared parameter 'postfix_attribute' is missing [53] */
     82 const postfix_attribute __attribute__((deprecated));
     83 /* expect+1: error: declared parameter 'infix_attribute' is missing [53] */
     84 const __attribute__((deprecated)) infix_attribute __attribute__((deprecated));
     85 /* The __attribute__ before the '*' is consumed by some other grammar rule. */
     86 /* expect+7: error: declared parameter 'pointer_prefix_attribute' is missing [53] */
     87 const
     88     __attribute__((deprecated))
     89     *
     90     __attribute__((deprecated))
     91     __attribute__((deprecated))
     92     __attribute__((deprecated))
     93     pointer_prefix_attribute;
     94 {
     95 	return arg;
     96 }
     97 
     98 // The attribute 'unused' belongs to the parameter.
     99 // The attribute 'format' belongs to the function type.
    100 void
    101 param_func_attr_unused(
    102     void (*pr)(const char *, ...)
    103 	__attribute__((__unused__))
    104 	__attribute__((__format__(__printf__, 1, 2)))
    105 )
    106 {
    107 }
    108 
    109 // The attribute 'unused' belongs to the parameter.
    110 // The attribute 'format' belongs to the function type.
    111 void
    112 param_func_attr_printf(
    113     void (*pr)(const char *, ...)
    114 	__attribute__((__unused__))
    115 	__attribute__((__format__(__printf__, 1, 2)))
    116 )
    117 {
    118 	// GCC and Clang already warn about the malformed format string,
    119 	// so there is nothing left to do for lint.
    120 	pr("%");
    121 }
    122 
    123 /*
    124  * XXX: To cover the grammar rule 'direct_notype_param_decl', the parameters
    125  *  need to be enclosed by one more pair of parentheses than usual.
    126  */
    127 void cover_direct_notype_param_decl(
    128     double (identifier),
    129     double ((parenthesized)),
    130     double (array[]),
    131     double (array_size[3]),
    132     double (*)(void (function()))
    133 );
    134 
    135 /*
    136  * Just some unrealistic code to cover the grammar rule parameter_declaration.
    137  */
    138 /* expect+4: error: only 'register' is valid as storage class in parameter [9] */
    139 void cover_parameter_declaration(
    140     volatile,			/* 1 */
    141     double,			/* 2 */
    142     static storage_class,	/* 3.1 */
    143     const type_qualifier,	/* 3.2 */
    144     double (identifier),	/* 4 */
    145     const (*),			/* 5 */
    146     double *const,		/* 6 */
    147     ...
    148 );
    149 
    150 void cover_asm_or_symbolrename_asm(void)
    151     __asm("assembly code");
    152 
    153 void cover_asm_or_symbolrename_symbolrename(void)
    154     __symbolrename(alternate_name);
    155 
    156 
    157 double
    158 f(e, s, r, a, t, n)
    159 	/* expect+1: error: only 'register' is valid as storage class in parameter [9] */
    160 	extern double e;
    161 	/* expect+1: error: only 'register' is valid as storage class in parameter [9] */
    162 	static double s;
    163 	register double r;
    164 	/* expect+1: error: only 'register' is valid as storage class in parameter [9] */
    165 	auto double a;
    166 	/* expect+1: error: only 'register' is valid as storage class in parameter [9] */
    167 	typedef double t;
    168 	double n;
    169 {
    170 	return e + s + r + a + t + n;
    171 }
    172 
    173 
    174 // FIXME: internal error in decl.c:906 near decl_arg.c:134: length(0)
    175 //void cover_abstract_declarator_typeof(void (*)(typeof(no_args)));
    176