From a599af84b2170d8d66385674ef59283ac7567beb Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Thu, 20 Sep 2018 01:56:58 +0000 Subject: [PATCH] Add -Wclass-conversion. * c.opt (Wclass-conversion): New. * decl.c (grok_op_properties): Change a warning from -Wconversion to -Wclass-conversion. Make it print the types. * doc/invoke.texi: Document -Wclass-conversion. * g++.dg/conversion/op4.C: Add dg-warning. * g++.dg/warn/Wclass-conversion1.C: New test. * g++.dg/warn/Wclass-conversion2.C: New test. * g++.dg/warn/Wconversion5.C: Remove file. * g++.dg/warn/conversion-function-1.C: Use -Wno-class-converison. * g++.old-deja/g++.bugs/900215_01.C: Adjust dg-warning. * g++.old-deja/g++.jason/conversion5.C: Likewise. From-SVN: r264438 --- gcc/ChangeLog | 4 +++ gcc/c-family/ChangeLog | 4 +++ gcc/c-family/c.opt | 4 +++ gcc/cp/ChangeLog | 6 +++++ gcc/cp/decl.c | 26 ++++++++++--------- gcc/doc/invoke.texi | 8 ++++++ gcc/testsuite/ChangeLog | 10 +++++++ gcc/testsuite/g++.dg/conversion/op4.C | 2 +- .../g++.dg/warn/Wclass-conversion1.C | 19 ++++++++++++++ .../g++.dg/warn/Wclass-conversion2.C | 20 ++++++++++++++ gcc/testsuite/g++.dg/warn/Wconversion5.C | 20 -------------- .../g++.dg/warn/conversion-function-1.C | 2 +- .../g++.old-deja/g++.bugs/900215_01.C | 2 +- .../g++.old-deja/g++.jason/conversion5.C | 2 +- 14 files changed, 93 insertions(+), 36 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wclass-conversion1.C create mode 100644 gcc/testsuite/g++.dg/warn/Wclass-conversion2.C delete mode 100644 gcc/testsuite/g++.dg/warn/Wconversion5.C diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 729fb57fe85..728799d70a6 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2018-09-19 Marek Polacek + + * doc/invoke.texi: Document -Wclass-conversion. + 2018-09-19 John David Anglin * config/pa/pa.c (pa_adjust_priority): Delete. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index f2ec3942416..8fc773cac2e 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,7 @@ +2018-09-19 Marek Polacek + + * c.opt (Wclass-conversion): New. + 2018-09-17 David Malcolm * c-format.c (range_label_for_format_type_mismatch::get_text): diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 092ec940d86..43d1d27ac00 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -850,6 +850,10 @@ Wnon-template-friend C++ ObjC++ Var(warn_nontemplate_friend) Init(1) Warning Warn when non-templatized friend functions are declared within a template. +Wclass-conversion +C++ ObjC++ Var(warn_class_conversion) Init(1) Warning +Warn when a conversion function will never be called due to the type it converts to. + Wclass-memaccess C++ ObjC++ Var(warn_class_memaccess) Warning LangEnabledBy(C++ ObjC++, Wall) Warn for unsafe raw memory writes to objects of class types. diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 546943c9a0e..75286d53fdb 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-09-19 Marek Polacek + + Add -Wclass-conversion. + * decl.c (grok_op_properties): Change a warning from -Wconversion to + -Wclass-conversion. Make it print the types. + 2018-09-19 Paolo Carlini PR c++/87324 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 503b433cbd1..2d9d56ef6e1 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -13544,7 +13544,7 @@ grok_op_properties (tree decl, bool complain) /* Warn about conversion operators that will never be used. */ if (IDENTIFIER_CONV_OP_P (name) && ! DECL_TEMPLATE_INFO (decl) - && warn_conversion) + && warn_class_conversion) { tree t = TREE_TYPE (name); int ref = TYPE_REF_P (t); @@ -13553,27 +13553,29 @@ grok_op_properties (tree decl, bool complain) t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); if (VOID_TYPE_P (t)) - warning_at (loc, OPT_Wconversion, "conversion to void " - "will never use a type conversion operator"); + warning_at (loc, OPT_Wclass_conversion, "converting %qT to % " + "will never use a type conversion operator", class_type); else if (class_type) { if (same_type_ignoring_top_level_qualifiers_p (t, class_type)) - warning_at (loc, OPT_Wconversion, + warning_at (loc, OPT_Wclass_conversion, ref - ? G_("conversion to a reference to the same type " + ? G_("converting %qT to a reference to the same type " "will never use a type conversion operator") - : G_("conversion to the same type " - "will never use a type conversion operator")); + : G_("converting %qT to the same type " + "will never use a type conversion operator"), + class_type); /* Don't force t to be complete here. */ else if (MAYBE_CLASS_TYPE_P (t) && COMPLETE_TYPE_P (t) && DERIVED_FROM_P (t, class_type)) - warning_at (loc, OPT_Wconversion, + warning_at (loc, OPT_Wclass_conversion, ref - ? G_("conversion to a reference to a base class " - "will never use a type conversion operator") - : G_("conversion to a base class " - "will never use a type conversion operator")); + ? G_("converting %qT to a reference to a base class " + "%qT will never use a type conversion operator") + : G_("converting %qT to a base class %qT " + "will never use a type conversion operator"), + class_type, t); } } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 685c211e176..aab5fcec35a 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -237,6 +237,7 @@ in the following sections. -Weffc++ -Wstrict-null-sentinel -Wtemplates @gol -Wno-non-template-friend -Wold-style-cast @gol -Woverloaded-virtual -Wno-pmf-conversions @gol +-Wno-class-conversion -Wno-terminate @gol -Wsign-promo -Wvirtual-inheritance} @item Objective-C and Objective-C++ Language Options @@ -3367,6 +3368,13 @@ use the STL. One may also use using directives and qualified names. @opindex Wno-terminate Disable the warning about a throw-expression that will immediately result in a call to @code{terminate}. + +@item -Wno-class-conversion @r{(C++ and Objective-C++ only)} +@opindex Wno-class-conversion +@opindex Wclass-conversion +Disable the warning about the case when a conversion function converts an +object to the same type, to a base class of that type, or to void; such +a conversion function will never be called. @end table @node Objective-C and Objective-C++ Dialect Options diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f3b55d157f8..25f61d2f7f1 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,13 @@ +2018-09-19 Marek Polacek + + * g++.dg/conversion/op4.C: Add dg-warning. + * g++.dg/warn/Wclass-conversion1.C: New test. + * g++.dg/warn/Wclass-conversion2.C: New test. + * g++.dg/warn/Wconversion5.C: Remove file. + * g++.dg/warn/conversion-function-1.C: Use -Wno-class-converison. + * g++.old-deja/g++.bugs/900215_01.C: Adjust dg-warning. + * g++.old-deja/g++.jason/conversion5.C: Likewise. + 2018-09-19 Paolo Carlini PR c++/87324 diff --git a/gcc/testsuite/g++.dg/conversion/op4.C b/gcc/testsuite/g++.dg/conversion/op4.C index cb99a380b43..3fb850cb825 100644 --- a/gcc/testsuite/g++.dg/conversion/op4.C +++ b/gcc/testsuite/g++.dg/conversion/op4.C @@ -4,7 +4,7 @@ struct X { int x; X (int i = 0) : x (i) {} - operator X& (void) const { + operator X& (void) const { // { dg-warning "will never use" } return *(new X); } }; diff --git a/gcc/testsuite/g++.dg/warn/Wclass-conversion1.C b/gcc/testsuite/g++.dg/warn/Wclass-conversion1.C new file mode 100644 index 00000000000..2599a53dbeb --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wclass-conversion1.C @@ -0,0 +1,19 @@ +// PR c++/87357 +// { dg-do compile } + +struct B { }; + +struct X : public B { + operator X(); // { dg-warning "3:converting .X. to the same type will never use a type conversion operator" } + operator X&(); // { dg-warning "3:converting .X. to a reference to the same type will never use a type conversion operator" } + operator X() const; // { dg-warning "3:converting .X. to the same type will never use a type conversion operator" } + operator const X(); // { dg-warning "3:converting .X. to the same type will never use a type conversion operator" } + + operator B(); // { dg-warning "3:converting .X. to a base class .B. will never use a type conversion operator" } + operator B&(); // { dg-warning "3:converting .X. to a reference to a base class .B. will never use a type conversion operator" } + operator B() const; // { dg-warning "3:converting .X. to a base class .B. will never use a type conversion operator" } + operator const B(); // { dg-warning "3:converting .X. to a base class .const B. will never use a type conversion operator" } + + operator void(); // { dg-warning "3:converting .X. to .void. will never use a type conversion operator" } + operator void() const; // { dg-warning "3:converting .X. to .void. will never use a type conversion operator" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wclass-conversion2.C b/gcc/testsuite/g++.dg/warn/Wclass-conversion2.C new file mode 100644 index 00000000000..b6919f0bfea --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wclass-conversion2.C @@ -0,0 +1,20 @@ +// PR c++/87357 +// { dg-do compile } +// { dg-options "-Wno-class-conversion" } + +struct B { }; + +struct X : public B { + operator X(); // { dg-bogus "3:converting .X. to the same type will never use a type conversion operator" } + operator X&(); // { dg-bogus "3:converting .X. to a reference to the same type will never use a type conversion operator" } + operator X() const; // { dg-bogus "3:converting .X. to the same type will never use a type conversion operator" } + operator const X(); // { dg-bogus "3:converting .X. to the same type will never use a type conversion operator" } + + operator B(); // { dg-bogus "3:converting .X. to a base class .B. will never use a type conversion operator" } + operator B&(); // { dg-bogus "3:converting .X. to a reference to a base class .B. will never use a type conversion operator" } + operator B() const; // { dg-bogus "3:converting .X. to a base class .B. will never use a type conversion operator" } + operator const B(); // { dg-bogus "3:converting .X. to a base class .const B. will never use a type conversion operator" } + + operator void(); // { dg-bogus "3:converting .X. to .void. will never use a type conversion operator" } + operator void() const; // { dg-bogus "3:converting .X. to .void. will never use a type conversion operator" } +}; diff --git a/gcc/testsuite/g++.dg/warn/Wconversion5.C b/gcc/testsuite/g++.dg/warn/Wconversion5.C deleted file mode 100644 index 00b1ddab188..00000000000 --- a/gcc/testsuite/g++.dg/warn/Wconversion5.C +++ /dev/null @@ -1,20 +0,0 @@ -// PR c++/87357 -// { dg-do compile } -// { dg-options "-Wconversion" } - -struct B { }; - -struct X : public B { - operator X(); // { dg-warning "3:conversion to the same type will never use a type conversion operator" } - operator X&(); // { dg-warning "3:conversion to a reference to the same type will never use a type conversion operator" } - operator X() const; // { dg-warning "3:conversion to the same type will never use a type conversion operator" } - operator const X(); // { dg-warning "3:conversion to the same type will never use a type conversion operator" } - - operator B(); // { dg-warning "3:conversion to a base class will never use a type conversion operator" } - operator B&(); // { dg-warning "3:conversion to a reference to a base class will never use a type conversion operator" } - operator B() const; // { dg-warning "3:conversion to a base class will never use a type conversion operator" } - operator const B(); // { dg-warning "3:conversion to a base class will never use a type conversion operator" } - - operator void(); // { dg-warning "3:conversion to void will never use a type conversion operator" } - operator void() const; // { dg-warning "3:conversion to void will never use a type conversion operator" } -}; diff --git a/gcc/testsuite/g++.dg/warn/conversion-function-1.C b/gcc/testsuite/g++.dg/warn/conversion-function-1.C index 878011cf328..890719b3477 100644 --- a/gcc/testsuite/g++.dg/warn/conversion-function-1.C +++ b/gcc/testsuite/g++.dg/warn/conversion-function-1.C @@ -1,6 +1,6 @@ // Copyright (C) 2003 Free Software Foundation // Contributed by Gabriel Dos Reis -// { dg-options "-Wno-conversion" } +// { dg-options "-Wno-class-conversion" } struct A { operator A&(); diff --git a/gcc/testsuite/g++.old-deja/g++.bugs/900215_01.C b/gcc/testsuite/g++.old-deja/g++.bugs/900215_01.C index 0cd9b321e3e..fd075685cec 100644 --- a/gcc/testsuite/g++.old-deja/g++.bugs/900215_01.C +++ b/gcc/testsuite/g++.old-deja/g++.bugs/900215_01.C @@ -24,7 +24,7 @@ struct struct0 { - operator void (); // { dg-warning "3:conversion to void will never use a type conversion operator" } + operator void (); // { dg-warning "3:converting .struct0. to .void. will never use a type conversion operator" } }; int exit_status = 1; diff --git a/gcc/testsuite/g++.old-deja/g++.jason/conversion5.C b/gcc/testsuite/g++.old-deja/g++.jason/conversion5.C index a9531a6d209..7f04de48378 100644 --- a/gcc/testsuite/g++.old-deja/g++.jason/conversion5.C +++ b/gcc/testsuite/g++.old-deja/g++.jason/conversion5.C @@ -3,7 +3,7 @@ struct A { }; struct B: public A { A a; - operator A () { return a; } // { dg-warning "3:conversion to a base class will never use a type conversion operator" } + operator A () { return a; } // { dg-warning "3:converting .B. to a base class .A. will never use a type conversion operator" } }; void f (const A&); void g() -- 2.30.2