Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Jul 2016 21:09:23 +0000 (UTC)
From:      "Pedro F. Giffuni" <pfg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r303598 - head/usr.bin/indent
Message-ID:  <201607312109.u6VL9NLg047848@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pfg
Date: Sun Jul 31 21:09:22 2016
New Revision: 303598
URL: https://svnweb.freebsd.org/changeset/base/303598

Log:
  indent(1): Untangle the connection between pr_comment.c and io.c.
  
  It's pr_comment.c that should decide whether to put a "star comment
  continuation" or not. This duplicates code a bit, but it simplifies
  pr_comment() at the same time since pr_comment() no longer has to "signal"
  whether a star continuation is needed or not.
  
  This change requires indent(1) to not wrap comment lines that lack a blank
  character, but I think it's for the better if you look at cases when that
  happens (mostly long URIs and file system paths, which arguably shouldn't
  be wrapped).
  
  It also fixes two bugs:
  
  1. Cases where asterisk is a part of the comment's content (like in "*we*
  are the champions") and happens to appear at the beginning of the line,
  misleading dump_line() into thinking that this is part of the star comment
  continuation, leading to misalignment.
  
  2. Cases where blank starred lines had three too many characters on the
  line when wrapped.
  
  Reference:
  https://github.com/pstef/freebsd_indent/commit/3b41ee78aafafc7c3e662b794835e3253218dbb3
  
  Differential Revision: https://reviews.freebsd.org/D6966  (Partial)
  Submitted by:	Piotr Stefaniak

Modified:
  head/usr.bin/indent/io.c
  head/usr.bin/indent/pr_comment.c

Modified: head/usr.bin/indent/io.c
==============================================================================
--- head/usr.bin/indent/io.c	Sun Jul 31 20:13:00 2016	(r303597)
+++ head/usr.bin/indent/io.c	Sun Jul 31 21:09:22 2016	(r303598)
@@ -242,17 +242,8 @@ dump_line(void)
 		while (e_com > com_st && isspace(e_com[-1]))
 		    e_com--;
 		cur_col = pad_output(cur_col, target);
-		if (!ps.box_com) {
-		    if (star_comment_cont && (com_st[1] != '*' || e_com <= com_st + 1)) {
-			if (com_st[1] == ' ' && com_st[0] == ' ' && e_com > com_st + 1)
-			    com_st[1] = '*';
-			else
-			    fwrite(" * ", com_st[0] == '\t' ? 2 : com_st[0] == '*' ? 1 : 3, 1, output);
-		    }
-		}
 		fwrite(com_st, e_com - com_st, 1, output);
 		ps.comment_delta = ps.n_comment_delta;
-		cur_col = count_spaces(cur_col, com_st);
 		++ps.com_lines;	/* count lines with comments */
 	    }
 	}
@@ -282,7 +273,7 @@ inhibit_newline:
     ps.dumped_decl_indent = 0;
     *(e_lab = s_lab) = '\0';	/* reset buffers */
     *(e_code = s_code) = '\0';
-    *(e_com = s_com) = '\0';
+    *(e_com = s_com = combuf + 1) = '\0';
     ps.ind_level = ps.i_l_follow;
     ps.paren_level = ps.p_l_follow;
     paren_target = -ps.paren_indents[ps.paren_level - 1];

Modified: head/usr.bin/indent/pr_comment.c
==============================================================================
--- head/usr.bin/indent/pr_comment.c	Sun Jul 31 20:13:00 2016	(r303597)
+++ head/usr.bin/indent/pr_comment.c	Sun Jul 31 21:09:22 2016	(r303598)
@@ -179,11 +179,11 @@ pr_comment(void)
 	if (blanklines_before_blockcomments)
 	    prefix_blankline_requested = 1;
 	dump_line();
-	e_com = t;
-	s_com[0] = s_com[1] = s_com[2] = ' ';
+	e_com = s_com = t;
+	if (!ps.box_com && star_comment_cont)
+	    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
     }
 
-    *e_com = '\0';
     if (troff)
 	adj_max_col = 80;
 
@@ -199,10 +199,10 @@ pr_comment(void)
 		/* fix so dump_line uses a form feed */
 		dump_line();
 		last_bl = NULL;
