This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH RFA: Initialize variable


"Tobias SchlÃter" <iant@google.com> writes:

> How about rewriting this as
>    has_ts = (c->initializer->ts.cl
> 	     && c->initializer->ts.cl->length_from_typespec);
>
>    /* Remember the length of the first element ...  */
>    if (ctor)
>      {
>         gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
> 	gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
> 	first_len = ctor->expr->value.character.length;
>      }
>
>    for (; ctor; ctor = ctor->next)
>      {
>         if (ctor->expr->expr_type == EXPR_CONSTANT)
>           ...
> Looks much cleaner to me, except for the fact that now ctor != NULL is
> established twice.

How about this patch?  Bootstrapped and tested on
x86_64-unknown-linux-gnu.

Ian


2009-06-16  Ian Lance Taylor  <iant@google.com>

	* decl.c (build_struct): Rewrite loop over constructor elements.


Index: fortran/decl.c
===================================================================
--- fortran/decl.c	(revision 148544)
+++ fortran/decl.c	(working copy)
@@ -1435,28 +1435,26 @@ build_struct (const char *name, gfc_char
 	  bool has_ts;
 	  gfc_constructor *ctor = c->initializer->value.constructor;
 
-	  bool first = true;
-	  int first_len;
-
 	  has_ts = (c->initializer->ts.cl
 		    && c->initializer->ts.cl->length_from_typespec);
 
-	  for (; ctor; ctor = ctor->next)
+	  if (ctor)
 	    {
-	      /* Remember the length of the first element for checking that
-		 all elements *in the constructor* have the same length.  This
-		 need not be the length of the LHS!  */
-	      if (first)
+	      int first_len;
+
+	      /* Remember the length of the first element for checking
+		 that all elements *in the constructor* have the same
+		 length.  This need not be the length of the LHS!  */
+	      gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
+	      gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
+	      first_len = ctor->expr->value.character.length;
+
+	      for (; ctor; ctor = ctor->next)
 		{
-		  gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
-		  gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
-		  first_len = ctor->expr->value.character.length;
-		  first = false;
+		  if (ctor->expr->expr_type == EXPR_CONSTANT)
+		    gfc_set_constant_character_len (len, ctor->expr,
+						    has_ts ? -1 : first_len);
 		}
-
-	      if (ctor->expr->expr_type == EXPR_CONSTANT)
-		gfc_set_constant_character_len (len, ctor->expr,
-						has_ts ? -1 : first_len);
 	    }
 	}
     }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]