Bug 39322 - Missed aliasing warning
Summary: Missed aliasing warning
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.4.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, monitored
Depends on: 39117
Blocks:
  Show dependency treegraph
 
Reported: 2009-02-27 22:20 UTC by Volker Reichelt
Modified: 2009-03-04 09:38 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-02-27 22:37:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2009-02-27 22:20:21 UTC
A IMHO the trunk issues a bogus warning for the following code snippet
when compiling it with "-O2 -Wall" using the C++ frontend:

==============================
int foo()
{
  char a[10];
  int i = *(int*)&a[4];
  int j = *(int*)(a+4);
  return i+j;
}
==============================

bug.cc: In function 'int foo()':
bug.cc:4: warning: dereferencing type-punned pointer will break strict-aliasing rules

Interestingly, the warning is given only for line 4, but not line 5.
Also the C frontend is not affected.

If there's wrong alias info involved, this might lead to wrong-code bugs.
Comment 1 Andrew Pinski 2009-02-27 22:23:58 UTC
Actually this is not a bogus aliasing warning at all.  You are accessing a character type as an int which is an aliasing violation.
Comment 2 Andrew Pinski 2009-02-27 22:25:27 UTC
We should give a warning on both lines.
Comment 3 Richard Biener 2009-02-27 22:37:29 UTC
The FE warning code doesn't warn for *((int *) &a + 4) because it doesn't
recognize the form.  This is what we get in both cases from the C frontend
and in the second case from the C++ frontend.

The PTA warning code doesn't trigger here because we do not prune a from
the points-to sets.
Comment 4 Andrew Pinski 2009-02-27 22:52:45 UTC
Related a little bit to bug 39117 or at least for the PTA side of things.
Comment 5 Volker Reichelt 2009-03-03 20:55:34 UTC
> Actually this is not a bogus aliasing warning at all.  You are accessing a
> character type as an int which is an aliasing violation.

Yeah, you're right. One can only access an int array via a char pointer, but not
the other way round. Sorry for screwing things up.