File: comments.c

package info (click to toggle)
indent 2.2.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 872 kB
  • ctags: 718
  • sloc: ansic: 5,941; sh: 392; makefile: 96
file content (675 lines) | stat: -rw-r--r-- 18,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/* Copyright (c) 1993 - 1998, Joseph Arceneaux.  All rights reserved.

   This file is subject to the terms of the GNU General Public License as
   published by the Free Software Foundation.  A copy of this license is
   included with this software distribution in the file COPYING.  If you
   do not have a copy, you may obtain a copy by writing to the Free
   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details. */

#include "sys.h"
#include "indent.h"
#include "io.h"
#include "comments.h"
#include "globs.h"
#include "parse.h"

#include <string.h>

RCSTAG_CC ("$Id: comments.c,v 1.13 1999/10/26 00:20:56 carlo Exp $");

/* Check the limits of the comment buffer, and expand as neccessary. */

#define CHECK_COM_SIZE \
	if (e_com >= l_com) \
          { \
	    int nsize = l_com-s_com+400; \
	    combuf = (char *) xrealloc (combuf, nsize); \
	    e_com = combuf + (e_com-s_com) + 1; \
	    l_com = combuf + nsize - 5; \
	    s_com = combuf + 1; \
	  }

/* Output a comment.  `buf_ptr' is pointing to the character after
   the beginning comment delimiter when this is called.  This handles
   both C and C++ comments.

   As far as indent is concerned, there are basically two types
   of comments -- those on lines by themselves and those which are
   on lines with other code.  Variables (and the options specifying them)
   affecting the printing of comments are:

   `format_comments'                ("fca"):  Ignore newlines in the
       comment and perform filling up to `comment_max_col'.  Double
       newlines indicate paragraph breaks.

   `format_col1_comments'           ("fc1"):  Format comments which
       begin in column 1.

   `unindent_displace'              ("d"):  The hanging indentation for
       comments which do not appear to the right of code.

   `comment_delimiter_on_blankline' ("cdb"):  If set, place the comment
       delimiters on lines by themselves.  This only affects comments
       which are not to the right of code.

   `com_ind'                        ("c"):  The column in which to begin
       comments that are to the right of code.

   `decl_com_ind'                   ("cd"):  The column in which to begin
       comments that are to the right of declarations.

   `else_endif_col'                 ("cp"):  The column in which to begin
       comments to the right of preprocessor directives.

   `star_comment_cont'              ("sc"):  Place a star ('*') to the
       left of the comment body.

   `comment_max_col'                ("lc"): The length of a comment line.
       Formatted comments which extend past this column will be continued on
       the following line.  If this option is not specified, `max_col' is
       used.
   */

