indent.h revision 1.47 1 /* $NetBSD: indent.h,v 1.47 2021/10/24 22:38:20 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
73 typedef enum token_type {
74 end_of_file,
75 newline,
76 lparen_or_lbracket,
77 rparen_or_rbracket,
78 unary_op, /* e.g. '+' or '&' */
79 binary_op, /* e.g. '<<' or '+' or '&&' or '/=' */
80 postfix_op, /* trailing '++' or '--' */
81 question, /* the '?' from a '?:' expression */
82 case_label,
83 colon,
84 semicolon,
85 lbrace,
86 rbrace,
87 ident, /* identifier, constant or string */
88 comma,
89 comment,
90 switch_expr, /* 'switch' '(' <expr> ')' */
91 preprocessing, /* '#' */
92 form_feed,
93 decl,
94 tt_lex_for,
95 tt_lex_if,
96 tt_lex_while,
97 tt_lex_do,
98 tt_lex_else,
99 if_expr, /* 'if' '(' <expr> ')' */
100 while_expr, /* 'while' '(' <expr> ')' */
101 for_exprs, /* 'for' '(' ... ')' */
102 stmt,
103 stmt_list,
104 tt_ps_else,
105 tt_ps_do,
106 do_stmt, /* 'do' <stmt> */
107 if_expr_stmt, /* 'if' '(' <expr> ')' <stmt> */
108 if_expr_stmt_else, /* 'if' '(' <expr> ')' <stmt> 'else' */
109 period,
110 string_prefix, /* 'L' */
111 storage_class,
112 funcname,
113 type_def,
114 keyword_struct_union_enum
115 } token_type;
116
117 #define sc_size 5000 /* size of save_com buffer */
118 #define label_offset 2 /* number of levels a label is placed to left
119 * of code */
120
121
122 struct buffer {
123 char *buf; /* buffer */
124 char *s; /* start */
125 char *e; /* end */
126 char *l; /* limit */
127 };
128
129 extern FILE *input;
130 extern FILE *output;
131
132 extern struct buffer lab; /* label or preprocessor directive */
133 extern struct buffer code; /* code */
134 extern struct buffer com; /* comment */
135 extern struct buffer token; /* the last token scanned */
136
137 extern struct buffer inp;
138
139 extern char sc_buf[sc_size]; /* input text is saved here when looking for
140 * the brace after an if, while, etc */
141 extern char *save_com; /* start of the comment stored in sc_buf */
142
143 extern char *saved_inp_s; /* saved value of inp.s when taking input from
144 * save_com */
145 extern char *saved_inp_e; /* similarly saved value of inp.e */
146
147
148 extern struct options {
149 bool blanklines_around_conditional_compilation;
150 bool blanklines_after_decl_at_top; /* this is vaguely similar to
151 * blanklines_after_decl except that
152 * it only applies to the first set of
153 * declarations in a procedure (just
154 * after the first '{') and it causes
155 * a blank line to be generated even
156 * if there are no declarations */
157 bool blanklines_after_decl;
158 bool blanklines_after_procs;
159 bool blanklines_before_block_comments;
160 bool break_after_comma; /* whether to break declarations after commas */
161 bool brace_same_line; /* whether brace should be on same line as if,
162 * while, etc */
163 bool blank_after_sizeof; /* whether a blank should always be inserted
164 * after sizeof */
165 bool comment_delimiter_on_blankline;
166 int decl_comment_column; /* the column in which comments after
167 * declarations should be put */
168 bool cuddle_else; /* whether 'else' should cuddle up to '}' */
169 int continuation_indent; /* the indentation between the edge of code
170 * and continuation lines */
171 float case_indent; /* The distance (measured in indentation
172 * levels) to indent case labels from the
173 * switch statement */
174 int comment_column; /* the column in which comments to the right
175 * of code should start */
176 int decl_indent; /* indentation of identifier in declaration */
177 bool ljust_decl; /* true if declarations should be left
178 * justified */
179 int unindent_displace; /* comments not to the right of code will be
180 * placed this many indentation levels to the
181 * left of code */
182 bool extra_expr_indent; /* whether continuation lines from the
183 * expression part of "if(e)", "while(e)",
184 * "for(e;e;e)" should be indented an extra
185 * tab stop so that they don't conflict with
186 * the code that follows */
187 bool else_if; /* whether else-if pairs should be handled
188 * specially */
189 bool function_brace_split; /* split function declaration and brace onto
190 * separate lines */
191 bool format_col1_comments; /* If comments which start in column 1 are to
192 * be magically reformatted (just like
193 * comments that begin in later columns) */
194 bool format_block_comments; /* whether comments beginning with '/ * \n'
195 * are to be reformatted */
196 bool indent_parameters;
197 int indent_size; /* the size of one indentation level */
198 int block_comment_max_line_length;
199 int local_decl_indent; /* like decl_indent but for locals */
200 bool lineup_to_parens_always; /* whether to not(?) attempt to keep
201 * lined-up code within the margin */
202 bool lineup_to_parens; /* whether continued code within parens will
203 * be lined up to the open paren */
204 bool proc_calls_space; /* whether function calls look like: foo (bar)
205 * rather than foo(bar) */
206 bool procnames_start_line; /* whether the names of procedures being
207 * defined get placed in column 1 (i.e. a
208 * newline is placed between the type of the
209 * procedure and its name) */
210 bool space_after_cast; /* "b = (int) a" vs "b = (int)a" */
211 bool star_comment_cont; /* whether comment continuation lines should
212 * have stars at the beginning of each line. */
213 bool swallow_optional_blanklines;
214 bool auto_typedefs; /* whether to recognize identifiers ending in
215 * "_t" like typedefs */
216 int tabsize; /* the size of a tab */
217 int max_line_length;
218 bool use_tabs; /* set true to use tabs for spacing, false
219 * uses all spaces */
220 bool verbose; /* whether non-essential error messages are
221 * printed */
222 } opt;
223
224 enum keyword_kind {
225 kw_0,
226 kw_offsetof,
227 kw_sizeof,
228 kw_struct_or_union_or_enum,
229 kw_type,
230 kw_for,
231 kw_if,
232 kw_while,
233 kw_do,
234 kw_else,
235 kw_switch,
236 kw_case_or_default,
237 kw_jump,
238 kw_storage_class,
239 kw_typedef,
240 kw_inline_or_restrict
241 };
242
243
244 extern bool found_err;
245 extern int blank_lines_to_output;
246 extern bool blank_line_before;
247 extern bool blank_line_after;
248 extern bool break_comma; /* when true and not in parens, break after a
249 * comma */
250 extern float case_ind; /* indentation level to be used for a "case
251 * n:" */
252 extern bool had_eof; /* whether input is exhausted */
253 extern int line_no; /* the current line number. */
254 extern bool inhibit_formatting; /* true if INDENT OFF is in effect */
255
256 #define STACKSIZE 256
257
258 /* TODO: group the members by purpose, don't sort them alphabetically */
259 extern struct parser_state {
260 token_type last_token;
261
262 int tos; /* pointer to top of stack */
263 token_type s_ttype[STACKSIZE];
264 int s_ind_level[STACKSIZE];
265 float s_case_ind_level[STACKSIZE];
266
267 int comment_delta; /* used to set up indentation for all lines of
268 * a boxed comment after the first one */
269 int n_comment_delta; /* remembers how many columns there were
270 * before the start of a box comment so that
271 * forthcoming lines of the comment are
272 * indented properly */
273 int cast_mask; /* indicates which close parens potentially
274 * close off casts */
275 int not_cast_mask; /* indicates which close parens definitely
276 * close off something else than casts */
277 bool block_init; /* whether inside a block initialization */
278 int block_init_level; /* The level of brace nesting in an
279 * initialization */
280 bool last_nl; /* whether the last thing scanned was a
281 * newline */
282 bool init_or_struct; /* whether there has been a declarator (e.g.
283 * int or char) and no left parenthesis since
284 * the last semicolon. When true, a '{' is
285 * starting a structure definition or an
286 * initialization list */
287 bool col_1; /* whether the last token started in column 1 */
288 int com_ind; /* indentation of the current comment */
289 int decl_nest; /* current nesting level for structure or init */
290 bool decl_on_line; /* whether this line of code has part of a
291 * declaration on it */
292 int ind_level_follow; /* the level to which ind_level should be set
293 * after the current line is printed */
294 bool in_decl; /* whether we are in a declaration stmt. The
295 * processing of braces is then slightly
296 * different */
297 bool in_stmt;
298 int ind_level; /* the current indentation level */
299 bool ind_stmt; /* whether the next line should have an extra
300 * indentation level because we are in the
301 * middle of a stmt */
302 bool next_unary; /* whether the following operator should be
303 * unary */
304 int p_l_follow; /* used to remember how to indent the
305 * following statement */
306 int paren_level; /* parenthesization level. used to indent
307 * within statements */
308 short paren_indents[20]; /* indentation of the operand/argument of each
309 * level of parentheses or brackets, relative
310 * to the enclosing statement */
311 bool is_case_label; /* 'case' and 'default' labels are indented
312 * differently from regular labels */
313 bool search_brace; /* whether it is necessary to buffer up all
314 * info up to the start of a stmt after an if,
315 * while, etc */
316 bool want_blank; /* whether the following token should be
317 * prefixed by a blank. (Said prefixing is
318 * ignored in some cases.) */
319 enum keyword_kind keyword;
320 bool dumped_decl_indent;
321 bool in_parameter_declaration;
322 char procname[100]; /* The name of the current procedure */
323 int just_saw_decl;
324
325 struct {
326 int comments;
327 int lines;
328 int code_lines;
329 int comment_lines;
330 } stats;
331 } ps;
332
333
334 #define array_length(array) (sizeof (array) / sizeof (array[0]))
335
336 void add_typename(const char *);
337 int compute_code_indent(void);
338 int compute_label_indent(void);
339 int indentation_after_range(int, const char *, const char *);
340 int indentation_after(int, const char *);
341 #ifdef debug
342 void
343 debug_vis_range(const char *, const char *, const char *,
344 const char *);
345 void debug_printf(const char *, ...)__printflike(1, 2);
346 void debug_println(const char *, ...)__printflike(1, 2);
347 const char *token_type_name(token_type);
348 #else
349 #define debug_printf(fmt, ...) do { } while (false)
350 #define debug_println(fmt, ...) do { } while (false)
351 #define debug_vis_range(prefix, s, e, suffix) do { } while (false)
352 #endif
353 void inbuf_skip(void);
354 char inbuf_next(void);
355 token_type lexi(struct parser_state *);
356 void diag(int, const char *, ...)__printflike(2, 3);
357 void dump_line(void);
358 void dump_line_ff(void);
359 void inbuf_read_line(void);
360 void parse(token_type);
361 void process_comment(void);
362 void set_option(const char *, const char *);
363 void load_profiles(const char *);
364
365 void *xmalloc(size_t);
366 void *xrealloc(void *, size_t);
367 char *xstrdup(const char *);
368
369 void buf_expand(struct buffer *, size_t);
370
371 static inline bool
372 is_hspace(char ch)
373 {
374 return ch == ' ' || ch == '\t';
375 }
376
377 static inline int
378 next_tab(int ind)
379 {
380 return ind - ind % opt.tabsize + opt.tabsize;
381 }
382