From patchwork Thu Aug 29 15:24:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 270874 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "www.sourceware.org", Issuer "StartCom Class 1 Primary Intermediate Server CA" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 5FE852C0095 for ; Fri, 30 Aug 2013 01:25:03 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=LBNTgkxauapMnMj+Z8cfFYh/R7/6u0m2P9Y4L1WsSgL Hdjv0MDV93nGnn8Aropcf9lA5WTJQjEZmhBQSd0Jo+vumVV994XmtXee4ThN0wC4 NQk0BsjzNcWtsxTVe5tFh4gwBALWo6q69BfJg1STrasHa3X80iBQ6Bx07NsXPbT8 = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=ZI4pbJhKYf+eD1NhTJOfLMG8LpM=; b=u74Lotme96DTEfXjT sq4tBhUfvH3pJmZ9lM5MONHtyfZsdRxr2mkMdjsQwxR+dyClcYGLbE6ruT6FQ5cs RHLNayAI8fUH5Hte4/oh9X2/vkZ40a11VCfMUPGM4m/QafAWST5c1V/7V7UjXt/O 4Bg9dFNnQV3m6eRrlvw9xKggpo= Received: (qmail 11408 invoked by alias); 29 Aug 2013 15:24:56 -0000 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 Received: (qmail 11395 invoked by uid 89); 29 Aug 2013 15:24:56 -0000 Received: from aserp1040.oracle.com (HELO aserp1040.oracle.com) (141.146.126.69) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 29 Aug 2013 15:24:56 +0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.9 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_SOFTFAIL, UNPARSEABLE_RELAY autolearn=ham version=3.3.2 X-HELO: aserp1040.oracle.com Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r7TFOqM2017609 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 29 Aug 2013 15:24:52 GMT Received: from aserz7021.oracle.com (aserz7021.oracle.com [141.146.126.230]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r7TFOpoh027782 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 29 Aug 2013 15:24:52 GMT Received: from abhmt119.oracle.com (abhmt119.oracle.com [141.146.116.71]) by aserz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r7TFOp7L027778; Thu, 29 Aug 2013 15:24:51 GMT Received: from poldo4.casa (/79.17.190.22) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 29 Aug 2013 08:24:51 -0700 Message-ID: <521F67C1.20104@oracle.com> Date: Thu, 29 Aug 2013 17:24:49 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch] PR 51424 Hi, thus I have this simple patch which at least catches pure self-delegation (no cycles). Better than nothing, I would say, given its simplicity ;) At first I thought I would put the check in expand_default_init but then I noticed that in case of, eg, virtual bases, the simple pattern matching (which looks inside CALL_EXPRs) would miss some cases, like when the self-delegation is only in the derived type, not in the base. Tested x86_64-linux. Thanks! Paolo. ///////////////////////////// /cp 2013-08-29 Paolo Carlini PR c++/51424 * cp-tree.h (LOOKUP_DELEGATING_CONS): Add. * init.c (perform_target_ctor): Use it. * call.c (build_special_member_call): Diagnose self-delegating constructors. /testsuite 2013-08-29 Paolo Carlini PR c++/51424 * g++.dg/cpp0x/dc8.C: New. * g++.dg/template/meminit1.C: Adjust. Index: cp/call.c =================================================================== --- cp/call.c (revision 202071) +++ cp/call.c (working copy) @@ -7442,6 +7442,14 @@ build_special_member_call (tree instance, tree nam if (allocated != NULL) release_tree_vector (allocated); + if ((complain & tf_error) + && (flags & LOOKUP_DELEGATING_CONS) + && name == complete_ctor_identifier + && TREE_CODE (ret) == CALL_EXPR + && (DECL_ABSTRACT_ORIGIN (TREE_OPERAND (CALL_EXPR_FN (ret), 0)) + == current_function_decl)) + error ("constructor delegates to itself"); + return ret; } Index: cp/cp-tree.h =================================================================== --- cp/cp-tree.h (revision 202071) +++ cp/cp-tree.h (working copy) @@ -4509,6 +4509,8 @@ enum overload_flags { NO_SPECIAL = 0, DTOR_FLAG, T #define LOOKUP_NO_RVAL_BIND (LOOKUP_EXPLICIT_TMPL_ARGS << 1) /* Used by case_conversion to disregard non-integral conversions. */ #define LOOKUP_NO_NON_INTEGRAL (LOOKUP_NO_RVAL_BIND << 1) +/* Used for delegating constructors in order to diagnose self-delegation. */ +#define LOOKUP_DELEGATING_CONS (LOOKUP_NO_NON_INTEGRAL << 1) #define LOOKUP_NAMESPACES_ONLY(F) \ (((F) & LOOKUP_PREFER_NAMESPACES) && !((F) & LOOKUP_PREFER_TYPES)) Index: cp/init.c =================================================================== --- cp/init.c (revision 202071) +++ cp/init.c (working copy) @@ -500,8 +500,9 @@ perform_target_ctor (tree init) tree decl = current_class_ref; tree type = current_class_type; - finish_expr_stmt (build_aggr_init (decl, init, LOOKUP_NORMAL, - tf_warning_or_error)); + finish_expr_stmt (build_aggr_init (decl, init, + LOOKUP_NORMAL|LOOKUP_DELEGATING_CONS, + tf_warning_or_error)); if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) { tree expr = build_delete (type, decl, sfk_complete_destructor, Index: testsuite/g++.dg/cpp0x/dc8.C =================================================================== --- testsuite/g++.dg/cpp0x/dc8.C (revision 0) +++ testsuite/g++.dg/cpp0x/dc8.C (working copy) @@ -0,0 +1,66 @@ +// PR c++/51424 +// { dg-do compile { target c++11 } } + +template +struct S +{ + S() : S() {} // { dg-error "delegates to itself" } + S(int x) : S(x) {} // { dg-error "delegates to itself" } +}; + +struct B1 +{ + B1() : B1() {} // { dg-error "delegates to itself" } + B1(int y) : B1(y) {} // { dg-error "delegates to itself" } +}; + +struct V1 : virtual B1 +{ + V1() : B1() {} + V1(int x) : B1(x) {} +}; + +struct B2 +{ + B2() : B2() {} // { dg-error "delegates to itself" } + B2(int y) : B2(y) {} // { dg-error "delegates to itself" } +}; + +struct V2 : virtual B2 +{ + V2() : V2() {} // { dg-error "delegates to itself" } + V2(int x) : V2(x) {} // { dg-error "delegates to itself" } +}; + +struct B3 +{ + B3() {} + B3(int y) {} +}; + +struct V3 : virtual B3 +{ + V3() : V3() {} // { dg-error "delegates to itself" } + V3(int x) : V3(x) {} // { dg-error "delegates to itself" } +}; + +struct CE1 +{ + constexpr CE1() : CE1() {} // { dg-error "delegates to itself" } + constexpr CE1(int x) : CE1(x) {} // { dg-error "delegates to itself" } +}; + +struct CEB2 +{ + constexpr CEB2() : CEB2() {} // { dg-error "delegates to itself" } + constexpr CEB2(int x) : CEB2(x) {} // { dg-error "delegates to itself" } +}; + +struct CE2 : CEB2 +{ + constexpr CE2() : CEB2() {} + constexpr CE2(int x) : CEB2(x) {} +}; + +S s1; +S s2(1); Index: testsuite/g++.dg/template/meminit1.C =================================================================== --- testsuite/g++.dg/template/meminit1.C (revision 202068) +++ testsuite/g++.dg/template/meminit1.C (working copy) @@ -3,6 +3,6 @@ template struct S { S() : S() {} // { dg-message "delegating constructors" } -}; +}; // { dg-error "delegates to itself" "" { target *-*-* } 5 } S s;