Message ID | 20220726190340.432777-1-polacek@redhat.com |
---|---|
State | New, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2442991pxt; Tue, 26 Jul 2022 12:04:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vJRwYLXKWHH6fodsuW5/rtzWxta4csdxGzYL/dhc1EYTFtxQSoAGAjV2P0cZnd8+tHb4AE X-Received: by 2002:a05:6402:b84:b0:43c:1ab7:9544 with SMTP id cf4-20020a0564020b8400b0043c1ab79544mr8990608edb.53.1658862296456; Tue, 26 Jul 2022 12:04:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658862296; cv=none; d=google.com; s=arc-20160816; b=gfcvlyYA6ffoKFLJ+KzWPYmbQP+qbUgxdLtlK2njwwC09hX/W3USwRah4XKTfkSRSE DRW77LrO9tLC7lqAmQbAK7rz6zCqlZV5OiiowBrhehpQZ7S7xEkBn207I9x33Ow52pqP NKz3P0Z98CdE6WxolRX7Y+kBJmQq9EGSFhmHM9+5uqxgsVokxL1dXTZ2pVPZp4VShn4F Vt+XNbS/ibvCaLGbv0K9RkqioTR3e9Sz1dCk9ReuZJ8xLHv8FZV6O6t5wTkDdfl964mQ Oi7PyiKBBQIV3SDL2wyOHW3L4swOAGcpw2JxD+MIxfgSbl97osYR2qtb9xUhQf8hcMib YiLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=iWLFyG7v7I61Pqvk11UsOfBUzTjLcox0tEjav4lKNoc=; b=qvC+Ws5V5RSbCljNLP7g0yyd2C4quD4fnkGavqjRw8mmGo07Ji/DwUbdfvo5ilpy8k YiCRlgfNLuEeURf3crU8ulYzIgDSM4+qYFM9PHEflJMlZiED8PHT7FfCuhsFZLO7jLFh gzDB+FPON7fA0hgmdK/hW/Z/fcljud9K1YyFnLPGD8AhpJ2ENk9VxmRSwbctHa9t8lNb yekboqPCa43Pq6cNdE+4EG3l8kjom0tEbVrtLJUHnw9WC7oMa6zfUXN8u1d5i0Vx8s+d CDqYRAe/Fuz4Zz28naV7k7DhS+TWc80F4w5vowPV6cgO0jCKR0AIC7yVPRruYX6FiWqc JkmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YPCFIkde; 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=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sc40-20020a1709078a2800b00726cf00b7c0si18148272ejc.469.2022.07.26.12.04.56 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 12:04:56 -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=@gcc.gnu.org header.s=default header.b=YPCFIkde; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 542A638582B3 for <ouuuleilei@gmail.com>; Tue, 26 Jul 2022 19:04:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 542A638582B3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658862295; bh=iWLFyG7v7I61Pqvk11UsOfBUzTjLcox0tEjav4lKNoc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=YPCFIkdemtFOj5hLR6YB4PpcCpkrBgPiTuI07M+CL3y7GdsWwT29L6V+TfKjN2ZLX 8wE21LcCL9Og1hOygajckZEiHthXy71ZR9m2HGUGJtw6RkgorE7IF9Fhz5nO9xjMAs 1dDdG17VpabfLWm70/Q32lRz9P3XqORpjfw2RS0U= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 49E573858407 for <gcc-patches@gcc.gnu.org>; Tue, 26 Jul 2022 19:04:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 49E573858407 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-426-0HEoZQ_1M6aSpsPErCu8Nw-1; Tue, 26 Jul 2022 15:04:09 -0400 X-MC-Unique: 0HEoZQ_1M6aSpsPErCu8Nw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6839F806015; Tue, 26 Jul 2022 19:03:50 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.32.245]) by smtp.corp.redhat.com (Postfix) with ESMTP id 138861415118; Tue, 26 Jul 2022 19:03:49 +0000 (UTC) To: GCC Patches <gcc-patches@gcc.gnu.org> Subject: [PATCH] c-family: Honor -Wno-init-self for cv-qual vars [PR102633] Date: Tue, 26 Jul 2022 15:03:40 -0400 Message-Id: <20220726190340.432777-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.5 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, 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.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Marek Polacek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Marek Polacek <polacek@redhat.com> Cc: Richard Biener <rguenther@suse.de>, Joseph Myers <joseph@codesourcery.com> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739443191410929611?= X-GMAIL-MSGID: =?utf-8?q?1739443191410929611?= |
Series |
c-family: Honor -Wno-init-self for cv-qual vars [PR102633]
|
|
Commit Message
Marek Polacek
July 26, 2022, 7:03 p.m. UTC
Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r conversion by creating a NOP_EXPR. For e.g. const int i = i; that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. Consequently, we don't suppress_warning here: 711 case DECL_EXPR: 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) 719 && !warn_init_self) 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); because of the check on line 718 -- (int) i is not i. So -Wno-init-self doesn't disable the warning as it's supposed to. The following patch fixes it...except it doesn't, for volatile variables in C++. The problem is that for volatile int k = k; we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic initialization. So there's no DECL_INITIAL and the suppress_warning call above is never done. I suppose we could amend get_no_uninit_warning to return true for volatile-qualified expressions. I mean, can we ever say for a fact that a volatile variable is uninitialized? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR middle-end/102633 gcc/c-family/ChangeLog: * c-gimplify.cc (c_gimplify_expr): Strip NOPs of DECL_INITIAL. gcc/testsuite/ChangeLog: * c-c++-common/Winit-self1.c: New test. * c-c++-common/Winit-self2.c: New test. --- gcc/c-family/c-gimplify.cc | 18 +++++++------ gcc/testsuite/c-c++-common/Winit-self1.c | 32 ++++++++++++++++++++++++ gcc/testsuite/c-c++-common/Winit-self2.c | 31 +++++++++++++++++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Winit-self1.c create mode 100644 gcc/testsuite/c-c++-common/Winit-self2.c base-commit: 600956c81c784f4a0cc9d10f6e03e01847afd961
Comments
On 7/26/22 15:03, Marek Polacek wrote: > Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r > conversion by creating a NOP_EXPR. For e.g. > > const int i = i; > > that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. > Consequently, we don't suppress_warning here: > > 711 case DECL_EXPR: > 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) > 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > 719 && !warn_init_self) > 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > > because of the check on line 718 -- (int) i is not i. So -Wno-init-self > doesn't disable the warning as it's supposed to. > > The following patch fixes it...except it doesn't, for volatile variables > in C++. The problem is that for > > volatile int k = k; > > we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic > initialization. So there's no DECL_INITIAL and the suppress_warning > call above is never done. I suppose we could amend get_no_uninit_warning > to return true for volatile-qualified expressions. I mean, can we ever > say for a fact that a volatile variable is uninitialized? > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR middle-end/102633 > > gcc/c-family/ChangeLog: > > * c-gimplify.cc (c_gimplify_expr): Strip NOPs of DECL_INITIAL. I wonder if we want to handle this i = i case earlier, like in finish_decl. > gcc/testsuite/ChangeLog: > > * c-c++-common/Winit-self1.c: New test. > * c-c++-common/Winit-self2.c: New test. > --- > gcc/c-family/c-gimplify.cc | 18 +++++++------ > gcc/testsuite/c-c++-common/Winit-self1.c | 32 ++++++++++++++++++++++++ > gcc/testsuite/c-c++-common/Winit-self2.c | 31 +++++++++++++++++++++++ > 3 files changed, 74 insertions(+), 7 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Winit-self1.c > create mode 100644 gcc/testsuite/c-c++-common/Winit-self2.c > > diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc > index a6f26c9b0d3..2e011830846 100644 > --- a/gcc/c-family/c-gimplify.cc > +++ b/gcc/c-family/c-gimplify.cc > @@ -712,13 +712,17 @@ c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED, > /* This is handled mostly by gimplify.cc, but we have to deal with > not warning about int x = x; as it is a GCC extension to turn off > this warning but only if warn_init_self is zero. */ > - if (VAR_P (DECL_EXPR_DECL (*expr_p)) > - && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > - && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > - && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > - && !warn_init_self) > - suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > - break; > + { > + tree &decl = DECL_EXPR_DECL (*expr_p); > + if (VAR_P (decl) > + && !DECL_EXTERNAL (decl) > + && !TREE_STATIC (decl) > + && (DECL_INITIAL (decl) > + && tree_strip_nop_conversions (DECL_INITIAL (decl)) == decl) > + && !warn_init_self) > + suppress_warning (decl, OPT_Winit_self); > + break; > + } > > case PREINCREMENT_EXPR: > case PREDECREMENT_EXPR: > diff --git a/gcc/testsuite/c-c++-common/Winit-self1.c b/gcc/testsuite/c-c++-common/Winit-self1.c > new file mode 100644 > index 00000000000..2a1a755fc71 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Winit-self1.c > @@ -0,0 +1,32 @@ > +/* PR middle-end/102633 */ > +/* { dg-do compile } */ > +/* { dg-options "-Wuninitialized -Wno-init-self" } */ > + > +int > +fn1 (void) > +{ > + int i = i; > + return i; > +} > + > +int > +fn2 () > +{ > + const int j = j; > + return j; > +} > + > +int > +fn3 () > +{ > + /* ??? Do we want this warning in C++? Probably not with -Wno-init-self. */ > + volatile int k = k; /* { dg-warning "used uninitialized" "" { target c++ } } */ > + return k; > +} > + > +int > +fn4 () > +{ > + const volatile int l = l; /* { dg-warning "used uninitialized" "" { target c++ } } */ > + return l; > +} > diff --git a/gcc/testsuite/c-c++-common/Winit-self2.c b/gcc/testsuite/c-c++-common/Winit-self2.c > new file mode 100644 > index 00000000000..13aa9efdf26 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Winit-self2.c > @@ -0,0 +1,31 @@ > +/* PR middle-end/102633 */ > +/* { dg-do compile } */ > +/* { dg-options "-Wuninitialized -Winit-self" } */ > + > +int > +fn1 (void) > +{ > + int i = i; /* { dg-warning "used uninitialized" } */ > + return i; > +} > + > +int > +fn2 () > +{ > + const int j = j; /* { dg-warning "used uninitialized" } */ > + return j; > +} > + > +int > +fn3 () > +{ > + volatile int k = k; /* { dg-warning "used uninitialized" } */ > + return k; > +} > + > +int > +fn4 () > +{ > + const volatile int l = l; /* { dg-warning "used uninitialized" } */ > + return l; > +} > > base-commit: 600956c81c784f4a0cc9d10f6e03e01847afd961
On Tue, Jul 26, 2022 at 04:24:18PM -0400, Jason Merrill wrote: > On 7/26/22 15:03, Marek Polacek wrote: > > Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r > > conversion by creating a NOP_EXPR. For e.g. > > > > const int i = i; > > > > that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. > > Consequently, we don't suppress_warning here: > > > > 711 case DECL_EXPR: > > 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) > > 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > > 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > > 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > > 719 && !warn_init_self) > > 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > > > > because of the check on line 718 -- (int) i is not i. So -Wno-init-self > > doesn't disable the warning as it's supposed to. > > > > The following patch fixes it...except it doesn't, for volatile variables > > in C++. The problem is that for > > > > volatile int k = k; > > > > we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic > > initialization. So there's no DECL_INITIAL and the suppress_warning > > call above is never done. I suppose we could amend get_no_uninit_warning > > to return true for volatile-qualified expressions. I mean, can we ever > > say for a fact that a volatile variable is uninitialized? > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR middle-end/102633 > > > > gcc/c-family/ChangeLog: > > > > * c-gimplify.cc (c_gimplify_expr): Strip NOPs of DECL_INITIAL. > > I wonder if we want to handle this i = i case earlier, like in finish_decl. I could, something like @@ -5381,7 +5381,14 @@ finish_decl (tree decl, location_t init_loc, tree init, init = NULL_TREE; if (init) - store_init_value (init_loc, decl, init, origtype); + { + /* In the self-init case, undo the artificial NOP_EXPR we may have + added in convert_lvalue_to_rvalue so that c_gimplify_expr/DECL_EXPR + can perform suppress_warning. */ + if (TREE_CODE (init) == NOP_EXPR && TREE_OPERAND (init, 0) == decl) + init = TREE_OPERAND (init, 0); + store_init_value (init_loc, decl, init, origtype); + } but then I'd have to do the same thing in cp_finish_decl because decay_conversion also adds a NOP_EXPR for cv-qualified non-class prvalues. Is that what we want? To me that seems less clean than having c_gimplify_expr see through NOP_EXPRs. Marek
On Tue, 26 Jul 2022, Marek Polacek wrote: > Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r > conversion by creating a NOP_EXPR. For e.g. > > const int i = i; > > that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. > Consequently, we don't suppress_warning here: > > 711 case DECL_EXPR: > 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) > 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > 719 && !warn_init_self) > 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > > because of the check on line 718 -- (int) i is not i. So -Wno-init-self > doesn't disable the warning as it's supposed to. > > The following patch fixes it...except it doesn't, for volatile variables > in C++. The problem is that for > > volatile int k = k; > > we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic > initialization. So there's no DECL_INITIAL and the suppress_warning > call above is never done. I suppose we could amend get_no_uninit_warning > to return true for volatile-qualified expressions. I mean, can we ever > say for a fact that a volatile variable is uninitialized? As I said in the bug the bug is probably that we emit uninitialized diagnostics for volatiles at all? OTOH 'volatile' is recommended for vars live around setjmp/longjmp and there diagnostics would be welcome. It's probably the difference between "compiler-hands-off" and "hardware-controlled" :/ > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR middle-end/102633 > > gcc/c-family/ChangeLog: > > * c-gimplify.cc (c_gimplify_expr): Strip NOPs of DECL_INITIAL. > > gcc/testsuite/ChangeLog: > > * c-c++-common/Winit-self1.c: New test. > * c-c++-common/Winit-self2.c: New test. > --- > gcc/c-family/c-gimplify.cc | 18 +++++++------ > gcc/testsuite/c-c++-common/Winit-self1.c | 32 ++++++++++++++++++++++++ > gcc/testsuite/c-c++-common/Winit-self2.c | 31 +++++++++++++++++++++++ > 3 files changed, 74 insertions(+), 7 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Winit-self1.c > create mode 100644 gcc/testsuite/c-c++-common/Winit-self2.c > > diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc > index a6f26c9b0d3..2e011830846 100644 > --- a/gcc/c-family/c-gimplify.cc > +++ b/gcc/c-family/c-gimplify.cc > @@ -712,13 +712,17 @@ c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED, > /* This is handled mostly by gimplify.cc, but we have to deal with > not warning about int x = x; as it is a GCC extension to turn off > this warning but only if warn_init_self is zero. */ > - if (VAR_P (DECL_EXPR_DECL (*expr_p)) > - && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > - && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > - && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > - && !warn_init_self) > - suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > - break; > + { > + tree &decl = DECL_EXPR_DECL (*expr_p); > + if (VAR_P (decl) > + && !DECL_EXTERNAL (decl) > + && !TREE_STATIC (decl) > + && (DECL_INITIAL (decl) > + && tree_strip_nop_conversions (DECL_INITIAL (decl)) == decl) > + && !warn_init_self) > + suppress_warning (decl, OPT_Winit_self); > + break; > + } > > case PREINCREMENT_EXPR: > case PREDECREMENT_EXPR: > diff --git a/gcc/testsuite/c-c++-common/Winit-self1.c b/gcc/testsuite/c-c++-common/Winit-self1.c > new file mode 100644 > index 00000000000..2a1a755fc71 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Winit-self1.c > @@ -0,0 +1,32 @@ > +/* PR middle-end/102633 */ > +/* { dg-do compile } */ > +/* { dg-options "-Wuninitialized -Wno-init-self" } */ > + > +int > +fn1 (void) > +{ > + int i = i; > + return i; > +} > + > +int > +fn2 () > +{ > + const int j = j; > + return j; > +} > + > +int > +fn3 () > +{ > + /* ??? Do we want this warning in C++? Probably not with -Wno-init-self. */ > + volatile int k = k; /* { dg-warning "used uninitialized" "" { target c++ } } */ > + return k; > +} > + > +int > +fn4 () > +{ > + const volatile int l = l; /* { dg-warning "used uninitialized" "" { target c++ } } */ > + return l; > +} > diff --git a/gcc/testsuite/c-c++-common/Winit-self2.c b/gcc/testsuite/c-c++-common/Winit-self2.c > new file mode 100644 > index 00000000000..13aa9efdf26 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Winit-self2.c > @@ -0,0 +1,31 @@ > +/* PR middle-end/102633 */ > +/* { dg-do compile } */ > +/* { dg-options "-Wuninitialized -Winit-self" } */ > + > +int > +fn1 (void) > +{ > + int i = i; /* { dg-warning "used uninitialized" } */ > + return i; > +} > + > +int > +fn2 () > +{ > + const int j = j; /* { dg-warning "used uninitialized" } */ > + return j; > +} > + > +int > +fn3 () > +{ > + volatile int k = k; /* { dg-warning "used uninitialized" } */ > + return k; > +} > + > +int > +fn4 () > +{ > + const volatile int l = l; /* { dg-warning "used uninitialized" } */ > + return l; > +} > > base-commit: 600956c81c784f4a0cc9d10f6e03e01847afd961 >
On Wed, Jul 27, 2022 at 06:41:09AM +0000, Richard Biener via Gcc-patches wrote: > On Tue, 26 Jul 2022, Marek Polacek wrote: > > > Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r > > conversion by creating a NOP_EXPR. For e.g. > > > > const int i = i; > > > > that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. > > Consequently, we don't suppress_warning here: > > > > 711 case DECL_EXPR: > > 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) > > 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) > > 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) > > 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) > > 719 && !warn_init_self) > > 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); > > > > because of the check on line 718 -- (int) i is not i. So -Wno-init-self > > doesn't disable the warning as it's supposed to. > > > > The following patch fixes it...except it doesn't, for volatile variables > > in C++. The problem is that for > > > > volatile int k = k; > > > > we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic > > initialization. So there's no DECL_INITIAL and the suppress_warning > > call above is never done. I suppose we could amend get_no_uninit_warning > > to return true for volatile-qualified expressions. I mean, can we ever > > say for a fact that a volatile variable is uninitialized? > > As I said in the bug the bug is probably that we emit uninitialized > diagnostics for volatiles at all? Non-volatile const variables also have this problem, so I think we still need this patch (or something like it). > OTOH 'volatile' is recommended > for vars live around setjmp/longjmp and there diagnostics would be > welcome. It's probably the difference between "compiler-hands-off" > and "hardware-controlled" :/ Ah, you're right. Then I guess it's better to leave the warning be, (but we still have a discrepancy between C and C++). Marek
On 7/26/22 14:31, Marek Polacek wrote: > On Tue, Jul 26, 2022 at 04:24:18PM -0400, Jason Merrill wrote: >> On 7/26/22 15:03, Marek Polacek wrote: >>> Since r11-5188-g32934a4f45a721, we drop qualifiers during l-to-r >>> conversion by creating a NOP_EXPR. For e.g. >>> >>> const int i = i; >>> >>> that means that the DECL_INITIAL is '(int) i' and not 'i' anymore. >>> Consequently, we don't suppress_warning here: >>> >>> 711 case DECL_EXPR: >>> 715 if (VAR_P (DECL_EXPR_DECL (*expr_p)) >>> 716 && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) >>> 717 && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) >>> 718 && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) >>> 719 && !warn_init_self) >>> 720 suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); >>> >>> because of the check on line 718 -- (int) i is not i. So -Wno-init-self >>> doesn't disable the warning as it's supposed to. >>> >>> The following patch fixes it...except it doesn't, for volatile variables >>> in C++. The problem is that for >>> >>> volatile int k = k; >>> >>> we see that the initializer has TREE_SIDE_EFFECTS, so we perform dynamic >>> initialization. So there's no DECL_INITIAL and the suppress_warning >>> call above is never done. I suppose we could amend get_no_uninit_warning >>> to return true for volatile-qualified expressions. I mean, can we ever >>> say for a fact that a volatile variable is uninitialized? >>> >>> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? >>> >>> PR middle-end/102633 >>> >>> gcc/c-family/ChangeLog: >>> >>> * c-gimplify.cc (c_gimplify_expr): Strip NOPs of DECL_INITIAL. >> >> I wonder if we want to handle this i = i case earlier, like in finish_decl. > > I could, something like > > @@ -5381,7 +5381,14 @@ finish_decl (tree decl, location_t init_loc, tree init, > init = NULL_TREE; > > if (init) > - store_init_value (init_loc, decl, init, origtype); > + { > + /* In the self-init case, undo the artificial NOP_EXPR we may have > + added in convert_lvalue_to_rvalue so that c_gimplify_expr/DECL_EXPR > + can perform suppress_warning. */ > + if (TREE_CODE (init) == NOP_EXPR && TREE_OPERAND (init, 0) == decl) > + init = TREE_OPERAND (init, 0); > + store_init_value (init_loc, decl, init, origtype); > + } > > but then I'd have to do the same thing in cp_finish_decl because > decay_conversion also adds a NOP_EXPR for cv-qualified non-class prvalues. > Is that what we want? To me that seems less clean than having c_gimplify_expr > see through NOP_EXPRs. I was thinking of checking the form of the initializer before decay_conversion or anything else messes with it, and calling suppress_warning at that point instead of in c_gimplify_expr. Jason
diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index a6f26c9b0d3..2e011830846 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -712,13 +712,17 @@ c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED, /* This is handled mostly by gimplify.cc, but we have to deal with not warning about int x = x; as it is a GCC extension to turn off this warning but only if warn_init_self is zero. */ - if (VAR_P (DECL_EXPR_DECL (*expr_p)) - && !DECL_EXTERNAL (DECL_EXPR_DECL (*expr_p)) - && !TREE_STATIC (DECL_EXPR_DECL (*expr_p)) - && (DECL_INITIAL (DECL_EXPR_DECL (*expr_p)) == DECL_EXPR_DECL (*expr_p)) - && !warn_init_self) - suppress_warning (DECL_EXPR_DECL (*expr_p), OPT_Winit_self); - break; + { + tree &decl = DECL_EXPR_DECL (*expr_p); + if (VAR_P (decl) + && !DECL_EXTERNAL (decl) + && !TREE_STATIC (decl) + && (DECL_INITIAL (decl) + && tree_strip_nop_conversions (DECL_INITIAL (decl)) == decl) + && !warn_init_self) + suppress_warning (decl, OPT_Winit_self); + break; + } case PREINCREMENT_EXPR: case PREDECREMENT_EXPR: diff --git a/gcc/testsuite/c-c++-common/Winit-self1.c b/gcc/testsuite/c-c++-common/Winit-self1.c new file mode 100644 index 00000000000..2a1a755fc71 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Winit-self1.c @@ -0,0 +1,32 @@ +/* PR middle-end/102633 */ +/* { dg-do compile } */ +/* { dg-options "-Wuninitialized -Wno-init-self" } */ + +int +fn1 (void) +{ + int i = i; + return i; +} + +int +fn2 () +{ + const int j = j; + return j; +} + +int +fn3 () +{ + /* ??? Do we want this warning in C++? Probably not with -Wno-init-self. */ + volatile int k = k; /* { dg-warning "used uninitialized" "" { target c++ } } */ + return k; +} + +int +fn4 () +{ + const volatile int l = l; /* { dg-warning "used uninitialized" "" { target c++ } } */ + return l; +} diff --git a/gcc/testsuite/c-c++-common/Winit-self2.c b/gcc/testsuite/c-c++-common/Winit-self2.c new file mode 100644 index 00000000000..13aa9efdf26 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Winit-self2.c @@ -0,0 +1,31 @@ +/* PR middle-end/102633 */ +/* { dg-do compile } */ +/* { dg-options "-Wuninitialized -Winit-self" } */ + +int +fn1 (void) +{ + int i = i; /* { dg-warning "used uninitialized" } */ + return i; +} + +int +fn2 () +{ + const int j = j; /* { dg-warning "used uninitialized" } */ + return j; +} + +int +fn3 () +{ + volatile int k = k; /* { dg-warning "used uninitialized" } */ + return k; +} + +int +fn4 () +{ + const volatile int l = l; /* { dg-warning "used uninitialized" } */ + return l; +}