indent.h revision 1.97 1 /* $NetBSD: indent.h,v 1.97 2021/11/19 19:55:15 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 #if 0
68 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.h 336333 2018-07-16 05:46:50Z pstef $");
69 #endif
70
71 #include <stdbool.h>
72 #include <stdio.h>
73
74 typedef enum lexer_symbol {
75 lsym_eof,
76 lsym_preprocessing, /* '#' */
77 lsym_newline,
78 lsym_form_feed,
79 lsym_comment, /* the initial '/ *' or '//' of a comment */
80 lsym_lparen_or_lbracket,
81 lsym_rparen_or_rbracket,
82 lsym_lbrace,
83 lsym_rbrace,
84 lsym_period,
85 lsym_unary_op, /* e.g. '*', '&', '-' or leading '++' */
86 lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */
87 lsym_postfix_op, /* trailing '++' or '--' */
88 lsym_question, /* the '?' from a '?:' expression */
89 lsym_colon,
90 lsym_comma,
91 lsym_semicolon,
92 lsym_typedef,
93 lsym_storage_class,
94 lsym_type_outside_parentheses,
95 lsym_type_in_parentheses,
96 lsym_tag, /* 'struct', 'union' or 'enum' */
97 lsym_case_label, /* 'case' or 'default' */
98 lsym_string_prefix, /* 'L' */
99 lsym_sizeof,
100 lsym_offsetof,
101 lsym_word, /* identifier, constant or string */
102 lsym_funcname,
103 lsym_do,
104 lsym_else,
105 lsym_for,
106 lsym_if,
107 lsym_switch,
108 lsym_while,
109 lsym_return
110 } lexer_symbol;
111
112 typedef enum parser_symbol {
113 psym_semicolon, /* rather a placeholder than a semicolon */
114 psym_lbrace,
115 psym_rbrace,
116 psym_decl,
117 psym_stmt,
118 psym_stmt_list,
119 psym_for_exprs, /* 'for' '(' ... ')' */
120 psym_if_expr, /* 'if' '(' expr ')' */
121 psym_if_expr_stmt, /* 'if' '(' expr ')' stmt */
122 psym_if_expr_stmt_else, /* 'if' '(' expr ')' stmt 'else' */
123 psym_else, /* 'else' */
124 psym_switch_expr, /* 'switch' '(' expr ')' */
125 psym_do, /* 'do' */
126 psym_do_stmt, /* 'do' stmt */
127 psym_while_expr, /* 'while' '(' expr ')' */
128 } parser_symbol;
129
130 typedef enum stmt_head {
131 hd_0, /* placeholder for uninitialized */
132 hd_for,
133 hd_if,
134 hd_switch,
135 hd_while,
136 } stmt_head;
137
138 /* A range of characters, in some cases null-terminated. */
139 struct buffer {
140 char *s; /* start of the usable text */
141 char *e; /* end of the usable text */
142 char *buf; /* start of the allocated memory */
143 char *l; /* end of the allocated memory */
144 };
145
146 extern FILE *input;
147 extern FILE *output;
148
149 extern struct buffer token; /* the current token to be processed, is
150 * typically copied to the buffer 'code',
151 * or in some cases to 'lab'. */
152
153 extern struct buffer lab; /* the label or preprocessor directive */
154 extern struct buffer code; /* the main part of the current line of code */
155 extern struct buffer com; /* the trailing comment of the line, or the
156 * start or end of a multi-line comment, or
157 * while in process_comment, a single line of
158 * a multi-line comment */
159
160 extern struct options {
161 bool blanklines_around_conditional_compilation;
162 bool blanklines_after_decl_at_top; /* this is vaguely similar to
163 * blanklines_after_decl except that
164 * it only applies to the first set of
165 * declarations in a procedure (just
166 * after the first '{') and it causes
167 * a blank line to be generated even
168 * if there are no declarations */
169 bool blanklines_after_decl;
170 bool blanklines_after_procs;
171 bool blanklines_before_block_comments;
172 bool break_after_comma; /* whether to break declarations after commas */
173 bool brace_same_line; /* whether brace should be on same line as if,
174 * while, etc */
175 bool blank_after_sizeof; /* whether a blank should always be inserted
176 * after sizeof */
177 bool comment_delimiter_on_blankline;
178 int decl_comment_column; /* the column in which comments after
179 * declarations should be put */
180 bool cuddle_else; /* whether 'else' should cuddle up to '}' */
181 int continuation_indent; /* the indentation between the edge of code
182 * and continuation lines */
183 float case_indent; /* The distance (measured in indentation
184 * levels) to indent case labels from the
185 * switch statement */
186 int comment_column; /* the column in which comments to the right
187 * of code should start */
188 int decl_indent; /* indentation of identifier in declaration */
189 bool ljust_decl; /* true if declarations should be left
190 * justified */
191 int unindent_displace; /* comments not to the right of code will be
192 * placed this many indentation levels to the
193 * left of code */
194 bool extra_expr_indent; /* whether continuation lines from the
195 * expression part of "if(e)", "while(e)",
196 * "for(e;e;e)" should be indented an extra
197 * tab stop so that they don't conflict with
198 * the code that follows */
199 bool else_if; /* whether else-if pairs should be handled
200 * specially */
201 bool function_brace_split; /* split function declaration and brace onto
202 * separate lines */
203 bool format_col1_comments; /* If comments which start in column 1 are to
204 * be magically reformatted (just like
205 * comments that begin in later columns) */
206 bool format_block_comments; /* whether comments beginning with '/ * \n'
207 * are to be reformatted */
208 bool indent_parameters;
209 int indent_size; /* the size of one indentation level */
210 int block_comment_max_line_length;
211 int local_decl_indent; /* like decl_indent but for locals */
212 bool lineup_to_parens_always; /* whether to not(?) attempt to keep
213 * lined-up code within the margin */
214 bool lineup_to_parens; /* whether continued code within parens will
215 * be lined up to the open paren */
216 bool proc_calls_space; /* whether function calls look like: foo (bar)
217 * rather than foo(bar) */
218 bool procnames_start_line; /* whether the names of procedures being
219 * defined get placed in column 1 (i.e. a
220 * newline is placed between the type of the
221 * procedure and its name) */
222 bool space_after_cast; /* "b = (int) a" vs "b = (int)a" */
223 bool star_comment_cont; /* whether comment continuation lines should
224 * have stars at the beginning of each line. */
225 bool swallow_optional_blanklines;
226 bool auto_typedefs; /* whether to recognize identifiers ending in
227 * "_t" like typedefs */
228 int tabsize; /* the size of a tab */
229 int max_line_length;
230 bool use_tabs; /* set true to use tabs for spacing, false
231 * uses all spaces */
232 bool verbose; /* whether non-essential error messages are
233 * printed */
234 } opt;
235
236 extern bool found_err;
237 extern int blank_lines_to_output;
238 extern bool blank_line_before;
239 extern bool blank_line_after;
240 extern bool break_comma; /* when true and not in parentheses, break
241 * after a comma */
242 extern float case_ind; /* indentation level to be used for a "case
243 * n:" */
244 extern bool had_eof; /* whether input is exhausted */
245 extern int line_no; /* the current line number. */
246 extern bool inhibit_formatting; /* true if INDENT OFF is in effect */
247
248 #define STACKSIZE 256
249
250 extern struct parser_state {
251 lexer_symbol prev_token; /* the previous token, but never comment,
252 * newline or preprocessing line */
253 bool curr_col_1; /* whether the current token started in column
254 * 1 of the unformatted input */
255 bool next_col_1;
256 bool next_unary; /* whether the following operator should be
257 * unary */
258
259 bool is_function_definition;
260
261 bool want_blank; /* whether the following token should be
262 * prefixed by a blank. (Said prefixing is
263 * ignored in some cases.) */
264
265 int paren_level; /* parenthesization level. used to indent
266 * within statements, initializers and
267 * declarations */
268 /* TODO: rename to next_line_paren_level */
269 int p_l_follow; /* how to indent the remaining lines of the
270 * statement or initializer or declaration */
271 short paren_indents[20]; /* indentation of the operand/argument of each
272 * level of parentheses or brackets, relative
273 * to the enclosing statement; if negative,
274 * reflected at -1 */
275 int cast_mask; /* indicates which close parentheses
276 * potentially close off casts */
277 int not_cast_mask; /* indicates which close parentheses
278 * definitely close off something else than
279 * casts */
280
281 int comment_delta; /* used to set up indentation for all lines of
282 * a boxed comment after the first one */
283 int n_comment_delta; /* remembers how many columns there were
284 * before the start of a box comment so that
285 * forthcoming lines of the comment are
286 * indented properly */
287 int com_ind; /* indentation of the current comment */
288
289 bool block_init; /* whether inside a block initialization */
290 int block_init_level; /* The level of brace nesting in an
291 * initialization */
292 bool init_or_struct; /* whether there has been a declarator (e.g.
293 * int or char) and no left parenthesis since
294 * the last semicolon. When true, a '{' is
295 * starting a structure definition or an
296 * initialization list */
297
298 int ind_level; /* the indentation level for the line that is
299 * currently prepared for output */
300 int ind_level_follow; /* the level to which ind_level should be set
301 * after the current line is printed */
302
303 int decl_level; /* current nesting level for a structure
304 * declaration or an initializer */
305 bool decl_on_line; /* whether this line of code has part of a
306 * declaration on it */
307 bool in_decl; /* whether we are in a declaration stmt. The
308 * processing of braces is then slightly
309 * different */
310 int just_saw_decl;
311 bool in_parameter_declaration;
312 bool decl_indent_done; /* whether the indentation for a declaration
313 * has been added to the code buffer. */
314
315 bool in_stmt; /* TODO: rename to something appropriate; this
316 * is set to true in struct declarations as
317 * well, so 'stmt' isn't accurate */
318 bool ind_stmt; /* whether the next line should have an extra
319 * indentation level because we are in the
320 * middle of a statement */
321 bool is_case_label; /* 'case' and 'default' labels are indented
322 * differently from regular labels */
323
324 bool search_stmt; /* whether it is necessary to buffer up all
325 * text up to the start of a statement after
326 * an 'if (expr)', 'while (expr)', etc., to
327 * move the comments after the opening brace
328 * of the following statement */
329
330 int tos; /* pointer to top of stack */
331 parser_symbol s_sym[STACKSIZE];
332 int s_ind_level[STACKSIZE];
333 float s_case_ind_level[STACKSIZE];
334
335 struct {
336 int comments;
337 int lines;
338 int code_lines;
339 int comment_lines;
340 } stats;
341 } ps;
342
343
344 #define array_length(array) (sizeof(array) / sizeof((array)[0]))
345
346 #ifdef debug
347 void
348 debug_vis_range(const char *, const char *, const char *,
349 const char *);
350 void debug_printf(const char *, ...)__printflike(1, 2);
351 void debug_println(const char *, ...)__printflike(1, 2);
352 #else
353 #define debug_printf(fmt, ...) do { } while (false)
354 #define debug_println(fmt, ...) do { } while (false)
355 #define debug_vis_range(prefix, s, e, suffix) do { } while (false)
356 #endif
357
358 void register_typename(const char *);
359 int compute_code_indent(void);
360 int compute_label_indent(void);
361 int ind_add(int, const char *, const char *);
362
363 void inp_init(void);
364
365 const char *inp_p(void);
366 const char *inp_line_start(void);
367 const char *inp_line_end(void);
368 char inp_peek(void);
369 char inp_lookahead(size_t);
370 void inp_skip(void);
371 char inp_next(void);
372
373 void inp_comment_init_newline(void);
374 void inp_comment_init_comment(void);
375 void inp_comment_init_preproc(void);
376 void inp_comment_add_char(char);
377 void inp_comment_add_range(const char *, const char *);
378 bool inp_comment_complete_block(void);
379 bool inp_comment_seen(void);
380 void inp_comment_rtrim(void);
381 void inp_comment_rtrim_newline(void);
382 void inp_comment_insert_lbrace(void);
383
384 void inp_from_comment(void);
385
386 #ifdef debug
387 void debug_inp(const char *);
388 #else
389 #define debug_inp(prefix) do { } while (false)
390 #endif
391
392 lexer_symbol lexi(void);
393 void diag(int, const char *, ...)__printflike(2, 3);
394 void dump_line(void);
395 void dump_line_ff(void);
396 void inp_read_line(void);
397 void parse(parser_symbol);
398 void parse_stmt_head(stmt_head);
399 void process_comment(void);
400 void set_option(const char *, const char *);
401 void load_profiles(const char *);
402
403 void *xmalloc(size_t);
404 void *xrealloc(void *, size_t);
405 char *xstrdup(const char *);
406
407 void buf_expand(struct buffer *, size_t);
408
409 static inline bool
410 ch_isblank(char ch)
411 {
412 return ch == ' ' || ch == '\t';
413 }
414
415 static inline int
416 next_tab(int ind)
417 {
418 return ind - ind % opt.tabsize + opt.tabsize;
419 }
420