Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Jun 2018 15:28:55 +0000 (UTC)
From:      Piotr Pawel Stefaniak <pstef@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r334563 - in head/usr.bin/indent: . tests
Message-ID:  <201806031528.w53FSttd051776@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pstef
Date: Sun Jun  3 15:28:55 2018
New Revision: 334563
URL: https://svnweb.freebsd.org/changeset/base/334563

Log:
  indent(1): improve handling of boxed comments indentation
  
  The trick is to copy everything from the start of the line into the buffer
  that stores newlines and comments until indent finds a brace or an else.
  pr_comment() will use that information to calculate the original indentation
  of the boxed comment.
  
  This requires storing two pieces of information: the real start of the
  buffer (sc_buf) and the start of the comment (save_com).

Modified:
  head/usr.bin/indent/indent.c
  head/usr.bin/indent/indent_globs.h
  head/usr.bin/indent/pr_comment.c
  head/usr.bin/indent/tests/comments.0
  head/usr.bin/indent/tests/comments.0.stdout

Modified: head/usr.bin/indent/indent.c
==============================================================================
--- head/usr.bin/indent/indent.c	Sun Jun  3 15:12:40 2018	(r334562)
+++ head/usr.bin/indent/indent.c	Sun Jun  3 15:28:55 2018	(r334563)
@@ -341,6 +341,7 @@ main(int argc, char **argv)
 	    switch (type_code) {
 	    case newline:
 		if (sc_end == NULL) {
+		    save_com = sc_buf;
 		    save_com[0] = save_com[1] = ' ';
 		    sc_end = &save_com[2];
 		}
@@ -359,6 +360,13 @@ main(int argc, char **argv)
 		break;
 	    case comment:
 		if (sc_end == NULL) {
+		    /*
+		     * Copy everything from the start of the line, because
+		     * pr_comment() will use that to calculate original
+		     * indentation of a boxed comment.
+		     */
+		    memcpy(sc_buf, in_buffer, buf_ptr - in_buffer - 4);
+		    save_com = sc_buf + (buf_ptr - in_buffer - 4);
 		    save_com[0] = save_com[1] = ' ';
 		    sc_end = &save_com[2];
 		}
@@ -1172,9 +1180,11 @@ check_type:
 		    e_lab--;
 		if (e_lab - s_lab == com_end && bp_save == NULL) {
 		    /* comment on preprocessor line */
-		    if (sc_end == NULL)	/* if this is the first comment, we
-					 * must set up the buffer */
-			sc_end = &(save_com[0]);
+		    if (sc_end == NULL) {	/* if this is the first comment,
+						 * we must set up the buffer */
+			save_com = sc_buf;
+			sc_end = &save_com[0];
+		    }
 		    else {
 			*sc_end++ = '\n';	/* add newline between
 						 * comments */

Modified: head/usr.bin/indent/indent_globs.h
==============================================================================
--- head/usr.bin/indent/indent_globs.h	Sun Jun  3 15:12:40 2018	(r334562)
+++ head/usr.bin/indent/indent_globs.h	Sun Jun  3 15:28:55 2018	(r334563)
@@ -126,8 +126,9 @@ char       *buf_ptr;		/* ptr to next character to be t
 				 * in_buffer */
 char       *buf_end;		/* ptr to first after last char in in_buffer */
 
-char        save_com[sc_size];	/* input text is saved here when looking for
+char        sc_buf[sc_size];	/* input text is saved here when looking for
 				 * the brace after an if, while, etc */
+char       *save_com;		/* start of the comment stored in sc_buf */
 char       *sc_end;		/* pointer into save_com buffer */
 
 char       *bp_save;		/* saved value of buf_ptr when taking input
@@ -241,8 +242,12 @@ struct parser_state {
     int         box_com;	/* set to true when we are in a "boxed"
 				 * comment. In that case, the first non-blank
 				 * char should be lined up with the / in / followed by * */
-    int         comment_delta,
-                n_comment_delta;
+    int         comment_delta;	/* used to set up indentation for all lines
+				 * of a boxed comment after the first one */
+    int         n_comment_delta;/* remembers how many columns there were
+				 * before the start of a box comment so that
+				 * forthcoming lines of the comment are
+				 * indented properly */
     int         cast_mask;	/* indicates which close parens potentially
 				 * close off casts */
     int         not_cast_mask;	/* indicates which close parens definitely

Modified: head/usr.bin/indent/pr_comment.c
==============================================================================
--- head/usr.bin/indent/pr_comment.c	Sun Jun  3 15:12:40 2018	(r334562)
+++ head/usr.bin/indent/pr_comment.c	Sun Jun  3 15:28:55 2018	(r334563)
@@ -158,8 +158,11 @@ pr_comment(void)
 	 * The comment we're about to read usually comes from in_buffer,
 	 * unless it has been copied into save_com.
 	 */
-	char *start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ? bp_save : buf_ptr;
-	ps.n_comment_delta = 1 - count_spaces_until(1, in_buffer, start - 2);
+	char *start;
+
+	start = buf_ptr >= save_com && buf_ptr < save_com + sc_size ?
+	    sc_buf : in_buffer;
+	ps.n_comment_delta = 1 - count_spaces_until(1, start, buf_ptr - 2);
     }
     else {
 	ps.n_comment_delta = 0;

Modified: head/usr.bin/indent/tests/comments.0
==============================================================================
--- head/usr.bin/indent/tests/comments.0	Sun Jun  3 15:12:40 2018	(r334562)
+++ head/usr.bin/indent/tests/comments.0	Sun Jun  3 15:28:55 2018	(r334563)
@@ -30,3 +30,23 @@ void t(void) {
 	
 	/* r309343	*/
 }
+
+int c(void)
+{
+	if (1) { /*- a christmas tree  *
+				      ***
+				     ***** */
+		    /*- another one *
+				   ***
+				  ***** */
+	    7;
+	}
+
+	if (1) /*- a christmas tree  *
+				    ***
+				   ***** */
+		    /*- another one *
+				   ***
+				  ***** */
+	    1;
+}

Modified: head/usr.bin/indent/tests/comments.0.stdout
==============================================================================
--- head/usr.bin/indent/tests/comments.0.stdout	Sun Jun  3 15:12:40 2018	(r334562)
+++ head/usr.bin/indent/tests/comments.0.stdout	Sun Jun  3 15:28:55 2018	(r334563)
@@ -37,3 +37,24 @@ t(void)
 
 	/* r309343	*/
 }
+
+int
+c(void)
+{
+	if (1) {		/*- a christmas tree  *
+					             ***
+					            ***** */
+		/*- another one *
+			       ***
+			      ***** */
+		7;
+	}
+
+	if (1)			/*- a christmas tree  *
+						     ***
+						    ***** */
+		/*- another one *
+			       ***
+			      ***** */
+		1;
+}



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