bug-indent
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Allow customising goto label indentation


From: Matthew Wilcox
Subject: Re: Allow customising goto label indentation
Date: Mon, 1 Oct 2007 12:55:13 -0600
User-agent: Mutt/1.5.13 (2006-08-11)

On Mon, Oct 01, 2007 at 06:02:51PM +0200, david wrote:
> 2) updates to the regression tests that demonstrate correct working of
> the feature:
> 
>         regression/TEST         - runs indent with the required options
> on your <name>.c sample code
>         regression/<name>.c   - new code sample pre application of intent
>         regression/standard/<name>.c - code sample that shows correctly
> applied indentation with your feature active.
> 
> 3) you should have also verified that all the existing regression tests
> still work correctly.

OK.  Here's some new test cases, and the patch.  All existing regression
tests worked fine.  I wasn't quite sure of the best way to add the new
code sample, so I put it in EXAMPLES, and I munged the sample two
different ways with the options -li1 and -li-4 to test both positive and
negative indents.

Thanks!

diff -u indent-2.2.9/src/indent.h indent-2.2.9/src/indent.h
--- indent-2.2.9/src/indent.h
+++ indent-2.2.9/src/indent.h
@@ -80,6 +80,8 @@
 
 #define DEFAULT_RIGHT_COMMENT_MARGIN 78
 
+#define DEFAULT_LABEL_INDENT -2
+
 /* Name of input file.  */
 extern char *in_name;
 
@@ -201,6 +203,7 @@
     int leave_preproc_space; /* if true, leave the spaces between  '#' and 
preprocessor commands. */
     int force_preproc_width; /* if this is >0, this becomes the preproc 
indent-level */
     int lineup_to_parens;    /* if true, continued code within parens will be 
lined up to the open paren */
+    int label_indent;       /* if >0 an absolute indent, if <0 relative to 
curret indent level */
     int honour_newlines;     /* True when positions at which we read a newline 
in the input file, should get
                               * a high priority to break long lines at. */
     int format_comments; /* If any comments are to be reformatted */
--- indent-2.2.9.orig/regression/TEST
+++ indent-2.2.9/regression/TEST
@@ -31,7 +31,7 @@
 
 EXAMPLES="do.c else.c for.c func-def.c lshift.c ncs.c \
         proto.c typedef.h no-newline.c emacs-compatible.h fp.c unary.c \
-        struct_param.c no-newline2.c no-newline3.c wide.c"
+        struct_param.c no-newline2.c no-newline3.c wide.c goto.c"
 
 BUGS="case-label.c one-line-1.c one-line-2.c one-line-3.c \
         one-line-4.c struct-decl.c sizeof-in-while.c line-break-comment.c \
@@ -64,7 +64,8 @@
         initializers.c else-if.c else-comment-br.c else-comment-bl.c \
         else-comment-br-ce.c const.c case.c case-br.c scope.c scope-br.c \
         comment-break.c cdb.c bbreak.c bad-break.c pre.c 
dont-line-up-parentheses.c \
-        bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c"
+        bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c \
+       goto-1.c goto-2.c"
 
 ARGS="-bad"
 $INDENT -npro $ARGS bad-break.c -o output/bad-break.c
@@ -178,6 +179,9 @@
 # Check line counting of input file
 $INDENT -npro line-count.c -o /dev/null 2>&1 | cut -d: -f3 > 
output/line-count.c
 
+$INDENT -npro -li1 goto.c -o output/goto-1.c
+$INDENT -npro -li-4 goto.c -o output/goto-2.c
+
 echo "Done."
 
 ERR=output/errors
--- indent-2.2.9.orig/regression/standard/goto.c
+++ indent-2.2.9/regression/standard/goto.c
@@ -0,0 +1,20 @@
+int
+f (void)
+{
+  for (i = 0; i < 10; i++)
+    {
+      if (i > 5)
+       goto two;
+      i++;
+    two:
+    }
+}
+
+int
+main (void)
+{
+  if (f ())
+    goto out;
+out:
+  return 0;
+}
--- indent-2.2.9.orig/regression/standard/goto-1.c
+++ indent-2.2.9/regression/standard/goto-1.c
@@ -0,0 +1,20 @@
+int
+f (void)
+{
+  for (i = 0; i < 10; i++)
+    {
+      if (i > 5)
+       goto two;
+      i++;
+ two:
+    }
+}
+
+int
+main (void)
+{
+  if (f ())
+    goto out;
+ out:
+  return 0;
+}
--- indent-2.2.9.orig/regression/standard/goto-2.c
+++ indent-2.2.9/regression/standard/goto-2.c
@@ -0,0 +1,20 @@
+int
+f (void)
+{
+  for (i = 0; i < 10; i++)
+    {
+      if (i > 5)
+       goto two;
+      i++;
+  two:
+    }
+}
+
+int
+main (void)
+{
+  if (f ())
+    goto out;
+out:
+  return 0;
+}
--- indent-2.2.9.orig/regression/goto.c
+++ indent-2.2.9/regression/goto.c
@@ -0,0 +1,17 @@
+int f(void)
+{
+       for (i = 0; i < 10; i++) {
+               if (i > 5)
+                       goto two;
+               i++;
+      two:
+       }
+}
+
+int main(void)
+{
+       if (f())
+               goto out;
+      out:
+       return 0;
+}
--- indent-2.2.9.orig/doc/indent.texinfo
+++ indent-2.2.9/doc/indent.texinfo
@@ -1188,6 +1188,16 @@
 columns wide, but (as of version 1.2) may be changed by the @option{-ts}
 option.  Tabs are treated as the equivalent number of spaces.
 
