This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH: PR 21228
- From: Mark Mitchell <mark at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 20 Dec 2005 00:30:14 -0800
- Subject: C++ PATCH: PR 21228
- Reply-to: mark at codesourcery dot com
This patch fixes PR 21228, a bogus warning with -Wunreachable-code.
The C++ front end was creating an EH_SPEC_BLOCK in implicitly defined
methods, but since the exception-specification for such a function is
the union of the specifications for all of the functions it calls,
the exception-specification will never be violated, and the middle
end warns about that fact. Fixed by causing the front-end not to
generate the EH_SPEC_BLOCK for such functions. (We still set
TYPE_RAISES_EXCEPTION on the function type for such functions, of
course.)
Tested on x86_64-unknown-linux-gnu, applied on the 4.0 branch, 4.1
branch, and (in a few minutes) the mainline.
--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
2005-12-20 Mark Mitchell <mark@codesourcery.com>
PR c++/21228
* decl.c (use_eh_spec_block): New function.
(store_parm_decls): Use it.
(finish_function): Likewise.
2005-12-20 Mark Mitchell <mark@codesourcery.com>
PR c++/21228
* g++.dg/warn/Wunreachable-code-2.C: New test.
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c (revision 108836)
+++ gcc/cp/decl.c (working copy)
@@ -10482,6 +10482,26 @@ start_function (cp_decl_specifier_seq *d
return 1;
}
+/* Returns true iff an EH_SPEC_BLOCK should be created in the body of
+ FN. */
+
+static bool
+use_eh_spec_block (tree fn)
+{
+ return (flag_exceptions && flag_enforce_eh_specs
+ && !processing_template_decl
+ && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
+ /* Implicitly-generated constructors and destructors have
+ exception specifications. However, those specifications
+ are the union of the possible exceptions specified by the
+ constructors/destructors for bases and members, so no
+ unallowed exception will ever reach this function. By
+ not creating the EH_SPEC_BLOCK we save a little memory,
+ and we avoid spurious warnings about unreachable
+ code. */
+ && !DECL_ARTIFICIAL (fn));
+}
+
/* Store the parameter declarations into the current function declaration.
This is called after parsing the parameter declarations, before
digesting the body of the function.
@@ -10552,16 +10572,8 @@ store_parm_decls (tree current_function_
DECL_ARGUMENTS is not modified. */
current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
- /* For a cloned function, we've already got all the code we need;
- there's no need to add any extra bits. */
- if (!DECL_CLONED_FUNCTION_P (fndecl))
- {
- /* Do the starting of the exception specifications, if we have any. */
- if (flag_exceptions && !processing_template_decl
- && flag_enforce_eh_specs
- && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
- current_eh_spec_block = begin_eh_spec_block ();
- }
+ if (use_eh_spec_block (current_function_decl))
+ current_eh_spec_block = begin_eh_spec_block ();
}
@@ -10848,10 +10860,7 @@ finish_function (int flags)
#endif
}
- /* Finish dealing with exception specifiers. */
- if (flag_exceptions && !processing_template_decl
- && flag_enforce_eh_specs
- && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
+ if (use_eh_spec_block (current_function_decl))
finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
(TREE_TYPE (current_function_decl)),
current_eh_spec_block);
Index: gcc/testsuite/g++.dg/warn/Wunreachable-code-2.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wunreachable-code-2.C (revision 0)
+++ gcc/testsuite/g++.dg/warn/Wunreachable-code-2.C (revision 0)
@@ -0,0 +1,23 @@
+// PR c++/21228
+/* { dg-options "-Wunreachable-code" } */
+
+class testStringBase
+{
+public:
+ char *stringPtr;
+};
+
+class testString : public testStringBase
+{
+public:
+ testString();
+};
+
+testString::testString()
+{
+ stringPtr = (char *) 9;
+}
+
+int main(int argc, char **argv) {
+ testString s;
+}