void
print_comment ()
{
  register int column, format;
  enum codes comment_type;

  int start_column, found_column;
  int first_comment_line, right_margin;
  int boxed_comment, stars, blankline_delims, paragraph_break,
    merge_blank_comment_lines;

  int two_contiguous_comments = 0;
  int save_length = 0;
  char *save_ptr = 0;
  char *text_on_line = 0;
  char *line_break_ptr = 0;
  char *start_delim;

  char *line_preamble;
  int line_preamble_length, visible_preamble;

  /* GDB_HOOK_print_comment() */

  /* Increment the parser stack, as we will store some things
     there for dump_line to use. */
  inc_pstack ();

  /* Have to do it this way because this piece of shit program doesn't
     always place the last token code on the stack. */
  if (*(token + 1) == '/')
    comment_type = cplus_comment;
  else
    comment_type = comment;

  /* First, decide what kind of comment this is: C++, C, or boxed C.
     Even if this appears to be a normal C comment, we may change our
     minds if we find a star in the right column of the second line,
     in which case that's a boxed comment too. */
  if (comment_type == cplus_comment)
    {
      start_delim = "//";
      line_preamble = "// ";
      line_preamble_length = 3;
      visible_preamble = 1;
      boxed_comment = 0;
      stars = 0;
      blankline_delims = 0;
    }
  else if (*buf_ptr == '*' || *buf_ptr == '-'
	   || *buf_ptr == '=' || *buf_ptr == '_'
	   || (parser_state_tos->col_1 && !format_col1_comments))
    /* Boxed comment.  This block of code will return. */
    {
      int comment_lines_count = 1;

      found_column = start_column = current_column () - 2;
      parser_state_tos->box_com = 1;
      parser_state_tos->com_col = found_column;

      if (blanklines_before_blockcomments)
	prefix_blankline_requested = 1;

      *e_com++ = '/';
      *e_com++ = '*';
      while (1)
	{
	  do
	    {
	      if (*buf_ptr == EOL)	/* Count line numbers within comment blocks */
		++line_no;
	      *e_com++ = *buf_ptr++;
	    }
	  while (*buf_ptr != '*' && buf_ptr < buf_end);

	  CHECK_COM_SIZE;

	  /* We have reached the end of the comment, and it's all on
	     this line. */
	  if (*buf_ptr == '*' && *(buf_ptr + 1) == '/')
	    {
	      if (buf_ptr == buf_end)
		fill_buffer ();

	      buf_ptr += 2;
	      if (buf_ptr == buf_end)
		fill_buffer ();

	      *e_com++ = '*';
	      *e_com++ = '/';
	      *e_com = '\0';
	      parser_state_tos->tos--;

	      /* If this is the only line of a boxed comment, it may
	         be after some other text (e.g., #if foo <comment>),
	         in which case we want to specify the correct column.
	         In the other cases, the leading spaces account for
	         the columns and we start it in column 1. */

	      if (comment_lines_count > 1)
		parser_state_tos->com_col = 1;
	      else
		parser_state_tos->com_col = found_column;
	      return;
	    }

	  /* End of the line, or end of file. */
	  if (buf_ptr == buf_end)
	    {
	      if (*(buf_ptr - 1) == EOL)
		{
		  *(--e_com) = '\0';
		  dump_line (true);
		  comment_lines_count++;
		  parser_state_tos->com_col = 1;
		}

	      fill_buffer ();
	      if (had_eof)
		{
		  *e_com++ = '\0';
		  parser_state_tos->tos--;
		  parser_state_tos->com_col = start_column;
		  return;
		}
	    }
	}
    }
  else
    {
      start_delim = "/*";
      line_preamble = 0;
      line_preamble_length = 0;
      visible_preamble = 0;
      boxed_comment = 0;
      stars = star_comment_cont;
      blankline_delims = comment_delimiter_on_blankline;
    }

  paragraph_break = 0;
  merge_blank_comment_lines = 0;
  first_comment_line = com_lines;
  right_margin = comment_max_col;

  /* Now, compute the correct indentation for this comment
     and whether or not it should be formatted. */
  found_column = current_column () - 2;

  if ((s_lab == e_lab) && (s_code == e_code))
    /* First handle comments which begin the line. */
    {
      if (parser_state_tos->col_1 && !format_col1_comments)
	{
	  format = format_col1_comments;
	  start_column = 1;
	}
      else if (s_com != e_com)
	/* The fool has a line consisting of two contiguous comments.
	   In this case, we don't try too hard, 'cause nothing will
	   look good. */
	{
	  format = 0;
	  start_column = found_column;
	  two_contiguous_comments = 1;
	}
      else
	{
	  format = format_comments;

	  if (parser_state_tos->ind_level <= 0
	      && (!parser_state_tos->in_stmt
		  || (parser_state_tos->in_decl
		      && parser_state_tos->paren_level == 0)))
	    start_column = found_column;
	  else
	    start_column = compute_code_target ();
	}
    }
  else
    /* This comment follows code of some sort. */
    {
      int target;

      /* First, compute where the comment SHOULD go. */
      if (parser_state_tos->decl_on_line)
	target = decl_com_ind;
      else if (else_or_endif)
	target = else_endif_col;
      else
	target = com_ind;

      /* Now determine if the code on the line is short enough
         to allow the comment to begin where it should. */
      if (s_code != e_code)
	start_column =
	  count_columns (compute_code_target (), s_code, NULL_CHAR);
      else
	/* s_lab != e_lab : there is a label here. */
	start_column =
	  count_columns (compute_label_target (), s_lab, NULL_CHAR);

      if (start_column < target)
	start_column = target;
      else
	{
	  /* If the too-long code is a pre-processor command,
	     start the comment 1 space afterwards, otherwise
	     start at the next tab mark. */
	  if (else_or_endif)
	    {
	      start_column++;
	      else_or_endif = false;
	    }
	  else
	    start_column += tabsize - ((start_column - 1) % tabsize);
	}

      format = format_comments;
    }

  if (!line_preamble)
    {
      line_preamble_length = 3;
      if (stars)
	{
	  line_preamble = " * ";
	  visible_preamble = 1;
	}
      else
	{
	  line_preamble = "   ";
	  visible_preamble = 0;
	}
    }

  /* These are the parser stack variables used to communicate
     formatting information to dump_line (). */
  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
  parser_state_tos->box_com = boxed_comment;

  /* Output the beginning comment delimiter.  They are both two
     characters long. */
  *e_com++ = *start_delim;
  *e_com++ = *(start_delim + 1);
  column = start_column + 2;

  /* If the user specified -cdb, put the delimiter on one line. */
  if (blankline_delims)
    {
      char *p = buf_ptr;

      *e_com = '\0';
      dump_line (true);

      /* Check if the delimiter was already on a line by itself,
         and skip whitespace if formating. */
      while (*p == ' ' || *p == TAB)
	p++;
      if (*p == EOL)
	buf_ptr = p + 1;
      else if (format)
	buf_ptr = p;
      if (buf_ptr >= buf_end)
	fill_buffer ();

      column = start_column;
      goto begin_line;
    }
  else if (format)
    {
      *e_com++ = ' ';
      column = start_column + 3;
      while (*buf_ptr == ' ' || *buf_ptr == TAB)
	if (++buf_ptr >= buf_end)
	  fill_buffer ();
    }

  /* Iterate through the lines of the comment */
  while (!had_eof)
    {
      /* Iterate through the characters on one line */
      while (!had_eof)
	{
	  CHECK_COM_SIZE;

	  switch (*buf_ptr)
	    {
	    case ' ':
	    case TAB:
	      /* If formatting, and previous break marker is
	         nonexistant, or before text on line, reset
	         it to here. */
	      if (format && line_break_ptr < text_on_line)
		line_break_ptr = e_com;

	      if (*buf_ptr == ' ')
		{
		  *e_com++ = ' ';
		  column++;
		}
	      else
		{
		  /* Convert the tab to the appropriate number of spaces,
		     based on the column we found the comment in, not
		     the one we're printing in. */
		  int tab_width
		    = (tabsize - ((column + found_column - start_column - 1)
				  % tabsize));
		  column += tab_width;
		  while (tab_width--)
		    *e_com++ = ' ';
		}
	      break;


	    case EOL:
	      /* We may be at the end of a C++ comment */
	      if (comment_type == cplus_comment)
		{
		cplus_exit:
		  parser_state_tos->tos--;
		  parser_state_tos->com_col
		    = (two_contiguous_comments ? 1 : start_column);
		  parser_state_tos->box_com = boxed_comment;
		  return;
		}

	      if (format)
		{
		  /* Newline and null are the two characters which
		     end an input line, so check here if we need to
		     get the next line. */
		  if (*buf_ptr == EOL)
		    ++line_no;
		  buf_ptr++;
		  if (buf_ptr >= buf_end)
		    fill_buffer ();

		  /* If there are any spaces between the text and this
		     newline character, remove them. */
		  if (e_com > line_break_ptr && text_on_line < line_break_ptr)
		    e_com = line_break_ptr;

		  /* If this is "\n\n", or "\n<whitespace>\n",
		     it's a paragraph break. */
		  while (*buf_ptr == TAB || *buf_ptr == ' ')
		    if (buf_ptr++ >= buf_end)
		      fill_buffer ();
		  if (*buf_ptr == EOL || !text_on_line)
		    {
		      paragraph_break = 1;
		      goto end_line;
		    }

		  /* This is a single newline.  Transform it (and any
		     following whitespace) into a single blank. */
		  line_break_ptr = e_com;
		  *e_com++ = ' ';
		  column++;
		  continue;
		}

	      /* We are printing this line "as is", so output it
		 and continue on to the next line. */
	      goto end_line;

	    case '*':
	      /* Check if we've reached the end of the comment. */
	      if (comment_type == comment)
		{
		  if (*(buf_ptr + 1) == '/')
		    {
		      /* If it's not a boxed comment, put some whitespace
		         before the ending delimiter.  Otherwise, simply
		         insert the delimiter. */
		      if (!boxed_comment)
			{
			  if (text_on_line)
			    {
			      if (blankline_delims)
				{
				  *e_com = '\0';
				  dump_line (true);
				  *e_com++ = ' ';
				}
			      else
				/* Insert space before closing delim */
			      if (*(e_com - 1) != ' ' && *(e_com - 1) != TAB)
				*e_com++ = ' ';
			    }
			  /* If no text on line, then line is completely empty
			     or starts with preamble, or is beginning of
			     comment and starts with beginning delimiter. */
			  else if (s_com == e_com || *s_com != '/')
			    {
			      e_com = s_com;
			      *e_com++ = ' ';
			    }
			  else
			    /* This is case of first comment line.  Test
			       with:
			       if (first_comment_line != com_lines)
			       abort (); */
			  if (*(e_com - 1) != ' ' && *(e_com - 1) != TAB)
			    *e_com++ = ' ';
			}

		      /* Now insert the ending delimiter */
		      *e_com++ = '*';
		      *e_com++ = '/';
		      *e_com = '\0';

		      /* Skip any whitespace following the comment.  If
		         there is only whitespace after it, print the line.

		         NOTE:  We're not printing the line: TRY IT! */
		      buf_ptr += 2;
		      while (*buf_ptr == ' ' || *buf_ptr == TAB)
			buf_ptr++;
		      if (buf_ptr >= buf_end)
			fill_buffer ();

		      parser_state_tos->tos--;
		      parser_state_tos->com_col
			= (two_contiguous_comments ? 1 : start_column);
		      parser_state_tos->box_com = boxed_comment;
		      return;
		    }

		  /* If this star is on the second line of the
		     comment in the same column as the star of the
		     beginning delimiter, then consider it
		     a boxed comment. */
		  if (first_comment_line == com_lines - 1
		      && e_com == s_com + line_preamble_length
		      && current_column () == found_column + 1)
		    {
		      /* Account for change in line_preamble_length: */
		      column -= line_preamble_length - 1;
		      line_preamble = " ";
		      line_preamble_length = 1;
		      boxed_comment = 1;
		      format = 0;
		      blankline_delims = 0;
		      *s_com = ' ';
		      *(s_com + 1) = '*';
		      e_com = s_com + 2;
		      column++;
		      break;
		    }
		}
	      /* If it was not the end of the comment, drop through
	         and insert the star on the line. */

	    default:
	      /* Some textual character. */
	      text_on_line = e_com;
	      *e_com++ = *buf_ptr;
	      column++;
	      break;
	    }


	  /* If we are formatting, check that we haven't exceeded the
	     line length.  If we haven't set line_break_ptr, keep going. */
	  if (format && column > right_margin && line_break_ptr)
	    {
	      if (line_break_ptr < e_com - 1)
		/* Here if we are really "breaking" the line:  the line
		   break is before some text we've seen. */
		{
		  *line_break_ptr = '\0';
		  save_ptr = line_break_ptr + 1;
		  save_length = e_com - save_ptr;
		  e_com = line_break_ptr;

		  /* If we had to go past `right_margin' to print stuff out,
		     extend `right_margin' out to this point. */
		  if ((column - save_length) > right_margin)
		    right_margin = column - save_length;
		}
	      else
		/* The line break is after the last text;  we're really
		   truncating the line. */
		{
		  if (comment_type == cplus_comment)
		    {
		      while (*buf_ptr == TAB || *buf_ptr == ' ')
			buf_ptr++;
		      buf_ptr--;
		      if (*buf_ptr == EOL)
			goto cplus_exit;
		    }
		  else
		    {
		      while (*buf_ptr == TAB || *buf_ptr == ' '
			     || *buf_ptr == EOL)
			buf_ptr++;

		      buf_ptr--;
		    }

		  *e_com = '\0';
		}
	      goto end_line;
	    }

	  if (*buf_ptr == EOL)
	    ++line_no;
	  buf_ptr++;
	  if (buf_ptr == buf_end)
	    fill_buffer ();
	}


    end_line:
      /* Compress pure whitespace lines into newlines. */
      if (!text_on_line
	  && !visible_preamble && !(first_comment_line == com_lines))
	e_com = s_com;
      *e_com = '\0';
      dump_line (true);

      /* If formatting (paragraph_break is only used for formatted
         comments) and user wants blank lines merged, kill all white
         space after the "\n\n" indicating a paragraph break. */
      if (paragraph_break)
	{
	  if (merge_blank_comment_lines)
	    while (*buf_ptr == EOL || *buf_ptr == ' ' || *buf_ptr == TAB)
	      {
		if (*buf_ptr == EOL)
		  ++line_no;
		if (++buf_ptr >= buf_end)
		  fill_buffer ();
	      }
	  paragraph_break = 0;
	}
      else
	{
	  /* If it was a paragraph break (`if' clause), we scanned ahead
	     one character.  So, here in the `else' clause, advance buf_ptr. */
	  if (*buf_ptr == EOL)
	    ++line_no;
	  buf_ptr++;
	  if (buf_ptr >= buf_end)
	    fill_buffer ();
	}

    begin_line:
      if (had_eof)
	break;

      /* Indent the line properly.  If it's a boxed comment, align with
         the '*' in the beginning slash-star and start inserting there.
         Otherwise, insert blanks for alignment, or a star if the
         user specified -sc. */
      if (line_preamble)
	{
	  (void) memcpy (e_com, line_preamble, line_preamble_length);
	  e_com += line_preamble_length;
	  column = start_column + line_preamble_length;
	}
      else
	column = start_column;
      line_break_ptr = 0;

      /* If we have broken the line before the end for formatting,
         copy the text after the break onto the beginning of this
         new comment line. */
      if (save_ptr)
	{
	  while ((*save_ptr == ' ' || *save_ptr == TAB) && save_length)
	    {
	      save_ptr++;
	      save_length--;
	    }
	  (void) memcpy (e_com, save_ptr, save_length);
	  text_on_line = e_com;
	  e_com += save_length;
	  /* We only break if formatting, in which cases there
	     are no tabs, only spaces. */
	  column += save_length;
	  save_ptr = 0;
	  save_length = 0;
	}
      else
	{
	  while (*buf_ptr == ' ' || *buf_ptr == TAB)
	    if (++buf_ptr >= buf_end)
	      fill_buffer ();
	  text_on_line = 0;
	}
    }

  parser_state_tos->tos--;
  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
  parser_state_tos->box_com = boxed_comment;
}