address@hidden address@hidden
address@hidden address@hidden
+The indentation of goto labels is controlled by the @option{-li} parameter.  If
+it is set to zero or a positive number, this indicates how far from the left
+margin to indent a label.  If it is set to a negative number, this indicates
+how far back from the current indent level to place the label.  The default
+setting is -2 which matches the behaviour of earlier versions of indent.
+Note that this parameter does not affect the placing of case labels; see the
address@hidden parameter for that.
+
 @kindex address@hidden
 @kindex address@hidden
 @kindex -nip
@@ -1656,6 +1666,11 @@
 Set maximum line length for comment formatting to @address@hidden
 @xref{Comments}.
 
address@hidden address@hidden
address@hidden address@hidden
+Set indentation for labels to @address@hidden
address@hidden
+
 @item -lp
 @itemx --continue-at-parentheses
 Line up continued lines at address@hidden
--- indent-2.2.9.orig/src/io.c
+++ indent-2.2.9/src/io.c
@@ -54,9 +54,6 @@
 
 RCSTAG_CC ("$Id: io.c,v 1.50 2002/08/04 17:08:41 david Exp $");
 
-/* number of levels a label is placed to left of code */
-#define LABEL_OFFSET 2
-
 
/******************************************************************************/
 /* Stuff that needs to be shared with the rest of indent. Documented in
  * indent.h.
@@ -312,9 +309,14 @@
         /* FIXME: does this belong here at all? */
         return 1;
     }
+
+    if (settings.label_indent < 0)
+    {
+        return parser_state_tos->ind_level + settings.label_indent + 1;
+    }
     else
     {
-        return parser_state_tos->ind_level - LABEL_OFFSET + 1;
+        return settings.label_indent + 1;
     }
 }
 
--- indent-2.2.9.orig/src/args.c
+++ indent-2.2.9/src/args.c
@@ -125,6 +125,7 @@
 static int exp_kr   = 0;
 static int exp_l    = 0;
 static int exp_lc   = 0;
+static int exp_li   = 0;
 static int exp_lp   = 0;
 static int exp_lps  = 0;
 static int exp_nip  = 0; 
@@ -252,6 +253,7 @@
     {"nbacc",   PRO_BOOL,                           false,      OFF, 
&settings.blanklines_around_conditional_compilation, &exp_bacc},
     {"lps",     PRO_BOOL,                           false,       ON, 
&settings.leave_preproc_space,              &exp_lps},
     {"lp",      PRO_BOOL,                            true,       ON, 
&settings.lineup_to_parens,                 &exp_lp},
+    {"li",      PRO_INT,             DEFAULT_LABEL_INDENT, ONOFF_NA, 
&settings.label_indent,                     &exp_li},
     {"lc",      PRO_INT,     DEFAULT_RIGHT_COMMENT_MARGIN, ONOFF_NA, 
&settings.comment_max_col,                  &exp_lc},
     {"l",       PRO_INT,             DEFAULT_RIGHT_MARGIN, ONOFF_NA, 
&settings.max_col,                          &exp_l},
     {"kr",      PRO_SETTINGS,                           0, ONOFF_NA, 
KR_SETTINGS_STRING,                         &exp_kr},
@@ -365,6 +367,7 @@
     {"nbacc",   PRO_BOOL,                           false,      OFF, 
&settings.blanklines_around_conditional_compilation, &exp_bacc},
     {"lps",     PRO_BOOL,                           false,       ON, 
&settings.leave_preproc_space,              &exp_lps},
     {"lp",      PRO_BOOL,                            true,       ON, 
&settings.lineup_to_parens,                 &exp_lp},
+    {"li",      PRO_INT,             DEFAULT_LABEL_INDENT, ONOFF_NA, 
&settings.label_indent,                     &exp_li},
     {"lc",      PRO_INT,     DEFAULT_RIGHT_COMMENT_MARGIN, ONOFF_NA, 
&settings.comment_max_col,                  &exp_lc},
     {"l",       PRO_INT,             DEFAULT_RIGHT_MARGIN, ONOFF_NA, 
&settings.max_col,                          &exp_l},
     {"kr",      PRO_SETTINGS,                           0, ONOFF_NA, 
KR_SETTINGS_STRING,                         &exp_kr},
@@ -475,6 +478,7 @@
     {"left-justify-declarations",                   "dj"},
     {"leave-preprocessor-space",                    "lps"},
     {"leave-optional-blank-lines",                  "nsob"},
+    {"label-indentation",                           "li"},
     {"kernighan-and-ritchie-style",                 "kr"},
     {"kernighan-and-ritchie",                       "kr"},
     {"k-and-r-style",                               "kr"},

-- 
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours.  We can't possibly take such
a retrograde step."




reply via email to

[Prev in Thread] Current Thread [Next in Thread]