On Mon, Aug 18, 2014 at 9:35 PM, lin zuojian <manjian2...@gmail.com> wrote: > Hi, > I want to add a warning to inlining function when violate strict > aliasing. > See bug: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60546 > For details. > > * tree-inline.c (global): Add new include to c-family/c-common.h. To > access strict aliasing check. > (setup_one_parameter): Add strict aliasing check. > * testsuite/gcc.dg/Wstrict-aliasing-inline-parameter.C:New test. > --- > gcc/ChangeLog | 5 +++ > .../gcc.dg/Wstrict-aliasing-inline-parameter.C | 42 > ++++++++++++++++++++++ > gcc/tree-inline.c | 6 ++++ > 3 files changed, 53 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/Wstrict-aliasing-inline-parameter.C > > diff --git a/gcc/ChangeLog b/gcc/ChangeLog > index 8ccdde2..f584567 100644 > --- a/gcc/ChangeLog > +++ b/gcc/ChangeLog > @@ -1,3 +1,8 @@ > +2014-08-19 Lin Zuojian <manjian2...@gmail.com> > + * tree-inline.c (global): Add new include to c-family/c-common.h. To > + access strict aliasing check. > + (setup_one_parameter): Add strict aliasing check. > + * testsuite/gcc.dg/Wstrict-aliasing-inline.C: New test. > 2014-08-19 David Malcolm <dmalc...@redhat.com> > > * basic-block.h (BB_HEAD): Convert to a function. Strengthen the > diff --git a/gcc/testsuite/gcc.dg/Wstrict-aliasing-inline-parameter.C > b/gcc/testsuite/gcc.dg/Wstrict-aliasing-inline-parameter.C > new file mode 100644 > index 0000000..9a667be > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/Wstrict-aliasing-inline-parameter.C > @@ -0,0 +1,42 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -Wstrict-aliasing=10 -fstrict-aliasing" } */ > + > +struct A > +{ > + int a; > + int b; > + int c; > +}; > + > +static inline int hash2(const unsigned short* change2, int len) > +{ > + int result = 0; > + for(int i = 0; i < len; ++i) { > + > + result += change2[i]; > + result ^= result << 11; > + result += result >> 17; > + > + // Force "avalanching" of final 31 bits. > + result ^= result << 3; > + result += result >> 5; > + result ^= result << 2; > + result += result >> 15; > + result ^= result << 10; > + } > + return result; > +} > + > +static inline int hash(const void* change1, int len) > +{ > + return hash2(static_cast<const unsigned short*>(change1), len / 2); > +} > + > + > +int foo(int a, int b, int c) > +{ > + struct A a_struct = {a, b, c}; > + return hash(&a_struct, sizeof(struct A)); > +} > + > +/* { dg-message "dereferencing type-punned pointer will break > strict-aliasing rules" } */ > diff --git a/gcc/tree-inline.c b/gcc/tree-inline.c > index b6ecaa4..33d7017 100644 > --- a/gcc/tree-inline.c > +++ b/gcc/tree-inline.c > @@ -22,6 +22,8 @@ along with GCC; see the file COPYING3. If not see > #include "system.h" > #include "coretypes.h" > #include "tm.h" > +/* For warn_strict_aliasing. */ > +#include "c-family/c-common.h"
You should never be including a c-family header file from tree-inline.c. if you need warn_strict_aliasing, include options.h instead. Now for strict_aliasing_warning, you should move the definition to tree.c or alias.c and move the declaration. Your patch breaks building Fortran, Ada, Java and Go front-ends as it is right now. Also how did you test your patch? Thanks, Andrew Pinski > #include "diagnostic-core.h" > #include "tree.h" > #include "stor-layout.h" > @@ -2913,6 +2915,10 @@ setup_one_parameter (copy_body_data *id, tree p, tree > value, tree fn, > } > } > > + if (warn_strict_aliasing > 2) > + if (strict_aliasing_warning (TREE_TYPE (rhs), TREE_TYPE(p), rhs)) > + warning (OPT_Wstrict_aliasing, "during inlining function %s into > function %s", fndecl_name(fn), function_name(cfun)); > + > /* Make an equivalent VAR_DECL. Note that we must NOT remap the type > here since the type of this decl must be visible to the calling > function. */ > -- > 1.9.1 > > Lin Zuojian