From: Volker Reichelt Date: Thu, 25 May 2017 17:05:07 +0000 (+0000) Subject: invoke.texi (-Wcatch-value=): Document new warning option. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=63dbcd13e93578e21e0717d4990c1701d6957e3e;p=gcc.git invoke.texi (-Wcatch-value=): Document new warning option. * doc/invoke.texi (-Wcatch-value=): Document new warning option. * c.opt (Wcatch-value=): New C++ warning flag. * semantics.c (finish_handler_parms): Warn about non-reference * type catch handlers. * g++.dg/warn/Wcatch-value-1.C: New test. * g++.dg/warn/Wcatch-value-2.C: New test. * g++.dg/warn/Wcatch-value-3.C: New test. From-SVN: r248466 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 59292354056..3d14b4c8ae6 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2017-05-25 Volker Reichelt + + * doc/invoke.texi (-Wcatch-value=): Document new warning option. + 2017-05-25 Nathan Sidwell * doc/invoke.texi (--enable-languages): Update documentation. diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index c8fff24e375..818d39ba802 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,7 @@ +2017-05-25 Volker Reichelt + + * c.opt (Wcatch-value=): New C++ warning flag. + 2017-05-24 Nathan Sidwell * c=common.c (field_decl_cmp, resort_field_decl_cmp): Adjust T diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 9ad2f6e1fcc..648e0add9c6 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -388,6 +388,10 @@ Wcast-qual C ObjC C++ ObjC++ Var(warn_cast_qual) Warning Warn about casts which discard qualifiers. +Wcatch-value= +C++ ObjC++ Var(warn_catch_value) Warning Joined RejectNegative UInteger +Warn about catch handlers of non-reference type. + Wchar-subscripts C ObjC C++ ObjC++ Var(warn_char_subscripts) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn about subscripts whose type is \"char\". diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index f2df2b62611..6d71bd66b38 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2017-05-25 Volker Reichelt + + * semantics.c (finish_handler_parms): Warn about non-reference type + catch handlers. + 2017-05-25 Nathan Sidwell Reimplement unqualified namespace lookup. diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 1ba961ec226..df83d23c713 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -1323,7 +1323,28 @@ finish_handler_parms (tree decl, tree handler) } } else - type = expand_start_catch_block (decl); + { + type = expand_start_catch_block (decl); + if (warn_catch_value + && type != NULL_TREE + && type != error_mark_node + && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE) + { + tree orig_type = TREE_TYPE (decl); + if (CLASS_TYPE_P (orig_type)) + { + if (TYPE_POLYMORPHIC_P (orig_type)) + warning (OPT_Wcatch_value_, + "catching polymorphic type %q#T by value", orig_type); + else if (warn_catch_value > 1) + warning (OPT_Wcatch_value_, + "catching type %q#T by value", orig_type); + } + else if (warn_catch_value > 2) + warning (OPT_Wcatch_value_, + "catching non-reference type %q#T", orig_type); + } + } HANDLER_TYPE (handler) = type; } diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index a65e78f4376..1b40c51cc80 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -265,7 +265,7 @@ Objective-C and Objective-C++ Dialects}. -Wno-builtin-declaration-mismatch @gol -Wno-builtin-macro-redefined -Wc90-c99-compat -Wc99-c11-compat @gol -Wc++-compat -Wc++11-compat -Wc++14-compat -Wcast-align -Wcast-qual @gol --Wchar-subscripts -Wchkp -Wclobbered -Wcomment @gol +-Wchar-subscripts -Wchkp -Wcatch-value=@var{n} -Wclobbered -Wcomment @gol -Wconditionally-supported @gol -Wconversion -Wcoverage-mismatch -Wno-cpp -Wdangling-else -Wdate-time @gol -Wdelete-incomplete @gol @@ -5834,6 +5834,14 @@ When compiling C++, warn about the deprecated conversion from string literals to @code{char *}. This warning is enabled by default for C++ programs. +@item -Wcatch-value=@var{n} @r{(C++ and Objective-C++ only)} +@opindex Wcatch-value +Warn about catch handlers that do not catch via reference. +With @option{-Wcatch-value=1} warn about polymorphic class types that +are caught by value. With @option{-Wcatch-value=2} warn about all class +types that are caught by value. With @option{-Wcatch-value=3} warn about +all types that are not caught by reference. + @item -Wclobbered @opindex Wclobbered @opindex Wno-clobbered diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 33b1cc47f8c..9df0539a145 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2017-05-25 Volker Reichelt + + * g++.dg/warn/Wcatch-value-1.C: New test. + * g++.dg/warn/Wcatch-value-2.C: New test. + * g++.dg/warn/Wcatch-value-3.C: New test. + 2017-05-25 Nathan Sidwell * g++.dg/lookup/using17.C: Adjust diagnostics. diff --git a/gcc/testsuite/g++.dg/warn/Wcatch-value-1.C b/gcc/testsuite/g++.dg/warn/Wcatch-value-1.C new file mode 100644 index 00000000000..94ee934b6ae --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wcatch-value-1.C @@ -0,0 +1,64 @@ +// { dg-options "-Wcatch-value=1" } + +struct A { virtual ~A() {} }; +struct B : A {}; +struct C {}; +struct D : C {}; + +void foo() +{ + try {} + catch (D) {} + catch (C) {} + catch (B) {} // { dg-warning "catching polymorphic type" } + catch (A) {} // { dg-warning "catching polymorphic type" } + catch (A*) {} + catch (int) {} + + try {} + catch (D&) {} + catch (C&) {} + catch (B&) {} + catch (A&) {} + catch (A*) {} + catch (int&) {} +} + +template void foo1() +{ + try {} + catch (T) {} // { dg-warning "catching polymorphic type" } +} + +template void foo2() +{ + try {} + catch (T*) {} + + try {} + catch (T&) {} + + try {} + catch (const T&) {} +} + +void bar() +{ + foo1(); + foo1(); + foo1(); + foo1(); + foo1(); + + foo1(); + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); + foo1(); + + foo2(); + foo2(); + foo2(); + foo2(); + foo2(); +} diff --git a/gcc/testsuite/g++.dg/warn/Wcatch-value-2.C b/gcc/testsuite/g++.dg/warn/Wcatch-value-2.C new file mode 100644 index 00000000000..1bcf4056027 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wcatch-value-2.C @@ -0,0 +1,64 @@ +// { dg-options "-Wcatch-value=2" } + +struct A { virtual ~A() {} }; +struct B : A {}; +struct C {}; +struct D : C {}; + +void foo() +{ + try {} + catch (D) {} // { dg-warning "catching type" } + catch (C) {} // { dg-warning "catching type" } + catch (B) {} // { dg-warning "catching polymorphic type" } + catch (A) {} // { dg-warning "catching polymorphic type" } + catch (A*) {} + catch (int) {} + + try {} + catch (D&) {} + catch (C&) {} + catch (B&) {} + catch (A&) {} + catch (A*) {} + catch (int&) {} +} + +template void foo1() +{ + try {} + catch (T) {} // { dg-warning "catching" } +} + +template void foo2() +{ + try {} + catch (T*) {} + + try {} + catch (T&) {} + + try {} + catch (const T&) {} +} + +void bar() +{ + foo1(); + foo1(); + foo1(); + foo1(); + foo1(); + + foo1(); + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + + foo2(); + foo2(); + foo2(); + foo2(); + foo2(); +} diff --git a/gcc/testsuite/g++.dg/warn/Wcatch-value-3.C b/gcc/testsuite/g++.dg/warn/Wcatch-value-3.C new file mode 100644 index 00000000000..88ae698caf6 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wcatch-value-3.C @@ -0,0 +1,64 @@ +// { dg-options "-Wcatch-value=3" } + +struct A { virtual ~A() {} }; +struct B : A {}; +struct C {}; +struct D : C {}; + +void foo() +{ + try {} + catch (D) {} // { dg-warning "catching type" } + catch (C) {} // { dg-warning "catching type" } + catch (B) {} // { dg-warning "catching polymorphic type" } + catch (A) {} // { dg-warning "catching polymorphic type" } + catch (A*) {} // { dg-warning "catching non-reference type" } + catch (int) {} // { dg-warning "catching non-reference type" } + + try {} + catch (D&) {} + catch (C&) {} + catch (B&) {} + catch (A&) {} + catch (A*) {} // { dg-warning "catching non-reference type" } + catch (int&) {} +} + +template void foo1() +{ + try {} + catch (T) {} // { dg-warning "catching" } +} + +template void foo2() +{ + try {} + catch (T*) {} // { dg-warning "catching non-reference type" } + + try {} + catch (T&) {} + + try {} + catch (const T&) {} +} + +void bar() +{ + foo1(); + foo1(); + foo1(); + foo1(); + foo1(); + + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + foo1(); // { dg-message "required" } + + foo2(); // { dg-message "required" } + foo2(); // { dg-message "required" } + foo2(); // { dg-message "required" } + foo2(); // { dg-message "required" } + foo2(); // { dg-message "required" } +}