On Wed, Apr 27, 2022 at 09:00:51AM +0200, Richard Biener wrote:
> > > 2022-04-25  Richard Biener  <rguent...@suse.de>
> > > 
> > >   PR middle-end/104492
> > >   * gimple-ssa-warn-access.cc
> > >   (pass_waccess::warn_invalid_pointer): Exclude equality compare
> > >   diagnostics for all kind of invalidations.
> > > 
> > >   * c-c++-common/Wdangling-pointer.c: Adjust for changed
> > >   suppression.
> > >   * c-c++-common/Wdangling-pointer-2.c: Likewise.
> > 
> > I spoke with Martin on IRC and his comment was that this is ok
> > but should be accompanied with a doc/invoke.texi change that clarifies
> > that behavior in the documentation.
> > I think that is a reasonable request.
> 
> Like so?
> 
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 07b440190c3..4decbf84a43 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -8642,8 +8642,10 @@ Warn about uses of pointers (or C++ references) to 
> objects with automatic
>  storage duration after their lifetime has ended.  This includes local
>  variables declared in nested blocks, compound literals and other unnamed
>  temporary objects.  In addition, warn about storing the address of such
> -objects in escaped pointers.  The warning is enabled at all optimization
> -levels but may yield different results with optimization than without.
> +objects in escaped pointers.  As exception we do not warn when pointers
> +are used in equality compares after the lifetime of the storage they 
> point
> +to ended.  The warning is enabled at all optimization levels but may 
> yield
> +different results with optimization than without.
>  
>  @table @gcctabopt
>  @item -Wdangling-pointer=1

Reading the patch again, I don't think the above is what the patch does,
but furthermore, I'm not sure the patch is right.

Before your change, the code was about 2 warnings, -Wuse-after-free=
with levels 0 (well, -Wno-use-after-free), 1, 2, 3 where 3 is enabling
equality and the warning is done when the invalidating statement is
a call.

Then there is code for the -Wdangling-pointer= warning with levels 0 (well,
-Wno-dangling-pointer), 1, 2.

Your change moves the -Wuse-after-free= stanza for both warnings including
-Wuse-after-free= suppressions for both warnings and kills the
-Wdangling-pointer= stanza.  I think that means there would be no
difference between -Wdangling-pointer=1 and -Wdangling-pointer=2 anymore
and whether the warning is given for the maybe case (or equality case)
would be instead determined by -Wuse-after-free= level.

I'd say the right thing would be to keep the -Wuse-after-free= stuff as is
and just adjust
-  if ((maybe && warn_dangling_pointer < 2)
+  if ((equality && warn_dangling_pointer < 3)
+      || (maybe && warn_dangling_pointer < 2)
       || warning_suppressed_p (use_stmt, OPT_Wdangling_pointer_))
     return;
i.e. basically introduce -Wdangling-pointer=3 level.  That would need
 Wdangling-pointer=
-C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_dangling_pointer) 
Warning LangEnabledBy(C ObjC C++ ObjC++,Wall, 2, 0) IntegerRange(0, 2)
+C ObjC C++ ObjC++ Joined RejectNegative UInteger Var(warn_dangling_pointer) 
Warning LangEnabledBy(C ObjC C++ ObjC++,Wall, 2, 0) IntegerRange(0, 3)
change, documentation what the -Wdangling-pointer=3 level means in
invoke.texi (similar to -Wuse-after-free=3 documentation?) and another
testcase with -Wdangling-pointer=3 in dg-options where it warns also
about equality.

        Jakub

Reply via email to