This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
A customer moving from gcc-2.9x to gcc-3.x ran into problems with -fstrict-aliasing, and was disappointed that -Wstrict-aliasing did not help. The problem is that -Wstrict-aliasing does not warn about ambiguous cases. If you have a structure that contains a char in addition to other types, then it is valid to cast a pointer to the structure type and dereference the char, but not valid to dereference one of the other types. Since a cast to this structure type might be valid, the current -Wstrict-aliasing code gives no warning at all. However, this makes it impossible for users to find some of their invalid code. I wrote a patch that warns for the ambiguous cases, adding "may" to the message, to indicate that the code may or may not be OK. This allowed the customer to find the problems in their code. Consider this testcase: struct foo { char c; char d; short s; int i; } bar; int sub1 (long long int foobar) { struct foo *tmp = (struct foo *) &foobar; return tmp->i; } short sub2 (long long int foobar) { struct foo *tmp = (struct foo *) &foobar; return tmp->c; } The function sub2 is OK, but the function sub1 is not, and gets miscompiled at -O2 on x86-linux. -Wstrict-aliasing gives no warning for either function. With the following patch, I get[wilson@leaf gcc]$ ./xgcc -B./ -O2 -Wstrict-aliasing -S tmp.c tmp.c: In function `sub1': tmp.c:11: warning: dereferencing type-punned pointer may break strict-aliasing rules tmp.c: In function `sub2': tmp.c:18: warning: dereferencing type-punned pointer may break strict-aliasing rules The warning for sub2 is a false positive, but the warning for sub1 is accurate, and allows the user to find the bug in their code. I am not sure if others think this addition is a good idea, which is why I am asking for comments. If people don't want this in -Wstrict-aliasing, then perhaps another option can be added for it, -Wmaybe-strict-aliasing perhaps? Either way, this needs a doc change which I haven't written yet. -- Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com
Attachment:
patch.warn.alias
Description: Text document
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |