Bug 70295 - [6 Regression] bogus -Wnonnull-compare for "dynamic_cast<const FuSelection *>(this) != nullptr"
Summary: [6 Regression] bogus -Wnonnull-compare for "dynamic_cast<const FuSelection *>...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P1 normal
Target Milestone: 6.0
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-18 13:56 UTC by Markus Trippelsdorf
Modified: 2016-03-22 08:29 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-03-18 00:00:00


Attachments
gcc6-pr70295.patch (619 bytes, patch)
2016-03-18 15:12 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Trippelsdorf 2016-03-18 13:56:45 UTC
trippels@CFARM-IUT-TLSE3 ~ % cat fudraw.ii
enum SdrHitKind {};
class FuSelection {
  void m_fn1(void *);
};
void FuSelection::m_fn1(void *) {
  SdrHitKind eHit;
  if (eHit && dynamic_cast<const FuSelection *>(this) != nullptr)
    ;
}
trippels@CFARM-IUT-TLSE3 ~ % g++ -c -O2 -Wnonnull-compare fudraw.ii 
fudraw.ii: In member function ‘void FuSelection::m_fn1(void*)’:
fudraw.ii:7:55: warning: nonnull argument ‘this’ compared to NULL [-Wnonnull-compare]
   if (eHit && dynamic_cast<const FuSelection *>(this) != nullptr)
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
Comment 1 Marek Polacek 2016-03-18 14:02:33 UTC
Wait, surely we don't want to warn here?
Comment 2 Markus Trippelsdorf 2016-03-18 14:19:42 UTC
The warning goes away without the "eHit &&" in the if condition.

And please note that the testcase was reduced from a big Libreoffice C++ file
with a larger class hierarchy.
Comment 3 Jakub Jelinek 2016-03-18 14:50:19 UTC
Let's make it
struct A { A (); virtual ~A (); bool foo (bool); };
struct B : virtual public A { B (); virtual ~B (); };

bool
A::foo (bool x)
{
  if (x && dynamic_cast<B *>(this) != nullptr)
    return true;
  return false;
}
then.
Comment 4 Jakub Jelinek 2016-03-18 15:12:03 UTC
Created attachment 38025 [details]
gcc6-pr70295.patch

Untested fix.
Comment 5 Jakub Jelinek 2016-03-18 18:08:39 UTC
Unfortunately that patch regresses the c-c++-common/nonnull-1.c testcase.
The problem is that finish_parenthesized_expr which sets TREE_NO_WARNING on all expressions.  Will see if setting it solely on MODIFY_EXPRs isn't enough.
Comment 6 Jakub Jelinek 2016-03-22 08:11:46 UTC
Author: jakub
Date: Tue Mar 22 08:11:14 2016
New Revision: 234392

URL: https://gcc.gnu.org/viewcvs?rev=234392&root=gcc&view=rev
Log:
	PR c++/70295
	* gimplify.c (gimplify_modify_expr): Call gimple_set_no_warning
	on assign if (*from_p) is a comparison, set it to
	TREE_NO_WARNING (*from_p).

	* c-c++-common/nonnull-1.c (func): Remove parens around cp4 != 0.
	(func2): New function for cond with parens, xfail warning for c++.
	* g++.dg/warn/Wnonnull-compare-8.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wnonnull-compare-8.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimplify.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/c-c++-common/nonnull-1.c
Comment 7 Jakub Jelinek 2016-03-22 08:29:52 UTC
Fixed.