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