Thu Nov 4 17:37:03 2021 UTC ()
indent: split process_comments into separate functions

No functional change.


(rillig)
diff -r1.94 -r1.95 src/usr.bin/indent/pr_comment.c

cvs diff -r1.94 -r1.95 src/usr.bin/indent/pr_comment.c (expand / switch to unified diff)

--- src/usr.bin/indent/pr_comment.c 2021/11/03 21:47:35 1.94
+++ src/usr.bin/indent/pr_comment.c 2021/11/04 17:37:03 1.95
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 rillig Exp $ */ 1/* $NetBSD: pr_comment.c,v 1.95 2021/11/04 17:37:03 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -33,27 +33,27 @@ @@ -33,27 +33,27 @@
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#if 0 40#if 0
41static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93"; 41static char sccsid[] = "@(#)pr_comment.c 8.1 (Berkeley) 6/6/93";
42#endif 42#endif
43 43
44#include <sys/cdefs.h> 44#include <sys/cdefs.h>
45#if defined(__NetBSD__) 45#if defined(__NetBSD__)
46__RCSID("$NetBSD: pr_comment.c,v 1.94 2021/11/03 21:47:35 rillig Exp $"); 46__RCSID("$NetBSD: pr_comment.c,v 1.95 2021/11/04 17:37:03 rillig Exp $");
47#elif defined(__FreeBSD__) 47#elif defined(__FreeBSD__)
48__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); 48__FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
49#endif 49#endif
50 50
51#include <assert.h> 51#include <assert.h>
52#include <ctype.h> 52#include <ctype.h>
53#include <stdio.h> 53#include <stdio.h>
54#include <string.h> 54#include <string.h>
55 55
56#include "indent.h" 56#include "indent.h"
57 57
58static void 58static void
59com_add_char(char ch) 59com_add_char(char ch)
@@ -89,61 +89,37 @@ fits_in_one_line(int max_line_length) @@ -89,61 +89,37 @@ fits_in_one_line(int max_line_length)
89 for (const char *p = inp.s; *p != '\n'; p++) { 89 for (const char *p = inp.s; *p != '\n'; p++) {
90 assert(*p != '\0'); 90 assert(*p != '\0');
91 assert(inp.e - p >= 2); 91 assert(inp.e - p >= 2);
92 if (!(p[0] == '*' && p[1] == '/')) 92 if (!(p[0] == '*' && p[1] == '/'))
93 continue; 93 continue;
94 94
95 int len = ind_add(ps.com_ind + 3, inp.s, p); 95 int len = ind_add(ps.com_ind + 3, inp.s, p);
96 len += ch_isblank(p[-1]) ? 2 : 3; 96 len += ch_isblank(p[-1]) ? 2 : 3;
97 return len <= max_line_length; 97 return len <= max_line_length;
98 } 98 }
99 return false; 99 return false;
100} 100}
101 101
102/* 102static void
103 * Scan, reformat and output a single comment, which is either a block comment 103analyze_comment(int *p_adj_max_line_length, bool *p_break_delim,
104 * starting with '/' '*' or an end-of-line comment starting with '//'. 104 bool *p_may_wrap)
105 * 
106 * Try to keep comments from going over the maximum line length. If a line is 
107 * too long, move everything starting from the last blank to the next comment 
108 * line. Blanks and tabs from the beginning of the input line are removed. 
109 * 
110 * ALGORITHM: 
111 * 1) Decide where the comment should be aligned, and if lines should 
112 * be broken. 
113 * 2) If lines should not be broken and filled, just copy up to end of 
114 * comment. 
115 * 3) If lines should be filled, then scan through the input buffer, 
116 * copying characters to com_buf. Remember where the last blank, 
117 * tab, or newline was. When line is filled, print up to last blank 
118 * and continue copying. 
119 */ 
120void 
121process_comment(void) 
122{ 105{
123 int adj_max_line_length; /* Adjusted max_line_length for comments that 106 int adj_max_line_length; /* Adjusted max_line_length for comments that
124 * spill over the right margin */ 107 * spill over the right margin */
125 ssize_t last_blank; /* index of the last blank in com.buf */ 
126 bool break_delim = opt.comment_delimiter_on_blankline; 108 bool break_delim = opt.comment_delimiter_on_blankline;
127 int l_just_saw_decl = ps.just_saw_decl; 
128 int com_ind; 109 int com_ind;
129 110
130 adj_max_line_length = opt.max_line_length; 111 adj_max_line_length = opt.max_line_length;
131 ps.just_saw_decl = 0; 
132 last_blank = -1; /* no blanks found so far */ 
133 bool may_wrap = true; 112 bool may_wrap = true;
134 ps.stats.comments++; 
135 
136 /* Figure where to align and how to treat the comment */ 
137 113
138 if (ps.curr_col_1 && !opt.format_col1_comments) { 114 if (ps.curr_col_1 && !opt.format_col1_comments) {
139 may_wrap = false; 115 may_wrap = false;
140 break_delim = false; 116 break_delim = false;
141 com_ind = 0; 117 com_ind = 0;
142 118
143 } else { 119 } else {
144 if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' || 120 if (*inp.s == '-' || *inp.s == '*' || token.e[-1] == '/' ||
145 (*inp.s == '\n' && !opt.format_block_comments)) { 121 (*inp.s == '\n' && !opt.format_block_comments)) {
146 may_wrap = false; 122 may_wrap = false;
147 break_delim = false; 123 break_delim = false;
148 } 124 }
149 125
@@ -210,27 +186,35 @@ process_comment(void) @@ -210,27 +186,35 @@ process_comment(void)
210 186
211 if (break_delim) { 187 if (break_delim) {
212 char *t = com.e; 188 char *t = com.e;
213 com.e = com.s + 2; 189 com.e = com.s + 2;
214 *com.e = '\0'; 190 *com.e = '\0';
215 if (opt.blanklines_before_block_comments && 191 if (opt.blanklines_before_block_comments &&
216 ps.prev_token != lsym_lbrace) 192 ps.prev_token != lsym_lbrace)
217 blank_line_before = true; 193 blank_line_before = true;
218 dump_line(); 194 dump_line();
219 com.e = com.s = t; 195 com.e = com.s = t;
220 com_add_delim(); 196 com_add_delim();
221 } 197 }
222 198
223 /* Now copy the comment. */ 199 *p_adj_max_line_length = adj_max_line_length;
 200 *p_break_delim = break_delim;
 201 *p_may_wrap = may_wrap;
 202}
 203
 204static void
 205copy_comment(int adj_max_line_length, bool break_delim, bool may_wrap)
 206{
 207 ssize_t last_blank = -1; /* index of the last blank in com.buf */
224 208
225 for (;;) { 209 for (;;) {
226 switch (*inp.s) { 210 switch (*inp.s) {
227 case '\f': 211 case '\f':
228 if (may_wrap) { /* in a text comment, break the line here */ 212 if (may_wrap) { /* in a text comment, break the line here */
229 dump_line_ff(); 213 dump_line_ff();
230 last_blank = -1; 214 last_blank = -1;
231 com_add_delim(); 215 com_add_delim();
232 inp.s++; 216 inp.s++;
233 while (ch_isblank(*inp.s)) 217 while (ch_isblank(*inp.s))
234 inp.s++; 218 inp.s++;
235 } else { 219 } else {
236 inbuf_skip(); 220 inbuf_skip();
@@ -296,28 +280,26 @@ process_comment(void) @@ -296,28 +280,26 @@ process_comment(void)
296 dump_line(); 280 dump_line();
297 else 281 else
298 com.s = com.e; /* XXX: why not e = s? */ 282 com.s = com.e; /* XXX: why not e = s? */
299 com_add_char(' '); 283 com_add_char(' ');
300 } 284 }
301 285
302 if (!ch_isblank(com.e[-1]) && may_wrap) 286 if (!ch_isblank(com.e[-1]) && may_wrap)
303 com_add_char(' '); 287 com_add_char(' ');
304 if (token.e[-1] != '/') { 288 if (token.e[-1] != '/') {
305 com_add_char('*'); 289 com_add_char('*');
306 com_add_char('/'); 290 com_add_char('/');
307 } 291 }
308 com_terminate(); 292 com_terminate();
309 
310 ps.just_saw_decl = l_just_saw_decl; 
311 return; 293 return;
312 294
313 } else /* handle isolated '*' */ 295 } else /* handle isolated '*' */
314 com_add_char('*'); 296 com_add_char('*');
315 break; 297 break;
316 298
317 default: /* we have a random char */ 299 default: /* we have a random char */
318 ; 300 ;
319 int now_len = ind_add(ps.com_ind, com.s, com.e); 301 int now_len = ind_add(ps.com_ind, com.s, com.e);
320 for (;;) { 302 for (;;) {
321 char ch = inbuf_next(); 303 char ch = inbuf_next();
322 if (ch_isblank(ch)) 304 if (ch_isblank(ch))
323 last_blank = com.e - com.buf; 305 last_blank = com.e - com.buf;
@@ -344,13 +326,49 @@ process_comment(void) @@ -344,13 +326,49 @@ process_comment(void)
344 326
345 const char *last_word_s = com.buf + last_blank + 1; 327 const char *last_word_s = com.buf + last_blank + 1;
346 size_t last_word_len = (size_t)(com.e - last_word_s); 328 size_t last_word_len = (size_t)(com.e - last_word_s);
347 com.e = com.buf + last_blank; 329 com.e = com.buf + last_blank;
348 dump_line(); 330 dump_line();
349 com_add_delim(); 331 com_add_delim();
350 332
351 memcpy(com.e, last_word_s, last_word_len); 333 memcpy(com.e, last_word_s, last_word_len);
352 com.e += last_word_len; 334 com.e += last_word_len;
353 last_blank = -1; 335 last_blank = -1;
354 } 336 }
355 } 337 }
356} 338}
 339
 340/*
 341 * Scan, reformat and output a single comment, which is either a block comment
 342 * starting with '/' '*' or an end-of-line comment starting with '//'.
 343 *
 344 * Try to keep comments from going over the maximum line length. If a line is
 345 * too long, move everything starting from the last blank to the next comment
 346 * line. Blanks and tabs from the beginning of the input line are removed.
 347 *
 348 * ALGORITHM:
 349 * 1) Decide where the comment should be aligned, and if lines should
 350 * be broken.
 351 * 2) If lines should not be broken and filled, just copy up to end of
 352 * comment.
 353 * 3) If lines should be filled, then scan through the input buffer,
 354 * copying characters to com_buf. Remember where the last blank,
 355 * tab, or newline was. When line is filled, print up to last blank
 356 * and continue copying.
 357 */
 358void
 359process_comment(void)
 360{
 361 int adj_max_line_length; /* Adjusted max_line_length for comments that
 362 * spill over the right margin */
 363 bool break_delim = opt.comment_delimiter_on_blankline;
 364
 365 adj_max_line_length = opt.max_line_length;
 366 ps.just_saw_decl = 0;
 367 bool may_wrap = true;
 368 ps.stats.comments++;
 369
 370 int l_just_saw_decl = ps.just_saw_decl;
 371 analyze_comment(&adj_max_line_length, &break_delim, &may_wrap);
 372 copy_comment(adj_max_line_length, break_delim, may_wrap);
 373 ps.just_saw_decl = l_just_saw_decl;
 374}