From patchwork Wed May 24 01:45:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 766277 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.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wXZxB5R2hz9sNp for ; Wed, 24 May 2017 11:46:05 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="DIomj1Zf"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:cc:content-type; q=dns; s=default; b=Ll4Iv1INN2CYHmjbXRllmm+RnrbXshiXBDbAbGd9wFp S6gzZlxzWLEnc+exwf7fXf3/bWWUUww2/IQLnTs6bxx2OnmkgtfSN5I3wMEXrgd5 iZ9n8ehv3zkkWDAXiiLyUzd8UJjut56jCQOAMtVKyVEUj7DaKdDBYHDwZDPVAdN8 = 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 :mime-version:from:date:message-id:subject:to:cc:content-type; s=default; bh=+k6U6mT/mUAwIspgDg1LLhcbzrQ=; b=DIomj1Zf1K+ngGNxM PwPHdCmSlYhETeZHV07dbGTpLN+pI/0O26ZXNKD0QC29Gc44yADtK4J11+2WfI5N L3dpJ1gYiQuShJ0RYhuH3p5QVPjkP9PzR6hIpPtJJPZnKtIqgfni/KNObP0BRbgA mqgQ43G62zJSG+6G71TTQtXWHc= Received: (qmail 30940 invoked by alias); 24 May 2017 01:45:49 -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 30797 invoked by uid 89); 24 May 2017 01:45:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.2 spammy=complained X-HELO: mail-io0-f172.google.com Received: from mail-io0-f172.google.com (HELO mail-io0-f172.google.com) (209.85.223.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 24 May 2017 01:45:37 +0000 Received: by mail-io0-f172.google.com with SMTP id k91so108922505ioi.1 for ; Tue, 23 May 2017 18:45:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=hl+kPbuH3XylyCrdv4BhSANtOMuFULsGqL7UPVZvF6w=; b=LZ0ho6/EwFzT5qXARN6EWFvgVXd2B0UVbt14gWxThtaULvzwZ8Hp14v2A3kvXVxk2x 7bPAk4nbKj/7A3rV5lpedoNN0AtMvKXS43MNZCzHc+J/gUR+LD7O7lKuBd+sCFOuLwLB nGjfmiyA9ewHUltwVXmmJdYrbi7Ol4vJiyj0kZgx7DQkqwdyk+gZv4olsUMdk984/9JS Q/Fj7jPImdg0evZbhatTWitQDaoo5uAl0pXXrPitAgGpA946UedaGN6VtzSI42n5nTfy oTgG+KVyCfdv2tOqemYNpHFemJFalG2pJnEvmng2SAlP8v98n9UiJSmztSG2evsGP67D l/lQ== X-Gm-Message-State: AODbwcAOBF2KC9PHezvBCVeDeFE3TEk6xDIa4g1Fsz3ezVO1SgM0XrpD iNiGlJLohLs/mbHWAdGg8ZblEIuHFQh/ X-Received: by 10.107.6.218 with SMTP id f87mr29364653ioi.2.1495590331435; Tue, 23 May 2017 18:45:31 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.6.226 with HTTP; Tue, 23 May 2017 18:45:10 -0700 (PDT) From: Jason Merrill Date: Tue, 23 May 2017 21:45:10 -0400 Message-ID: Subject: C++ PATCH to -Wunused with C++17 structured bindings To: gcc-patches List Cc: Jakub Jelinek X-IsSubscribed: yes Someone on IRC complained that there was no way to suppress -Wunused on structured bindings. It seemed to me that the way the feature works, we shouldn't warn about the bindings individually; users need to give each of the subobjects a name even if they're only interested in using one of them. So this patch switches to tracking whether the underlying aggregate object as a whole is used; using one of the bindings will avoid any warning. This doesn't apply to tuple structured bindings, since in that case the bindings are actual variables rather than aliases to subobjects. Tested x86_64-pc-linux-gnu, applying to trunk. commit a10b737bee6f269c6d6cf2a668d03fb322e1c45e Author: Jason Merrill Date: Thu May 11 13:30:24 2017 -0400 -Wunused and C++17 structured bindings * decl.c (poplevel): Don't warn about unused structured bindings, only real variables. * error.c (dump_simple_decl): Handle structured bindings. * expr.c (mark_exp_read): Look through DECL_VALUE_EXPR. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 5877f37..afd47bb 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -656,7 +656,10 @@ poplevel (int keep, int reverse, int functionbody) if (VAR_P (decl) && (! TREE_USED (decl) || !DECL_READ_P (decl)) && ! DECL_IN_SYSTEM_HEADER (decl) - && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl) + /* For structured bindings, consider only real variables, not + subobjects. */ + && (DECL_DECOMPOSITION_P (decl) ? !DECL_VALUE_EXPR (decl) + : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))) && type != error_mark_node && (!CLASS_TYPE_P (type) || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) diff --git a/gcc/cp/error.c b/gcc/cp/error.c index b65cee4..66a4b60 100644 --- a/gcc/cp/error.c +++ b/gcc/cp/error.c @@ -992,6 +992,8 @@ dump_simple_decl (cxx_pretty_printer *pp, tree t, tree type, int flags) else dump_decl (pp, DECL_NAME (t), flags); } + else if (DECL_DECOMPOSITION_P (t)) + pp_string (pp, M_("")); else pp_string (pp, M_("")); if (flags & TFF_DECL_SPECIFIERS) diff --git a/gcc/cp/expr.c b/gcc/cp/expr.c index 77af54e..75e99e5 100644 --- a/gcc/cp/expr.c +++ b/gcc/cp/expr.c @@ -133,6 +133,9 @@ mark_exp_read (tree exp) switch (TREE_CODE (exp)) { case VAR_DECL: + if (DECL_VALUE_EXPR (exp)) + mark_exp_read (DECL_VALUE_EXPR (exp)); + gcc_fallthrough (); case PARM_DECL: DECL_READ_P (exp) = 1; break; diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp29.C b/gcc/testsuite/g++.dg/cpp1z/decomp29.C new file mode 100644 index 0000000..daf07a0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp29.C @@ -0,0 +1,26 @@ +// { dg-options "-std=c++17 -Wunused" } + +#include + +struct A { int i,j,k; }; + +A f(); + +int z; + +int main() +{ + { + auto [i,j,k] = f(); // { dg-warning "unused" } + } + { + auto [i,j,k] = f(); + z = i; + } + { + auto [i,j] = std::tuple{1,2}; // { dg-warning "unused" } + } + // No parallel second test, because in this case i and j are variables rather + // than mere bindings, so there isn't a link between them and using i will + // not prevent a warning about unused j. +}