-		*e_com++ = ' ';
-		*e_com++ = '*';
-		*e_com++ = ' ';
-		while (*++buf_ptr == ' ' || *buf_ptr == '\t');
+		if (!ps.box_com && star_comment_cont)
+		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+		while (*++buf_ptr == ' ' || *buf_ptr == '\t')
+		    ;
 	    }
 	    else {
 		if (++buf_ptr >= buf_end)
@@ -214,25 +214,22 @@ pr_comment(void)
 	case '\n':
 	    if (had_eof) {	/* check for unexpected eof */
 		printf("Unterminated comment\n");
-		*e_com = '\0';
 		dump_line();
 		return;
 	    }
 	    last_bl = NULL;
 	    if (ps.box_com || ps.last_nl) {	/* if this is a boxed comment,
 						 * we dont ignore the newline */
-		if (s_com == e_com) {
-		    *e_com++ = ' ';
+		if (s_com == e_com)
 		    *e_com++ = ' ';
-		}
-		*e_com = '\0';
 		if (!ps.box_com && e_com - s_com > 3) {
 		    dump_line();
-		    CHECK_SIZE_COM;
-		    *e_com++ = ' ';
-		    *e_com++ = ' ';
+		    if (star_comment_cont)
+			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
 		}
 		dump_line();
+		if (!ps.box_com && star_comment_cont)
+		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
 	    }
 	    else {
 		ps.last_nl = 1;
@@ -276,20 +273,18 @@ pr_comment(void)
 	end_of_comment:
 		if (++buf_ptr >= buf_end)
 		    fill_buffer();
-		/* ensure blank before end */
-		if (e_com[-1] != ' ' && !ps.box_com) {
-		    *e_com++ = ' ';
-		}
+		CHECK_SIZE_COM;
 		if (break_delim) {
 		    if (e_com > s_com + 3) {
-			*e_com = '\0';
 			dump_line();
 		    }
+		    else
+			s_com = e_com;
+		    *e_com++ = ' ';
 		}
-		CHECK_SIZE_COM;
-		*e_com++ = '*';
-		*e_com++ = '/';
-		*e_com = '\0';
+		if (e_com[-1] != ' ' && !ps.box_com)
+		    *e_com++ = ' ';	/* ensure blank before end */
+		*e_com++ = '*', *e_com++ = '/', *e_com = '\0';
 		ps.just_saw_decl = l_just_saw_decl;
 		return;
 	    }
@@ -307,40 +302,32 @@ pr_comment(void)
 		++e_com;
 		now_col++;
 	    } while (!memchr("*\n\r\b\t", *buf_ptr, 6) &&
-		now_col <= adj_max_col);
+		(now_col <= adj_max_col || !last_bl));
 	    ps.last_nl = false;
 	    if (now_col > adj_max_col && !ps.box_com && e_com[-1] > ' ') {
 		/*
 		 * the comment is too long, it must be broken up
 		 */
-		if (last_bl == NULL) {	/* we have seen no blanks */
-		    last_bl = e_com;	/* fake it */
-		    *e_com++ = ' ';
+		if (last_bl == NULL) {
+		    dump_line();
+		    if (!ps.box_com && star_comment_cont)
+			*e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+		    break;
 		}
-		*e_com = '\0';	/* print what we have */
-		*last_bl = '\0';
-		while (last_bl > s_com && last_bl[-1] < 040)
-		    *--last_bl = 0;
+		*e_com = '\0';
 		e_com = last_bl;
 		dump_line();
-
-		*e_com++ = ' ';	/* add blanks for continuation */
-		*e_com++ = ' ';
-		*e_com++ = ' ';
-
-		t_ptr = last_bl + 1;
+		if (!ps.box_com && star_comment_cont)
+		    *e_com++ = ' ', *e_com++ = '*', *e_com++ = ' ';
+		for (t_ptr = last_bl + 1; *t_ptr == ' ' || *t_ptr == '\t';
+		    t_ptr++)
+			;
 		last_bl = NULL;
-		if (t_ptr >= e_com) {
-		    while (*t_ptr == ' ' || *t_ptr == '\t')
-			t_ptr++;
-		    while (*t_ptr != '\0') {	/* move unprinted part of
-						 * comment down in buffer */
-			if (*t_ptr == ' ' || *t_ptr == '\t')
-			    last_bl = e_com;
-			*e_com++ = *t_ptr++;
-		    }
-		}
-		*e_com = '\0';
+		while (*t_ptr != '\0') {
+		    if (*t_ptr == ' ' || *t_ptr == '\t')
+			last_bl = e_com;
+		    *e_com++ = *t_ptr++;
+ 		}
 	    }
 	    break;
 	}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201607312109.u6VL9NLg047848>