From patchwork Mon Aug 3 14:14:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1340326 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=gk7m+wtT; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BL0KF4g07z9sPC for ; Tue, 4 Aug 2020 00:15:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4B7693857C5B; Mon, 3 Aug 2020 14:15:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B7693857C5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1596464125; bh=ePS/kWy7bAtfGtRdQ+0mVF2lB9M6Z9rEV3uhOn597N0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=gk7m+wtTMglr5KXDzFxFFCBgyITTrfHRhmciWfcqv6gKQ+SzqyMVqAlwm7ZU17czo 7rmHKpMXcJOx/ONGYVu+rHhRgI/VG4/oZ7PxdvwKsDwKHHJ8KC235vIlitkFf1vFli 9Cfz2rv5mJc8ef2wm0Ub/3+w/nGxCVTki6EcotHA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 1A3433858D35 for ; Mon, 3 Aug 2020 14:15:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1A3433858D35 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-197-vI_DrXIEOqmNcnNol5w-bQ-1; Mon, 03 Aug 2020 10:15:18 -0400 X-MC-Unique: vI_DrXIEOqmNcnNol5w-bQ-1 Received: by mail-qk1-f198.google.com with SMTP id v188so26275572qkb.17 for ; Mon, 03 Aug 2020 07:15:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ePS/kWy7bAtfGtRdQ+0mVF2lB9M6Z9rEV3uhOn597N0=; b=J3Wpyt0gGkTcPnU4CzyRjUJRrKvtVf5tTPyI8Pi8iMb97QzbFTbEWWokCZ/h8mypj0 aCQP+YPflAsDK2ZnOOyyocyMHaBrm0laXdb+R6HaV848RKiBSqt+wV1WcGSQv60FxLPW ryjdmU9O/kcLHkDFipQsDe7yvDb9DiadT3iiTCNNNZnuaZuIGcpOVL8W5Av7xTrhbqOZ vECw9vocyh6PZSNr+cTeK1//iF6UGQGzuizQP9oWZvp7hLpE0pgIxwUQA+I0fXA+VaPQ rduB81R8nuJHTYW3CgcXCSVLi/KnWSvTEYgneSa1y12oaqZGv4pXiKJKVu3e2OXKZw0r Pn6g== X-Gm-Message-State: AOAM530bPlnXdrH+wM36pIg6WHsueUQQZ7OGYD3F2z5wqRTiZDGVcTVH DRy7jh3HFaqKH4V21OGP+lSVq91jF8T892ktAbnvBX+RZlCVM8HB6mhV36O0R2YxoXtKj6PLJRA N9PQROp772KVcGUui0A== X-Received: by 2002:a37:556:: with SMTP id 83mr16228931qkf.208.1596464117625; Mon, 03 Aug 2020 07:15:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwr2+Rzwws55KVBqmzv+Jrc5LmFThuAv4aER8v/EJd6mrxXwOMNJ4JdZjIOFRsnXWuRqTJisw== X-Received: by 2002:a37:556:: with SMTP id 83mr16228900qkf.208.1596464117350; Mon, 03 Aug 2020 07:15:17 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id x7sm18756030qkn.53.2020.08.03.07.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2020 07:15:16 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Member initializer list diagnostic locations [PR94024] Date: Mon, 3 Aug 2020 10:14:51 -0400 Message-Id: <20200803141451.867700-1-ppalka@redhat.com> X-Mailer: git-send-email 2.28.0.89.g85b4e0a6dc MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.4 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" This patch preserves the source locations of each node in a member initializer list so that during processing of the list we can set input_location appropriately for generally more accurate diagnostic locations. Since TREE_LIST nodes are tcc_exceptional, they can't have source locations, so we instead store the location in a dummy tcc_expression node within the TREE_TYPE of the list node. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK to commit? gcc/cp/ChangeLog: PR c++/94024 * init.c (sort_mem_initializers): Preserve TREE_TYPE of the member initializer list node. (emit_mem_initializers): Set input_location when performing each member initialization. * parser.c (cp_parser_mem_initializer_list): Attach the source location of this initializer to a dummy EMPTY_CLASS_EXPR within the TREE_TYPE of the list node. * pt.c (tsubst_initializer_list): Preserve TREE_TYPE of the member initializer list node. gcc/testsuite/ChangeLog: PR c++/94024 * g++.dg/diagnostic/mem-init1.C: New test. --- gcc/cp/init.c | 6 +++++ gcc/cp/parser.c | 8 ++++++ gcc/cp/pt.c | 3 +++ gcc/testsuite/g++.dg/diagnostic/mem-init1.C | 29 +++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 gcc/testsuite/g++.dg/diagnostic/mem-init1.C diff --git a/gcc/cp/init.c b/gcc/cp/init.c index cb9bd2dbfba..3f089404cf2 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -1151,6 +1151,8 @@ sort_mem_initializers (tree t, tree mem_inits) /* Record the initialization. */ TREE_VALUE (subobject_init) = TREE_VALUE (init); + /* Carry over the dummy TREE_TYPE node containing the source location. */ + TREE_TYPE (subobject_init) = TREE_TYPE (init); next_subobject = subobject_init; } @@ -1367,6 +1369,10 @@ emit_mem_initializers (tree mem_inits) /* Initialize the data members. */ while (mem_inits) { + /* If this initializer was explicitly provided, then the dummy TREE_TYPE + node contains the source location. */ + iloc_sentinel ils (EXPR_LOCATION (TREE_TYPE (mem_inits))); + perform_member_init (TREE_PURPOSE (mem_inits), TREE_VALUE (mem_inits)); mem_inits = TREE_CHAIN (mem_inits); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index ab088874ba7..2f9b8786121 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -15326,6 +15326,14 @@ cp_parser_mem_initializer_list (cp_parser* parser) { TREE_CHAIN (mem_initializer) = mem_initializer_list; mem_initializer_list = mem_initializer; + + /* We can't attach the source location of this initializer directly to + the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR + within the TREE_TYPE of the list node. */ + TREE_TYPE (mem_initializer) = build0 (EMPTY_CLASS_EXPR, NULL_TREE); + SET_EXPR_LOCATION (TREE_TYPE (mem_initializer), + make_location (token->location, + token->location, input_location)); } /* If the next token is not a `,', we're done. */ if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)) diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 3a168d506cf..bed1a3532e7 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -26014,6 +26014,9 @@ tsubst_initializer_list (tree t, tree argvec) if (decl) { init = build_tree_list (decl, init); + /* Carry over the dummy TREE_TYPE node containing the source + location. */ + TREE_TYPE (init) = TREE_TYPE (t); TREE_CHAIN (init) = inits; inits = init; } diff --git a/gcc/testsuite/g++.dg/diagnostic/mem-init1.C b/gcc/testsuite/g++.dg/diagnostic/mem-init1.C new file mode 100644 index 00000000000..b749c72cd8b --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/mem-init1.C @@ -0,0 +1,29 @@ +// PR c++/94024 +// { dg-do compile } + +struct A { + A() + : a() // { dg-error "reference type" } + , b(1) // { dg-error "incompatible" } + , c(0) // { dg-bogus "" } + {} + + int &a; + int b[1]; + char c; +}; + +template +struct B { + B() + : a() // { dg-error "reference type" } + , b(1) // { dg-error "incompatible" } + , c(0) // { dg-bogus "" } + {} + + T a; + U b; + char c; +}; + +B b;