From patchwork Sun Apr 23 07:52:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Church X-Patchwork-Id: 86668 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2076998vqo; Sun, 23 Apr 2023 00:53:14 -0700 (PDT) X-Google-Smtp-Source: AKy350YWdd3GLlK0+jx/+GgN+yoF8ECHQEo20DMg9FFKGBmnEXzddWu/R17EDNEMt9ZwNP/ru68k X-Received: by 2002:a17:906:970c:b0:94a:474a:4dd5 with SMTP id k12-20020a170906970c00b0094a474a4dd5mr7621553ejx.9.1682236394376; Sun, 23 Apr 2023 00:53:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682236394; cv=none; d=google.com; s=arc-20160816; b=H/jcwUjxDp8kFq7LSBwoyYoJkEfzq4zQYB9pRfpjJOWaYZqwIyedePewO1DmMiY8qf k/U/mfiiD+PJVTq/pb1yeUpxNAmMt8tg3Gm8bd6d5FwmVq8UBZNiVoLHuye0O51l82yp Ot9AQW/uhCCbKPIG00BIjY1p+5Ix+I1rqbpTvcrOyRe6FemoUiBjbgk6Hye1FkyDlFtU ZCYbSEHJfNWN0S18Ub1bSLjRjF+icGNLY0EwR/h8Hz5+JYhyHJPievT6E4OIDA4Bt4VP YkKFPp+FmYFylpZMao9+o5sjrdqQcT4xIyCxINicEyZzNXff3av4kn4aSkU55Dn8qLur ZJLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=T/lPfgjsawD14DMJNCsVqBPGMXkCZ2ocmGy9dPdMknM=; b=KUNTaBgHXi0oy11cAi2a8pijgjzHzSUntaYVpDH6hSF2+MwUiGPVMPmfU2hP5kL5Lw 6N/2gLsAiL4z4QrtLKlFv0sZtCgP6I1rJSPkiRuQLSnLrohMAT9LcVwaGlk/338Tjsgn ZRLjoFkq90bJ1N8uW6sV8ryo7wkMJh35I5qEyWpez/lYuE7X8Zs2siLOVgHqR4bhj/6U XhjAp2jBU7dui1LM1TGtG5k5mXCXoCcHpH8u06dKFySmHlkJuDYO/tTFbsWFr00736cV kKphOqVhyZwUoSzwrk4Yn812UE06OjQchMoTTaHCRhV2BuMkGBy4CXo7ohQdriuoojpa YbTg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k26-20020a17090646da00b0094f1ec5ea4csi6566508ejs.193.2023.04.23.00.53.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Apr 2023 00:53:14 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A76BA3858005 for ; Sun, 23 Apr 2023 07:53:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from achurch.org (p105139-ipoefx.ipoe.ocn.ne.jp [153.246.140.138]) by sourceware.org (Postfix) with ESMTPS id 8CBCC3858C74 for ; Sun, 23 Apr 2023 07:52:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8CBCC3858C74 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=achurch.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=achurch.org X-wmail-status: normal Received: by achurch.org (wmail 0.10.6) id 6444e3c7.16516; Sun, 23 Apr 2023 16:52:39 +0900 From: Andrew Church To: gcc-patches@gcc.gnu.org Subject: [PATCH] PoC: add -Wunused-result=strict Date: Sun, 23 Apr 2023 16:52:38 +0900 X-Mailer: MMail v5.54 Message-ID: <6444e3c7.16516@msgid.achurch.org> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 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 Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763952709143569812?= X-GMAIL-MSGID: =?utf-8?q?1763952709143569812?= As requested in , this is a proof-of-concept patch to change -Wunused-result to not warn about return values explicitly discarded by casting to void, and add -Wunused-result=strict for the current behavior (warn even on void casts). The core behavior change is based on an earlier patch at ; it appears to do the correct thing based on the tests added by the patch, but it also breaks a number of other attribute- and analyzer-related tests, so some fine-tuning is probably needed (and I don't have the GCC internals knowledge to know where to start). I haven't touched [[nodiscard]] behavior, since GCC already ignores explicit discards in that case; for consistency's sake, it might make sense to also warn for explicitly discarded [[nodiscard]] values with -Wunused-result=strict. Please CC me on any replies, as I am not subscribed to the list. --Andrew Church https://achurch.org/ diff -urN gcc-12.2.0-orig/gcc/c-family/c.opt gcc-12.2.0/gcc/c-family/c.opt --- gcc-12.2.0-orig/gcc/c-family/c.opt 2022-08-19 17:09:52 +0900 +++ gcc-12.2.0/gcc/c-family/c.opt 2023-04-23 03:39:58 +0900 @@ -1361,6 +1361,10 @@ C ObjC C++ ObjC++ Var(warn_unused_result) Init(1) Warning Warn if a caller of a function, marked with attribute warn_unused_result, does not use its return value. +Wunused-result=strict +C ObjC C++ ObjC++ RejectNegative Var(warn_unused_result,2) Init(1) Warning +Warn if a caller of a function, marked with attribute warn_unused_result, does not use its return value. + Wunused-variable C ObjC C++ ObjC++ LangEnabledBy(C ObjC C++ ObjC++,Wunused) ; documented in common.opt diff -urN gcc-12.2.0-orig/gcc/doc/invoke.texi gcc-12.2.0/gcc/doc/invoke.texi --- gcc-12.2.0-orig/gcc/doc/invoke.texi 2022-08-19 17:09:52 +0900 +++ gcc-12.2.0/gcc/doc/invoke.texi 2023-04-23 03:32:36 +0900 @@ -406,7 +406,7 @@ -Wunused-const-variable -Wunused-const-variable=@var{n} @gol -Wunused-function -Wunused-label -Wunused-local-typedefs @gol -Wunused-macros @gol --Wunused-parameter -Wno-unused-result @gol +-Wunused-parameter -Wunused-result -Wunused-result=strict @gol -Wunused-value -Wunused-variable @gol -Wno-varargs -Wvariadic-macros @gol -Wvector-operation-performance @gol @@ -7037,12 +7037,20 @@ To suppress this warning use the @code{unused} attribute (@pxref{Variable Attributes}). -@item -Wno-unused-result +@item -Wunused-result @opindex Wunused-result @opindex Wno-unused-result -Do not warn if a caller of a function marked with attribute +Warn if a caller of a function marked with attribute @code{warn_unused_result} (@pxref{Function Attributes}) does not use -its return value. The default is @option{-Wunused-result}. +its return value, unless the return value is explicitly discarded with a +cast to @code{void}. This warning is enabled by default. + +@item -Wunused-result=strict +@opindex Wunused-result=strict +Warn if a caller of a function marked with attribute +@code{warn_unused_result} (@pxref{Function Attributes}) does not use +its return value, even if the return value is explicitly discarded with +a cast to @code{void}. @item -Wunused-variable @opindex Wunused-variable diff -urN gcc-12.2.0-orig/gcc/gimplify.cc gcc-12.2.0/gcc/gimplify.cc --- gcc-12.2.0-orig/gcc/gimplify.cc 2022-08-19 17:09:52 +0900 +++ gcc-12.2.0/gcc/gimplify.cc 2023-04-23 03:34:02 +0900 @@ -15183,10 +15183,18 @@ || fallback == fb_none) { /* Just strip a conversion to void (or in void context) and - try again. */ - *expr_p = TREE_OPERAND (*expr_p, 0); - ret = GS_OK; - break; + try again. But if this is a function call cast to void + and strict unused-result warnings are not enabled, + preserve the cast so that do_warn_unused_result() knows + not to emit a warning. */ + if (!(warn_unused_result == 1 + && TREE_CODE (TREE_OPERAND (*expr_p, 0)) == CALL_EXPR + && VOID_TYPE_P (TREE_TYPE (*expr_p)))) + { + *expr_p = TREE_OPERAND (*expr_p, 0); + ret = GS_OK; + break; + } } ret = gimplify_conversion (expr_p); diff -urN gcc-12.2.0-orig/gcc/testsuite/c-c++-common/attr-warn-unused-result.c gcc-12.2.0/gcc/testsuite/c-c++-common/attr-warn-unused-result.c --- gcc-12.2.0-orig/gcc/testsuite/c-c++-common/attr-warn-unused-result.c 2022-08-19 17:09:53 +0900 +++ gcc-12.2.0/gcc/testsuite/c-c++-common/attr-warn-unused-result.c 2023-04-22 20:01:42 +0900 @@ -1,6 +1,6 @@ /* warn_unused_result attribute tests. */ /* { dg-do compile } */ -/* { dg-options "-O -ftrack-macro-expansion=0" } */ +/* { dg-options "-O -ftrack-macro-expansion=0 -Wunused-result=strict" } */ #define WUR __attribute__((warn_unused_result)) #define WURAI __attribute__((warn_unused_result, always_inline)) inline diff -urN gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-2.c gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-2.c --- gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-2.c 1970-01-01 09:00:00 +0900 +++ gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-2.c 2023-04-23 04:17:01 +0900 @@ -0,0 +1,11 @@ +/* Test for -Wno-unused-result inhibiting all unused-result warnings. + { dg-do compile } + { dg-options "-Wno-unused-result" } */ + +__attribute__ ((warn_unused_result)) int fwur (void); + +void wur (void) +{ + fwur (); // no warning + (void) fwur (); // no warning +} diff -urN gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-3.c gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-3.c --- gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-3.c 1970-01-01 09:00:00 +0900 +++ gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-3.c 2023-04-23 03:29:19 +0900 @@ -0,0 +1,12 @@ +/* Test for -Wunused-result inhibiting unused-result warnings when the + value is explicitly cast to void. + { dg-do compile } + { dg-options "-Wunused-result" } */ + +__attribute__ ((warn_unused_result)) int fwur (void); + +void wur (void) +{ + fwur (); // { dg-warning "\\\[-Wunused-result" } + (void) fwur (); // no warning +} diff -urN gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-4.c gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-4.c --- gcc-12.2.0-orig/gcc/testsuite/gcc.dg/Wunused-result-4.c 1970-01-01 09:00:00 +0900 +++ gcc-12.2.0/gcc/testsuite/gcc.dg/Wunused-result-4.c 2023-04-23 03:29:35 +0900 @@ -0,0 +1,12 @@ +/* Test for -Wunused-result=strict emitting unused-result warnings even + when the value is explicitly cast to void. + { dg-do compile } + { dg-options "-Wunused-result=strict" } */ + +__attribute__ ((warn_unused_result)) int fwur (void); + +void wur (void) +{ + fwur (); // { dg-warning "\\\[-Wunused-result" } + (void) fwur (); // { dg-warning "\\\[-Wunused-result" } +}