bug-indent
[Top][All Lists]
Advanced

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

[PATCH 05/11] Add an option to allow single-line conditionals


From: Tim Hentenaar
Subject: [PATCH 05/11] Add an option to allow single-line conditionals
Date: Mon, 15 Jun 2015 21:52:56 +0200

-slc (or --single-line-conditionals):
    Allow a conditional (such as if, else, etc.) and it's inner
    statement to reside on the same line. The default behavior is to
    force a newline and indent the inner statement.

This should fix GNU bug #42357.
---
 AUTHORS                                        |  1 +
 ChangeLog                                      |  3 ++
 doc/indent.texi                                | 42 ++++++++++++++++++++++++--
 regression/TEST                                |  5 ++-
 regression/input/single-line-conditionals.c    | 10 ++++++
 regression/standard/single-line-conditionals.c | 11 +++++++
 src/args.c                                     |  8 +++++
 src/handletoken.c                              | 12 +++++++-
 src/indent.c                                   |  9 +++++-
 src/indent.h                                   |  4 ++-
 10 files changed, 98 insertions(+), 7 deletions(-)
 create mode 100644 regression/input/single-line-conditionals.c
 create mode 100644 regression/standard/single-line-conditionals.c

diff --git a/AUTHORS b/AUTHORS
index eb3df02..ddbc6c7 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -1,3 +1,4 @@
+2015        : Tim Hentenaar
 2014 -      : Andrew Shadura, Daniel Valentine
 2001 - 2010 : David Ingamells
 1999 - 2001 : Carlo Wood
diff --git a/ChangeLog b/ChangeLog
index 151009b..f5ad166 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,9 @@
          the __gnu_inlne__ attribute on the functions in gperf.c/gperf-cc.c
        * Fixed HTML documentation generation
        * Fix regression introduced in hg revision 25c27d429590
+       * Add an option (-slc/--single-line-conditionals) to allow
+         conditionals and their inner statement to be on the same line if
+         they're not braced. This should fix GNU bug #42357.
 
 2014-04-07  Andrew Shadura  <address@hidden>
        * Don't add extra 'const' keyword to C++ programs (closes Debian bugs
diff --git a/doc/indent.texi b/doc/indent.texi
index 7d75fe9..c7f7f61 100644
--- a/doc/indent.texi
+++ b/doc/indent.texi
@@ -28,11 +28,12 @@ for Indent Version @value{VERSION}, last updated 
@value{UPDATED}.
 @c @vskip 0pt plus 1filll
 
 @c !BEGIN COPYING
-Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 2014 Free Software 
Foundation, Inc.
+Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 2014, 2015 Free Software 
Foundation, Inc.
 Copyright (C) 1995, 1996 Joseph Arceneaux.
 Copyright (C) 1999 Carlo Wood.
 Copyright (C) 2001 David Ingamells.
 Copyright (C) 2013 Łukasz Stelmach.
+Copyright (C) 2015 Tim Hentenaar.
 
 Permission is granted to make and distribute verbatim copies of
 this manual provided the copyright notice and this permission notice
@@ -62,6 +63,7 @@ original English.
 @subtitle Edition @value{EDITION}, for Indent Version @value{VERSION}
 @subtitle @value{UPDATED}
 @c !BEGIN AUTHORS
address@hidden Tim Hentenaar
 @author Carlo Wood
 @author Joseph Arceneaux
 @author Jim Kingdon
@@ -80,6 +82,8 @@ Copyright @copyright{} 2001, David Ingamells.
 
 Copyright @copyright{} 2013, Łukasz Stelmach.
 
+Copyright @copyright{} 2015, Tim Hentenaar.
+
 Permission is granted to make and distribute verbatim copies of
 this manual provided the copyright notice and this permission notice
 are preserved on all copies.
@@ -850,6 +854,30 @@ while (x);
 @end group
 @end example
 
