1 /* $NetBSD: fmt_decl.c,v 1.36 2022/04/24 10:36:37 rillig Exp $ */ 2 3 /* 4 * Tests for declarations of global variables, external functions, and local 5 * variables. 6 * 7 * See also: 8 * opt_di.c 9 */ 10 11 /* See FreeBSD r303570 */ 12 13 /* 14 * A type definition usually declares a single type, so there is no need to 15 * align the newly declared type name with the other variables. 16 */ 17 //indent input 18 typedef void ( * function_ptr ) ( int * ) ; 19 //indent end 20 21 //indent run 22 typedef void (*function_ptr)(int *); 23 //indent end 24 25 26 /* 27 * In variable declarations, the names of the first declarators are indented 28 * by the amount given in '-di', which defaults to 16. 29 */ 30 //indent input 31 extern void ( * function_pointer ) ( int * ) ; 32 extern void * pointer; 33 //indent end 34 35 //indent run 36 /* $ XXX: Why is the token 'function_pointer' not aligned with 'pointer'? */ 37 extern void (*function_pointer)(int *); 38 extern void *pointer; 39 //indent end 40 41 42 //indent input 43 static const struct 44 { 45 double x; 46 double y, z; 47 } n[m + 1] = 48 { 49 { 50 .0, 51 .9, 52 5 53 } 54 }; 55 //indent end 56 57 //indent run 58 static const struct { 59 double x; 60 double y, z; 61 } n[m + 1] = 62 { 63 { 64 .0, 65 .9, 66 5 67 } 68 }; 69 //indent end 70 71 72 //indent input 73 typedef struct Complex 74 { 75 double x; 76 double y; 77 } Complex; 78 //indent end 79 80 //indent run 81 typedef struct Complex { 82 double x; 83 double y; 84 } Complex; 85 //indent end 86 87 88 /* 89 * As of 2021-11-07, indent parses the following function definition as these 90 * tokens: 91 * 92 * line 1: type_outside_parentheses "void" 93 * line 1: newline "\n" 94 * line 2: funcname "t1" 95 * line 2: newline "\n" repeated, see search_stmt 96 * line 3: funcname "t1" XXX: wrong line_no 97 * line 3: lparen_or_lbracket "(" 98 * line 3: type_in_parentheses "char" 99 * line 3: unary_op "*" 100 * line 3: word "a" 101 * line 3: comma "," 102 * line 3: type_in_parentheses "int" 103 * line 3: word "b" 104 * line 3: comma "," 105 * line 3: newline "\n" 106 * line 4: type_in_parentheses "void" 107 * line 4: lparen_or_lbracket "(" 108 * line 4: unary_op "*" 109 * line 4: word "fn" 110 * line 4: rparen_or_rbracket ")" 111 * line 4: lparen_or_lbracket "(" 112 * line 4: type_in_parentheses "void" 113 * line 4: rparen_or_rbracket ")" 114 * line 4: rparen_or_rbracket ")" 115 * line 4: newline "\n" 116 * line 5: lbrace "{" 117 * line 5: lbrace "{" repeated, see search_stmt 118 * line 5: newline "\n" FIXME: there is no newline in the source 119 * line 6: rbrace "}" 120 * line 6: eof "\n" 121 */ 122 //indent input 123 void 124 t1 (char *a, int b, 125 void (*fn)(void)) 126 {} 127 //indent end 128 129 //indent run 130 void 131 t1(char *a, int b, 132 void (*fn)(void)) 133 { 134 } 135 //indent end 136 137 138 /* See opt_bc.c. */ 139 //indent input 140 void t2 (char *x, int y) 141 { 142 int a, 143 b, 144 c; 145 int 146 *d, 147 *e, 148 *f; 149 int (*g)(), 150 (*h)(), 151 (*i)(); 152 int j, 153 k, 154 l; 155 int m 156 ,n 157 ,o 158 ; 159 int chars[ /* push the comma beyond column 74 .... */ ], x; 160 } 161 //indent end 162 163 //indent run 164 void 165 t2(char *x, int y) 166 { 167 int a, b, c; 168 int 169 *d, *e, *f; 170 int (*g)(), (*h)(), (*i)(); 171 int j, k, l; 172 int m 173 ,n 174 ,o 175 ; 176 int chars[ /* push the comma beyond column 74 .... */ ], 177 x; 178 } 179 //indent end 180 181 182 //indent input 183 const int int_minimum_size = 184 MAXALIGN(offsetof(int, test)) + MAXIMUM_ALIGNOF; 185 //indent end 186 187 //indent run-equals-input 188 189 190 /* 191 * Ensure that the usual GCC-style function attributes are formatted in a 192 * sensible way. 193 */ 194 //indent input 195 void function(const char *, ...) __attribute__((format(printf, 1, 2))); 196 //indent end 197 198 /* FIXME: missing space before '__attribute__' */ 199 //indent run -di0 200 void function(const char *, ...)__attribute__((format(printf, 1, 2))); 201 //indent end 202 203 /* FIXME: missing space before '__attribute__' */ 204 //indent run 205 void function(const char *, ...)__attribute__((format(printf, 1, 2))); 206 //indent end 207 208 209 //indent input 210 static 211 _attribute_printf(1, 2) 212 void 213 print_error(const char *fmt,...) 214 { 215 } 216 //indent end 217 218 //indent run 219 static 220 _attribute_printf(1, 2) 221 void 222 print_error(const char *fmt, ...) 223 { 224 } 225 //indent end 226 227 228 //indent input 229 static _attribute_printf(1, 2) 230 void 231 print_error(const char *fmt,...) 232 { 233 } 234 //indent end 235 236 //indent run 237 static _attribute_printf(1, 2) 238 void 239 print_error(const char *fmt, ...) 240 { 241 } 242 //indent end 243 244 245 //indent input 246 static void _attribute_printf(1, 2) 247 print_error(const char *fmt,...) 248 { 249 } 250 //indent end 251 252 //indent run 253 static void 254 _attribute_printf(1, 2) 255 print_error(const char *fmt, ...) 256 { 257 } 258 //indent end 259 260 261 /* See FreeBSD r309380 */ 262 //indent input 263 static LIST_HEAD(, alq) ald_active; 264 static int ald_shutting_down = 0; 265 struct thread *ald_thread; 266 //indent end 267 268 //indent run 269 static LIST_HEAD(, alq) ald_active; 270 static int ald_shutting_down = 0; 271 struct thread *ald_thread; 272 //indent end 273 274 275 //indent input 276 static int 277 old_style_definition(a, b, c) 278 struct thread *a; 279 int b; 280 double ***c; 281 { 282 283 } 284 //indent end 285 286 //indent run 287 static int 288 old_style_definition(a, b, c) 289 struct thread *a; 290 int b; 291 double ***c; 292 { 293 294 } 295 //indent end 296 297 298 /* 299 * Demonstrate how variable declarations are broken into several lines when 300 * the line length limit is set quite low. 301 */ 302 //indent input 303 struct s a,b; 304 struct s0 a,b; 305 struct s01 a,b; 306 struct s012 a,b; 307 struct s0123 a,b; 308 struct s01234 a,b; 309 struct s012345 a,b; 310 struct s0123456 a,b; 311 struct s01234567 a,b; 312 struct s012345678 a,b; 313 struct s0123456789 a,b; 314 struct s01234567890 a,b; 315 struct s012345678901 a,b; 316 struct s0123456789012 a,b; 317 struct s01234567890123 a,b; 318 //indent end 319 320 //indent run -l20 -di0 321 struct s a, b; 322 /* $ XXX: See process_comma, varname_len for why this line is broken. */ 323 struct s0 a, 324 b; 325 /* $ XXX: The indentation of the second line is wrong. The variable names */ 326 /* $ XXX: 'a' and 'b' should be in the same column; the word 'struct' is */ 327 /* $ XXX: missing in the calculation for the indentation. */ 328 struct s01 a, 329 b; 330 struct s012 a, 331 b; 332 struct s0123 a, 333 b; 334 struct s01234 a, 335 b; 336 struct s012345 a, 337 b; 338 struct s0123456 a, 339 b; 340 struct s01234567 a, 341 b; 342 struct s012345678 a, 343 b; 344 struct s0123456789 a, 345 b; 346 struct s01234567890 a, 347 b; 348 struct s012345678901 a, 349 b; 350 struct s0123456789012 a, 351 b; 352 struct s01234567890123 a, 353 b; 354 //indent end 355 356 357 //indent input 358 char * x(void) 359 { 360 type identifier; 361 type *pointer; 362 unused * value; 363 (void)unused * value; 364 365 dmax = (double)3 * 10.0; 366 dmin = (double)dmax * 10.0; 367 davg = (double)dmax * dmin; 368 369 return NULL; 370 } 371 //indent end 372 373 //indent run 374 char * 375 x(void) 376 { 377 type identifier; 378 type *pointer; 379 unused *value; 380 (void)unused * value; 381 382 dmax = (double)3 * 10.0; 383 dmin = (double)dmax * 10.0; 384 davg = (double)dmax * dmin; 385 386 return NULL; 387 } 388 //indent end 389 390 391 //indent input 392 int * 393 y(void) { 394 395 } 396 397 int 398 z(void) { 399 400 } 401 //indent end 402 403 //indent run 404 int * 405 y(void) 406 { 407 408 } 409 410 int 411 z(void) 412 { 413 414 } 415 //indent end 416 417 418 //indent input 419 int x; 420 int *y; 421 int * * * * z; 422 //indent end 423 424 //indent run 425 int x; 426 int *y; 427 int ****z; 428 //indent end 429 430 431 //indent input 432 int main(void) { 433 char (*f1)() = NULL; 434 char *(*f1)() = NULL; 435 char *(*f2)(); 436 } 437 //indent end 438 439 /* 440 * Before NetBSD io.c 1.103 from 2021-10-27, indent wrongly placed the second 441 * and third variable declaration in column 1. This bug has been introduced 442 * to NetBSD when FreeBSD indent was imported in 2019. 443 */ 444 //indent run -ldi0 445 int 446 main(void) 447 { 448 char (*f1)() = NULL; 449 char *(*f1)() = NULL; 450 char *(*f2)(); 451 } 452 //indent end 453 454 //indent run 455 int 456 main(void) 457 { 458 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */ 459 char (*f1)() = NULL; 460 /* $ XXX: Not really pretty, the name 'f1' should be aligned, if at all. */ 461 char *(* f1)() = NULL; 462 /* $ XXX: Not really pretty, the name 'f2' should be aligned, if at all. */ 463 char *(* f2)(); 464 } 465 //indent end 466 467 468 /* 469 * In some ancient time long before ISO C90, variable declarations with 470 * initializer could be written without '='. The C Programming Language from 471 * 1978 doesn't mention this form anymore. 472 * 473 * Before NetBSD lexi.c 1.123 from 2021-10-31, indent treated the '-' as a 474 * unary operator. 475 */ 476 //indent input 477 int a - 1; 478 { 479 int a - 1; 480 } 481 //indent end 482 483 //indent run -di0 484 int a - 1; 485 { 486 int a - 1; 487 } 488 //indent end 489 490 491 /* 492 * Between 2019-04-04 and before lexi.c 1.146 from 2021-11-19, the indentation 493 * of the '*' depended on the function name, which did not make sense. For 494 * function names that matched [A-Za-z]+, the '*' was placed correctly, for 495 * all other function names (containing [$0-9_]) the '*' was right-aligned on 496 * the declaration indentation, which defaults to 16. 497 */ 498 //indent input 499 int * 500 f2(void) 501 { 502 } 503 504 int * 505 yy(void) 506 { 507 } 508 509 int * 510 int_create(void) 511 { 512 } 513 //indent end 514 515 //indent run-equals-input 516 517 518 /* 519 * Since 2019-04-04, the space between the '){' is missing. 520 */ 521 //indent input 522 int * 523 function_name_____20________30________40________50 524 (void) 525 {} 526 //indent end 527 528 /* FIXME: The space between '){' is missing. */ 529 //indent run 530 int *function_name_____20________30________40________50 531 (void){ 532 } 533 //indent end 534 535 536 /* 537 * Since 2019-04-04 and before lexi.c 1.144 from 2021-11-19, some function 538 * names were preserved while others were silently discarded. 539 */ 540 //indent input 541 int * 542 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 543 (void) 544 {} 545 //indent end 546 547 //indent run 548 int *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 549 (void){ 550 } 551 //indent end 552 553 554 /* 555 * Before 1990, when C90 standardized function prototypes, a function 556 * declaration or definition did not contain a '*' that may have looked 557 * similar to the binary operator '*' because it was surrounded by two 558 * identifiers. 559 * 560 * As of 2021-11-21, indent interpreted the '*' in the function declaration in 561 * line 1 as a binary operator, even though the '*' was followed by a ',' 562 * directly. This was not visible in the output though since indent never 563 * outputs a space before a comma. 564 * 565 * In the function declaration in line 2 and the function definition in line 566 * 5, indent interpreted the '*' as a binary operator as well and accordingly 567 * placed spaces around the '*'. On a very low syntactical analysis level, 568 * this may have made sense since the '*' was surrounded by words, but still 569 * the '*' is part of a declaration, where a binary operator does not make 570 * sense. 571 * 572 * Essentially, as of 2021, indent had missed the last 31 years of advances in 573 * the C programming language, in particular the invention of function 574 * prototypes. Instead, the workaround had been to require all type names to 575 * be specified via the options '-ta' and '-T'. This put the burden on the 576 * user instead of the implementer. 577 * 578 * Fixed in lexi.c 1.156 from 2021-11-25. 579 */ 580 //indent input 581 void buffer_add(buffer *, char); 582 void buffer_add(buffer *buf, char ch); 583 584 void 585 buffer_add(buffer *buf, char ch) 586 { 587 *buf->e++ = ch; 588 } 589 //indent end 590 591 /* Before lexi.c 1.156 from 2021-11-25, indent generated 'buffer * buf'. */ 592 //indent run 593 void buffer_add(buffer *, char); 594 /* $ FIXME: space after '*' */ 595 void buffer_add(buffer * buf, char ch); 596 597 void 598 buffer_add(buffer *buf, char ch) 599 { 600 *buf->e++ = ch; 601 } 602 //indent end 603 604 605 /* 606 * Indent gets easily confused by type names it does not know about. 607 */ 608 //indent input 609 static Token 610 ToToken(bool cond) 611 { 612 } 613 //indent end 614 615 //indent run-equals-input -TToken 616 617 /* Since lexi.c 1.153 from 2021-11-25. */ 618 //indent run-equals-input 619 620 621 /* 622 * Indent gets easily confused by unknown type names in struct declarations. 623 */ 624 //indent input 625 typedef struct OpenDirs { 626 CachedDirList list; 627 HashTable /* of CachedDirListNode */ table; 628 } OpenDirs; 629 //indent end 630 631 /* FIXME: The word 'HashTable' must not be aligned like a member name. */ 632 //indent run 633 typedef struct OpenDirs { 634 CachedDirList list; 635 HashTable /* of CachedDirListNode */ table; 636 } OpenDirs; 637 //indent end 638 639 //indent run-equals-input -THashTable 640 641 642 /* 643 * Indent gets easily confused by unknown type names, even in declarations 644 * that are syntactically unambiguous. 645 */ 646 //indent input 647 static CachedDir *dot = NULL; 648 //indent end 649 650 //indent run-equals-input -TCachedDir 651 652 /* Since lexi.c 1.153 from 2021-11-25. */ 653 //indent run-equals-input 654 655 656 /* 657 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown 658 * type names in declarations and generated 'HashEntry * he' with an extra 659 * space. 660 */ 661 //indent input 662 static CachedDir * 663 CachedDir_New(const char *name) 664 { 665 } 666 //indent end 667 668 /* Since lexi.c 1.153 from 2021-11-25. */ 669 //indent run-equals-input 670 671 672 /* 673 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown 674 * type names in declarations and generated 'CachedDir * dir' with an extra 675 * space. 676 */ 677 //indent input 678 static CachedDir * 679 CachedDir_Ref(CachedDir *dir) 680 { 681 } 682 //indent end 683 684 //indent run-equals-input 685 686 687 /* 688 * Before lexi.c 1.156 from 2021-11-25, indent easily got confused by unknown 689 * type names in declarations and generated 'HashEntry * he' with an extra 690 * space. 691 * 692 * Before lexi.c 1.153 from 2021-11-25, indent also placed the '{' at the end 693 * of the line. 694 */ 695 //indent input 696 static bool 697 HashEntry_KeyEquals(const HashEntry *he, Substring key) 698 { 699 } 700 //indent end 701 702 //indent run-equals-input 703 704 705 /* 706 * Before lexi.c 1.156 from 2021-11-25, indent didn't notice that the two '*' 707 * are in a declaration, instead it interpreted the first '*' as a binary 708 * operator, therefore generating 'CachedDir * *var' with an extra space. 709 */ 710 //indent input 711 static void 712 CachedDir_Assign(CachedDir **var, CachedDir *dir) 713 { 714 } 715 //indent end 716 717 //indent run-equals-input 718 719 //indent run-equals-input -TCachedDir 720 721 722 /* 723 * Before lexi.c 1.153 from 2021-11-25, all initializer expressions after the 724 * first one were indented as if they would be statement continuations. This 725 * was because the token 'Shell' was identified as a word, not as a type name. 726 */ 727 //indent input 728 static Shell shells[] = { 729 { 730 first, 731 second, 732 }, 733 }; 734 //indent end 735 736 /* Since lexi.c 1.153 from 2021-11-25. */ 737 //indent run-equals-input 738 739 740 /* 741 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by function 742 * attribute macros that followed the function declaration. Its primitive 743 * heuristic between deciding between a function declaration and a function 744 * definition only looked for ')' immediately followed by ',' or ';'. This was 745 * sufficient for well-formatted code before 1990. With the addition of 746 * function prototypes and GCC attributes, the situation became more 747 * complicated, and it took indent 31 years to adapt to this new reality. 748 */ 749 //indent input 750 static void JobInterrupt(bool, int) MAKE_ATTR_DEAD; 751 static void JobRestartJobs(void); 752 //indent end 753 754 //indent run 755 /* $ FIXME: Missing space before 'MAKE_ATTR_DEAD'. */ 756 static void JobInterrupt(bool, int)MAKE_ATTR_DEAD; 757 static void JobRestartJobs(void); 758 //indent end 759 760 761 /* 762 * Before lexi.c 1.158 from 2021-11-25, indent easily got confused by the 763 * tokens ')' and ';' in the function body. It wrongly regarded them as 764 * finishing a function declaration. 765 */ 766 //indent input 767 MAKE_INLINE const char * 768 GNode_VarTarget(GNode *gn) { return GNode_ValueDirect(gn, TARGET); } 769 //indent end 770 771 /* 772 * Before lexi.c 1.156 from 2021-11-25, indent generated 'GNode * gn' with an 773 * extra space. 774 * 775 * Before lexi.c 1.158 from 2021-11-25, indent wrongly placed the function 776 * name in line 1, together with the '{'. 777 */ 778 //indent run 779 MAKE_INLINE const char * 780 GNode_VarTarget(GNode *gn) 781 { 782 return GNode_ValueDirect(gn, TARGET); 783 } 784 //indent end 785 786 //indent run-equals-prev-output -TGNode 787 788 789 /* 790 * Ensure that '*' in declarations is interpreted (or at least formatted) as 791 * a 'pointer to' type derivation, not as a binary or unary operator. 792 */ 793 //indent input 794 number *var = a * b; 795 796 void 797 function(void) 798 { 799 number *var = a * b; 800 } 801 //indent end 802 803 //indent run-equals-input -di0 804 805 806 /* 807 * In declarations, most occurrences of '*' are pointer type derivations. 808 * There are a few exceptions though. Some of these are hard to detect 809 * without knowing which identifiers are type names. 810 */ 811 //indent input 812 char str[expr * expr]; 813 char str[expr**ptr]; 814 char str[*ptr**ptr]; 815 char str[sizeof(expr * expr)]; 816 char str[sizeof(int) * expr]; 817 char str[sizeof(*ptr)]; 818 char str[sizeof(type**)]; 819 char str[sizeof(**ptr)]; 820 //indent end 821 822 //indent run -di0 823 char str[expr * expr]; 824 char str[expr * *ptr]; 825 char str[*ptr * *ptr]; 826 char str[sizeof(expr * expr)]; 827 char str[sizeof(int) * expr]; 828 char str[sizeof(*ptr)]; 829 /* $ FIXME: should be 'type **' */ 830 char str[sizeof(type * *)]; 831 char str[sizeof(**ptr)]; 832 //indent end 833 834 835 /* 836 * Since lexi.c 1.158 from 2021-11-25, whether the function 'a' was considered 837 * a declaration or a definition depended on the preceding struct, in 838 * particular the length of the 'pn' line. This didn't make sense at all and 839 * was due to an out-of-bounds memory access. 840 * 841 * Seen amongst others in args.c 1.72, function add_typedefs_from_file. 842 * Fixed in lexi.c 1.165 from 2021-11-27. 843 */ 844 //indent input 845 struct { 846 } v = { 847 pn("ta"), 848 }; 849 850 static void 851 a(char *fe) 852 { 853 } 854 855 struct { 856 } v = { 857 pn("t"), 858 }; 859 860 static void 861 a(char *fe) 862 { 863 } 864 //indent end 865 866 //indent run -di0 867 struct { 868 } v = { 869 pn("ta"), 870 }; 871 872 static void 873 a(char *fe) 874 { 875 } 876 877 struct { 878 } v = { 879 pn("t"), 880 }; 881 882 static void 883 a(char *fe) 884 { 885 } 886 //indent end 887 888 889 /* 890 * Before NetBSD indent.c 1.178 from 2021-10-29, indent removed the blank 891 * before the '=', in the second and third of these function pointer 892 * declarations. This was because indent interpreted the prototype parameters 893 * 'int' and 'int, int' as type casts, which doesn't make sense at all. Fixing 894 * this properly requires large style changes since indent is based on simple 895 * heuristics all over. This didn't change in indent.c 1.178; instead, the 896 * rule for inserting a blank before a binary operator was changed to always 897 * insert a blank, except at the beginning of a line. 898 */ 899 //indent input 900 char *(*fn)() = NULL; 901 char *(*fn)(int) = NULL; 902 char *(*fn)(int, int) = NULL; 903 //indent end 904 905 /* XXX: The parameter '(int)' is wrongly interpreted as a type cast. */ 906 /* XXX: The parameter '(int, int)' is wrongly interpreted as a type cast. */ 907 //indent run-equals-input -di0 908