public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/48001] New: -Wuninitialized warning caught a different way for printf and ++ in loop
@ 2011-03-05 20:43 jerome.borme at gmail dot com
  2011-03-05 20:46 ` [Bug middle-end/48001] " jerome.borme at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jerome.borme at gmail dot com @ 2011-03-05 20:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48001

           Summary: -Wuninitialized warning caught a different way for
                    printf and ++ in loop
           Product: gcc
           Version: 4.5.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jerome.borme@gmail.com


I checked bug meta-bug #24639, also bug #43361 and bug #27589, but I could not
b sure this is a duplicate. The 3rd test below is the surprising one.

gcc -c -Wuninitialized test.c

/* 1st test */
int main () {
  int i, x ;
  x++ ;       // printf ("%d", x) ; would give the same result
  return 0 ;
}
-->  ‘x’ is used uninitialized in this function
This is expected, we should be incrementing x without first initialization. It
works the same for x++ and printf x.

/* 2nd test */
#include <stdio.h>
int main () {
  int i, x ;
  for (i = 0 ; i < 10 ; i++) printf ("%d", x) ;
  return 0 ;
}
--> ‘x’ may be used uninitialized in this function
I think this corresponds to bug #27589. We should not printf x without
initializing, but the compiler cannot be sure if we will actually be printed or
not before  evaluating the “for” test at runtime.

/* 3rd test */
int main () {
  int i, x ;
  for (i = 0 ; i < 10 ; i++) x++ ;
  return 0 ;
}
--> No warning at all.
This is surprising:
* Outside a loop, both printf and x++ give the same warning.
* Inside a loop, printf will give a warning, while x++ will not do it.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/48001] -Wuninitialized warning caught a different way for printf and ++ in loop
  2011-03-05 20:43 [Bug middle-end/48001] New: -Wuninitialized warning caught a different way for printf and ++ in loop jerome.borme at gmail dot com
@ 2011-03-05 20:46 ` jerome.borme at gmail dot com
  2011-03-05 21:41 ` [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop manu at gcc dot gnu.org
  2011-03-05 22:25 ` jerome.borme at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: jerome.borme at gmail dot com @ 2011-03-05 20:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48001

jerome.borme at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |24639

--- Comment #1 from jerome.borme at gmail dot com 2011-03-05 20:46:12 UTC ---
Added link to meta-


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop
  2011-03-05 20:43 [Bug middle-end/48001] New: -Wuninitialized warning caught a different way for printf and ++ in loop jerome.borme at gmail dot com
  2011-03-05 20:46 ` [Bug middle-end/48001] " jerome.borme at gmail dot com
@ 2011-03-05 21:41 ` manu at gcc dot gnu.org
  2011-03-05 22:25 ` jerome.borme at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: manu at gcc dot gnu.org @ 2011-03-05 21:41 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48001

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu.org
            Summary|-Wuninitialized warning     |inconsistent warning within
                   |caught a different way for  |loop always taken and
                   |printf and ++ in loop       |outside the loop

--- Comment #2 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2011-03-05 21:41:16 UTC ---
This is because printf has side-effects (printing) but x++ does not have any
side-effects (x is unused), therefore, gcc removes x completely in the third
testcase. I guess the loop is also removed completely. This is a too artificial
testcase, in the sense that we do not care that x is uninitialized because it
is never used for anything.

We warn for the first testcase because the mechanism for detecting "is" is
different from the mechanism for detecting "may be". The former happens before
we remove the unused x, but the latter happens after in order to not warn for
dead code.

There is also the problem that GCC cannot detect that loops are taken at least
once, which makes it say "maybe" when it should actually be "is".

To be consistent, I would say that GCC shouldn't warn in the first testcase, or
should warn as "is used" in the third testcase.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop
  2011-03-05 20:43 [Bug middle-end/48001] New: -Wuninitialized warning caught a different way for printf and ++ in loop jerome.borme at gmail dot com
  2011-03-05 20:46 ` [Bug middle-end/48001] " jerome.borme at gmail dot com
  2011-03-05 21:41 ` [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop manu at gcc dot gnu.org
@ 2011-03-05 22:25 ` jerome.borme at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: jerome.borme at gmail dot com @ 2011-03-05 22:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48001

--- Comment #3 from jerome.borme at gmail dot com 2011-03-05 22:25:23 UTC ---
Here is a more complete part of a program where I found the problem. The
following code will allocates 3 vectors fx, fy, fz with specific length, then
call a function to display the averaged value of the fx, fy and fz, and the
averaged value of sqrt (fx^2+fy^2+fz^2). In the even more complete version,
maximum and minimum values of fx, fy, fz and sqrt (...) are also computed.
After calculation, values are displayed.

The initialization of sum_f has been forgotten, but no warning is issued,
although sum_f is used in the loop.

(Actually for some reason when I run this test, sum_f seems to have 0 even if
it is not written there, but I believe this is coincidence, because in the real
calculation code the displayed value was wrong.)

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100

void average (int nmax, double *fx, double *fy, double *fz)  {
  double sum_fx = 0, sum_fy = 0, sum_fz = 0, cur_f = 0, sum_f ;
  long int i ;
  for (i = 0 ; i < nmax ; i++) {
    sum_fx += fx[i] ;
    sum_fy += fy[i] ;
    sum_fz += fz[i] ;
    cur_f  = pow ((fx[i] * fx[i]) + (fy[i] * fy[i]) + (fz[i] * fz[i]), 0.5) ;
    sum_f  += cur_f ;
  }
  printf ("# Average field : %g %g %g %g\n", sum_fx/nmax, sum_fy/nmax,
sum_fz/nmax, sum_f/nmax) ;
}

int main () {
  long int i ;
  double *fx, *fy, *fz ;
  fx = (double *) malloc (SIZE * sizeof (double)) ;
  fy = (double *) malloc (SIZE * sizeof (double)) ;
  fz = (double *) malloc (SIZE * sizeof (double)) ;
  for (i = 0 ; i < SIZE ; i++) {
    fx[i] = 1 ;
    fy[i] = 1 ;
    fz[i] = 1 ;
  }
  average (SIZE, fx, fy, fz) ;
  return 0 ;
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-03-05 22:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-05 20:43 [Bug middle-end/48001] New: -Wuninitialized warning caught a different way for printf and ++ in loop jerome.borme at gmail dot com
2011-03-05 20:46 ` [Bug middle-end/48001] " jerome.borme at gmail dot com
2011-03-05 21:41 ` [Bug middle-end/48001] inconsistent warning within loop always taken and outside the loop manu at gcc dot gnu.org
2011-03-05 22:25 ` jerome.borme at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).