address@hidden -slc
address@hidden --single-line-conditionals
+The @option{-slc} option allows for an unbraced conditional and its inner
+statement to appear on the same line. For example:
+
address@hidden
address@hidden
+if (x) x--;
+else x++;
address@hidden group
address@hidden example
+
address@hidden
+Without @option{-slc} that code would appear as
+
address@hidden
address@hidden
+if (x)
+  x--;
+else
+  x++;
address@hidden group
address@hidden example
+
 @kindex address@hidden
 @kindex address@hidden
 The @option{-cli} option specifies the number of spaces that case labels
@@ -1612,6 +1640,7 @@ beginning of @file{indent.texinfo} and 
@file{indent.info}, and near the
 end of @file{indent.1}.
 
 @display
+Copyright @copyright{} 2015 Tim Hentenaar.
 Copyright @copyright{} 2001 David Ingamells.
 Copyright @copyright{} 1999 Carlo Wood.
 Copyright @copyright{} 1995, 1996 Joseph Arceneaux.
@@ -2032,7 +2061,6 @@ types: ``char* p''.
 Put asterisks in pointer declarations on the right of spaces, next to
 variable names: ``char *p''. This is the default behavior.
 
-
 @item -pcs  
 @itemx --space-after-procedure-calls
 Insert a space between the name of the 
@@ -2090,6 +2118,12 @@ Indent braces of a struct, union or enum N address@hidden
 Put the @samp{*} character at the left of address@hidden
 @xref{Comments}.
 
address@hidden -slc
address@hidden --single-line-conditionals
+Allow for unbraced conditionals (@code{if}, @code{else}, etc.) to have
+their inner statement on the same address@hidden
address@hidden
+
 @item -sob
 @itemx --swallow-optional-blank-lines
 Swallow optional blank address@hidden
@@ -2216,7 +2250,8 @@ the corresponding short option.
 \line{ --preprocessor-indentation \leaderfill      address@hidden }
 \line{ --preserve-mtime \leaderfill                -pmt\ }
 \line{ --procnames-start-lines \leaderfill         -psl\ }  
-\line{ --space-after-cast \leaderfill              -cs\ \ } 
+\line{ --single-line-conditionals \leaderfill      -slc\ }
+\line{ --space-after-cast \leaderfill              -cs\ \ }
 \line{ --space-after-for \leaderfill               -saf\ }
 \line{ --space-after-if \leaderfill                -sai\ }
 \line{ --space-after-parentheses \leaderfill       -prs\ }
@@ -2307,6 +2342,7 @@ the corresponding short option.
 --preserve-mtime                               -pmt
 --preprocessor-indentation                      address@hidden
 --procnames-start-lines                         -psl            
+--single-line-conditionals                      -slc
 --space-after-cast                              -cs             
 --space-after-for                              -saf
 --space-after-if                               -sai
diff --git a/regression/TEST b/regression/TEST
index ce9bc1d..fa312dc 100755
--- a/regression/TEST
+++ b/regression/TEST
@@ -67,7 +67,7 @@ SPECIALS="typedef-bug.c bug-hp.c bug-di.c newlined-parms.c 
indent.pro.c \
         comment-break.c cdb.c bbreak.c bad-break.c pre.c pre_lps.c pre_nlps.c 
dont-line-up-parentheses.c \
         bad-comment.c unknown-type.c unknown-type-npsl.c bug-npsl.c bug-psl.c 
do-cdw.c \
         label.c goto-1.c goto-2.c line-count.c decl_block.c pointer-pal.c 
else-comment-2-br.c else-comment-2-bl.c \
-        else-comment-2-br-ce.c preserve-newline-after-right-brace.c 
gettext-strings.c"
+        else-comment-2-br-ce.c preserve-newline-after-right-brace.c 
gettext-strings.c single-line-conditionals.c"
 
 ARGS="-bad"
 $INDENT -npro $ARGS input/bad-break.c -o output/bad-break.c
@@ -173,6 +173,9 @@ $INDENT -npro -br     input/else-comment-2.c -o 
output/else-comment-2-br.c
 $INDENT -npro -br -ce input/else-comment-2.c -o output/else-comment-2-br-ce.c
 $INDENT -npro -br -ce input/preserve-newline-after-right-brace.c -o 
