From patchwork Thu Oct 19 21:07:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 155708 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp652595vqb; Thu, 19 Oct 2023 14:08:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEBGvrfRd8rdWtC1073hCoW0fnYpGJrIHyMDrGhE/wAcApqkvdRrain3py7GU3euBUjMnB6 X-Received: by 2002:a05:620a:1097:b0:778:90a1:d3e7 with SMTP id g23-20020a05620a109700b0077890a1d3e7mr2865551qkk.41.1697749703197; Thu, 19 Oct 2023 14:08:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697749703; cv=pass; d=google.com; s=arc-20160816; b=bCfG3qwOI+4zniR7C0WlFNHt4l9QeptDH8eMki+MyZiB2gQtzrQDsffoQ8OnI3Cjno nR7mRx2yileXJaHoEGF2MjNuDo+ZtUNl+FIkTZ/Y6R912wOwpKZ0v2rwY8icZ3QIUsrE BUn3P/hvhjMRuzU5o1g2lyZf/wMrR1jZ/WpMdsU9PFwEUh48T0dOpqMMf/GKtw6NmdVB ip57hEKTxyEDs72bBkIojs4ULx/be/zpX2EhqrayZ25Aql/zOIDlAKuL1RjswaAtrmLb XVuK/s+4lwZiGmBUJ7fRpMNeOxiCi/83zPLLCknOqymrFopBTcaACizNofQMPekNRX13 ixGQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:user-agent :message-id:date:subject:to:from:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=Ct31b8vSNzazaT1OycMMc302NtP5EMDkbUKGJcqc5Ls=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=xY8tdqm/IgdjfKhgnE+oWdadTbsxetlFZWaqQSO6hSK/+xdvKZnRLIeKBKVveCTJV3 4msgRIAG58NSkVez5hyPObenfM4XRPGgxSAdyNDJzNQNX/rkgU5P/h/kycW7FtK4Q9IL /uXNaWUkr7kS0XmBJEJXUrh2thIsbbtMRDOJLjXHD4ZshdozYtm6bNi+GvuEqldEJeCv yhi5ud+0T94os/V3M65f92W1YEnRj0qNiXoTeYKgEBsfgbBfefInnwH9dVYtDrdw/rDh sgEiwMoT/nwBXIaytjVmYkVk/1QVcdi5J7L5jdcS+SF36Vf/ymuqmgn9gv3rG98ryzz6 6VeQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJm682g0; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w14-20020a05620a444e00b0076cdf0d3eefsi245002qkp.245.2023.10.19.14.08.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 14:08:23 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJm682g0; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E5A493858D35 for ; Thu, 19 Oct 2023 21:08:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 65FF43858D28 for ; Thu, 19 Oct 2023 21:07:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65FF43858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 65FF43858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697749679; cv=none; b=GB9DaNVqIzrP/fuCq6f3jDk/K47V0gwzZnOM5vFOiBQr+p5epuXv5s7Nslsz1H48M3zWhA5LQ9AaXhh87Tiehm+S9+SI3u9qIOSHP/8RYd0UwxMw9DfxNiWGFcflmB07NBsiEo/pNw5TGqSKWsqz7Fb8MXNWw8pvnJPpUZ/vE8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697749679; c=relaxed/simple; bh=guk7zqcjkE2u+wbE+rbGdEgrvJqL4A0OYZELT6QnI5k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=e6RnC06e8cg7KZodQCWn7pcYM9sta4N/MAa6lWt1PAVm/mTiySPQS8+i95uH+U7ZiGP6GLJR5AruXX+5QJCab5Fn8bp+bSrecxCAx2FhH/G4NxGwaM2BgjC2ZGnldca9Z1xw8V4uW/+xqDfZEUOZd3bhz2o0d/CuiIDP37DSx8k= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697749677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=Ct31b8vSNzazaT1OycMMc302NtP5EMDkbUKGJcqc5Ls=; b=ZJm682g0cW4wKm9aU6X/egQ79TJQr4ybaQawYg1U4vuDVsFIL2LIk+K0Gk09v2N2A9pKfK KvpkuAqBjhXUlG0EM7geRtj1OWOgvJoBtz9H38zzlUkruHScRhcOlwD0xcDWtC0nPymq8f 98+ntiq0kSWm35JRZtbyCY3SMXKPrr0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-413-MH-ziq0VNYKMupnDi9BJfg-1; Thu, 19 Oct 2023 17:07:55 -0400 X-MC-Unique: MH-ziq0VNYKMupnDi9BJfg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 43AD71C05137 for ; Thu, 19 Oct 2023 21:07:55 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 938E62026D4C for ; Thu, 19 Oct 2023 21:07:54 +0000 (UTC) From: Florian Weimer To: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Add -Wreturn-mismatch warning, split from -Wreturn-type Date: Thu, 19 Oct 2023 23:07:53 +0200 Message-ID: <87fs26pbh2.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780219593078980824 X-GMAIL-MSGID: 1780219593078980824 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/noncompile/pr55976-1.c: Change -Werror=return-type to -Werror=return-mismatch. * gcc.dg/noncompile/pr55976-2.c: Likewise. --- gcc/c-family/c.opt | 4 +++ gcc/c/c-typeck.cc | 15 +++-------- gcc/doc/invoke.texi | 35 +++++++++++++++--------- gcc/testsuite/gcc.dg/Wreturn-mismatch-1.c | 40 ++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wreturn-mismatch-2.c | 41 +++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wreturn-mismatch-3.c | 40 ++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wreturn-mismatch-4.c | 40 ++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/Wreturn-mismatch-5.c | 40 ++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/noncompile/pr55976-1.c | 2 +- gcc/testsuite/gcc.dg/noncompile/pr55976-2.c | 2 +- 10 files changed, 234 insertions(+), 25 deletions(-) base-commit: 8f4bbdc28df6e87a7ad5ec5ca191a7a836a4f016 diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 44b9c862c14..4c2ebd08af5 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1263,6 +1263,10 @@ Wreorder C++ ObjC++ Var(warn_reorder) Warning LangEnabledBy(C++ ObjC++,Wall) Warn when the compiler reorders code. +Wreturn-mismatch +C ObjC Var(warn_return_mismatch) Warning Init(1) +Warn whenever void-returning functions return a non-void expressions, or a return expression is missing in a function not returning void. + Wreturn-type C ObjC C++ ObjC++ Var(warn_return_type) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Init(-1) Warn whenever a function's return type defaults to \"int\" (C), or about inconsistent return types (C++). diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 6e044b4afbc..1da622160a3 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -11281,17 +11281,10 @@ 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, - "% with no value, in function returning non-void"); - else - warned_here = warning_at - (loc, OPT_Wreturn_type, - "% with no value, in function returning non-void"); no_warning = true; - if (warned_here) + if (pedwarn (loc, OPT_Wreturn_mismatch, + "% with no value," + " in function returning non-void")) inform (DECL_SOURCE_LOCATION (current_function_decl), "declared here"); } @@ -11302,7 +11295,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, "% with a value, in function returning void"); else warned_here = pedwarn diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 9b5ff457027..c0c155af7cd 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -7338,22 +7338,33 @@ This warning is enabled by @option{-Wall} for C and C++. Do not warn about returning a pointer (or in C++, a reference) to a variable that goes out of scope after the function returns. +@opindex Wreturn-mismatch +@opindex Wno-return-mismatch +@item -Wreturn-mismatch +Warn about return statements without an expressions in functions which +do not return @code{void}. Also warn about a @code{return} statement +with an expression in a function whose return type is @code{void}, +unless the expression type is also @code{void}. As a GNU extension, the +latter case is accepted without a warning unless @option{-Wpedantic} is +used. + +Attempting to use the return value of a non-@code{void} function other +than @code{main} that flows off the end by reaching the closing curly +brace that terminates the function is undefined. + +This warning is specific to C and enabled by default. + @opindex Wreturn-type @opindex Wno-return-type @item -Wreturn-type -Warn whenever a function is defined with a return type that defaults -to @code{int}. Also warn about any @code{return} statement with no -return value in a function whose return type is not @code{void} -(falling off the end of the function body is considered returning -without a value). +Warn whenever a function is defined with a return type that defaults to +@code{int} (unless @option{-Wimplicit-int} is active, which takes +precedence). Also warn if execution may reach the end of the function +body, or if the function does not contain any return statement at all. -For C only, warn about a @code{return} statement with an expression in a -function whose return type is @code{void}, unless the expression type is -also @code{void}. As a GNU extension, the latter case is accepted -without a warning unless @option{-Wpedantic} is used. Attempting -to use the return value of a non-@code{void} function other than @code{main} -that flows off the end by reaching the closing curly brace that terminates -the function is undefined. +Attempting to use the return value of a non-@code{void} function other +than @code{main} that flows off the end by reaching the closing curly +brace that terminates the function is undefined. Unlike in C, in C++, flowing off the end of a non-@code{void} function other than @code{main} results in undefined behavior even when the value of diff --git a/gcc/testsuite/gcc.dg/Wreturn-mismatch-1.c b/gcc/testsuite/gcc.dg/Wreturn-mismatch-1.c new file mode 100644 index 00000000000..3bad847ecf7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-mismatch-1.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ + +void f1 (void); + +int +f2 (void) +{ + f1 (); +} + +static inline int +f3 (void) +{ + f1 (); +} + +void +f4 (void) +{ + return 1; /* { dg-warning "'return' with a value\[^\n\r\]*-Wreturn-mismatch" } */ +} + +void +f5 (void) +{ + return f1 (); /* { dg-bogus "ISO C" } */ +} + +int +f6 (void) +{ + return; /* { dg-warning "'return' with no value\[^\n\r\]*-Wreturn-mismatch" } */ +} + +int +f7 (void) +{ + return f1 (); /* { dg-error "void value not ignored as it ought to be" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wreturn-mismatch-2.c b/gcc/testsuite/gcc.dg/Wreturn-mismatch-2.c new file mode 100644 index 00000000000..49eb5a5a95c --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-mismatch-2.c @@ -0,0 +1,41 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + +void f1 (void); + +int +f2 (void) +{ + f1 (); +} /* { dg-warning "control reaches end of non-void\[^\n\r\]*-Wreturn-type" } */ + +static inline int +f3 (void) +{ + f1 (); +} /* { dg-warning "no return statement in function\[^\n\r\]*-Wreturn-type" } */ + +void +f4 (void) +{ + return 1; /* { dg-warning "with a value,\[^\n\r\]*-Wreturn-mismatch" } */ +} + +void +f5 (void) +{ + return f1 (); +} + +int +f6 (void) +{ + return; /* { dg-warning "with no value,\[^\n\r\]*Wreturn-mismatch" } */ +} + +int +f7 (void) +{ + return f1 (); /* { dg-error "void value not ignored as it ought to be" } */ +} /* { dg-warning "control reaches end of non-void\[^\n\r\]*-Wreturn-type" } */ + diff --git a/gcc/testsuite/gcc.dg/Wreturn-mismatch-3.c b/gcc/testsuite/gcc.dg/Wreturn-mismatch-3.c new file mode 100644 index 00000000000..ee77ec6a769 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-mismatch-3.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "-pedantic-errors" } */ + +void f1 (void); + +int +f2 (void) +{ + f1 (); +} + +static inline int +f3 (void) +{ + f1 (); +} + +void +f4 (void) +{ + return 1; /* { dg-error "with a value,\[^\n\r\]*-Wreturn-mismatch" } */ +} + +void +f5 (void) +{ + return f1 (); /* { dg-error "with expression, in function\[^\n\r\]*-Wpedantic" } */ +} + +int +f6 (void) +{ + return; /* { dg-error "with no value,\[^\n\r\]*Wreturn-mismatch" } */ +} + +int +f7 (void) +{ + return f1 (); /* { dg-error "void value not ignored as it ought to be" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wreturn-mismatch-4.c b/gcc/testsuite/gcc.dg/Wreturn-mismatch-4.c new file mode 100644 index 00000000000..f73e8a05dc6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-mismatch-4.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "-std=gnu89" } */ + +void f1 (void); + +int +f2 (void) +{ + f1 (); +} + +static inline int +f3 (void) +{ + f1 (); +} + +void +f4 (void) +{ + return 1; /* { dg-warning "with a value,\[^\n\r\]*-Wreturn-mismatch" } */ +} + +void +f5 (void) +{ + return f1 (); +} + +int +f6 (void) +{ + return; /* { dg-bogus "with no value" } */ +} + +int +f7 (void) +{ + return f1 (); /* { dg-error "void value not ignored as it ought to be" } */ +} diff --git a/gcc/testsuite/gcc.dg/Wreturn-mismatch-5.c b/gcc/testsuite/gcc.dg/Wreturn-mismatch-5.c new file mode 100644 index 00000000000..f2fd3294b7b --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wreturn-mismatch-5.c @@ -0,0 +1,40 @@ +/* { dg-do compile } */ +/* { dg-options "-std=gnu89 -pedantic-errors" } */ + +void f1 (void); + +int +f2 (void) +{ + f1 (); +} + +static inline int +f3 (void) +{ + f1 (); +} + +void +f4 (void) +{ + return 1; /* { dg-error "with a value,\[^\n\r\]*-Wreturn-mismatch" } */ +} + +void +f5 (void) +{ + return f1 (); /* { dg-error "with expression\[^\n\r\]*-Wpedantic" } */ +} + +int +f6 (void) +{ + return; +} + +int +f7 (void) +{ + return f1 (); /* { dg-error "void value not ignored as it ought to be" } */ +} diff --git a/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c b/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c index d078990bd88..d348b03ad94 100644 --- a/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c +++ b/gcc/testsuite/gcc.dg/noncompile/pr55976-1.c @@ -1,6 +1,6 @@ /* PR c/55976 */ /* { dg-do compile } */ -/* { dg-options "-Werror=return-type" } */ +/* { dg-options "-Werror=return-mismatch" } */ /* { dg-prune-output "some warnings being treated as errors" } */ /* Verify warnings for return type become errors. */ diff --git a/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c b/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c index 0e493d0e8af..a9494255369 100644 --- a/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c +++ b/gcc/testsuite/gcc.dg/noncompile/pr55976-2.c @@ -1,6 +1,6 @@ /* PR c/55976 */ /* { dg-do compile } */ -/* { dg-options "-Wno-return-type" } */ +/* { dg-options "-Wno-return-mismatch" } */ /* Verify that -Wno-return-type turns off warnings about function return type. */