From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id 88F223858D20 for ; Wed, 29 May 2024 13:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88F223858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 88F223858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::332 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716991123; cv=none; b=iZUrDF0RY5Wju9+ukhNBti6N3364HNHveD/cZ1WkhUTrT2JUruJzkzBFTKGqi2ogCMpH2NSOyX6Hg35yH8MGmfwNJHIAwGKRhtT5iTt6dvQN2iNEBOKYN9slWjfWtEBTCUHYLQgHHD4WXvAU28d5m2fdxnnw4KhrESb7hN7wp8Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716991123; c=relaxed/simple; bh=G+O+iDEg0Ibh0QuvmA37Dvasf7CFd5gIVRkTmPyHOw0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GO1PHd370rNWquRtEYyEYjwPsZePzBA9ohsVwJRdi4xWPEytBLunpYXjXbamcDKM6jHorMcJUJ1NZCOkeGge5AQvf1Qn1MNF1u6Bf+7PV8J++bbE6ZNkZNwdPafZvYhpDWbcUtgXG2YHRhfdPAzoaOb9AM5Wjx1pEs8erlGMM7Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-421124a0b37so11716055e9.1 for ; Wed, 29 May 2024 06:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716991120; x=1717595920; darn=gcc.gnu.org; h=mime-version:content-transfer-encoding:msip_labels:content-language :accept-language:message-id:date:thread-index:thread-topic:subject :to:from:from:to:cc:subject:date:message-id:reply-to; bh=+EmXT1gWQf7C/HOBeM8vXSwssgoPYkESjfLh+ICjyTY=; b=iAM8C8YfMsOMq5rfALOv6ddwj4NrbGY4VQduqL21Va7JLF47sOUniAsJ3B/u82Gbsg qL/9Ad0BkLz0Gw1J2iVI4eaxoV0/ORw+dwvUiqED9ixAY5FnOXVOV0lfKVYiqC/c0DG0 vJ3ttQcFafJFM5eusgE8D96TfTj9DrLyOKuehZQ5pA5kSlDUzry4DX9z39bemzxY1NSu G2cKM7VwaTHUBw4tvNNy2+MkssGrk+V6NXMP3CVvaJN4042gXziTEt4k0XpBpJupqvZX Gt9alLSMoyYUlAGwx/lQC152rItKR4NN3biji/t2tVDtv2zJGm08+AO2bX5fTrR2efGl defA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716991120; x=1717595920; h=mime-version:content-transfer-encoding:msip_labels:content-language :accept-language:message-id:date:thread-index:thread-topic:subject :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+EmXT1gWQf7C/HOBeM8vXSwssgoPYkESjfLh+ICjyTY=; b=lQ9f1n+0pvvW7h1SrsHKbTb8NjFThkwHsGLBc3CyLbT+Dmx/Of/nQ0Xsu1wbCCOQaU 8m7xIP4T2MQJNsQ9TY83AC4M3agfahr6tkDWLWfdQEWqy7zcA7eMqwMZ03WISACoTqbU MJpLT0bazKeeGrUzJ5sFBXmdi+1wXldN5WOGF9zCDPlAo0Hp6FELPBqrbFSlowFCW8Nl LhGo1YSI+xD6z+buVLiID5CnWF5qjkmzUOpanraOW8LQfxunGHB3nkHoZFnP4jEv91RT 2GRS4q+PDK3QyrGRROOAF7qOg+zvHn3AwWMhKcLIf+YRnzP2GjauDWXTuSmwFlD+fXwE HUQw== X-Gm-Message-State: AOJu0Yy0gqj9rSDrmcA0tpPoYes2ejLPlTm6tRb+JZLEWJc00MBA8jdh 11ZE8g6xCr76Eme/DQJj2nz13G0ZFbNhsJFogsLBnk7IGdfDs5uUiiszAg== X-Google-Smtp-Source: AGHT+IGvyqP5NaQ3peBx4hfPTn/Oo8TIGG9/Bw84mp/vJAC2Zc4v3eRF5EPd1e/zwgKhAS7JGKUdsw== X-Received: by 2002:a05:600c:6a11:b0:419:f241:633b with SMTP id 5b1f17b1804b1-421089d50d7mr167755585e9.8.1716991119727; Wed, 29 May 2024 06:58:39 -0700 (PDT) Received: from PR3P194MB1714.EURP194.PROD.OUTLOOK.COM ([2603:1026:c0a:8c9::5]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42100fb8f56sm214845145e9.40.2024.05.29.06.58.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 06:58:39 -0700 (PDT) From: Julian Waters To: "gcc-patches@gcc.gnu.org" Subject: [PATCH] c-family: Introduce the -Winvalid-noreturn flag from clang with extra tuneability Thread-Topic: [PATCH] c-family: Introduce the -Winvalid-noreturn flag from clang with extra tuneability Thread-Index: AQHasczXatxCqkGhkUy3bK7Z1f0nYw== X-MS-Exchange-MessageSentRepresentingType: 1 Date: Wed, 29 May 2024 13:58:38 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-Exchange-Organization-SCL: -1 X-MS-TNEF-Correlator: X-MS-Exchange-Organization-RecordReviewCfmType: 0 msip_labels: Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Spam-Status: No, score=-9.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Currently, gcc warns about noreturn marked functions that return both expli= citly and implicitly, with no way to turn this warning off. clang does have= an option for these classes of warnings, -Winvalid-noreturn. However, we c= an do better. Instead of just having 1 option that switches the warnings fo= r both on and off, we can define an extra layer of granularity, and have a = separate options for implicit returns and explicit returns, as in -Winvalid= -return=3Dexplicit and -Winvalid-noreturn=3Dimplicit. This patch adds both = to gcc, for compatibility with clang. Do note that I am relatively new to g= cc's codebase, and as such couldn't figure out how to cleanly define a gene= ral -Winvalid-noreturn warning that switch both on and off, for better comp= atibility with clang. If someone should point out how to do so, I'll happil= y rewrite my patch. I also do not have write access to gcc, and will need h= elp pushing this patch once the green light is given=0A= =0A= best regards,=0A= Julian=0A= =0A= gcc/c-family/ChangeLog:=0A= =0A= * c.opt: Introduce -Winvalid-noreturn=3Dexplicit and -Winvalid-noreturn=3D= implicit=0A= =0A= gcc/ChangeLog:=0A= =0A= * tree-cfg.cc (pass_warn_function_return::execute): Use it=0A= =0A= gcc/c/ChangeLog:=0A= =0A= * c-typeck.cc (c_finish_return): Use it=0A= * gimple-parser.cc (c_finish_gimple_return): Use it=0A= =0A= gcc/config/mingw/ChangeLog:=0A= =0A= * mingw32.h (EXTRA_OS_CPP_BUILTINS): Fix semicolons=0A= =0A= gcc/cp/ChangeLog:=0A= =0A= * coroutines.cc (finish_co_return_stmt): Use it=0A= * typeck.cc (check_return_expr): Use it=0A= =0A= gcc/doc/ChangeLog:=0A= =0A= * invoke.texi: Document new options=0A= =0A= >From 4daf884f8bbc1e318ba93121a6fdf4139da80b64 Mon Sep 17 00:00:00 2001=0A= From: TheShermanTanker =0A= Date: Wed, 29 May 2024 21:32:08 +0800=0A= Subject: [PATCH] Introduce the -Winvalid-noreturn flag from clang with extr= a=0A= tuneability=0A= =0A= Signed-off-by: TheShermanTanker =0A= ---=0A= gcc/c-family/c.opt | 8 ++++++++=0A= gcc/c/c-typeck.cc | 2 +-=0A= gcc/c/gimple-parser.cc | 2 +-=0A= gcc/config/mingw/mingw32.h | 6 +++---=0A= gcc/cp/coroutines.cc | 2 +-=0A= gcc/cp/typeck.cc | 2 +-=0A= gcc/doc/invoke.texi | 13 +++++++++++++=0A= gcc/tree-cfg.cc | 2 +-=0A= 8 files changed, 29 insertions(+), 8 deletions(-)=0A= =0A= diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt=0A= index fb34c3b7031..32a2859fdcc 100644=0A= --- a/gcc/c-family/c.opt=0A= +++ b/gcc/c-family/c.opt=0A= @@ -886,6 +886,14 @@ Winvalid-constexpr=0A= C++ ObjC++ Var(warn_invalid_constexpr) Init(-1) Warning=0A= Warn when a function never produces a constant expression.=0A= =0A= +Winvalid-noreturn=3Dexplicit=0A= +C ObjC C++ ObjC++ Warning=0A= +Warn when a function marked noreturn returns explicitly.=0A= +=0A= +Winvalid-noreturn=3Dimplicit=0A= +C ObjC C++ ObjC++ Warning=0A= +Warn when a function marked noreturn returns implicitly.=0A= +=0A= Winvalid-offsetof=0A= C++ ObjC++ Var(warn_invalid_offsetof) Init(1) Warning=0A= Warn about invalid uses of the \"offsetof\" macro.=0A= diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc=0A= index ad4c7add562..1941fbc44cb 100644=0A= --- a/gcc/c/c-typeck.cc=0A= +++ b/gcc/c/c-typeck.cc=0A= @@ -11468,7 +11468,7 @@ c_finish_return (location_t loc, tree retval, tree = origtype)=0A= location_t xloc =3D expansion_point_location_if_in_system_header (loc);= =0A= =0A= if (TREE_THIS_VOLATILE (current_function_decl))=0A= - warning_at (xloc, 0,=0A= + warning_at (xloc, OPT_Winvalid_noreturn_explicit,=0A= "function declared % has a % statement");=0A= =0A= if (retval)=0A= diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc=0A= index d156d83cd37..1acaf75f844 100644=0A= --- a/gcc/c/gimple-parser.cc=0A= +++ b/gcc/c/gimple-parser.cc=0A= @@ -2593,7 +2593,7 @@ c_finish_gimple_return (location_t loc, tree retval)= =0A= location_t xloc =3D expansion_point_location_if_in_system_header (loc);= =0A= =0A= if (TREE_THIS_VOLATILE (current_function_decl))=0A= - warning_at (xloc, 0,=0A= + warning_at (xloc, OPT_Winvalid_noreturn_explicit,=0A= "function declared % has a % statement");=0A= =0A= if (! retval)=0A= diff --git a/gcc/config/mingw/mingw32.h b/gcc/config/mingw/mingw32.h=0A= index fa6e307476c..a69926133b1 100644=0A= --- a/gcc/config/mingw/mingw32.h=0A= +++ b/gcc/config/mingw/mingw32.h=0A= @@ -35,9 +35,9 @@ along with GCC; see the file COPYING3. If not see=0A= | MASK_MS_BITFIELD_LAYOUT)=0A= =0A= #ifdef TARGET_USING_MCFGTHREAD=0A= -#define DEFINE_THREAD_MODEL builtin_define ("__USING_MCFGTHREAD__");=0A= +#define DEFINE_THREAD_MODEL builtin_define ("__USING_MCFGTHREAD__")=0A= #elif defined(TARGET_USE_PTHREAD_BY_DEFAULT)=0A= -#define DEFINE_THREAD_MODEL builtin_define ("__USING_POSIXTHREAD__");=0A= +#define DEFINE_THREAD_MODEL builtin_define ("__USING_POSIXTHREAD__")=0A= #else=0A= #define DEFINE_THREAD_MODEL=0A= #endif=0A= @@ -60,7 +60,7 @@ along with GCC; see the file COPYING3. If not see=0A= builtin_define_std ("WIN64"); \=0A= builtin_define ("_WIN64"); \=0A= } \=0A= - DEFINE_THREAD_MODEL \=0A= + DEFINE_THREAD_MODEL; \=0A= } \=0A= while (0)=0A= =0A= diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc=0A= index 97bc211ff67..53e56bd4959 100644=0A= --- a/gcc/cp/coroutines.cc=0A= +++ b/gcc/cp/coroutines.cc=0A= @@ -1375,7 +1375,7 @@ finish_co_return_stmt (location_t kw, tree expr)=0A= =0A= /* Makes no sense for a co-routine really. */=0A= if (TREE_THIS_VOLATILE (current_function_decl))=0A= - warning_at (kw, 0,=0A= + warning_at (kw, OPT_Winvalid_noreturn_explicit,=0A= "function declared % has a"=0A= " % statement");=0A= =0A= diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc=0A= index 1b7a31d32f3..74cc59bfb87 100644=0A= --- a/gcc/cp/typeck.cc=0A= +++ b/gcc/cp/typeck.cc=0A= @@ -11062,7 +11062,7 @@ check_return_expr (tree retval, bool *no_warning, b= ool *dangling)=0A= (This is a G++ extension, used to get better code for functions=0A= that call the `volatile' function.) */=0A= if (TREE_THIS_VOLATILE (current_function_decl))=0A= - warning (0, "function declared % has a % statement= ");=0A= + warning (OPT_Winvalid_noreturn_explicit, "function declared % has a % statement");=0A= =0A= /* Check for various simple errors. */=0A= if (DECL_DESTRUCTOR_P (current_function_decl))=0A= diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi=0A= index 2cba380718b..27d880fd4f0 100644=0A= --- a/gcc/doc/invoke.texi=0A= +++ b/gcc/doc/invoke.texi=0A= @@ -376,6 +376,7 @@ Objective-C and Objective-C++ Dialects}.=0A= -Winfinite-recursion=0A= -Winit-self -Winline -Wno-int-conversion -Wint-in-bool-context=0A= -Wno-int-to-pointer-cast -Wno-invalid-memory-model=0A= +-Winvalid-noreturn=3Dexplicit -Winvalid-noreturn=3Dimplicit=0A= -Winvalid-pch -Winvalid-utf8 -Wno-unicode -Wjump-misses-init=0A= -Wlarger-than=3D@var{byte-size} -Wlogical-not-parentheses -Wlogical-op= =0A= -Wlong-long -Wno-lto-type-mismatch -Wmain -Wmaybe-uninitialized=0A= @@ -10482,6 +10483,18 @@ an error. @option{Wint-to-pointer-cast} is enabled= by default.=0A= Suppress warnings from casts from a pointer to an integer type of a=0A= different size.=0A= =0A= +@opindex Winvalid-noreturn=3Dexplicit=0A= +@opindex Wno-invalid-noreturn=3Dexplicit=0A= +@item -Winvalid-noreturn=3Dexplicit=0A= +Warn if a function marked noreturn returns explicitly, that is, it=0A= +contains a return statement.=0A= +=0A= +@opindex Winvalid-noreturn=3Dimplicit=0A= +@opindex Wno-invalid-noreturn=3Dimplicit=0A= +@item -Winvalid-noreturn=3Dimplicit=0A= +Warn if a function marked noreturn returns implicitly, that is, it=0A= +has a path in its control flow that can reach the end of its body.=0A= +=0A= @opindex Winvalid-pch=0A= @opindex Wno-invalid-pch=0A= @item -Winvalid-pch=0A= diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc=0A= index 7fb7b92966b..cfe91038dd1 100644=0A= --- a/gcc/tree-cfg.cc=0A= +++ b/gcc/tree-cfg.cc=0A= @@ -9817,7 +9817,7 @@ pass_warn_function_return::execute (function *fun)=0A= }=0A= if (location =3D=3D UNKNOWN_LOCATION)=0A= location =3D cfun->function_end_locus;=0A= - warning_at (location, 0, "% function does return");=0A= + warning_at (location, OPT_Winvalid_noreturn_implicit, "% = function does return");=0A= }=0A= =0A= /* If we see "return;" in some basic block, then we do reach the end=0A= -- =0A= 2.41.0=0A= =0A=