Commit a64559a5 by Nathan Sidwell Committed by Nathan Sidwell

class.c (maybe_warn_about_overly_private_class): Ignore public copy ctors.

	* class.c (maybe_warn_about_overly_private_class): Ignore public
	copy ctors.

	* g++.dg/warn/ctor-dtor-privacy-3.C: New.

From-SVN: r250281
parent 619018d4
2017-07-17 Nathan Sidwell <nathan@acm.org> 2017-07-17 Nathan Sidwell <nathan@acm.org>
* class.c (maybe_warn_about_overly_private_class): Ignore public
copy ctors.
* class.c (type_has_user_declared_move_constructor, * class.c (type_has_user_declared_move_constructor,
type_has_user_declared_move_assign): Combine into ... type_has_user_declared_move_assign): Combine into ...
(classtype_has_user_move_assign_or_move_ctor_p): ... this new function. (classtype_has_user_move_assign_or_move_ctor_p): ... this new function.
......
...@@ -2240,10 +2240,10 @@ maybe_warn_about_overly_private_class (tree t) ...@@ -2240,10 +2240,10 @@ maybe_warn_about_overly_private_class (tree t)
/* Warn about classes that have private constructors and no friends. */ /* Warn about classes that have private constructors and no friends. */
if (TYPE_HAS_USER_CONSTRUCTOR (t) if (TYPE_HAS_USER_CONSTRUCTOR (t)
/* Implicitly generated constructors are always public. */ /* Implicitly generated constructors are always public. */
&& (!CLASSTYPE_LAZY_DEFAULT_CTOR (t) && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
|| !CLASSTYPE_LAZY_COPY_CTOR (t)))
{ {
bool nonprivate_ctor = false; bool nonprivate_ctor = false;
tree copy_or_move = NULL_TREE;
/* If a non-template class does not define a copy /* If a non-template class does not define a copy
constructor, one is defined for it, enabling it to avoid constructor, one is defined for it, enabling it to avoid
...@@ -2260,13 +2260,15 @@ maybe_warn_about_overly_private_class (tree t) ...@@ -2260,13 +2260,15 @@ maybe_warn_about_overly_private_class (tree t)
else else
for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
!nonprivate_ctor && iter; ++iter) !nonprivate_ctor && iter; ++iter)
/* Ideally, we wouldn't count copy constructors (or, in if (TREE_PRIVATE (*iter))
fact, any constructor that takes an argument of the class continue;
type as a parameter) because such things cannot be used else if (copy_fn_p (*iter) || move_fn_p (*iter))
to construct an instance of the class unless you already /* Ideally, we wouldn't count any constructor that takes
have one. But, for now at least, we're more an argument of the class type as a parameter, because
generous. */ such things cannot be used to construct an instance of
if (! TREE_PRIVATE (*iter)) the class unless you already have one. */
copy_or_move = *iter;
else
nonprivate_ctor = true; nonprivate_ctor = true;
if (!nonprivate_ctor) if (!nonprivate_ctor)
...@@ -2274,6 +2276,10 @@ maybe_warn_about_overly_private_class (tree t) ...@@ -2274,6 +2276,10 @@ maybe_warn_about_overly_private_class (tree t)
warning (OPT_Wctor_dtor_privacy, warning (OPT_Wctor_dtor_privacy,
"%q#T only defines private constructors and has no friends", "%q#T only defines private constructors and has no friends",
t); t);
if (copy_or_move)
inform (DECL_SOURCE_LOCATION (copy_or_move),
"%q#D is public, but requires an existing %q#T object",
copy_or_move, t);
return; return;
} }
} }
......
2017-07-17 Nathan Sidwell <nathan@acm.org>
* g++.dg/warn/ctor-dtor-privacy-3.C: New.
2017-07-17 Bernd Edlinger <bernd.edlinger@hotmail.de> 2017-07-17 Bernd Edlinger <bernd.edlinger@hotmail.de>
* lib/gcc-dg.exp: Increase expect's match buffer size. * lib/gcc-dg.exp: Increase expect's match buffer size.
......
// { dg-do compile { target c++11 } }
// { dg-additional-options "-Wctor-dtor-privacy" }
class X // { dg-message "only defines private" }
{
public:
X (X const &); // { dg-message "requires an existing" }
};
class Y // { dg-message "only defines private" }
{
public:
Y (Y &&); // { dg-message "requires an existing" }
};
class Z
{
public:
Z (int);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment