1 /* $NetBSD: indent.h,v 1.204 2023/06/26 20:03:09 rillig Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (c) 2001 Jens Schweikhardt 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 /*- 31 * SPDX-License-Identifier: BSD-4-Clause 32 * 33 * Copyright (c) 1985 Sun Microsystems, Inc. 34 * Copyright (c) 1980, 1993 35 * The Regents of the University of California. All rights reserved. 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #include <ctype.h> 68 #include <stdbool.h> 69 #include <stdio.h> 70 71 typedef enum lexer_symbol { 72 lsym_eof, 73 lsym_preprocessing, /* the initial '#' of a preprocessing line */ 74 lsym_newline, /* outside block comments */ 75 lsym_comment, /* the initial '/ *' or '//' of a comment */ 76 77 lsym_lparen, 78 lsym_rparen, 79 lsym_lbracket, 80 lsym_rbracket, 81 lsym_lbrace, 82 lsym_rbrace, 83 84 lsym_period, 85 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */ 86 lsym_sizeof, 87 lsym_offsetof, 88 lsym_postfix_op, /* trailing '++' or '--' */ 89 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */ 90 lsym_question, /* the '?' from a '?:' expression */ 91 lsym_question_colon, /* the ':' from a '?:' expression */ 92 lsym_comma, 93 94 lsym_typedef, 95 lsym_modifier, /* modifiers for types, functions, variables */ 96 lsym_tag, /* 'struct', 'union' or 'enum' */ 97 lsym_type, 98 lsym_word, /* identifier, constant or string */ 99 lsym_funcname, /* name of a function being defined */ 100 lsym_label_colon, /* the ':' after a label */ 101 lsym_other_colon, /* bit-fields, generic-association (C11), 102 * enum-type-specifier (C23), 103 * attribute-prefixed-token (C23), 104 * pp-prefixed-parameter (C23 6.10) */ 105 lsym_semicolon, 106 107 lsym_case, 108 lsym_default, 109 lsym_do, 110 lsym_else, 111 lsym_for, 112 lsym_if, 113 lsym_switch, 114 lsym_while, 115 lsym_return, 116 } lexer_symbol; 117 118 /* 119 * Structure of the source code, in terms of declarations, statements and 120 * braces; used to determine the indentation level of these parts. 121 */ 122 typedef enum parser_symbol { 123 psym_0, /* a placeholder; not stored on the stack */ 124 psym_lbrace_block, /* '{' for a block of code */ 125 psym_lbrace_struct, /* '{' in 'struct ... { ... }' */ 126 psym_lbrace_union, /* '{' in 'union ... { ... }' */ 127 psym_lbrace_enum, /* '{' in 'enum ... { ... }' */ 128 psym_rbrace, /* not stored on the stack */ 129 psym_decl, 130 psym_stmt, 131 psym_for_exprs, /* 'for' '(' ... ')' */ 132 psym_if_expr, /* 'if' '(' expr ')' */ 133 psym_if_expr_stmt, /* 'if' '(' expr ')' stmt */ 134 psym_if_expr_stmt_else, /* 'if' '(' expr ')' stmt 'else' */ 135 psym_else, /* 'else'; not stored on the stack */ 136 psym_switch_expr, /* 'switch' '(' expr ')' */ 137 psym_do, /* 'do' */ 138 psym_do_stmt, /* 'do' stmt */ 139 psym_while_expr, /* 'while' '(' expr ')' */ 140 } parser_symbol; 141 142 /* A range of characters, only null-terminated in debug mode. */ 143 struct buffer { 144 char *s; 145 size_t len; 146 size_t cap; 147 }; 148 149 extern FILE *input; 150 extern FILE *output; 151 152 /* 153 * The current line from the input file, used by the lexer to generate tokens. 154 * To read from the line, start at inp_p and continue up to and including the 155 * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will 156 * make the next line available, invalidating any pointers into the previous 157 * line. 158 */ 159 extern struct buffer inp; 160 extern const char *inp_p; 161 162 extern struct buffer token; /* the current token to be processed, is 163 * typically copied to the buffer 'code', or in 164 * some cases to 'lab'. */ 165 166 extern struct buffer lab; /* the label or preprocessor directive */ 167 extern struct buffer code; /* the main part of the current line of code, 168 * containing declarations or statements */ 169 extern struct buffer com; /* the trailing comment of the line, or the 170 * start or end of a multi-line comment, or 171 * while in process_comment, a single line of a 172 * multi-line comment */ 173 174 extern struct options { 175 bool blank_line_around_conditional_compilation; 176 bool blank_line_after_decl_at_top; /* this is vaguely similar to 177 * blank_line_after_decl except 178 * that it only applies to the 179 * first set of declarations in 180 * a procedure (just after the 181 * first '{') and it causes a 182 * blank line to be generated 183 * even if there are no 184 * declarations */ 185 bool blank_line_after_decl; 186 bool blank_line_after_proc; 187 bool blank_line_before_block_comment; 188 bool break_after_comma; /* whether to add a line break after each 189 * declarator */ 190 bool brace_same_line; /* whether a brace should be on same line as an 191 * if, while, etc. */ 192 bool blank_after_sizeof; 193 bool comment_delimiter_on_blank_line; 194 int decl_comment_column; /* the column in which comments after 195 * declarations should be put */ 196 bool cuddle_else; /* whether 'else' should cuddle up to '}' */ 197 int continuation_indent; /* the indentation between the edge of 198 * code and continuation lines */ 199 float case_indent; /* the distance (measured in indentation 200 * levels) to indent case labels from the 201 * switch statement */ 202 int comment_column; /* the column in which comments to the right of 203 * code should start */ 204 int decl_indent; /* indentation of identifier in declaration */ 205 bool left_justify_decl; 206 int unindent_displace; /* comments not to the right of code will be 207 * placed this many indentation levels to the 208 * left of code */ 209 bool extra_expr_indent; /* whether continuation lines from the 210 * expression part of "if (e)", "while (e)", 211 * "for (e; e; e)" should be indented an extra 212 * tab stop so that they are not confused with 213 * the code that follows */ 214 bool else_if_in_same_line; 215 bool function_brace_split; /* split function declaration and brace 216 * onto separate lines */ 217 bool format_col1_comments; /* whether comments that start in 218 * column 1 are to be reformatted (just 219 * like comments that begin in later 220 * columns) */ 221 bool format_block_comments; /* whether to reformat comments that 222 * begin with '/ * \n' */ 223 bool indent_parameters; 224 int indent_size; /* the size of one indentation level */ 225 int block_comment_max_line_length; 226 int local_decl_indent; /* like decl_indent but for locals */ 227 bool lineup_to_parens_always; /* whether to not(?) attempt to keep 228 * lined-up code within the margin */ 229 bool lineup_to_parens; /* whether continued code within parens will be 230 * lined up to the open paren */ 231 bool proc_calls_space; /* whether function calls look like 'foo (bar)' 232 * rather than 'foo(bar)' */ 233 bool procnames_start_line; /* whether the names of functions being 234 * defined get placed in column 1 (i.e. 235 * a newline is placed between the type 236 * of the function and its name) */ 237 bool space_after_cast; /* "b = (int) a" vs. "b = (int)a" */ 238 bool star_comment_cont; /* whether comment continuation lines should 239 * have stars at the beginning of each line */ 240 bool swallow_optional_blank_lines; 241 bool auto_typedefs; /* whether to recognize identifiers ending in 242 * "_t" like typedefs */ 243 int tabsize; /* the size of a tab */ 244 int max_line_length; 245 bool use_tabs; /* set true to use tabs for spacing, false uses 246 * all spaces */ 247 bool verbose; /* print configuration to stderr */ 248 } opt; 249 250 extern bool found_err; 251 extern bool had_eof; /* whether input is exhausted */ 252 extern int line_no; /* the current input line number */ 253 extern enum indent_enabled { 254 indent_on, 255 indent_off, 256 indent_last_off_line, 257 } indent_enabled; 258 259 /* Properties of each level of parentheses or brackets. */ 260 struct paren_level { 261 int indent; /* indentation of the operand/argument, 262 * relative to the enclosing statement; if 263 * negative, reflected at -1 */ 264 enum paren_level_cast { 265 cast_unknown, 266 cast_maybe, 267 cast_no, 268 } cast; /* whether the parentheses form a type cast */ 269 }; 270 271 struct psym_stack { 272 parser_symbol *sym; 273 int *ind_level; 274 size_t len; /* points to one behind the top of the stack; 1 275 * at the top level of the file outside a 276 * declaration or statement; 2 at the top level 277 */ 278 size_t cap; 279 }; 280 281 /* 282 * The parser state determines the layout of the formatted text. 283 * 284 * At each '#if', the parser state is copied so that the corresponding '#else' 285 * lines start in the same state. 286 * 287 * In a function body, the number of block braces determines the indentation 288 * of statements and declarations. 289 * 290 * In a statement, the number of parentheses or brackets determines the 291 * indentation of follow-up lines. 292 * 293 * In an expression, the token type determine whether to put spaces around. 294 * 295 * In a source file, the types of line determine the vertical spacing, such as 296 * around preprocessing directives or function bodies, or above block 297 * comments. 298 */ 299 extern struct parser_state { 300 lexer_symbol prev_lsym; /* the previous token, but never comment, 301 * newline or preprocessing line */ 302 303 /* Token classification */ 304 305 bool in_stmt_or_decl; /* whether in a statement or a struct 306 * declaration or a plain declaration */ 307 bool in_decl; /* XXX: double-check the exact meaning */ 308 bool in_typedef_decl; 309 bool in_var_decl; /* starts at a type name or a '){' from a 310 * compound literal; ends at the '(' from a 311 * function definition or a ';' outside '{}'; 312 * when active, '{}' form struct or union 313 * declarations, ':' marks a bit-field, and '=' 314 * starts an initializer */ 315 bool in_init; /* whether inside an initializer */ 316 int init_level; /* the number of '{}' in an initializer */ 317 bool line_has_func_def; /* starts either at the 'name(' from a function 318 * definition if it occurs at the beginning of 319 * a line, or at the first '*' from inside a 320 * declaration when the line starts with words 321 * followed by a '(' */ 322 bool in_func_def_params; /* for old-style functions */ 323 bool line_has_decl; /* whether this line of code has part of a 324 * declaration on it; used for indenting 325 * comments */ 326 parser_symbol lbrace_kind; /* the kind of brace to be pushed to 327 * the parser symbol stack next */ 328 parser_symbol spaced_expr_psym; /* the parser symbol to be shifted 329 * after the parenthesized expression 330 * from a 'for', 'if', 'switch' or 331 * 'while'; or psym_0 */ 332 bool seen_case; /* whether there was a 'case' or 'default', to 333 * properly space the following ':' */ 334 bool prev_paren_was_cast; 335 int quest_level; /* when this is positive, we have seen a '?' 336 * without the matching ':' in a '?:' 337 * expression */ 338 339 /* Indentation of statements and declarations */ 340 341 int ind_level; /* the indentation level for the line that is 342 * currently prepared for output */ 343 int ind_level_follow; /* the level to which ind_level should be set 344 * after the current line is printed */ 345 bool line_is_stmt_cont; /* whether the current line should have an 346 * extra indentation level because we are in 347 * the middle of a statement */ 348 int decl_level; /* current nesting level for a structure 349 * declaration or an initializer */ 350 int di_stack[20]; /* a stack of structure indentation levels */ 351 bool decl_indent_done; /* whether the indentation for a declaration 352 * has been added to the code buffer. */ 353 int decl_ind; /* current indentation for declarations */ 354 bool tabs_to_var; /* true if using tabs to indent to var name */ 355 356 enum { 357 eei_no, 358 eei_maybe, 359 eei_last 360 } extra_expr_indent; 361 362 struct psym_stack psyms; 363 364 /* Spacing inside a statement or declaration */ 365 366 bool next_unary; /* whether the following operator should be 367 * unary; is used in declarations for '*', as 368 * well as in expressions */ 369 bool want_blank; /* whether the following token should be 370 * prefixed by a blank. (Said prefixing is 371 * ignored in some cases.) */ 372 int ind_paren_level; /* the number of parentheses or brackets that 373 * is used for indenting a continuation line of 374 * a declaration, initializer or statement */ 375 struct paren_stack { 376 struct paren_level *item; 377 size_t len; 378 size_t cap; 379 } paren; /* the parentheses or brackets that are 380 * currently open; used to indent the remaining 381 * lines of the statement, initializer or 382 * declaration */ 383 384 /* Indentation of comments */ 385 386 int comment_ind; /* total indentation of the current comment */ 387 int comment_shift; /* all but the first line of a boxed comment 388 * are shifted this much to the right */ 389 bool comment_cont; /* after the first line of a comment */ 390 391 /* Vertical spacing */ 392 393 bool break_after_comma; /* whether to add a newline after the next 394 * comma; used in declarations but not in 395 * initializer lists */ 396 bool want_newline; /* whether the next token should go to a new 397 * line; used after 'if (expr)' and in similar 398 * situations; tokens like '{' or ';' may 399 * ignore this */ 400 401 enum declaration { 402 decl_no, /* no declaration anywhere nearby */ 403 decl_begin, /* collecting tokens of a declaration */ 404 decl_end, /* finished a declaration */ 405 } declaration; 406 bool blank_line_after_decl; 407 408 enum { 409 badp_none, 410 badp_seen_lbrace, 411 badp_decl, /* in an unfinished declaration in the first 412 * block of declarations in a function body */ 413 badp_seen_decl, /* seen the semicolon of a declaration; the 414 * next line is a candidate for inserting a 415 * blank line above */ 416 badp_yes, /* this line is a candidate for inserting a 417 * blank line above, unless the line turns out 418 * to start a declaration */ 419 } badp; /* insert a blank line before the first 420 * statement in a function body */ 421 } ps; 422 423 extern struct output_state { 424 enum line_kind { 425 lk_other, 426 lk_blank, 427 lk_pre_if, /* #if, #ifdef, #ifndef */ 428 lk_pre_endif, /* #endif */ 429 lk_pre_other, /* #else, #elif, #define, #undef */ 430 lk_stmt_head, /* the ')' of an incomplete statement such as 431 * 'if (expr)' or 'for (expr; expr; expr)' */ 432 lk_func_end, /* the last '}' of a function body */ 433 lk_block_comment, 434 lk_case_or_default, 435 } line_kind; /* kind of the line that is being prepared for 436 * output; is reset to lk_other each time after 437 * trying to send a line to the output, even if 438 * that line was a suppressed blank line; used 439 * for inserting or removing blank lines */ 440 enum line_kind prev_line_kind; /* the kind of line that was actually 441 * sent to the output */ 442 443 struct buffer indent_off_text; /* text from between 'INDENT OFF' and 444 * 'INDENT ON', both inclusive */ 445 } out; 446 447 448 #define array_length(array) (sizeof(array) / sizeof((array)[0])) 449 450 #ifdef debug 451 void debug_printf(const char *, ...) __printflike(1, 2); 452 void debug_println(const char *, ...) __printflike(1, 2); 453 void debug_blank_line(void); 454 void debug_vis_range(const char *, size_t); 455 void debug_parser_state(void); 456 void debug_psyms_stack(const char *); 457 void debug_print_buf(const char *, const struct buffer *); 458 void debug_buffers(void); 459 void parser_state_back_up(struct parser_state *); 460 void parser_state_free(struct parser_state *); 461 extern const char *const lsym_name[]; 462 extern const char *const psym_name[]; 463 extern const char *const paren_level_cast_name[]; 464 extern const char *const line_kind_name[]; 465 #define static_unless_debug /* nothing */ 466 #else 467 #define debug_noop() do { } while (false) 468 #define debug_printf(fmt, ...) debug_noop() 469 #define debug_println(fmt, ...) debug_noop() 470 #define debug_blank_line() debug_noop() 471 #define debug_vis_range(s, len) debug_noop() 472 #define debug_parser_state() debug_noop() 473 #define debug_psyms_stack(situation) debug_noop() 474 #define debug_print_buf(name, buf) debug_noop() 475 #define debug_buffers() debug_noop() 476 #define static_unless_debug static 477 #endif 478 479 void register_typename(const char *); 480 int compute_code_indent(void); 481 int compute_label_indent(void); 482 int ind_add(int, const char *, size_t); 483 484 void inp_skip(void); 485 char inp_next(void); 486 void finish_output(void); 487 488 lexer_symbol lexi(void); 489 void diag(int, const char *, ...) __printflike(2, 3); 490 void output_line(void); 491 void inp_read_line(void); 492 void parse(parser_symbol); 493 void process_comment(void); 494 void set_option(const char *, const char *); 495 void load_profile_files(const char *); 496 void ps_push(parser_symbol, bool); 497 498 void *nonnull(void *); 499 500 void buf_add_char(struct buffer *, char); 501 void buf_add_chars(struct buffer *, const char *, size_t); 502 503 static inline bool 504 ch_isalnum(char ch) 505 { 506 return isalnum((unsigned char)ch) != 0; 507 } 508 509 static inline bool 510 ch_isalpha(char ch) 511 { 512 return isalpha((unsigned char)ch) != 0; 513 } 514 515 static inline bool 516 ch_isblank(char ch) 517 { 518 return ch == ' ' || ch == '\t'; 519 } 520 521 static inline bool 522 ch_isdigit(char ch) 523 { 524 return '0' <= ch && ch <= '9'; 525 } 526 527 static inline bool 528 ch_isspace(char ch) 529 { 530 return isspace((unsigned char)ch) != 0; 531 } 532 533 static inline int 534 next_tab(int ind) 535 { 536 return ind - ind % opt.tabsize + opt.tabsize; 537 } 538 539 #ifdef debug 540 void buf_terminate(struct buffer *); 541 #else 542 #define buf_terminate(buf) debug_noop() 543 #endif 544 545 static inline void 546 buf_clear(struct buffer *buf) 547 { 548 buf->len = 0; 549 buf_terminate(buf); 550 } 551