[forwarded from http://bugs.debian.org/276843]

- gcc-3.4.3 -O2 -Wall foo.c doesn't print the warning
  foo.c: In function `foo':
  foo.c:2: warning: control reaches end of non-void function
  gcc-3.3.4 does.

- gcc-4.0 does print a warning, but the warning in 3.3 was
  more meaningful for the user:
  foo.c: In function 'main':
  foo.c:6: warning: '({anonymous})' is used uninitialized in this function

- for the bar.c example, gcc-3.3 and gcc-3.4 don't print any warning,
  4.0 prints
  foo.c: In function 'main':
  foo.c:6: warning: '({anonymous})' is used uninitialized in this function
  but probaly could do better.


Matthias


Original report follows:

Package: gcc-3.4
Version: 3.4.2-2
Severity: normal


It seems that if a function in a C program is declared "static", the
type of the function is not void, the programmer forgets to add a
"return" statement, and one compiles the program with gcc using -O2,
gcc gives no warning.

Also it seems that -O3 gives a warning only if there is no return
statement at all. It does not properly detect cases where control can
reach end of function.

Sample session follows.

    % cat foo.c          
    static int foo(void)
    {}

    int main(void)
    {
        return foo();
    }
    % cat bar.c
    static int foo(void)
    {
        if (0) {
            return 1;
        }
    }

    int main(void)
    {
        return foo();
    }
    % gcc -Wall foo.c
    foo.c: In function `foo':
    foo.c:2: warning: control reaches end of non-void function
    % gcc -Wall -O1 foo.c
    foo.c: In function `foo':
    foo.c:2: warning: control reaches end of non-void function
    % gcc -Wall -O2 foo.c
    % gcc -Wall -O3 foo.c
    foo.c: In function `foo':
    foo.c:2: warning: no return statement in function returning non-void
    % gcc -Wall bar.c    
    bar.c: In function `foo':
    bar.c:6: warning: control reaches end of non-void function
    % gcc -Wall -O1 bar.c
    bar.c: In function `foo':
    bar.c:6: warning: control reaches end of non-void function
    % gcc -Wall -O2 bar.c
    % gcc -Wall -O3 bar.c
    % gcc --version
    gcc (GCC) 3.4.2 (Debian 3.4.2-2)
    Copyright (C) 2004 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-- 
           Summary: [3.4 4.0 regression] -O2 disables warning "control
                    reaches end of non-void function"
           Product: gcc
           Version: 3.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: debian-gcc at lists dot debian dot org
                CC: gcc-bugs at gcc dot gnu dot org


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

Reply via email to