Bug 42488 - [4.4 only] spurious strict-aliasing warning
Summary: [4.4 only] spurious strict-aliasing warning
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2009-12-24 05:11 UTC by Mike Jarvis
Modified: 2009-12-29 17:59 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.5.0 4.3.2
Known to fail: 4.4.2
Last reconfirmed: 2009-12-29 17:59:50


Attachments
source file (218 bytes, text/plain)
2009-12-24 05:12 UTC, Mike Jarvis
Details
preprocessed source code (63.42 KB, text/plain)
2009-12-24 05:12 UTC, Mike Jarvis
Details
full g++ -v output (1022 bytes, text/plain)
2009-12-24 05:13 UTC, Mike Jarvis
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Jarvis 2009-12-24 05:11:17 UTC
The following very simple program is giving me a warning with g++ 4.4.2 with -Wall (or -Wstrict-aliasing=1, 2 or 3).  I don't think it should be giving the warning, so I think this is a bug. 

Replacing std::complex with a simple user-defined complex-like class makes the warning go away, so maybe there is a bug in the complex class? 

Also, replacing the virtual inheritance with normal inheritance makes the warning go away.  

Likewise removing the A class and moving the virtual inheritance to C : virtual public B.

Or if I make the argument of foo a reference.

===========================================================
#include <complex>

struct A { 
    virtual int value() const=0; 
};

struct B : virtual public A { 
    bool isOne() const { return A::value() == 1; } 
};

struct C : public B { 
    int value() const { return 1; } 
};

struct D {
    void foo(std::complex<float> x) const; // foo is extern
};

void callFoo()
{
    C c;
    D d;
    if (c.isOne()) d.foo(0);
}
===========================================================


The warning produced is:

>g++-4 -c -O2 -Wstrict-aliasing foo.cpp 
foo.cpp: In function ‘void callFoo()’:
foo.cpp:9: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules
foo.cpp:9: note: initialized from here

This appears to me to be different from similar bugs: 41838, 39390 which reference the same warning, but maybe it is related.
Comment 1 Mike Jarvis 2009-12-24 05:12:22 UTC
Created attachment 19383 [details]
source file
Comment 2 Mike Jarvis 2009-12-24 05:12:47 UTC
Created attachment 19384 [details]
preprocessed source code
Comment 3 Mike Jarvis 2009-12-24 05:13:10 UTC
Created attachment 19385 [details]
full g++ -v output
Comment 4 Andrew Pinski 2009-12-24 05:40:21 UTC
Here is a reduced testcase without the include:
struct complex
{
  complex(float b = 0.0, float c = 0.0) { __real__ a = b; __imag__ a = c; }
  _Complex float a;
};

struct A {
   virtual int value() const=0;
};

struct B : virtual public A {
   bool isOne() const { return A::value() == 1; }
};

struct C : public B {
   int value() const { return 1; }
};

struct D {
   void foo(complex x) const;
};

void callFoo()
{
   C c;
   D d;
   if (c.isOne()) d.foo(0);
}

---- CUT ----
Note making the complex a non-POD makes the warning go away.

Also it works on the trunk.
Comment 5 H.J. Lu 2009-12-24 16:59:27 UTC
It is fixed by revision 145494:

http://gcc.gnu.org/ml/gcc-cvs/2009-04/msg00115.html