From patchwork Sat Jan 1 22:36:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 77166 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 490C9B70AF for ; Sun, 2 Jan 2011 09:36:55 +1100 (EST) Received: (qmail 12890 invoked by alias); 1 Jan 2011 22:36:53 -0000 Received: (qmail 12880 invoked by uid 22791); 1 Jan 2011 22:36:52 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RFC_ABUSE_POST X-Spam-Check-By: sourceware.org Received: from mail-wy0-f175.google.com (HELO mail-wy0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 01 Jan 2011 22:36:47 +0000 Received: by wyb40 with SMTP id 40so12788760wyb.20 for ; Sat, 01 Jan 2011 14:36:45 -0800 (PST) MIME-Version: 1.0 Received: by 10.216.163.11 with SMTP id z11mr5070069wek.36.1293921405236; Sat, 01 Jan 2011 14:36:45 -0800 (PST) Received: by 10.216.160.131 with HTTP; Sat, 1 Jan 2011 14:36:45 -0800 (PST) In-Reply-To: References: Date: Sat, 1 Jan 2011 22:36:45 +0000 Message-ID: Subject: Re: PING: [patch] fix c++/18016 - warn about self-initialization in constructor init-list From: Jonathan Wakely To: gcc-patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org On 24 December 2010 00:31, Jonathan Wakely wrote: > Merry Pingmas, one and all. And a pingy new year. Still hoping for this patch to be reviewed... > On 21 December 2010 15:25, Jonathan Wakely wrote: >> Here's my attempt to fix PR c++/18016 so that we get a warning from >> >> struct S { >>  int i; >>  S() : i(i) { } >> }; >> >> -Winit-self is broken for C++ (PR c++/34772) so I made this warn with >> -Wuninitialized.  If you want to suppress the warning just don't put a >> mem-initializer in the constructor (which will work at least until >> someone fixes PR c++/2972) >> >> cp/ChangeLog: >> >>        PR c++/18016 >>        * init.c (perform_member_init): Check for self-initialization. >> >> >> testsuite/ChangeLog: >> >>        PR c++/18016 >>        * g++.dg/warn/pr18016.C: New. >> >> >> tested x86_64-linux with no regressions, ok for trunk? >> > Index: cp/init.c =================================================================== --- cp/init.c (revision 168023) +++ cp/init.c (working copy) @@ -449,6 +449,17 @@ perform_member_init (tree member, tree i if (decl == error_mark_node) return; + if (warn_uninitialized && init && TREE_CODE (init) == TREE_LIST + && TREE_CHAIN (init) == NULL_TREE) + { + tree val = TREE_VALUE (init); + if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member + && TREE_OPERAND (val, 0) == current_class_ref) + warning_at (DECL_SOURCE_LOCATION (current_function_decl), + OPT_Wuninitialized, "%qD is initialized with itself", + member); + } + if (init == void_type_node) { /* mem() means value-initialization. */ Index: testsuite/g++.dg/warn/pr18016.C =================================================================== --- testsuite/g++.dg/warn/pr18016.C (revision 0) +++ testsuite/g++.dg/warn/pr18016.C (revision 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-Wuninitialized" } */ + +class X { + int i; + X() : i(i) { } // { dg-warning "initialized with itself" } + X(int i) : i(i) { } + X(const X& x) : i(x.i) { } +}; + +// { dg-prune-output "In constructor" }