output/preserve-newline-after-right-brace.c
 
+ARGS=""
+$INDENT -npro -slc input/single-line-conditionals.c -o 
output/single-line-conditionals.c
+
 ARGS="-kr -cp0 -l132 -lps -br -psl"
 $INDENT -npro $ARGS input/const.c -o output/const.c
 
diff --git a/regression/input/single-line-conditionals.c 
b/regression/input/single-line-conditionals.c
new file mode 100644
index 0000000..7f29b0a
--- /dev/null
+++ b/regression/input/single-line-conditionals.c
@@ -0,0 +1,10 @@
+int aa(int b)
+{
+       int a = 1;
+       if (a == 1) a = 1;
+       if (a == 3) a = 1;
+       else a = 2;
+
+       if (x()) y = 1;
+       if (z()) goto a;
+}
diff --git a/regression/standard/single-line-conditionals.c 
b/regression/standard/single-line-conditionals.c
new file mode 100644
index 0000000..0b68730
--- /dev/null
+++ b/regression/standard/single-line-conditionals.c
@@ -0,0 +1,11 @@
+int
+aa (int b)
+{
+  int a = 1;
+  if (a == 1) a = 1;
+  if (a == 3) a = 1;
+  else a = 2;
+
+  if (x ()) y = 1;
+  if (z ()) goto a;
+}
diff --git a/src/args.c b/src/args.c
index b5a3f12..2a7dbe4 100644
--- a/src/args.c
+++ b/src/args.c
@@ -1,4 +1,5 @@
 /** \file
+ * Copyright (c) 2015 Tim Hentenaar. All rights reserved.<br>
  * Copyright (c) 2013 Łukasz Stelmach.  All rights reserved.<br>
  * Copyright (c) 1999, 2000 Carlo Wood.  All rights reserved.<br>
  * Copyright (c) 1994 Joseph Arceneaux.  All rights reserved.<br>
@@ -197,6 +198,7 @@ static int exp_ut   = 0;
 static int exp_v    = 0;
 static int exp_version = 0;
 static int exp_par  = 0;
+static int exp_slc  = 0;
 
 /**
  * The following structure is controlled by command line parameters and
@@ -361,6 +363,8 @@ const pro_ty pro[] =
     {"ppi",     PRO_INT,                                0, ONOFF_NA, 
&settings.force_preproc_width,              &exp_ppi},
     {"pal",     PRO_BOOL,                            true,      OFF, 
&settings.pointer_align_right,              &exp_par},
     {"par",     PRO_BOOL,                            true,       ON, 
&settings.pointer_align_right,              &exp_par},
+    {"slc",     PRO_BOOL,                            false,      ON, 
&settings.allow_single_line_conditionals,   &exp_slc},
+
     /* Signify end of structure.  */
     {0,         PRO_IGN,                                0, ONOFF_NA, 0,        
                                  0}
 };
@@ -485,6 +489,8 @@ const pro_ty pro[] =
     {"ppi",     PRO_INT,                                0, ONOFF_NA, 
&settings.force_preproc_width,              &exp_ppi},
     {"pal",     PRO_BOOL,                            true,      OFF, 
&settings.pointer_align_right,              &exp_par},
     {"par",     PRO_BOOL,                            true,       ON, 
&settings.pointer_align_right,              &exp_par},
+    {"slc",     PRO_BOOL,                            false,      ON, 
&settings.allow_single_line_conditionals,   &exp_slc},
+
     /* Signify end of structure.  */
     {0,         PRO_IGN,                                0, ONOFF_NA, 0,        
                                  0}
 };
@@ -619,6 +625,8 @@ const long_option_conversion_ty option_conversions[] =
     {"preprocessor-indentation",                    "ppi"},
     {"pointer-align-right",                         "par"},
     {"pointer-align-left",                          "pal"},
+    {"single-line-conditionals",                    "slc"},
+
     /* Signify end of structure.  */
     {0,                                             0},
 };
