[35/40] sim/sh: Initialize some variables

Message ID c397f9132c484071c54fc01aa7dd8c1d5fe06490.1666258361.git.research_trasio@irq.a4lg.com
State Committed
Headers
Series sim+gdb: Suppress warnings if built with Clang (big batch 1) |

Commit Message

Tsukasa OI Oct. 20, 2022, 9:32 a.m. UTC
  Clang generates a warning if a variable may be used uninitialized on some
cases ("-Wsometimes-uninitialized").  On the default configuration, it
causes a build failure (unless "--disable-werror" is specified).

The cause of this error, $(builddir)/sim/sh/ppi.c is generated from
$(srcdir)/sim/sh/gencode.c.  Clang will detect the variable res may be used
uninitialized when used on some cases.

Likewise, GCC generates a warning if a variable may be used uninitialized
on some cases ("-Wmaybe-uninitialized").

GCC will detect variables res, res_grd, carry, overflow and greater_equal
may be used uninitialized on some cases.  Despite that GCC will not cause a
build failure even when "--enable-werror" is specified, it would be better
to fix this as well since the cause is in the same function.
---
 sim/sh/gencode.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Mike Frysinger Oct. 23, 2022, 3:19 p.m. UTC | #1
On 20 Oct 2022 09:32, Tsukasa OI wrote:
> Clang generates a warning if a variable may be used uninitialized on some
> cases ("-Wsometimes-uninitialized").  On the default configuration, it
> causes a build failure (unless "--disable-werror" is specified).
> 
> The cause of this error, $(builddir)/sim/sh/ppi.c is generated from
> $(srcdir)/sim/sh/gencode.c.  Clang will detect the variable res may be used
> uninitialized when used on some cases.
> 
> Likewise, GCC generates a warning if a variable may be used uninitialized
> on some cases ("-Wmaybe-uninitialized").
> 
> GCC will detect variables res, res_grd, carry, overflow and greater_equal
> may be used uninitialized on some cases.  Despite that GCC will not cause a
> build failure even when "--enable-werror" is specified, it would be better
> to fix this as well since the cause is in the same function.
> ---
>  sim/sh/gencode.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> --- a/sim/sh/gencode.c
> +++ b/sim/sh/gencode.c
> @@ -3257,8 +3257,8 @@ ppi_gensim (void)
>    printf ("  static char const u_tab[] = { 8, 10,  7,  5};\n");
>    printf ("\n");
>    printf ("  int z;\n");
> -  printf ("  int res, res_grd;\n");
> -  printf ("  int carry, overflow, greater_equal;\n");
> +  printf ("  int res = 0, res_grd = 0;\n");
> +  printf ("  int carry = 0, overflow = 0, greater_equal = 0;\n");

i've left this warning in because i don't know what the right answer is, and
i'm not inclined to dig into the ISA to find out.  simply initializing the
variables to 0 might mean we're masking real bugs where we should be loading
the state from the current CPU registers instead.
-mike
  

Patch

diff --git a/sim/sh/gencode.c b/sim/sh/gencode.c
index 5eb7caf2589..03979695b08 100644
--- a/sim/sh/gencode.c
+++ b/sim/sh/gencode.c
@@ -3257,8 +3257,8 @@  ppi_gensim (void)
   printf ("  static char const u_tab[] = { 8, 10,  7,  5};\n");
   printf ("\n");
   printf ("  int z;\n");
-  printf ("  int res, res_grd;\n");
-  printf ("  int carry, overflow, greater_equal;\n");
+  printf ("  int res = 0, res_grd = 0;\n");
+  printf ("  int carry = 0, overflow = 0, greater_equal = 0;\n");
   printf ("\n");
   printf ("  switch (ppi_table[iword >> 4]) {\n");