aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2023-11-09 09:50:54 +0100
committerFlorian Weimer <fweimer@redhat.com>2023-11-09 09:53:40 +0100
commit6e312b2b864bf923a9d772429f014375bf9dabc8 (patch)
tree42a9ab533a216f375efd9dbd5163482fe4738af8 /gcc/c
parentf88b78b1196d71042cf129be7a0bf02b9a4de0d3 (diff)
downloadgcc-6e312b2b864bf923a9d772429f014375bf9dabc8.zip
gcc-6e312b2b864bf923a9d772429f014375bf9dabc8.tar.gz
gcc-6e312b2b864bf923a9d772429f014375bf9dabc8.tar.bz2
c: Add -Wreturn-mismatch warning, split from -Wreturn-type
The existing -Wreturn-type option covers both constraint violations (which are mandatory to diagnose) and warnings that have known false positives. The new -Wreturn-mismatch warning is only about the constraint violations (missing or extra return expressions), and should eventually be turned into a permerror. The -std=gnu89 test cases show that by default, we do not warn for return; in a function not returning void. This matches previous practice for -Wreturn-type. gcc/c-family/ * c.opt (Wreturn-mismatch): New. gcc/c/ * c-typeck.cc (c_finish_return): Use pedwarn with OPT_Wreturn_mismatch for missing/extra return expressions. gcc/ * doc/invoke.texi (Warning Options): Document -Wreturn-mismatch. Update -Wreturn-type documentation. gcc/testsuite/ * gcc.dg/Wreturn-mismatch-1.c: New. * gcc.dg/Wreturn-mismatch-2.c: New. * gcc.dg/Wreturn-mismatch-3.c: New. * gcc.dg/Wreturn-mismatch-4.c: New. * gcc.dg/Wreturn-mismatch-5.c: New. * gcc.dg/Wreturn-mismatch-6.c: New. * gcc.dg/noncompile/pr55976-1.c: Change -Werror=return-type to -Werror=return-mismatch. * gcc.dg/noncompile/pr55976-2.c: Change -Wreturn-type to -Wreturn-mismatch.
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/c-typeck.cc16
1 files changed, 5 insertions, 11 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 4580ff0..dd5016a 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -11311,17 +11311,11 @@ c_finish_return (location_t loc, tree retval, tree origtype)
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, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
- "%<return%> with no value, in function returning non-void");
- else
- warned_here = warning_at
- (loc, OPT_Wreturn_type,
- "%<return%> with no value, in function returning non-void");
no_warning = true;
- if (warned_here)
+ if (emit_diagnostic (flag_isoc99 ? DK_PEDWARN : DK_WARNING,
+ loc, OPT_Wreturn_mismatch,
+ "%<return%> with no value,"
+ " in function returning non-void"))
inform (DECL_SOURCE_LOCATION (current_function_decl),
"declared here");
}
@@ -11332,7 +11326,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, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
+ (xloc, OPT_Wreturn_mismatch,
"%<return%> with a value, in function returning void");
else
warned_here = pedwarn