Home | History | Annotate | Line # | Download | only in indent
indent.h revision 1.177
      1 /*	$NetBSD: indent.h,v 1.177 2023/06/07 15:46:12 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,
     75 	lsym_comment,		/* the initial '/ *' or '//' of a comment */
     76 	lsym_lparen,
     77 	lsym_lbracket,
     78 	lsym_rparen,
     79 	lsym_rbracket,
     80 	lsym_lbrace,
     81 	lsym_rbrace,
     82 	lsym_period,
     83 	lsym_unary_op,		/* e.g. '*', '&', '-' or leading '++' */
     84 	lsym_binary_op,		/* e.g. '*', '&', '<<', '&&' or '/=' */
     85 	lsym_postfix_op,	/* trailing '++' or '--' */
     86 	lsym_question,		/* the '?' from a '?:' expression */
     87 	lsym_colon_question,	/* the ':' from a '?:' expression */
     88 	lsym_colon_label,	/* the ':' after a label */
     89 	lsym_colon_other,	/* bit-fields, generic-association (C11),
     90 				 * enum-type-specifier (C23),
     91 				 * attribute-prefixed-token (C23),
     92 				 * pp-prefixed-parameter (C23 6.10) */
     93 	lsym_comma,
     94 	lsym_semicolon,
     95 	lsym_typedef,
     96 	lsym_modifier,		/* modifiers for types, functions, variables */
     97 	lsym_type_outside_parentheses,
     98 	lsym_type_in_parentheses,
     99 	lsym_tag,		/* 'struct', 'union' or 'enum' */
    100 	lsym_case,
    101 	lsym_default,
    102 	lsym_sizeof,
    103 	lsym_offsetof,
    104 	lsym_word,		/* identifier, constant or string */
    105 	lsym_funcname,		/* name of a function being defined */
    106 	lsym_do,
    107 	lsym_else,
    108 	lsym_for,
    109 	lsym_if,
    110 	lsym_switch,
    111 	lsym_while,
    112 	lsym_return,
    113 } lexer_symbol;
    114 
    115 /*
    116  * Structure of the source code, in terms of declarations, statements and
    117  * braces; used to determine the indentation level of these parts.
    118  */
    119 typedef enum parser_symbol {
    120 	psym_0,			/* a placeholder; not stored on the stack */
    121 	psym_lbrace_block,	/* '{' for a block of code */
    122 	psym_lbrace_struct,	/* '{' in 'struct ... { ... }' */
    123 	psym_lbrace_union,	/* '{' in 'union ... { ... }' */
    124 	psym_lbrace_enum,	/* '{' in 'enum ... { ... }' */
    125 	psym_rbrace,		/* not stored on the stack */
    126 	psym_decl,
    127 	psym_stmt,
    128 	psym_stmt_list,
    129 	psym_for_exprs,		/* 'for' '(' ... ')' */
    130 	psym_if_expr,		/* 'if' '(' expr ')' */
    131 	psym_if_expr_stmt,	/* 'if' '(' expr ')' stmt */
    132 	psym_if_expr_stmt_else,	/* 'if' '(' expr ')' stmt 'else' */
    133 	psym_else,		/* 'else'; not stored on the stack */
    134 	psym_switch_expr,	/* 'switch' '(' expr ')' */
    135 	psym_do,		/* 'do' */
    136 	psym_do_stmt,		/* 'do' stmt */
    137 	psym_while_expr,	/* 'while' '(' expr ')' */
    138 } parser_symbol;
    139 
    140 /* A range of characters, not null-terminated. */
    141 struct buffer {
    142 	char *s;
    143 	size_t len;
    144 	size_t cap;
    145 };
    146 
    147 extern FILE *input;
    148 extern FILE *output;
    149 
    150 /*
    151  * The current line from the input file, used by the lexer to generate tokens.
    152  * To read from the line, start at inp_p and continue up to and including the
    153  * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
    154  * make the next line available, invalidating any pointers into the previous
    155  * line.
    156  */
    157 extern struct buffer inp;
    158 extern const char *inp_p;
    159 
    160 extern struct buffer token;	/* the current token to be processed, is
    161 				 * typically copied to the buffer 'code', or in
    162 				 * some cases to 'lab'. */
    163 
    164 extern struct buffer lab;	/* the label or preprocessor directive */
    165 extern struct buffer code;	/* the main part of the current line of code,
    166 				 * containing declarations or statements */
    167 extern struct buffer com;	/* the trailing comment of the line, or the
    168 				 * start or end of a multi-line comment, or
    169 				 * while in process_comment, a single line of a
    170 				 * multi-line comment */
    171 
    172 extern struct options {
    173 	bool blanklines_around_conditional_compilation;
    174 	bool blank_line_after_decl_at_top;	/* this is vaguely similar to
    175 						 * blank_line_after_decl except
    176 						 * that it only applies to the
    177 						 * first set of declarations in
    178 						 * a procedure (just after the
    179 						 * first '{') and it causes a
    180 						 * blank line to be generated
    181 						 * even if there are no
    182 						 * declarations */
    183 	bool blank_line_after_decl;
    184 	bool blanklines_after_procs;
    185 	bool blanklines_before_block_comments;
    186 	bool break_after_comma;	/* whether to add a line break after each
    187 				 * declarator */
    188 	bool brace_same_line;	/* whether brace should be on same line as if,
    189 				 * while, etc */
    190 	bool blank_after_sizeof;	/* whether a blank should always be
    191 					 * inserted after sizeof */
    192 	bool comment_delimiter_on_blankline;
    193 	int decl_comment_column;	/* the column in which comments after
    194 					 * declarations should be put */
    195 	bool cuddle_else;	/* whether 'else' should cuddle up to '}' */
    196 	int continuation_indent;	/* the indentation between the edge of
    197 					 * code and continuation lines */
    198 	float case_indent;	/* The distance (measured in indentation
    199 				 * levels) to indent case labels from the
    200 				 * switch statement */
    201 	int comment_column;	/* the column in which comments to the right of
    202 				 * code should start */
    203 	int decl_indent;	/* indentation of identifier in declaration */
    204 	bool left_justify_decl;
    205 	int unindent_displace;	/* comments not to the right of code will be
    206 				 * placed this many indentation levels to the
    207 				 * left of code */
    208 	bool extra_expr_indent;	/* whether continuation lines from the
    209 				 * expression part of "if (e)", "while (e)",
    210 				 * "for (e; e; e)" should be indented an extra
    211 				 * tab stop so that they are not confused with
    212 				 * the code that follows */
    213 	bool else_if_in_same_line;
    214 	bool function_brace_split;	/* split function declaration and brace
    215 					 * onto separate lines */
    216 	bool format_col1_comments;	/* If comments which start in column 1
    217 					 * are to be reformatted (just like
    218 					 * comments that begin in later
    219 					 * columns) */
    220 	bool format_block_comments;	/* whether comments beginning with '/ *
    221 					 * \n' are to be reformatted */
    222 	bool indent_parameters;
    223 	int indent_size;	/* the size of one indentation level */
    224 	int block_comment_max_line_length;
    225 	int local_decl_indent;	/* like decl_indent but for locals */
    226 	bool lineup_to_parens_always;	/* whether to not(?) attempt to keep
    227 					 * lined-up code within the margin */
    228 	bool lineup_to_parens;	/* whether continued code within parens will be
    229 				 * lined up to the open paren */
    230 	bool proc_calls_space;	/* whether function calls look like: foo (bar)
    231 				 * rather than foo(bar) */
    232 	bool procnames_start_line;	/* whether the names of functions being
    233 					 * defined get placed in column 1 (i.e.
    234 					 * a newline is placed between the type
    235 					 * of the function and its name) */
    236 	bool space_after_cast;	/* "b = (int) a" vs. "b = (int)a" */
    237 	bool star_comment_cont;	/* whether comment continuation lines should
    238 				 * have stars at the beginning of each line */
    239 	bool swallow_optional_blanklines;
    240 	bool auto_typedefs;	/* whether to recognize identifiers ending in
    241 				 * "_t" like typedefs */
    242 	int tabsize;		/* the size of a tab */
    243 	int max_line_length;
    244 	bool use_tabs;		/* set true to use tabs for spacing, false uses
    245 				 * all spaces */
    246 	bool verbose;		/* print configuration to stderr */
    247 } opt;
    248 
    249 extern bool found_err;
    250 extern bool had_eof;		/* whether input is exhausted */
    251 extern int line_no;		/* the current line number. */
    252 extern enum indent_enabled {
    253 	indent_on,
    254 	indent_off,
    255 	indent_last_off_line,
    256 } indent_enabled;
    257 
    258 #define	STACKSIZE 256
    259 
    260 /* Properties of each level of parentheses or brackets. */
    261 typedef struct paren_level_props {
    262 	int indent;		/* indentation of the operand/argument,
    263 				 * relative to the enclosing statement; if
    264 				 * negative, reflected at -1 */
    265 	enum paren_level_cast {
    266 		cast_unknown,
    267 		cast_maybe,
    268 		cast_no,
    269 	} cast;			/* whether the parentheses form a type cast */
    270 } paren_level_props;
    271 
    272 struct psym_stack {
    273 	int top;		/* pointer to top of stack */
    274 	parser_symbol sym[STACKSIZE];
    275 	int ind_level[STACKSIZE];
    276 };
    277 
    278 /*
    279  * The parser state determines the layout of the formatted text.
    280  *
    281  * At each '#if', the parser state is copied so that the corresponding '#else'
    282  * lines start in the same state.
    283  *
    284  * In a function body, the number of block braces determines the indentation
    285  * of statements and declarations.
    286  *
    287  * In a statement, the number of parentheses or brackets determines the
    288  * indentation of follow-up lines.
    289  *
    290  * In an expression, the token type determine whether to put spaces around.
    291  *
    292  * In a source file, the types of line determine the vertical spacing, such as
    293  * around preprocessing directives or function bodies, or above block
    294  * comments.
    295  */
    296 extern struct parser_state {
    297 	lexer_symbol prev_lsym;	/* the previous token, but never comment,
    298 				 * newline or preprocessing line */
    299 
    300 	/* Token classification */
    301 
    302 	int quest_level;	/* when this is positive, we have seen a '?'
    303 				 * without the matching ':' in a '?:'
    304 				 * expression */
    305 	bool is_function_definition;	/* starts either at the 'name(' from a
    306 					 * function definition if it occurs at
    307 					 * the beginning of a line, or at the
    308 					 * first '*' from inside a declaration
    309 					 * when the line starts with words
    310 					 * followed by a '('; ends at the end
    311 					 * of that line */
    312 	bool block_init;	/* whether inside a block initialization */
    313 	int block_init_level;	/* the level of brace nesting in an
    314 				 * initialization */
    315 	bool init_or_struct;	/* whether there has been a type name and no
    316 				 * left parenthesis since the last semicolon.
    317 				 * When true, a '{' starts a structure
    318 				 * definition or an initialization list */
    319 	bool decl_on_line;	/* whether this line of code has part of a
    320 				 * declaration on it */
    321 	bool in_stmt_or_decl;	/* whether in a statement or a struct
    322 				 * declaration or a plain declaration */
    323 	bool in_decl;		/* whether we are in a declaration. The
    324 				 * processing of braces is then slightly
    325 				 * different */
    326 	bool in_func_def_params;
    327 	bool seen_case;		/* whether there was a 'case' or 'default', to
    328 				 * properly space the following ':' */
    329 	parser_symbol spaced_expr_psym;	/* the parser symbol to be shifted
    330 					 * after the parenthesized expression
    331 					 * from a 'for', 'if', 'switch' or
    332 					 * 'while'; or psym_0 */
    333 	parser_symbol lbrace_kind;	/* the kind of brace to be pushed to
    334 					 * the parser symbol stack next */
    335 
    336 	/* Indentation of statements and declarations */
    337 
    338 	int ind_level;		/* the indentation level for the line that is
    339 				 * currently prepared for output */
    340 	int ind_level_follow;	/* the level to which ind_level should be set
    341 				 * after the current line is printed */
    342 	bool in_stmt_cont;	/* whether the current line should have an
    343 				 * extra indentation level because we are in
    344 				 * the middle of a statement */
    345 	int decl_level;		/* current nesting level for a structure
    346 				 * declaration or an initializer */
    347 	int di_stack[20];	/* a stack of structure indentation levels */
    348 	bool decl_indent_done;	/* whether the indentation for a declaration
    349 				 * has been added to the code buffer. */
    350 	int decl_ind;		/* current indentation for declarations */
    351 	bool tabs_to_var;	/* true if using tabs to indent to var name */
    352 
    353 	enum {
    354 		eei_no,
    355 		eei_yes,
    356 		eei_last
    357 	} extra_expr_indent;
    358 
    359 	struct psym_stack psyms;
    360 
    361 	/* Spacing inside a statement or declaration */
    362 
    363 	bool next_unary;	/* whether the following operator should be
    364 				 * unary; is used in declarations for '*', as
    365 				 * well as in expressions */
    366 	bool want_blank;	/* whether the following token should be
    367 				 * prefixed by a blank. (Said prefixing is
    368 				 * ignored in some cases.) */
    369 	int line_start_nparen;	/* the number of parentheses or brackets that
    370 				 * were open at the beginning of the current
    371 				 * line; used to indent within statements,
    372 				 * initializers and declarations */
    373 	int nparen;		/* the number of parentheses or brackets that
    374 				 * are currently open; used to indent the
    375 				 * remaining lines of the statement,
    376 				 * initializer or declaration */
    377 	paren_level_props paren[20];
    378 	enum {
    379 		dp_start,	/* the beginning of a declaration */
    380 		dp_word,	/* seen a type name */
    381 		dp_word_asterisk,	/* seen a type name and some '*' */
    382 		dp_other,
    383 	} decl_ptr;		/* detects declarations like 'typename *x', to
    384 				 * prevent the '*' from being interpreted as a
    385 				 * binary operator */
    386 
    387 	/* Horizontal spacing for comments */
    388 
    389 	int comment_delta;	/* used to set up indentation for all lines of
    390 				 * a boxed comment after the first one */
    391 	int n_comment_delta;	/* remembers how many columns there were before
    392 				 * the start of a box comment so that
    393 				 * forthcoming lines of the comment are
    394 				 * indented properly */
    395 	int com_ind;		/* indentation of the current comment */
    396 
    397 	/* Vertical spacing */
    398 
    399 	bool break_after_comma;	/* whether to add a newline after the next
    400 				 * comma; used in declarations but not in
    401 				 * initializer lists */
    402 	bool force_nl;		/* whether the next token is forced to go to a
    403 				 * new line; used after 'if (expr)' and in
    404 				 * similar situations; tokens like '{' may
    405 				 * ignore this */
    406 
    407 	enum declaration {
    408 		decl_no,	/* no declaration anywhere nearby */
    409 		decl_begin,	/* collecting tokens of a declaration */
    410 		decl_end,	/* finished a declaration */
    411 	} declaration;
    412 	bool blank_line_after_decl;
    413 
    414 	/* Comments */
    415 
    416 	bool curr_col_1;	/* whether the current token started in column
    417 				 * 1 of the original input */
    418 	bool next_col_1;
    419 } ps;
    420 
    421 extern struct output_state {
    422 	enum line_kind {
    423 		lk_other,
    424 		lk_blank,
    425 		lk_if,		/* #if, #ifdef, #ifndef */
    426 		lk_endif,	/* #endif */
    427 		lk_stmt_head,	/* the ')' of an incomplete statement such as
    428 				 * 'if (expr)' or 'for (expr; expr; expr)' */
    429 		lk_func_end,	/* the last '}' of a function body */
    430 		lk_block_comment,
    431 		lk_case_or_default,
    432 	} line_kind;		/* kind of the line that is being prepared for
    433 				 * output; is reset to lk_other each time after
    434 				 * trying to send a line to the output, even if
    435 				 * that line was a suppressed blank line; used
    436 				 * for inserting or removing blank lines */
    437 	enum line_kind prev_line_kind;	/* the kind of line that was actually
    438 					 * sent to the output */
    439 
    440 	struct buffer indent_off_text;	/* text from between 'INDENT OFF' and
    441 					 * 'INDENT ON', both inclusive */
    442 } out;
    443 
    444 
    445 #define array_length(array) (sizeof(array) / sizeof((array)[0]))
    446 
    447 #ifdef debug
    448 void debug_printf(const char *, ...) __printflike(1, 2);
    449 void debug_println(const char *, ...) __printflike(1, 2);
    450 void debug_blank_line(void);
    451 void debug_vis_range(const char *, const char *, size_t, const char *);
    452 void debug_parser_state(void);
    453 void debug_parse_stack(const char *);
    454 void debug_print_buf(const char *, const struct buffer *);
    455 void debug_buffers(void);
    456 extern const char *const lsym_name[];
    457 extern const char *const psym_name[];
    458 extern const char *const paren_level_cast_name[];
    459 extern const char *const line_kind_name[];
    460 #else
    461 #define debug_noop() do { } while (false)
    462 #define	debug_printf(fmt, ...) debug_noop()
    463 #define	debug_println(fmt, ...) debug_noop()
    464 #define debug_blank_line() debug_noop()
    465 #define	debug_vis_range(prefix, s, e, suffix) debug_noop()
    466 #define	debug_parser_state() debug_noop()
    467 #define	debug_parse_stack(situation) debug_noop()
    468 #define debug_print_buf(name, buf) debug_noop()
    469 #define	debug_buffers() debug_noop()
    470 #endif
    471 
    472 void register_typename(const char *);
    473 int compute_code_indent(void);
    474 int compute_label_indent(void);
    475 int ind_add(int, const char *, size_t);
    476 
    477 void inp_skip(void);
    478 char inp_next(void);
    479 
    480 lexer_symbol lexi(void);
    481 void diag(int, const char *, ...) __printflike(2, 3);
    482 void output_line(void);
    483 void inp_read_line(void);
    484 void parse(parser_symbol);
    485 void process_comment(void);
    486 void set_option(const char *, const char *);
    487 void load_profile_files(const char *);
    488 
    489 void *nonnull(void *);
    490 
    491 void buf_add_char(struct buffer *, char);
    492 void buf_add_chars(struct buffer *, const char *, size_t);
    493 
    494 static inline bool
    495 ch_isalnum(char ch)
    496 {
    497 	return isalnum((unsigned char)ch) != 0;
    498 }
    499 
    500 static inline bool
    501 ch_isalpha(char ch)
    502 {
    503 	return isalpha((unsigned char)ch) != 0;
    504 }
    505 
    506 static inline bool
    507 ch_isblank(char ch)
    508 {
    509 	return ch == ' ' || ch == '\t';
    510 }
    511 
    512 static inline bool
    513 ch_isdigit(char ch)
    514 {
    515 	return '0' <= ch && ch <= '9';
    516 }
    517 
    518 static inline bool
    519 ch_isspace(char ch)
    520 {
    521 	return isspace((unsigned char)ch) != 0;
    522 }
    523 
    524 static inline int
    525 next_tab(int ind)
    526 {
    527 	return ind - ind % opt.tabsize + opt.tabsize;
    528 }
    529