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