From b67b9225f72e4b2d58e535c78dbeff83f94c8f66 Mon Sep 17 00:00:00 2001 From: David Pagan Date: Wed, 30 May 2018 22:55:38 +0000 Subject: [PATCH] re PR c/55976 (-Werror=return-type should error on returning a value from a void function) 2018-05-30 David Pagan PR c/55976 * c-decl.c (grokdeclarator): Update check for return type warnings. (start_function): Likewise. (finish_function): Likewise. * c-typeck.c (c_finish_return): Update check for return type warnings. Pass OPT_Wreturn_type to pedwarn when appropriate. PR c/55976 * c-opts.c (c_common_post_options): Set default for warn_return_type for C++/C++ with ObjC extensions only. For C, makes it possible to differentiate between default (no option), -Wreturn-type, and -Wno-return-type. PR c/55976 * gcc.dg/noncompile/pr55976-1.c: New test. * gcc.dg/noncompile/pr55976-2.c: New test. From-SVN: r260978 --- gcc/c-family/ChangeLog | 8 ++++++++ gcc/c-family/c-opts.c | 5 +++-- gcc/c/ChangeLog | 9 +++++++++ gcc/c/c-decl.c | 9 +++++---- gcc/c/c-typeck.c | 6 +++--- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/gcc.dg/noncompile/pr55976-1.c | 14 ++++++++++++++ gcc/testsuite/gcc.dg/noncompile/pr55976-2.c | 16 ++++++++++++++++ 8 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/noncompile/pr55976-1.c create mode 100644 gcc/testsuite/gcc.dg/noncompile/pr55976-2.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index cb6ee1319c7..d7f68046c22 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,11 @@ +2018-04-22 David Pagan + + PR c/55976 + * c-opts.c (c_common_post_options): Set default for warn_return_type + for C++/C++ with ObjC extensions only. For C, makes it possible to + differentiate between default (no option), -Wreturn-type, and + -Wno-return-type. + 2018-05-29 Jason Merrill * c.opt (Winit-list-lifetime): New flag. diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c index 3db01d453a6..ddaaef37e1d 100644 --- a/gcc/c-family/c-opts.c +++ b/gcc/c-family/c-opts.c @@ -994,8 +994,9 @@ c_common_post_options (const char **pfilename) flag_extern_tls_init = 1; } - if (warn_return_type == -1) - warn_return_type = c_dialect_cxx (); + /* Enable by default only for C++ and C++ with ObjC extensions. */ + if (warn_return_type == -1 && c_dialect_cxx ()) + warn_return_type = 1; if (num_in_fnames > 1) error ("too many filenames given. Type %s --help for usage", diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index f3a387de226..1be683d2c81 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,12 @@ +2018-05-30 David Pagan + + PR c/55976 + * c-decl.c (grokdeclarator): Update check for return type warnings. + (start_function): Likewise. + (finish_function): Likewise. + * c-typeck.c (c_finish_return): Update check for return type warnings. + Pass OPT_Wreturn_type to pedwarn when appropriate. + 2018-05-18 Richard Sandiford * gimple-parser.c (c_parser_gimple_postfix_expression): Remove diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 3c4b18edf56..54f58a56cde 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -5758,7 +5758,7 @@ grokdeclarator (const struct c_declarator *declarator, /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type and this is a function, or if -Wimplicit; prefer the former warning since it is more explicit. */ - if ((warn_implicit_int || warn_return_type || flag_isoc99) + if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99) && funcdef_flag) warn_about_return_type = 1; else @@ -8752,7 +8752,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, if (warn_about_return_type) warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int - : (warn_return_type ? OPT_Wreturn_type + : (warn_return_type > 0 ? OPT_Wreturn_type : OPT_Wimplicit_int), "return type defaults to %"); @@ -9463,8 +9463,9 @@ finish_function (void) finish_fname_decls (); - /* Complain if there's just no return statement. */ - if (warn_return_type + /* Complain if there's no return statement only if option specified on + command line. */ + if (warn_return_type > 0 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE && !current_function_returns_value && !current_function_returns_null /* Don't complain if we are no-return. */ diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 45a4529ed52..f346eaef031 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -10133,13 +10133,13 @@ c_finish_return (location_t loc, tree retval, tree origtype) if (!retval) { current_function_returns_null = 1; - if ((warn_return_type || flag_isoc99) + if ((warn_return_type >= 0 || flag_isoc99) && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE) { bool warned_here; if (flag_isoc99) warned_here = pedwarn - (loc, 0, + (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0, "% with no value, in function returning non-void"); else warned_here = warning_at @@ -10157,7 +10157,7 @@ c_finish_return (location_t loc, tree retval, tree origtype) bool warned_here; if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE) warned_here = pedwarn - (xloc, 0, + (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0, "% with a value, in function returning void"); else warned_here = pedwarn diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 9aa35c8900d..338cbb254b3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-05-30 David Pagan + + PR c/55976 + * gcc.dg/noncompile/pr55976-1.c: New test. + * gcc.dg/noncompile/pr55976-2.c: New test. + 2018-05-30 Jan Hubicka * gcc.dg/lto/20111213-1_0.c: Fix template. diff --git a/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c b/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c new file mode 100644 index 00000000000..b3e63283c90 --- /dev/null +++ b/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c @@ -0,0 +1,14 @@ +/* PR c/55976 */ +/* { dg-do compile } */ +/* { dg-options "-Werror=return-type" } */ + +/* Verify warnings for return type become errors. */ + +void t () { return 1; } /* { dg-error "return" "function returning void" } */ +int b () { return; } /* { dg-error "return" "function returning non-void" } */ + +int main() +{ + t(); b(); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c b/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c new file mode 100644 index 00000000000..0e493d0e8af --- /dev/null +++ b/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c @@ -0,0 +1,16 @@ +/* PR c/55976 */ +/* { dg-do compile } */ +/* { dg-options "-Wno-return-type" } */ + +/* Verify that -Wno-return-type turns off warnings about function return + type. */ + +void t () { return 1; } /* normally generates function returning void */ +int b () { return; } /* normally generates function returning non-void */ + +int main() +{ + t (); b (); + return 0; +} + -- 2.30.2