diff --git a/src/handletoken.c b/src/handletoken.c
index 8d020bb..5b54bd9 100644
--- a/src/handletoken.c
+++ b/src/handletoken.c
@@ -539,6 +539,16 @@ static void handle_token_rparen(
 
     *(e_code++) = token[0];
 
+    /* Something is swallowing spaces after a 2nd rparen, when we allow
+     * single-line conditionals, so make sure we copy it (if there is one).
+     */
+    if (settings.allow_single_line_conditionals && *(token - 1) == ')'
+        && *(token + 1) == ' ' && *(token + 2) != '{'
+        && !parser_state_tos->paren_depth)
+    {
+        *(e_code++) = *(++token);
+    }
+
     /* check for end of if (...), or some such */
 
     if (*sp_sw && (parser_state_tos->p_l_follow == 0))
@@ -555,7 +565,7 @@ static void handle_token_rparen(
         }
 
         *sp_sw = false;
-        *force_nl = true;    /* must force newline after if */
+        *force_nl = !settings.allow_single_line_conditionals;
         parser_state_tos->last_u_d = true;  /* inform lexi that a
                                              * following operator is
                                              * unary */
diff --git a/src/indent.c b/src/indent.c
index f774696..8e49264 100644
--- a/src/indent.c
+++ b/src/indent.c
@@ -1,7 +1,8 @@
 /** \file
+ * Copyright (c) 2015 Tim Hentenaar. All rights reserved.<br>
  * Copyright (c) 1999, 2000 Carlo Wood.  All rights reserved. <br>
  * Copyright (c) 1994, 1996, 1997 Joseph Arceneaux.  All rights reserved. <br>
- * Copyright (c) 1992, 2002, 2008, 2014 Free Software Foundation, Inc.  All 
rights reserved. <br>
+ * Copyright (c) 1992, 2002, 2008, 2014, 2015 Free Software Foundation, Inc.  
All rights reserved. <br>
  *
  * Copyright (c) 1980 The Regents of the University of California. <br>
  * Copyright (c) 1976 Board of Trustees of the University of Illinois. All 
rights reserved.
@@ -531,6 +532,12 @@ static exit_values_ty indent_main_loop(
 
         flushed_nl = false;
 
+        /* Don't force a newline after an unbraced if, else, etc. */
+        if (settings.allow_single_line_conditionals &&
+            (parser_state_tos->last_token == rparen ||
+            parser_state_tos->last_token == sp_else))
+            force_nl = false;
+
         if (!search_brace(&type_code, &force_nl, &flushed_nl, &last_else,
                           &is_procname_definition, pbreak_line))
         {
diff --git a/src/indent.h b/src/indent.h
index 45a47e7..4b535a5 100644
--- a/src/indent.h
+++ b/src/indent.h
@@ -1,8 +1,9 @@
 /** \file
+ * Copyright (c) 2015 Tim Hentenaar. All rights reserved.<br>
  * Copyright (c) 2013 Łukasz Stelmach.  All rights reserved.<br>
  * Copyright (c) 1999 Carlo Wood.  All rights reserved.<br>
  * Copyright (c) 1994 Joseph Arceneaux.  All rights reserved.<br>
- * Copyright (c) 1992, 2002, 2008, 2014 Free Software Foundation, Inc.  All 
rights reserved.<br>
+ * Copyright (c) 1992, 2002, 2008, 2014, 2015 Free Software Foundation, Inc.  
All rights reserved.<br>
  *
  * Copyright (c) 1985 Sun Microsystems, Inc. <br>
  * Copyright (c) 1980 The Regents of the University of California. <br>
@@ -326,6 +327,7 @@ typedef struct user_options_st
     int expect_output_file;  /*!< Means "-o" was specified. */
     int pointer_align_right; /*!< true: "char *a", false: "char* a" */
     int gettext_strings;     /*!< true: _("...") is a string, false: it's a 
function */ 
+    int allow_single_line_conditionals; /*!< Don't indent the body of an 
unbraced if, else, etc. */
 } user_options_ty;
 
 extern user_options_ty settings;
-- 
2.3.6




reply via email to

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