Ping^3: Patch: Implementation of -Wstrict-aliasing, take 2

Silvius Rus
Fri Mar 16 00:08:00 GMT 2007

Daniel Berlin checked most of the backend part of the patch already (the 
way flow sensitive points-to info is used).  There are a few more issues 
that need to be looked at:
1. Frontend modifications to the previous warning mechanism (small 
modifications in files c-common.c, c-common.h, c-opts.c, c-typeck.c, 
flags.h, opts.c, cp/typeck.c).
2. The way the actual warning message is issued in the backend (in file 
3. Integration of frontend and backend modifications, and coding style.

Thank you!

Original post:

Changes from take 1

I believe the patch addresses all the comments and recommendations.  The 
changes from the previous submission are:
1. Keep front end checks and avoid duplicates between frontend and backend.
2. Replace the --param based interface with warning levels.

Patch Description

The proposed patch adds an implementation of -Wstrict-aliasing in the
backend, and updates the -Wstrict-aliasing interface accordingly.

The current implementation of -Wstrict-aliasing looks at a single 
at once.  Although it does OK given the limited scope, it has several 
First, it produces false positives, i.e., it warns when it should not.
For instance, it warns about pointer conversions even when the pointers
are never dereferenced.
Second, it has many false negatives, i.e., it does not warn when it should.
It does not warn when an address is not taken, for instance:
float *f = ...
int *i = (int*)f;
*f = ...
... = *i

The proposed implementation lives in the backend and is based on 
points-to information, which is computed by analyzing the entire source of
each function.  It is not perfect (the problem is undecidable), but it 
in both directions: it checks whether pointers get dereferenced and
it detects aliasing across multiple statements.

The frontend was also modified to work better with the new analysis in the
backend (match levels, avoid duplicates).

Here is a brief description of proposed warning levels.  The default is 3.
1: All pointer conversions are flagged if the source and
 destination types are incompatible according to
 alias_sets_might_conflict_p.  Runs in frontend only.
2: All pointer conversions from "address taken" are flagged if the source
 and destination types are incompatible according to
 alias_sets_might_conflict_p.  Runs in frontend only.
3: Immediate dereferences of conversions from "address taken" are flagged
 if the source and destination are incompatible according to
 alias_sets_conflict_p.  This takes place in the frontend.
 Also, warn about aliased named objects that are referenced when
 their types are incompatible according to alias_sets_conflict_p.
 This runs in the backend.
4: Same as 3, but also include analysis of unnamed objects, such as those
 created by calls to malloc.

More information about the benefits and limitations of the new 
can be found in file gcc/tree-ssa-alias-warnings.c

Bootstrapping and Testing

The patch was tested by bootstrapping on i686-linux and
by running 'make -k check'.
I could not build the fortran frontend with or without the patch,
but did build c,c++,objc,java successfully.
There was no difference in diagnostics without and with the patch.


2007-02-14  Silvius Rus  <>

  * (OBJS-common): Add tree-ssa-alias-warnings.o.
  * c-common.c (strict_aliasing_warning): Modify -Wstrict-aliasing logic.
  * c-common.h (strict_aliasing_warning): Change return type.
  * c-opts.c (c_common_handle_option): Add call to set_Wstrict_aliasing.
  * c-typeck.c (build_indirect_ref): Add call to strict_aliasing_warning.
  (build_c_cast): Condition call to strict_aliasing_warning.
  * doc/invoke.texi: Update description of -Wstrict-aliasing[=n].
  * flags.h (set_Wstrict_aliasing): Declare.
  * opts.c (set_Wstrict_alising): Define, add call to.
  * tree-flow.h (strict_aliasing_warning_backend): Declare.
  * tree-ssa-alias-warnings.c: New file.
  * tree-ssa-alias.c (compute_may_aliases): Add call to


2007-02-14  Silvius Rus  <>

  * cp/typeck.c (build_indirect_ref): Add call to strict_aliasing_warning.
  (build_reinterpret_cast_1): Condition call to


2007-02-14  Silvius Rus  <>

  * gcc.dg/Wstrict-aliasing-bogus-const-ptr-nonconst-ptr.c: New test.
  * gcc.dg/Wstrict-aliasing-bogus-never-dereferenced.c: New test.
  * gcc.dg/Wstrict-aliasing-bogus-struct-included.c: New test.
  * gcc.dg/Wstrict-aliasing-converted-assigned.c: New test.
  * gcc.dg/Wstrict-aliasing-float-ptr-int-obj.c: New test.
  * gc.dg/Wstrict-aliasing-malloc.c: New test.
  * gcc.dg/alias-1.c: Update option: -Wstrict-aliasing=2.
  * gcc.dg/alias-9.c: Update option: -Wstrict-aliasing=2.
  * g++.dg/warn/Wstrict-aliasing-7.C: Update option: -Wstrict-aliasing=2.
  * g++.dg/warn/Wstrict-aliasing-bogus-base-derived.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-char-1.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-const.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-nested-arrays.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-signed-unsigned.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-struct-included.C: New test.
  * g++.dg/warn/Wstrict-aliasing-bogus-union.C: New test.
  * g++.dg/warn/Wstrict-aliasing-float-ref-int-obj.C: New test.
  * g++.dg/warn/Wstrict-aliasing-malloc.C: New test.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: wstrict-aliasing-take2.patch
Type: text/x-patch
Size: 58070 bytes
Desc: not available
URL: <>

More information about the Gcc-patches mailing list