* [PATCH] c++: Add -Wexceptions warning option [PR97675]
@ 2020-11-05 16:03 Marek Polacek
2020-11-05 20:46 ` Jason Merrill
2020-11-05 23:13 ` David Malcolm
0 siblings, 2 replies; 4+ messages in thread
From: Marek Polacek @ 2020-11-05 16:03 UTC (permalink / raw)
To: GCC Patches, Jason Merrill
This PR asks that we add a warning option for an existing (very old)
warning, so that it can be disabled selectively. clang++ uses
-Wexceptions for this, so I added this new option rather than using
e.g. -Wnoexcept.
gcc/c-family/ChangeLog:
PR c++/97675
* c.opt (Wexceptions): New option.
gcc/cp/ChangeLog:
PR c++/97675
* except.c (check_handlers_1): Use OPT_Wexceptions for the
warning. Use inform for the second part of the warning.
gcc/ChangeLog:
PR c++/97675
* doc/invoke.texi: Document -Wexceptions.
gcc/testsuite/ChangeLog:
PR c++/97675
* g++.old-deja/g++.eh/catch10.C: Adjust dg-warning.
* g++.dg/warn/Wexceptions1.C: New test.
* g++.dg/warn/Wexceptions2.C: New test.
---
gcc/c-family/c.opt | 4 ++++
gcc/cp/except.c | 9 ++++-----
gcc/doc/invoke.texi | 8 +++++++-
gcc/testsuite/g++.dg/warn/Wexceptions1.C | 9 +++++++++
gcc/testsuite/g++.dg/warn/Wexceptions2.C | 10 ++++++++++
gcc/testsuite/g++.old-deja/g++.eh/catch10.C | 4 ++--
6 files changed, 36 insertions(+), 8 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions1.C
create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions2.C
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 426636be839..9493acb82ff 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -579,6 +579,10 @@ Werror-implicit-function-declaration
C ObjC RejectNegative Warning Alias(Werror=, implicit-function-declaration)
This switch is deprecated; use -Werror=implicit-function-declaration instead.
+Wexceptions
+C++ ObjC++ Var(warn_exceptions) Init(1)
+Warn when an exception handler is shadowed by another handler.
+
Wextra
C ObjC C++ ObjC++ Warning
; in common.opt
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index cb1a4105dae..985206f6a64 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -975,11 +975,10 @@ check_handlers_1 (tree master, tree_stmt_iterator i)
tree handler = tsi_stmt (i);
if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
{
- warning_at (EXPR_LOCATION (handler), 0,
- "exception of type %qT will be caught",
- TREE_TYPE (handler));
- warning_at (EXPR_LOCATION (master), 0,
- " by earlier handler for %qT", type);
+ if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
+ "exception of type %qT will be caught by earlier "
+ "handler", TREE_TYPE (handler)))
+ inform (EXPR_LOCATION (master), "for type %qT", type);
break;
}
}
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 5320e6c1e1e..4c6435d5e14 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -240,7 +240,7 @@ in the following sections.
-Wctor-dtor-privacy -Wno-delete-incomplete @gol
-Wdelete-non-virtual-dtor -Wdeprecated-copy -Wdeprecated-copy-dtor @gol
-Wno-deprecated-enum-enum-conversion -Wno-deprecated-enum-float-conversion @gol
--Weffc++ -Wextra-semi -Wno-inaccessible-base @gol
+-Weffc++ -Wno-exceptions -Wextra-semi -Wno-inaccessible-base @gol
-Wno-inherited-variadic-ctor -Wno-init-list-lifetime @gol
-Wno-invalid-offsetof -Wno-literal-suffix -Wmismatched-tags @gol
-Wmultiple-inheritance -Wnamespaces -Wnarrowing @gol
@@ -3738,6 +3738,12 @@ When selecting this option, be aware that the standard library
headers do not obey all of these guidelines; use @samp{grep -v}
to filter out those warnings.
+@item -Wno-exceptions @r{(C++ and Objective-C++ only)}
+@opindex Wexceptions
+@opindex Wno-exceptions
+Disable the warning about the case when an exception handler is shadowed by
+another handler, which can point out a wrong ordering of exception handlers.
+
@item -Wstrict-null-sentinel @r{(C++ and Objective-C++ only)}
@opindex Wstrict-null-sentinel
@opindex Wno-strict-null-sentinel
diff --git a/gcc/testsuite/g++.dg/warn/Wexceptions1.C b/gcc/testsuite/g++.dg/warn/Wexceptions1.C
new file mode 100644
index 00000000000..af140fd0dc2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wexceptions1.C
@@ -0,0 +1,9 @@
+// PR c++/97675
+
+struct Base { };
+struct Child : Base { };
+int main() {
+ try { throw Child(); }
+ catch (Base const&) { }
+ catch (Child const&) { } // { dg-warning "exception of type .Child. will be caught by earlier handler" }
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wexceptions2.C b/gcc/testsuite/g++.dg/warn/Wexceptions2.C
new file mode 100644
index 00000000000..07c5155ac06
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wexceptions2.C
@@ -0,0 +1,10 @@
+// PR c++/97675
+// { dg-additional-options -Wno-exceptions }
+
+struct Base { };
+struct Child : Base { };
+int main() {
+ try { throw Child(); }
+ catch (Base const&) { }
+ catch (Child const&) { } // { dg-bogus "exception of type .Child. will be caught by earlier handler" }
+}
diff --git a/gcc/testsuite/g++.old-deja/g++.eh/catch10.C b/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
index 2300a946187..7cc609645a2 100644
--- a/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
+++ b/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
@@ -13,8 +13,8 @@ void g()
catch (A*) { }
try { f(); }
- catch (A*) { } // { dg-warning "" } A* before B*
- catch (B*) { } // { dg-warning "" } A* before B*
+ catch (A*) { } // { dg-message "for type" } A* before B*
+ catch (B*) { } // { dg-warning "will be caught" } A* before B*
try { f(); }
catch (A*) { }
base-commit: fab72592d86d11b89a01f0f3c2c9c329d43466c1
--
2.28.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: Add -Wexceptions warning option [PR97675]
2020-11-05 16:03 [PATCH] c++: Add -Wexceptions warning option [PR97675] Marek Polacek
@ 2020-11-05 20:46 ` Jason Merrill
2020-11-05 23:13 ` David Malcolm
1 sibling, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2020-11-05 20:46 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On 11/5/20 11:03 AM, Marek Polacek wrote:
> This PR asks that we add a warning option for an existing (very old)
> warning, so that it can be disabled selectively. clang++ uses
> -Wexceptions for this, so I added this new option rather than using
> e.g. -Wnoexcept.
OK.
> gcc/c-family/ChangeLog:
>
> PR c++/97675
> * c.opt (Wexceptions): New option.
>
> gcc/cp/ChangeLog:
>
> PR c++/97675
> * except.c (check_handlers_1): Use OPT_Wexceptions for the
> warning. Use inform for the second part of the warning.
>
> gcc/ChangeLog:
>
> PR c++/97675
> * doc/invoke.texi: Document -Wexceptions.
>
> gcc/testsuite/ChangeLog:
>
> PR c++/97675
> * g++.old-deja/g++.eh/catch10.C: Adjust dg-warning.
> * g++.dg/warn/Wexceptions1.C: New test.
> * g++.dg/warn/Wexceptions2.C: New test.
> ---
> gcc/c-family/c.opt | 4 ++++
> gcc/cp/except.c | 9 ++++-----
> gcc/doc/invoke.texi | 8 +++++++-
> gcc/testsuite/g++.dg/warn/Wexceptions1.C | 9 +++++++++
> gcc/testsuite/g++.dg/warn/Wexceptions2.C | 10 ++++++++++
> gcc/testsuite/g++.old-deja/g++.eh/catch10.C | 4 ++--
> 6 files changed, 36 insertions(+), 8 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions1.C
> create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions2.C
>
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 426636be839..9493acb82ff 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -579,6 +579,10 @@ Werror-implicit-function-declaration
> C ObjC RejectNegative Warning Alias(Werror=, implicit-function-declaration)
> This switch is deprecated; use -Werror=implicit-function-declaration instead.
>
> +Wexceptions
> +C++ ObjC++ Var(warn_exceptions) Init(1)
> +Warn when an exception handler is shadowed by another handler.
> +
> Wextra
> C ObjC C++ ObjC++ Warning
> ; in common.opt
> diff --git a/gcc/cp/except.c b/gcc/cp/except.c
> index cb1a4105dae..985206f6a64 100644
> --- a/gcc/cp/except.c
> +++ b/gcc/cp/except.c
> @@ -975,11 +975,10 @@ check_handlers_1 (tree master, tree_stmt_iterator i)
> tree handler = tsi_stmt (i);
> if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
> {
> - warning_at (EXPR_LOCATION (handler), 0,
> - "exception of type %qT will be caught",
> - TREE_TYPE (handler));
> - warning_at (EXPR_LOCATION (master), 0,
> - " by earlier handler for %qT", type);
> + if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
> + "exception of type %qT will be caught by earlier "
> + "handler", TREE_TYPE (handler)))
> + inform (EXPR_LOCATION (master), "for type %qT", type);
> break;
> }
> }
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index 5320e6c1e1e..4c6435d5e14 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -240,7 +240,7 @@ in the following sections.
> -Wctor-dtor-privacy -Wno-delete-incomplete @gol
> -Wdelete-non-virtual-dtor -Wdeprecated-copy -Wdeprecated-copy-dtor @gol
> -Wno-deprecated-enum-enum-conversion -Wno-deprecated-enum-float-conversion @gol
> --Weffc++ -Wextra-semi -Wno-inaccessible-base @gol
> +-Weffc++ -Wno-exceptions -Wextra-semi -Wno-inaccessible-base @gol
> -Wno-inherited-variadic-ctor -Wno-init-list-lifetime @gol
> -Wno-invalid-offsetof -Wno-literal-suffix -Wmismatched-tags @gol
> -Wmultiple-inheritance -Wnamespaces -Wnarrowing @gol
> @@ -3738,6 +3738,12 @@ When selecting this option, be aware that the standard library
> headers do not obey all of these guidelines; use @samp{grep -v}
> to filter out those warnings.
>
> +@item -Wno-exceptions @r{(C++ and Objective-C++ only)}
> +@opindex Wexceptions
> +@opindex Wno-exceptions
> +Disable the warning about the case when an exception handler is shadowed by
> +another handler, which can point out a wrong ordering of exception handlers.
> +
> @item -Wstrict-null-sentinel @r{(C++ and Objective-C++ only)}
> @opindex Wstrict-null-sentinel
> @opindex Wno-strict-null-sentinel
> diff --git a/gcc/testsuite/g++.dg/warn/Wexceptions1.C b/gcc/testsuite/g++.dg/warn/Wexceptions1.C
> new file mode 100644
> index 00000000000..af140fd0dc2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wexceptions1.C
> @@ -0,0 +1,9 @@
> +// PR c++/97675
> +
> +struct Base { };
> +struct Child : Base { };
> +int main() {
> + try { throw Child(); }
> + catch (Base const&) { }
> + catch (Child const&) { } // { dg-warning "exception of type .Child. will be caught by earlier handler" }
> +}
> diff --git a/gcc/testsuite/g++.dg/warn/Wexceptions2.C b/gcc/testsuite/g++.dg/warn/Wexceptions2.C
> new file mode 100644
> index 00000000000..07c5155ac06
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wexceptions2.C
> @@ -0,0 +1,10 @@
> +// PR c++/97675
> +// { dg-additional-options -Wno-exceptions }
> +
> +struct Base { };
> +struct Child : Base { };
> +int main() {
> + try { throw Child(); }
> + catch (Base const&) { }
> + catch (Child const&) { } // { dg-bogus "exception of type .Child. will be caught by earlier handler" }
> +}
> diff --git a/gcc/testsuite/g++.old-deja/g++.eh/catch10.C b/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
> index 2300a946187..7cc609645a2 100644
> --- a/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
> +++ b/gcc/testsuite/g++.old-deja/g++.eh/catch10.C
> @@ -13,8 +13,8 @@ void g()
> catch (A*) { }
>
> try { f(); }
> - catch (A*) { } // { dg-warning "" } A* before B*
> - catch (B*) { } // { dg-warning "" } A* before B*
> + catch (A*) { } // { dg-message "for type" } A* before B*
> + catch (B*) { } // { dg-warning "will be caught" } A* before B*
>
> try { f(); }
> catch (A*) { }
>
> base-commit: fab72592d86d11b89a01f0f3c2c9c329d43466c1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: Add -Wexceptions warning option [PR97675]
2020-11-05 16:03 [PATCH] c++: Add -Wexceptions warning option [PR97675] Marek Polacek
2020-11-05 20:46 ` Jason Merrill
@ 2020-11-05 23:13 ` David Malcolm
2020-11-05 23:26 ` Marek Polacek
1 sibling, 1 reply; 4+ messages in thread
From: David Malcolm @ 2020-11-05 23:13 UTC (permalink / raw)
To: Marek Polacek, GCC Patches, Jason Merrill
On Thu, 2020-11-05 at 11:03 -0500, Marek Polacek via Gcc-patches wrote:
> This PR asks that we add a warning option for an existing (very old)
> warning, so that it can be disabled selectively. clang++ uses
> -Wexceptions for this, so I added this new option rather than using
> e.g. -Wnoexcept.
>
> gcc/c-family/ChangeLog:
>
> PR c++/97675
> * c.opt (Wexceptions): New option.
>
> gcc/cp/ChangeLog:
>
> PR c++/97675
> * except.c (check_handlers_1): Use OPT_Wexceptions for the
> warning. Use inform for the second part of the warning.
>
> gcc/ChangeLog:
>
> PR c++/97675
> * doc/invoke.texi: Document -Wexceptions.
>
> gcc/testsuite/ChangeLog:
>
> PR c++/97675
> * g++.old-deja/g++.eh/catch10.C: Adjust dg-warning.
> * g++.dg/warn/Wexceptions1.C: New test.
> * g++.dg/warn/Wexceptions2.C: New test.
> ---
> gcc/c-family/c.opt | 4 ++++
> gcc/cp/except.c | 9 ++++-----
> gcc/doc/invoke.texi | 8 +++++++-
> gcc/testsuite/g++.dg/warn/Wexceptions1.C | 9 +++++++++
> gcc/testsuite/g++.dg/warn/Wexceptions2.C | 10 ++++++++++
> gcc/testsuite/g++.old-deja/g++.eh/catch10.C | 4 ++--
> 6 files changed, 36 insertions(+), 8 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions1.C
> create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions2.C
>
> diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> index 426636be839..9493acb82ff 100644
> --- a/gcc/c-family/c.opt
> +++ b/gcc/c-family/c.opt
> @@ -579,6 +579,10 @@ Werror-implicit-function-declaration
> C ObjC RejectNegative Warning Alias(Werror=, implicit-function-
> declaration)
> This switch is deprecated; use -Werror=implicit-function-declaration
> instead.
>
> +Wexceptions
> +C++ ObjC++ Var(warn_exceptions) Init(1)
> +Warn when an exception handler is shadowed by another handler.
> +
> Wextra
> C ObjC C++ ObjC++ Warning
> ; in common.opt
> diff --git a/gcc/cp/except.c b/gcc/cp/except.c
> index cb1a4105dae..985206f6a64 100644
> --- a/gcc/cp/except.c
> +++ b/gcc/cp/except.c
> @@ -975,11 +975,10 @@ check_handlers_1 (tree master,
> tree_stmt_iterator i)
> tree handler = tsi_stmt (i);
> if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE
> (handler)))
> {
Can you add an auto_diagnostic_group here please.
> - warning_at (EXPR_LOCATION (handler), 0,
> - "exception of type %qT will be caught",
> - TREE_TYPE (handler));
> - warning_at (EXPR_LOCATION (master), 0,
> - " by earlier handler for %qT", type);
> + if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
> + "exception of type %qT will be caught by
> earlier "
> + "handler", TREE_TYPE (handler)))
> + inform (EXPR_LOCATION (master), "for type %qT", type);
> break;
> }
> }
Thanks
Dave
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: Add -Wexceptions warning option [PR97675]
2020-11-05 23:13 ` David Malcolm
@ 2020-11-05 23:26 ` Marek Polacek
0 siblings, 0 replies; 4+ messages in thread
From: Marek Polacek @ 2020-11-05 23:26 UTC (permalink / raw)
To: David Malcolm; +Cc: GCC Patches, Jason Merrill
On Thu, Nov 05, 2020 at 06:13:41PM -0500, David Malcolm via Gcc-patches wrote:
> On Thu, 2020-11-05 at 11:03 -0500, Marek Polacek via Gcc-patches wrote:
> > This PR asks that we add a warning option for an existing (very old)
> > warning, so that it can be disabled selectively. clang++ uses
> > -Wexceptions for this, so I added this new option rather than using
> > e.g. -Wnoexcept.
> >
> > gcc/c-family/ChangeLog:
> >
> > PR c++/97675
> > * c.opt (Wexceptions): New option.
> >
> > gcc/cp/ChangeLog:
> >
> > PR c++/97675
> > * except.c (check_handlers_1): Use OPT_Wexceptions for the
> > warning. Use inform for the second part of the warning.
> >
> > gcc/ChangeLog:
> >
> > PR c++/97675
> > * doc/invoke.texi: Document -Wexceptions.
> >
> > gcc/testsuite/ChangeLog:
> >
> > PR c++/97675
> > * g++.old-deja/g++.eh/catch10.C: Adjust dg-warning.
> > * g++.dg/warn/Wexceptions1.C: New test.
> > * g++.dg/warn/Wexceptions2.C: New test.
> > ---
> > gcc/c-family/c.opt | 4 ++++
> > gcc/cp/except.c | 9 ++++-----
> > gcc/doc/invoke.texi | 8 +++++++-
> > gcc/testsuite/g++.dg/warn/Wexceptions1.C | 9 +++++++++
> > gcc/testsuite/g++.dg/warn/Wexceptions2.C | 10 ++++++++++
> > gcc/testsuite/g++.old-deja/g++.eh/catch10.C | 4 ++--
> > 6 files changed, 36 insertions(+), 8 deletions(-)
> > create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions1.C
> > create mode 100644 gcc/testsuite/g++.dg/warn/Wexceptions2.C
> >
> > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> > index 426636be839..9493acb82ff 100644
> > --- a/gcc/c-family/c.opt
> > +++ b/gcc/c-family/c.opt
> > @@ -579,6 +579,10 @@ Werror-implicit-function-declaration
> > C ObjC RejectNegative Warning Alias(Werror=, implicit-function-
> > declaration)
> > This switch is deprecated; use -Werror=implicit-function-declaration
> > instead.
> >
> > +Wexceptions
> > +C++ ObjC++ Var(warn_exceptions) Init(1)
> > +Warn when an exception handler is shadowed by another handler.
> > +
> > Wextra
> > C ObjC C++ ObjC++ Warning
> > ; in common.opt
> > diff --git a/gcc/cp/except.c b/gcc/cp/except.c
> > index cb1a4105dae..985206f6a64 100644
> > --- a/gcc/cp/except.c
> > +++ b/gcc/cp/except.c
> > @@ -975,11 +975,10 @@ check_handlers_1 (tree master,
> > tree_stmt_iterator i)
> > tree handler = tsi_stmt (i);
> > if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE
> > (handler)))
> > {
>
> Can you add an auto_diagnostic_group here please.
Yup, I've pushed this:
From 44e1f63e20fec07e3a10d8e75336cfda64c911bf Mon Sep 17 00:00:00 2001
From: Marek Polacek <polacek@redhat.com>
Date: Thu, 5 Nov 2020 18:23:56 -0500
Subject: [pushed] c++: Add auto_diagnostic_group to check_handlers_1.
This was missing.
gcc/cp/ChangeLog:
* except.c (check_handlers_1): Add auto_diagnostic_group.
---
gcc/cp/except.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index 985206f6a64..b72a28c1aa9 100644
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
@@ -975,6 +975,7 @@ check_handlers_1 (tree master, tree_stmt_iterator i)
tree handler = tsi_stmt (i);
if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
{
+ auto_diagnostic_group d;
if (warning_at (EXPR_LOCATION (handler), OPT_Wexceptions,
"exception of type %qT will be caught by earlier "
"handler", TREE_TYPE (handler)))
base-commit: e6fd02cc6d874c523466250a1cb724e0c7af9d75
--
2.28.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-11-05 23:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-05 16:03 [PATCH] c++: Add -Wexceptions warning option [PR97675] Marek Polacek
2020-11-05 20:46 ` Jason Merrill
2020-11-05 23:13 ` David Malcolm
2020-11-05 23:26 ` Marek Polacek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).