[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-indent] Re: Patch for handling comments
From: |
Roger Flores |
Subject: |
[Bug-indent] Re: Patch for handling comments |
Date: |
Sat, 31 Jan 2004 22:50:33 -0800 |
User-agent: |
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5b) Gecko/20030723 Thunderbird/0.1 |
Hmmm. Apparently attachments can't be binary. Here's the patch
uncompressed, which hopefully is treated as text somehow.
-Roger Flores
--- indent-2.2.9/src/indent.h 2002-11-10 13:02:48.000000000 -0800
+++ ../indent-2.2.9/src/indent.h 2004-01-06 18:14:29.000000000 -0800
@@ -29,7 +29,7 @@
#include "sys.h"
RCSTAG_H (indent, "$Id: indent.h,v 1.37 2002/11/10 21:02:48 david Exp $");
-#include <config.h>
+#include "config.h"
#include <libgettext.h>
#ifdef ENABLE_NLS
@@ -175,6 +175,26 @@ extern codes_ty postfix_blankline_reques
* requested */
extern int break_comma; /* when true and not in parens, break after a
* comma */
+enum comment_conversions
+{
+ unchanged_comments,
+ c_comments, /* convert
single line comments to
+ *
output only C style notation.
+ *
Sequential single line comments are
+ *
merged into a block comment. */
+ cpp_comments, /* convert single line
comments to
+ *
output only C++ style notation.
+ *
Block comments are printed as a series
+ * of
single line comments. Adjacent
+ *
block comments are separated with
+ * one
vertical whitespace. */
+ mixed_comments /* convert single line
comments to
+ *
output only C++ style notation.
+ *
Sequential single line comments and
+ *
block comments are printed as
+ * C
block comments. */
+};
+
/*
* This structure stores all the user options that come from (e.g.)
@@ -207,6 +227,7 @@ typedef struct user_options_st
* a high priority to break long lines at. */
int format_comments; /* If any comments are to be reformatted */
int format_col1_comments; /* If comments which start in column 1 are to be
magically reformatted */
+ enum comment_conversions comment_conversion;
int extra_expression_indent; /* True if continuation lines from the
expression part of "if(e)",
* "while(e)", "for(e;e;e)" should be
indented an extra tab stop so that
* they don't conflict with the code that
follows */
@@ -419,4 +440,5 @@ extern parser_state_ty *parser_state_tos
extern int output_line_length (void);
+
#endif /* INDENT_INDENT_H */
--- indent-2.2.9/src/args.c 2002-11-10 13:02:48.000000000 -0800
+++ ../indent-2.2.9/src/args.c 2004-01-06 19:03:11.000000000 -0800
@@ -118,6 +118,7 @@ static int exp_dj = 0;
static int exp_eei = 0;
static int exp_fc1 = 0;
static int exp_fca = 0;
+static int exp_com = 0;
static int exp_gnu = 0;
static int exp_hnl = 0;
static int exp_i = 0;
@@ -157,6 +158,26 @@ user_options_ty settings = {0};
static void usage (void);
+static void set_comments_unchanged(void)
+{
+ settings.comment_conversion = unchanged_comments;
+}
+
+static void set_comments_c(void)
+{
+ settings.comment_conversion = c_comments;
+}
+
+static void set_comments_cpp(void)
+{
+ settings.comment_conversion = cpp_comments;
+}
+
+static void set_comments_mix(void)
+{
+ settings.comment_conversion = mixed_comments;
+}
+
/* N.B.: because of the way the table here is scanned, options whose names
* are substrings of other options must occur later; that is, with -lp vs -l,
* -lp must be first. Also, while (most) booleans occur more than once, the
@@ -262,6 +283,10 @@ const pro_ty pro[] =
{"gnu", PRO_SETTINGS, 0, ONOFF_NA,
GNU_SETTINGS_STRING, &exp_gnu},
{"fca", PRO_BOOL, true, ON,
&settings.format_comments, &exp_fca},
{"fc1", PRO_BOOL, true, ON,
&settings.format_col1_comments, &exp_fc1},
+ {"commix", PRO_FUNCTION, mixed_comments, ONOFF_NA, (int *)
set_comments_mix, &exp_com},
+ {"comcpp", PRO_FUNCTION, cpp_comments, ONOFF_NA, (int *)
set_comments_cpp, &exp_com},
+ {"comc", PRO_FUNCTION, c_comments, ONOFF_NA, (int *)
set_comments_c, &exp_com},
+ {"com", PRO_FUNCTION, unchanged_comments, ONOFF_NA, (int *)
set_comments_unchanged, &exp_com},
{"eei", PRO_BOOL, false, ON,
&settings.extra_expression_indent, &exp_eei},
{"dj", PRO_BOOL, false, ON,
&settings.ljust_decl, &exp_dj},
{"di", PRO_INT, 16, ONOFF_NA,
&settings.decl_indent, &exp_di},
@@ -376,6 +401,10 @@ const pro_ty pro[] =
{"gnu", PRO_SETTINGS, 0, ONOFF_NA,
GNU_SETTINGS_STRING, &exp_gnu},
{"fca", PRO_BOOL, false, ON,
&settings.format_comments, &exp_fca},
{"fc1", PRO_BOOL, false, ON,
&settings.format_col1_comments, &exp_fc1},
+ {"commix", PRO_FUNCTION, mixed_comments, ONOFF_NA, (int *)
set_comments_mix, &exp_com},
+ {"comcpp", PRO_FUNCTION, cpp_comments, ONOFF_NA, (int *)
set_comments_cpp, &exp_com},
+ {"comc", PRO_FUNCTION, c_comments, ONOFF_NA, (int *)
set_comments_c, &exp_com},
+ {"com", PRO_FUNCTION, unchanged_comments, ONOFF_NA, (int *)
set_comments_unchanged, &exp_com},
{"eei", PRO_BOOL, false, ON,
&settings.extra_expression_indent, &exp_eei},
{"dj", PRO_BOOL, false, ON,
&settings.ljust_decl, &exp_dj},
{"di", PRO_INT, 2, ONOFF_NA,
&settings.decl_indent, &exp_di},
@@ -487,6 +516,10 @@ const long_option_conversion_ty option_c
{"gnu-style", "gnu"},
{"format-first-column-comments", "fc1"},
{"format-all-comments", "fca"},
+ {"comments-dont-change", "com"},
+ {"comments-c-only", "comc"},
+ {"comments-cpp-only", "comcpp"},
+ {"comments-mix", "commix"},
{"extra-expression-indentation", "eei"},
{"else-endif-column", "cp"},
{"dont-star-comments", "nsc"},
--- indent-2.2.9/src/comments.c 2002-08-04 10:08:41.000000000 -0700
+++ ../indent-2.2.9/src/comments.c 2004-01-06 20:45:59.000000000 -0800
@@ -20,9 +20,11 @@
#include "comments.h"
#include "globs.h"
#include "parse.h"
+#include "output.h"
RCSTAG_CC ("$Id: comments.c,v 1.33 2002/08/04 17:08:41 david Exp $");
+
/* Check the limits of the comment buffer, and expand as neccessary. */
#define CHECK_COM_SIZE \
@@ -35,6 +37,33 @@ RCSTAG_CC ("$Id: comments.c,v 1.33 2002/
s_com = combuf + 1; \
}
+
+/* Check for repeated characters, which indicate a visual divider like
+---------------------------------------- */
+static void extend_repeated_characters(int *column, int right_margin)
+{
+ char *p = e_com - 1; /* look at the last character written. */
+ int c = 0;
+
+ while (*p == *(p - 1))
+ {
+ p--;
+ c++;
+ }
+ if (c >= 20)
+ {
+ /* Expand the line if neccessary. Leave some room at the end
+ incase a C comment end should be there. */
+ p = e_com - 1;
+ while (*column < right_margin - 3)
+ {
+ *e_com++ = *p;
+ *column += 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.
@@ -79,7 +108,7 @@ RCSTAG_CC ("$Id: comments.c,v 1.33 2002/
void print_comment (
int *paren_targ)
{
- register int column, format;
+ int column, format;
codes_ty comment_type;
int start_column;
@@ -98,14 +127,19 @@ void print_comment (
char * text_on_line = NULL;
char * line_break_ptr = NULL;
char * start_delim = NULL;
+ char * comment_start = NULL;
char * line_preamble = NULL;
int line_preamble_length;
int visible_preamble;
int suppress_cdb = 0;
+ int leave_comment_unaltered = false;
+ int continue_cpp_comment = false;
+ int end_this_line = false;
+ int comment_start_width = 0;
/* GDB_HOOK_print_comment() */
-
+
/* Increment the parser stack, as we will store some things
* there for dump_line to use. */
@@ -137,8 +171,38 @@ void print_comment (
visible_preamble = 1;
boxed_comment = 0;
stars = 0;
- blankline_delims = 0;
- }
+ blankline_delims = settings.comment_delimiter_on_blankline;
+
+ // If mixed mode comment conversions, check for EOL WS* "//" to
detect
+ // sequential single line comments for merging.
+ // Note that often code is often commented out using
cplus_comment.
+ // Don't format comments in procedures that start in column one.
+#if 1
+ {
+ // This is code to demonstrate that we can detect
sequential C++ comments.
+ // The concern is that the following comments might be
already
+ // parsed and stored, in which case they'd need to be
used or cleaned.
+
+ // Don't merge right hand comments with non right hand
comments.
+ // This code isn't careful enough.
+ char *p = buf_ptr;
+ while (*p != '\0' && *p != EOL) p++;
+ if (*p == EOL) p++;
+ while (*p == ' ' || *p == '\t') p++;
+ if (strncmp(p, "//", 2) == 0)
+ {
+
+ continue_cpp_comment = true;
+ }
+ }
+#endif
+ if (settings.comment_conversion == mixed_comments)
+ {
+ /* Set the flags so that a second line is a c_comment.
*/
+ line_preamble = 0;
+ stars = settings.star_comment_cont;
+ }
+ }
else if ((*buf_ptr == '*') || (*buf_ptr == '-') || (*buf_ptr == '=') ||
(*buf_ptr == '_') || (parser_state_tos->col_1 &&
!settings.format_col1_comments))
@@ -243,6 +307,9 @@ void print_comment (
}
else
{
+ int nested_level;
+ char *p;
+
start_delim = "/*";
line_preamble = 0;
line_preamble_length = 0;
@@ -250,8 +317,40 @@ void print_comment (
boxed_comment = 0;
stars = settings.star_comment_cont;
blankline_delims = settings.comment_delimiter_on_blankline;
+
+ /* check if this comment closes on this line and has stuff
following. */
+ nested_level = 1;
+ p = buf_ptr;
+ while (*p != EOL)
+ {
+ if (*p == '*' && *(p + 1) == '/')
+ {
+ nested_level--;
+ p += 2;
+
+ }
+ else if (*p == '/' && *(p + 1) == '*')
+ {
+ nested_level++;
+ p += 2;
+
+ }
+ else
+ {
+ if (nested_level == 0 && *p != ' ' && *p !=
'\t')
+ {
+ leave_comment_unaltered = true;
+ break;
+ }
+ p++;
+ }
+
+ }
}
+ if (settings.comment_conversion == mixed_comments)
+ blankline_delims = 0;
+
paragraph_break = 0;
merge_blank_comment_lines = 0;
first_comment_line = com_lines;
@@ -289,6 +388,18 @@ void print_comment (
{
start_column = 1;
}
+ /* The comment is probably code. Don't format
it. It's better if
+ programmers comment out code using #if 0 ..
#endif, but
+ commenting out code is an extremely common
habit. */
+ if (parser_state_tos->col_1)
+ {
+ format = false;
+ continue_cpp_comment = false;
+ suppress_cdb = true;
+ leave_comment_unaltered = true;
+ stars = false;
+ start_column = 1;
+ }
}
}
}
@@ -350,11 +461,18 @@ void print_comment (
format = settings.format_comments;
}
- if (!line_preamble)
+ if (!line_preamble ||
+ settings.comment_conversion == c_comments)
{
line_preamble_length = 3;
- if (stars)
+ if (settings.comment_conversion == cpp_comments)
+ {
+ line_preamble = "// ";
+ visible_preamble = 1;
+ }
+ else
+ if (stars || (comment_type == cplus_comment &&
settings.star_comment_cont))
{
line_preamble = " * ";
visible_preamble = 1;
@@ -372,13 +490,94 @@ void print_comment (
parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
parser_state_tos->box_com = boxed_comment;
+ /* Check for known tool commands in comments and do not
+ disturb (format) the comment if present. */
+ /* PC-Lint at http://www.gimpel.com */
+ if (strncmp(buf_ptr, "lint", 4) == 0)
+ {
+ format = false;
+ blankline_delims = 0;
+ }
+ /* Splint at http://lclint.cs.virginia.edu/ */
+ else if (strncmp(buf_ptr, "@", 1) == 0)
+ {
+ leave_comment_unaltered = true;
+ format = false;
+ blankline_delims = 0;
+ }
+
/* Output the beginning comment delimiter. They are both two
* characters long. */
+ if (!leave_comment_unaltered && format)
+ {
+ switch (settings.comment_conversion)
+ {
+ case unchanged_comments:
+ break;
+ case c_comments:
+ start_delim = "/*";
+ break;
+ case cpp_comments:
+ start_delim = "//";
+ /* temp until cpp converts blocks to single
line sequences */
+ if (boxed_comment)
+ start_delim = "/*";
+ else
+ start_delim = "//";
+ break;
+ case mixed_comments:
+ if (boxed_comment)
+ start_delim = "/*";
+ else
+ start_delim = "//";
+ break;
+ };
+ }
+ /* point to the start of the comment. This is used in mixed_comments
when
+ the comment type needs to be changed. This is realized when the
+ comment wraps to the next line or not. */
+ comment_start = e_com;
*e_com++ = *start_delim;
*e_com++ = *(start_delim + 1);
column = start_column + 2;
+ /* Doxygen at http://www.doxygen.org */
+ if (strncmp(buf_ptr, "* ", 2) == 0 ||
+ strncmp(buf_ptr, "*<", 2) == 0 ||
+ strncmp(buf_ptr, "!", 1) == 0 ||
+ strncmp(buf_ptr, "/", 1) == 0)
+ {
+ /* Eventually a flag to not consume leading whitespace is
needed to support Doxygen nesting lists. */
+
+ /* If the user specified -cdb, these appear on that first line
alone. Write them here
+ and let hte -cdb code below dump the line. */
+ comment_start_width = 1;
+ *e_com++ = *buf_ptr++;
+
+ if (*buf_ptr == '<')
+ {
+ comment_start_width++;
+ *e_com++ = *buf_ptr++;
+ }
+ }
+
+
+ /* the comment should be formatted or have one preceeding blank line.
+ Skip all others. */
+ if (format || (blankline_delims && !suppress_cdb))
+ {
+ while (*buf_ptr == ' ' || *buf_ptr == TAB || *buf_ptr == EOL ||
+ (*buf_ptr == '*' && *(buf_ptr + 1) == ' ') || /* Skip
stars */
+ (*buf_ptr == '/' && *(buf_ptr + 1) == '/')) /* Skip
C++ start */
+ {
+ if (++buf_ptr >= buf_end)
+ {
+ fill_buffer ();
+ }
+ }
+ }
+
/* If the user specified -cdb, put the delimiter on one line. */
if (blankline_delims && !suppress_cdb)
@@ -413,7 +612,7 @@ void print_comment (
else if (format)
{
*e_com++ = ' ';
- column = start_column + 3;
+ column = start_column + 3 + comment_start_width;
skip_buffered_space(); /* adjusts buf_ptr */
}
@@ -473,10 +672,56 @@ void print_comment (
break;
case EOL:
+ /* Check for repeated characters, which
indicate a visual divider like
+
---------------------------------------- */
+ if (format)
+
extend_repeated_characters(&column, right_margin);
+
/* We may be at the end of a C++ comment */
- if (comment_type == cplus_comment)
+ if (comment_type == cplus_comment && !continue_cpp_comment)
{
cplus_exit:
+ if
((settings.comment_conversion == c_comments ||
+
(settings.comment_conversion == mixed_comments && comment_start == NULL)) &&
+
!leave_comment_unaltered)
+ {
+ if (blankline_delims &&
!suppress_cdb)
+ {
+ *e_com = '\0';
+ dump_line
(true, paren_targ);
+
+
+ column =
start_column;
+ *e_com++ = ' ';
+ }
+ /* Insert a space if
none is present. */
+ else if (*(e_com - 1)
!= ' ')
+ *e_com++ = ' ';
+
+ /* Now insert the
ending delimiter */
+ *e_com++ = '*';
+ *e_com++ = '/';
+ }
+ else if (blankline_delims &&
!suppress_cdb)
+ {
+ *e_com = '\0';
+ dump_line (true,
paren_targ);
+
+
+ column = start_column;
+
+ /* Now insert the
ending delimiter */
+ *e_com++ = '/';
+ *e_com++ = '/';
+ }
+
+ if (settings.comment_conversion
== mixed_comments && comment_start != NULL && !leave_comment_unaltered)
+ {
+ /* This comment does
not continue to another line, so make it a C++ comment. */
+ *(comment_start + 1) =
'/';
+ comment_start = NULL;
/* Change comment once. */
+ }
+
parser_state_tos->tos--;
parser_state_tos->com_col = (two_contiguous_comments ? 1 :
start_column);
parser_state_tos->box_com = boxed_comment;
@@ -513,27 +758,100 @@ void print_comment (
skip_buffered_space(); /* adjusts buf_ptr */
- if ((*buf_ptr == EOL) || !text_on_line)
- {
- paragraph_break = 1;
- goto end_line;
- }
-
- /* Also need to eat the preamble. */
- if (!boxed_comment && (current_column () == found_column +
1) &&
- (buf_ptr[0] == '*') && (buf_ptr[1] != '/'))
+
+ /* Merge C++ comments that follow
starting on a different line.
+ This is approximated by detecting
comments that start a line
+ when the current comments don't.
+
+ This code isn't careful enough. */
+ {
+ char *p = buf_ptr;
+ while (*p != '\0' && *p != EOL)
p++;
+ if (*p == EOL) p++;
+ while (*p == ' ' || *p == '\t')
p++;
+ if (strncmp(p, "//", 2) == 0 &&
+ !(*(p - 1) == EOL &&
start_column != 1))
+ {
+
+ continue_cpp_comment =
true;
+ }
+ else
+ continue_cpp_comment =
false;
+ }
+
+ /* Sometimes short comments are listed
one line each and not
+ to be merged. It would be clearer if
such comments were preceeded
+ by a '-' or vertical whitespace, but
oh well.
+
+ Also, this isn't 100% accurate. The
column of the input
+ is needed but isn't kept. The
column used belongs to the
+ output. This is usually right, but
can be wrong if a lot
+ of identation is changed or if the
right_margin is very
+ different. */
+ if (column < (right_margin * 3) / 4)
+ {
+ char *p = e_com - 1;
+ while (*p == ' ') p--;
+ if (*p == '.' || *p == ':' ||
*p == '!' || *p == '?')
+ end_this_line = true;
/* force the line to end. */
+ }
+
+ /* Also need to eat the preamble. This means skipping
either "* " or "// ".
+ Don't care where on they
line they start, to tolerate unaligned comments. */
+ if (!boxed_comment &&
+ ((current_column (),
(buf_ptr[0] == '*') && (buf_ptr[1] != '/')) ||
+ (buf_ptr[0] == '/' &&
buf_ptr[1] == '/' && comment_type == cplus_comment)))
{
- if (++buf_ptr >= buf_end)
+ if ((*buf_ptr == '*' || *buf_ptr == '/') && ++buf_ptr
>= buf_end)
{
fill_buffer ();
}
+ if (*buf_ptr == '/' && ++buf_ptr >= buf_end)
+ {
+ fill_buffer ();
+ }
+
if ((*buf_ptr == ' ') && (++buf_ptr >= buf_end))
{
fill_buffer ();
}
}
+ if (*buf_ptr == EOL || !text_on_line)
+ {
+#if 0
+ /* The idea of this code was to
remove trailing lines by
+ detecting when the end of
comment was visible next, and when
+ so skip the intervening
characters. This did not work for a long
+ comment where p == buf_end
resulting in fill_buffer being called
+ which somehow messed things up.
*/
+
+
+ /* If this is the last line, consume all blank lines
instead. */
+ char *p = buf_ptr;
+
+ while (*p == ' ' || *p == TAB
|| (*p == EOL && comment_type != cplus_comment))
+ {
+ if (++p >= buf_end)
+ {
+ fill_buffer ();
+ }
+ }
+
+ if (*p == '*' && *(p + 1) ==
'/')
+ {
+ /* skip to the end of
the comment. */
+ buf_ptr = p;
+ }
+ else
+#endif
+ {
+ paragraph_break = 1;
+ goto end_line;
+ }
+ }
+
/* This is a single newline. Transform it (and any
* following whitespace) into a single blank. */
@@ -543,6 +861,116 @@ void print_comment (
*e_com++ = ' ';
column++;
}
+
+ /* examine the line in buf_ptr
for patterns to structure the formatting. */
+
+ /* Check for repeated
characters, which indicate a visual divider like
+
---------------------------------------- */
+ {
+ char *p = buf_ptr;
+ int c = 0;
+
+ if (*p != EOL) p++;
+
+ while (*p != EOL && *p
== *(p - 1))
+ {
+ p++;
+ c++;
+ }
+ if (c >= 20)
+ {
+#if 0
+ *e_com = '\0';
+ dump_line
(true, paren_targ);
+
+
+ column =
start_column;
+ goto begin_line;
+#endif
+
+ /* Put a break
before this line */
+ paragraph_break
= 1;
+ goto end_line;
+ /* ??? How to
put a break after the line? */
+ /* more
preferable is to fill the line with
+ the repeated
character during printing. */
+ }
+ }
+
+ /* Doxygen commands at
http://www.doxygen.org */
+ /* Check for 'address@hidden '.
Such commands should start a line but can wrap to others. */
+ {
+ char *p = buf_ptr;
+
+ if (*p == '@' || *p ==
'\\')
+ {
+ p++;
+ while (*p !=
EOL && *p >= 'a' && *p <= 'z') p++;
+ if (*p == ' ')
+ {
+ /* Put
a break before this line */
+
paragraph_break = 1;
+ goto
end_line;
+ }
+ }
+ }
+
+
+ /* Check for '[a-zA-Z]*: ' */
+ /* Don't accept "http:\\" */
+ {
+ char *p = buf_ptr;
+ while (*p != EOL && *p
!= ':' && *p != ' ') p++;
+ if (*p == ':' && (*p ==
' ' || *p == '\t'))
+ {
+ if
(settings.comment_conversion == mixed_comments && comment_start != NULL &&
!leave_comment_unaltered)
+ {
+ /* This
comment does not continue to another line, so make it a C++ comment. */
+
*(comment_start + 1) = '*';
+
comment_start = NULL; /* Change comment once. */
+ }
+
+ /* Actually
dump the line for more vertical space. */
+ *e_com = '\0';
+ dump_line
(true, paren_targ);
+
+ if
(line_preamble)
+ {
+ (void)
memcpy (e_com, line_preamble, line_preamble_length);
+ e_com
+= line_preamble_length;
+ }
+ column =
start_column + line_preamble_length;
+
+ /* Put a break
before this line */
+ paragraph_break
= 1;
+ goto end_line;
+ }
+ }
+
+ /* Check for '[0-9]*[.)-] ' */
+ /* Doxygen lists '-' can be
nested too. That's not supported here. */
+ {
+ char *p = buf_ptr;
+ while (*p != EOL && *p
>= '0' && *p <= '9') p++;
+ if ((*p == '.' || *p ==
'-' || *p == ')') && *(p + 1) == ' ')
+ {
+ /* Put a break
before this line */
+ paragraph_break
= 1;
+ goto end_line;
+ }
+
+ /* When these occurr,
it would probably be nice to track an indentation
+ level for the
remaining lines. */
+ }
+
+ /* End the line as long as a C
comment end isn't next. Otherwise
+ the comment end will be left
alone on the line. */
+ if (end_this_line && !(*buf_ptr == '*' && *(buf_ptr +
1) == '/'))
+ {
+ paragraph_break = 1;
+ goto end_line;
+ }
+ end_this_line = false;
continue;
}
@@ -563,18 +991,42 @@ void print_comment (
if (!boxed_comment)
{
+ if
(settings.comment_conversion == mixed_comments && comment_start != NULL &&
!leave_comment_unaltered)
+ {
+ /* This
comment continues to another line, so make it a C comment. */
+
*(comment_start + 1) = '/';
+ }
+
if (text_on_line)
{
+ /* Check for
repeated characters, which indicate a visual divider like
+
---------------------------------------- */
+ if (format)
+ {
+ /* Look
for repeated characters before the space. */
+ if
(*(e_com - 1) == ' ')
+ {
+
e_com--;
+
column--;
+ }
+
+
extend_repeated_characters(&column, right_margin);
+ }
+
+
if (blankline_delims && !suppress_cdb)
{
*e_com = '\0';
dump_line (true, paren_targ);
- *e_com++ = ' ';
+
+ if (settings.comment_conversion !=
cpp_comments || leave_comment_unaltered)
+ *e_com++ = ' ';
}
else
{
- /* Insert space before closing delim */
- if ((*(e_com - 1) != ' ') && (*(e_com - 1)
!= TAB))
+ /* Insert space before closing delim.
Don't if there's a
+ '@',
which is normal for Splint annotations. */
+ if (*(e_com - 1) != ' ' && *(e_com - 1) !=
TAB && *(e_com - 1) != '@')
{
*e_com++ = ' ';
}
@@ -603,9 +1055,21 @@ void print_comment (
}
}
- /* Now insert the ending delimiter */
- *e_com++ = '*';
- *e_com++ = '/';
+ if (settings.comment_conversion
== c_comments || settings.comment_conversion == unchanged_comments ||
+
+ leave_comment_unaltered
||
+
(settings.comment_conversion == mixed_comments && comment_start == NULL))
+ {
+ /* Now insert the ending delimiter */
+ *e_com++ = '*';
+ *e_com++ = '/';
+ }
+ if (blankline_delims &&
!suppress_cdb && settings.comment_conversion == cpp_comments &&
!leave_comment_unaltered)
+ {
+ /* Now insert the
ending delimiter */
+ *e_com++ = '/';
+ *e_com++ = '/';
+ }
*e_com = '\0';
/* Skip any whitespace following the comment. If
@@ -620,6 +1084,26 @@ void print_comment (
if (buf_ptr >= buf_end)
{
fill_buffer ();
+ }
+
+
+ /* when converting
sequential c comments to cpp comments, keep a
+ vertical whitespace
inbetween to maintain separation. */
+ if
((settings.comment_conversion == cpp_comments ||
+
(settings.comment_conversion == mixed_comments && comment_start != NULL))
+ &&
!leave_comment_unaltered)
+ {
+ char *p =
buf_ptr;
+ if (*p == EOL)
p++;
+
+ while (*p == '
' || *p == '\t')
+ p++;
+
+ if (*p == '/'
&& *(p + 1) == '*')
+ {
+
dump_line (true, paren_targ);
+ *e_com
= '\0';
+ }
}
parser_state_tos->tos--;
@@ -634,7 +1118,8 @@ void print_comment (
* a boxed comment. */
if ((first_comment_line == com_lines - 1) &&
- (e_com == s_com + line_preamble_length))
+ (e_com == s_com + line_preamble_length) &&
+ (current_column() == found_column + 1))
{
/* Account for change in line_preamble_length: */
column -= line_preamble_length - 1;
@@ -665,64 +1150,99 @@ void print_comment (
/* 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)
- {
- buf_ptr = skip_horiz_space(buf_ptr);
-
- buf_ptr--;
- if (*buf_ptr == EOL)
- {
- goto cplus_exit;
- }
- }
- else
- {
- while ((*buf_ptr == TAB) ||
- (*buf_ptr == ' ') ||
- (*buf_ptr == EOL))
- {
- if (*buf_ptr == EOL)
- {
- ++line_no;
- }
-
- buf_ptr++;
-
- if (buf_ptr >= buf_end)
- {
- fill_buffer ();
- }
- }
-
- buf_ptr--;
- }
-
- *e_com = EOS;
- }
- goto end_line;
+ if (format && (column > right_margin))
+ {
+ /* Check for repeated characters, which
indicate a visual divider like
+
----------------------------------------
+ Consume any that extend past the end of the
line.*/
+ if (*(e_com - 1) == *(e_com - 2) && *(e_com -
2) == *(e_com - 3))
+ {
+ while (*buf_ptr == *(e_com - 1))
+ {
+ buf_ptr++;
+
+ if (buf_ptr == buf_end)
+ {
+ fill_buffer ();
+ }
+ }
+
+ /* Also strip off extra characters
already written. */
+ while (column > right_margin - 3 &&
*(e_com - 1) == *(e_com - 2))
+ {
+ e_com--;
+ column--;
+ }
+
+ }
+
+
+ if (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)
+ {
+ buf_ptr = skip_horiz_space(buf_ptr);
+
+ buf_ptr--;
+ if (*buf_ptr == EOL)
+ {
+ goto cplus_exit;
+ }
+ }
+ else
+ {
+ while ((*buf_ptr == TAB) ||
+ (*buf_ptr == ' ') ||
+ (*buf_ptr == EOL))
+ {
+ if (*buf_ptr == EOL)
+ {
+ ++line_no;
+ }
+
+ buf_ptr++;
+
+ if (buf_ptr >= buf_end)
+ {
+ fill_buffer ();
+ }
+ }
+
+ buf_ptr--;
+ }
+
+ *e_com = EOS;
+ }
+ if (settings.comment_conversion ==
mixed_comments && comment_start != NULL && !leave_comment_unaltered)
+ {
+ /* This comment continues to
another line, so make it a C comment. */
+ *(comment_start + 1) = '*';
+ }
+ comment_start = NULL; /* Change
comment once. */
+
+ goto end_line;
+ }
}
if (*buf_ptr == EOL)
@@ -740,6 +1260,13 @@ void print_comment (
end_line:
+ if (settings.comment_conversion == mixed_comments &&
comment_start != NULL && !leave_comment_unaltered)
+ {
+ /* This comment continue sto another line, so make it a
C comment. */
+ *(comment_start + 1) = '*';
+ comment_start = NULL; /* Change comment once. */
+ }
+
/* Compress pure whitespace lines into newlines. */
if (!text_on_line && !visible_preamble && !(first_comment_line ==
com_lines))
{
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Bug-indent] Re: Patch for handling comments,
Roger Flores <=