diff options
author | Marek Polacek <polacek@redhat.com> | 2020-11-05 21:46:06 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-11-06 16:40:54 -0500 |
commit | 43d3b7bc073df27800218b685e6d7dd78dd31905 (patch) | |
tree | db1e020925b51515cd9ef0b7a19077aba729d19f | |
parent | 7987a8d29a78eb373b5e054d539e332d75e92e92 (diff) | |
download | gcc-43d3b7bc073df27800218b685e6d7dd78dd31905.zip gcc-43d3b7bc073df27800218b685e6d7dd78dd31905.tar.gz gcc-43d3b7bc073df27800218b685e6d7dd78dd31905.tar.bz2 |
c++: Small tweak to can_convert_eh [PR81660]
While messing with check_handlers_1, I spotted this bug report which
complains that we don't warn about the case when we have two duplicated
handlers of type int. can_convert_eh implements [except.handle] and
that says: A handler is a match for an exception object of type E if
- The handler is of type cv T or cv T& and E and T are the same type
(ignoring the top-level cv-qualifiers), or [...]
but we don't implement this bullet properly for non-class types. The
fix therefore seems pretty obvious. Also change the return type to
bool when we're only returning yes/no.
gcc/cp/ChangeLog:
PR c++/81660
* except.c (can_convert_eh): Change the return type to bool. If
the type TO and FROM are the same, return true.
gcc/testsuite/ChangeLog:
PR c++/81660
* g++.dg/warn/Wexceptions3.C: New test.
* g++.dg/eh/pr42859.C: Add dg-warning.
* g++.dg/torture/pr81659.C: Likewise.
-rw-r--r-- | gcc/cp/except.c | 14 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/eh/pr42859.C | 2 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/torture/pr81659.C | 2 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Wexceptions3.C | 29 |
4 files changed, 39 insertions, 8 deletions
diff --git a/gcc/cp/except.c b/gcc/cp/except.c index b72a28c..0f6c76b 100644 --- a/gcc/cp/except.c +++ b/gcc/cp/except.c @@ -41,7 +41,6 @@ static tree do_allocate_exception (tree); static tree wrap_cleanups_r (tree *, int *, void *); static int complete_ptr_ref_or_void_ptr_p (tree, tree); static bool is_admissible_throw_operand_or_catch_parameter (tree, bool); -static int can_convert_eh (tree, tree); /* Sets up all the global eh stuff that needs to be initialized at the start of compilation. */ @@ -932,31 +931,34 @@ nothrow_libfn_p (const_tree fn) /* Returns nonzero if an exception of type FROM will be caught by a handler for type TO, as per [except.handle]. */ -static int +static bool can_convert_eh (tree to, tree from) { to = non_reference (to); from = non_reference (from); + if (same_type_ignoring_top_level_qualifiers_p (to, from)) + return true; + if (TYPE_PTR_P (to) && TYPE_PTR_P (from)) { to = TREE_TYPE (to); from = TREE_TYPE (from); if (! at_least_as_qualified_p (to, from)) - return 0; + return false; if (VOID_TYPE_P (to)) - return 1; + return true; /* Else fall through. */ } if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from) && publicly_uniquely_derived_p (to, from)) - return 1; + return true; - return 0; + return false; } /* Check whether any of the handlers in I are shadowed by another handler diff --git a/gcc/testsuite/g++.dg/eh/pr42859.C b/gcc/testsuite/g++.dg/eh/pr42859.C index a9f1473..0de9140 100644 --- a/gcc/testsuite/g++.dg/eh/pr42859.C +++ b/gcc/testsuite/g++.dg/eh/pr42859.C @@ -13,7 +13,7 @@ ptw32_terminate (void) catch (int) { } - catch (int) + catch (int) // { dg-warning "will be caught by earlier handler" } { } } diff --git a/gcc/testsuite/g++.dg/torture/pr81659.C b/gcc/testsuite/g++.dg/torture/pr81659.C index 3696957..074099b 100644 --- a/gcc/testsuite/g++.dg/torture/pr81659.C +++ b/gcc/testsuite/g++.dg/torture/pr81659.C @@ -12,7 +12,7 @@ a (int b) catch (int) { } - catch (int) + catch (int) // { dg-warning "will be caught by earlier handler" } { } } diff --git a/gcc/testsuite/g++.dg/warn/Wexceptions3.C b/gcc/testsuite/g++.dg/warn/Wexceptions3.C new file mode 100644 index 0000000..97fda9d --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wexceptions3.C @@ -0,0 +1,29 @@ +// PR c++/81660 + +void bar (int); + +void +fn (int b) +{ + if (b) + throw; + try + { + bar (3); + } + catch (int) + { + } + catch (int) // { dg-warning "will be caught by earlier handler" } + { + } + catch (const int) // { dg-warning "will be caught by earlier handler" } + { + } + catch (int &) // { dg-warning "will be caught by earlier handler" } + { + } + catch (const int &) // { dg-warning "will be caught by earlier handler" } + { + } +} |