Home | History | Annotate | Line # | Download | only in indent
fmt_decl.c revision 1.20
      1 /*	$NetBSD: fmt_decl.c,v 1.20 2021/11/20 09:59:53 rillig Exp $	*/
      2 /* $FreeBSD: head/usr.bin/indent/tests/declarations.0 334478 2018-06-01 09:41:15Z pstef $ */
      3 
      4 /*
      5  * Tests for declarations of global variables, external functions, and local
      6  * variables.
      7  *
      8  * See also:
      9  *	opt_di.c
     10  */
     11 
     12 /* See FreeBSD r303570 */
     13 
     14 /*
     15  * A type definition usually declares a single type, so there is no need to
     16  * align the newly declared type name with the other variables.
     17  */
     18 #indent input
     19 typedef   void   (   *   voidptr   )   (   int   *   )   ;
     20 #indent end
     21 
     22 #indent run
     23 typedef void (*voidptr)(int *);
     24 #indent end
     25 
     26 
     27 /*
     28  * In variable declarations, the names of the first declarators are indented
     29  * by the amount given in '-di', which defaults to 16.
     30  */
     31 #indent input
     32 extern   void   (   *   function_pointer   )   (   int   *   )   ;
     33 extern   void   *   pointer;
     34 #indent end
     35 
     36 #indent run
     37 /* $ XXX: Why is the token 'function_pointer' not aligned with 'pointer'? */
     38 extern void	(*function_pointer)(int *);
     39 extern void    *pointer;
     40 #indent end
     41 
     42 
     43 #indent input
     44 static const struct
     45 {
     46 	double		x;
     47 	double		y, z;
     48 } n[m + 1] =
     49 {
     50 	{
     51 		.0,
     52 		.9,
     53 		5
     54 	}
     55 };
     56 #indent end
     57 
     58 #indent run
     59 static const struct {
     60 	double		x;
     61 	double		y, z;
     62 }		n[m + 1] =
     63 {
     64 	{
     65 		.0,
     66 		.9,
     67 		5
     68 	}
     69 };
     70 #indent end
     71 
     72 
     73 #indent input
     74 typedef struct Complex
     75 {
     76 	double		x;
     77 	double		y;
     78 }	Complex;
     79 #indent end
     80 
     81 #indent run
     82 typedef struct Complex {
     83 	double		x;
     84 	double		y;
     85 }		Complex;
     86 #indent end
     87 
     88 
     89 /*
     90  * As of 2021-11-07, indent parses the following function definition as these
     91  * tokens:
     92  *
     93  * line 1: type_outside_parentheses "void"
     94  * line 1: newline "\n"
     95  * line 2: funcname "t1"
     96  * line 2: newline "\n"		repeated, see search_stmt
     97  * line 3: funcname "t1"	XXX: wrong line_no
     98  * line 3: lparen_or_lbracket "("
     99  * line 3: type_in_parentheses "char"
    100  * line 3: unary_op "*"
    101  * line 3: word "a"
    102  * line 3: comma ","
    103  * line 3: type_in_parentheses "int"
    104  * line 3: word "b"
    105  * line 3: comma ","
    106  * line 3: newline "\n"
    107  * line 4: type_in_parentheses "void"
    108  * line 4: lparen_or_lbracket "("
    109  * line 4: unary_op "*"
    110  * line 4: word "fn"
    111  * line 4: rparen_or_rbracket ")"
    112  * line 4: lparen_or_lbracket "("
    113  * line 4: type_in_parentheses "void"
    114  * line 4: rparen_or_rbracket ")"
    115  * line 4: rparen_or_rbracket ")"
    116  * line 4: newline "\n"
    117  * line 5: lbrace "{"
    118  * line 5: lbrace "{"		repeated, see search_stmt
    119  * line 5: newline "\n"		FIXME: there is no newline in the source
    120  * line 6: rbrace "}"
    121  * line 6: eof "\n"
    122  */
    123 #indent input
    124 void
    125 t1 (char *a, int b,
    126 	void (*fn)(void))
    127 {}
    128 #indent end
    129 
    130 #indent run
    131 void
    132 t1(char *a, int b,
    133    void (*fn)(void))
    134 {
    135 }
    136 #indent end
    137 
    138 
    139 /* See opt_bc.c. */
    140 #indent input
    141 void t2 (char *x, int y)
    142 {
    143 	int a,
    144 	b,
    145 	c;
    146 	int
    147 	*d,
    148 	*e,
    149 	*f;
    150 	int (*g)(),
    151 	(*h)(),
    152 	(*i)();
    153 	int j,
    154 	k,
    155 	l;
    156 	int m
    157 	,n
    158 	,o
    159 	;
    160 	int		chars[ /* push the comma beyond column 74 .... */ ], x;
    161 }
    162 #indent end
    163 
    164 #indent run
    165 void
    166 t2(char *x, int y)
    167 {
    168 	int		a, b, c;
    169 	int
    170 		       *d, *e, *f;
    171 	int		(*g)(), (*h)(), (*i)();
    172 	int		j, k, l;
    173 	int		m
    174 		       ,n
    175 		       ,o
    176 		       ;
    177 	int		chars[ /* push the comma beyond column 74 .... */ ],
    178 			x;
    179 }
    180 #indent end
    181 
    182 
    183 #indent input
    184 const int	int_minimum_size =
    185 MAXALIGN(offsetof(int, test)) + MAXIMUM_ALIGNOF;
    186 #indent end
    187 
    188 #indent run-equals-input
    189 
    190 
    191 #indent input
    192 static
    193 _attribute_printf(1, 2)
    194 void
    195 print_error(const char *fmt,...)
    196 {
    197 }
    198 #indent end
    199 
    200 #indent run
    201 static
    202 _attribute_printf(1, 2)
    203 void
    204 print_error(const char *fmt, ...)
    205 {
    206 }
    207 #indent end
    208 
    209 
    210 #indent input
    211 static _attribute_printf(1, 2)
    212 void
    213 print_error(const char *fmt,...)
    214 {
    215 }
    216 #indent end
    217 
    218 #indent run
    219 static _attribute_printf(1, 2)
    220 void
    221 print_error(const char *fmt, ...)
    222 {
    223 }
    224 #indent end
    225 
    226 
    227 #indent input
    228 static void _attribute_printf(1, 2)
    229 print_error(const char *fmt,...)
    230 {
    231 }
    232 #indent end
    233 
    234 #indent run
    235 static void
    236 _attribute_printf(1, 2)
    237 print_error(const char *fmt, ...)
    238 {
    239 }
    240 #indent end
    241 
    242 
    243 /* See FreeBSD r309380 */
    244 #indent input
    245 static LIST_HEAD(, alq) ald_active;
    246 static int ald_shutting_down = 0;
    247 struct thread *ald_thread;
    248 #indent end
    249 
    250 #indent run
    251 static LIST_HEAD(, alq) ald_active;
    252 static int	ald_shutting_down = 0;
    253 struct thread  *ald_thread;
    254 #indent end
    255 
    256 
    257 #indent input
    258 static int
    259 old_style_definition(a, b, c)
    260 	struct thread *a;
    261 	int b;
    262 	double ***c;
    263 {
    264 
    265 }
    266 #indent end
    267 
    268 #indent run
    269 static int
    270 old_style_definition(a, b, c)
    271 	struct thread  *a;
    272 	int		b;
    273 	double	     ***c;
    274 {
    275 
    276 }
    277 #indent end
    278 
    279 
    280 /*
    281  * Demonstrate how variable declarations are broken into several lines when
    282  * the line length limit is set quite low.
    283  */
    284 #indent input
    285 struct s a,b;
    286 struct s0 a,b;
    287 struct s01 a,b;
    288 struct s012 a,b;
    289 struct s0123 a,b;
    290 struct s01234 a,b;
    291 struct s012345 a,b;
    292 struct s0123456 a,b;
    293 struct s01234567 a,b;
    294 struct s012345678 a,b;
    295 struct s0123456789 a,b;
    296 struct s01234567890 a,b;
    297 struct s012345678901 a,b;
    298 struct s0123456789012 a,b;
    299 struct s01234567890123 a,b;
    300 #indent end
    301 
    302 #indent run -l20 -di0
    303 struct s a, b;
    304 /* $ XXX: See process_comma, varname_len for why this line is broken. */
    305 struct s0 a,
    306    b;
    307 /* $ XXX: The indentation of the second line is wrong. The variable names */
    308 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */
    309 /* $ XXX: missing in the calculation for the indentation. */
    310 struct s01 a,
    311     b;
    312 struct s012 a,
    313      b;
    314 struct s0123 a,
    315       b;
    316 struct s01234 a,
    317        b;
    318 struct s012345 a,
    319         b;
    320 struct s0123456 a,
    321          b;
    322 struct s01234567 a,
    323           b;
    324 struct s012345678 a,
    325            b;
    326 struct s0123456789 a,
    327             b;
    328 struct s01234567890 a,
    329              b;
    330 struct s012345678901 a,
    331               b;
    332 struct s0123456789012 a,
    333                b;
    334 struct s01234567890123 a,
    335                 b;
    336 #indent end
    337 
    338 
    339 #indent input
    340 char * x(void)
    341 {
    342     type identifier;
    343     type *pointer;
    344     unused * value;
    345     (void)unused * value;
    346 
    347     dmax = (double)3 * 10.0;
    348     dmin = (double)dmax * 10.0;
    349     davg = (double)dmax * dmin;
    350 
    351     return NULL;
    352 }
    353 #indent end
    354 
    355 #indent run
    356 char *
    357 x(void)
    358 {
    359 	type		identifier;
    360 	type	       *pointer;
    361 	unused	       *value;
    362 	(void)unused * value;
    363 
    364 	dmax = (double)3 * 10.0;
    365 	dmin = (double)dmax * 10.0;
    366 	davg = (double)dmax * dmin;
    367 
    368 	return NULL;
    369 }
    370 #indent end
    371 
    372 #indent input
    373 int *
    374 y(void) {
    375 
    376 }
    377 
    378 int
    379 z(void) {
    380 
    381 }
    382 #indent end
    383 
    384 #indent run
    385 int *
    386 y(void)
    387 {
    388 
    389 }
    390 
    391 int
    392 z(void)
    393 {
    394 
    395 }
    396 #indent end
    397 
    398 
    399 #indent input
    400 int x;
    401 int *y;
    402 int * * * * z;
    403 #indent end
    404 
    405 #indent run
    406 int		x;
    407 int	       *y;
    408 int	    ****z;
    409 #indent end
    410 
    411 
    412 #indent input
    413 int main(void) {
    414     char (*f1)() = NULL;
    415     char *(*f1)() = NULL;
    416     char *(*f2)();
    417 }
    418 #indent end
    419 
    420 /*
    421  * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second
    422  * and third variable declaration in column 1. This bug has been introduced
    423  * to NetBSD when FreeBSD indent was imported in 2019.
    424  */
    425 #indent run -ldi0
    426 int
    427 main(void)
    428 {
    429 	char (*f1)() = NULL;
    430 	char *(*f1)() = NULL;
    431 	char *(*f2)();
    432 }
    433 #indent end
    434 
    435 #indent run
    436 int
    437 main(void)
    438 {
    439 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
    440 	char		(*f1)() = NULL;
    441 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */
    442 	char *(*	f1)() = NULL;
    443 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */
    444 	char *(*	f2)();
    445 }
    446 #indent end
    447 
    448 
    449 /*
    450  * In some ancient time long before ISO C90, variable declarations with
    451  * initializer could be written without '='. The C Programming Language from
    452  * 1978 doesn't mention this form anymore.
    453  *
    454  * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a
    455  * unary operator.
    456  */
    457 #indent input
    458 int a - 1;
    459 {
    460 int a - 1;
    461 }
    462 #indent end
    463 
    464 #indent run -di0
    465 int a - 1;
    466 {
    467 	int a - 1;
    468 }
    469 #indent end
    470 
    471 
    472 /*
    473  * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-19, the indentation
    474  * of the '*' depended on the function name, which did not make sense.  For
    475  * function names that matched [A-Za-z]+, the '*' was placed correctly, for
    476  * all other function names (containing [$0-9_]) the '*' was right-aligned on
    477  * the declaration indentation, which defaults to 16.
    478  */
    479 #indent input
    480 int *
    481 f2(void)
    482 {
    483 }
    484 
    485 int *
    486 yy(void)
    487 {
    488 }
    489 
    490 int *
    491 int_create(void)
    492 {
    493 }
    494 #indent end
    495 
    496 #indent run-equals-input
    497 
    498 
    499 /*
    500  * Since 2019-04-04, the space between the '){' is missing.
    501  */
    502 #indent input
    503 int *
    504 function_name_____20________30________40________50
    505 (void)
    506 {}
    507 #indent end
    508 
    509 /* FIXME: The space between '){' is missing. */
    510 #indent run
    511 int	       *function_name_____20________30________40________50
    512 		(void){
    513 }
    514 #indent end
    515 
    516 
    517 /*
    518  * Since 2019-04-04 and before lexi.c 1.144 from 2021-11-19, some function
    519  * names were preserved while others were silently discarded.
    520  */
    521 #indent input
    522 int *
    523 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    524 (void)
    525 {}
    526 #indent end
    527 
    528 #indent run
    529 int	       *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    530 		(void){
    531 }
    532 #indent end
    533