From patchwork Tue Jan 9 08:52:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 186283 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp1522538dyq; Tue, 9 Jan 2024 00:55:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDIWCDwa7L3jCQTPKmvz4cfinIBlhQtcwBKdE7q9B5zyzTjOBt3s4MAwB4Ef4J4efnNNmg X-Received: by 2002:a05:6214:c84:b0:681:9ea:b99f with SMTP id r4-20020a0562140c8400b0068109eab99fmr2785425qvr.57.1704790533458; Tue, 09 Jan 2024 00:55:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704790533; cv=pass; d=google.com; s=arc-20160816; b=Ui1gvYZwZ0RltIM9/mX+vSj0eokyELIQCnLxGyB3wrT6vJpORlooKwmIjumt3yizoS HtK/DAqg8vdd7pDaxSWX1+MBaZ/kDzcOOZaV8O23jqFT9noYUKkIl6PZQc2DT6ZijxSE jOXJkNRmjiYAPYWSp/DW8CaLXExpcLr99Wa/w5hVEPqJnQd7xsB7iPrH4nqh/Jw2pOzL tTafEkmOEfK9AUMhAJOEwwkXht8mTpvt4MYyPbjLe/bzpLgv3eNTNQGPTcUu6xWxyVoi SmuDbrJ/+tBG/lLqbfRxe/LcgiCChUdMysqmEuY2T8MK8EyO1xZ/fY5Fl88tzrKwKDJ3 0pBg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=7gfSPZ2k6XiWOui3SSdn1HLvSQTwF1mryC+n5TfipdA=; fh=DGs+jeZfpjTjB5K0nu6Tt7FnZQIt3vF190ozU1Ya8zI=; b=doiedgm8tcIwPnQKf3grLUcHxuZfl3ga9goIoe3ysLY9Kuq5+LktgCHkrGudvWxg3f I2IdVXYVbq77QlsxFGXJ7JwFsBKNwRPC9gl7BWwucnkL1Z30Zs7NPSuVBKU0iMypxH/c ZI87Rzq+jkD5P3xfZqQGOsq/43JvZM2VNLnRXd/ny5gb5OM0poQ1eAbb1iiQLt8P0KqX oKjfnTn3PBVBJJ6WxfWs9R1Smh12wbOGjxy2ZCs8m7U2Y2SWhOLWzmJnrWnlOXH7BR2x FzrVEJf0tIZEWm8Sh1CMZ0aoFLH8JBaTA4Gdl1U1HbV90TSR03Fv+GFLJXUahQS8nF8Z A56A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EPcvGNev; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u13-20020a0cdd0d000000b0067f3a5e6af6si1796365qvk.267.2024.01.09.00.55.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 00:55:33 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=EPcvGNev; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C93B938582AC for ; Tue, 9 Jan 2024 08:55:32 +0000 (GMT) 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 1BEA838582B4 for ; Tue, 9 Jan 2024 08:52:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1BEA838582B4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1BEA838582B4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704790352; cv=none; b=E+XXLo3+1c3KvsM6svh+GUrqR/xccEH5u0f9HDl+LoOmwohsvf1NMK4F/zmWnRJPdOWP7Raqc4hS0xvY7GI/TTxivI3WpTzgxHT1K3K2OX89W81zlRBCj5vvnhMN8SyBaaYGYqy2Ta0RMpxyh0vu9Ah773WlxcS7MQPniyy/0tQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704790352; c=relaxed/simple; bh=AOrLy9yTIggV+MFUxh5TkA/niL0fyR5r2EV9G0gHXZo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=iv7TK2IZTUBUxRUO0idZucTgYGi7T/d1sU4ujyoWWRzkQylxlvzc3vqCSnfmGWl3Pw+e3lXf2q9p1g9q1xjlbIMYkIKWiHAfNoRwetCudAVLVASrWI5wxze1YtgUorPPRQT1aGBaAck5+cyRDM8GNc/EY6QcRRLpRUbeAD5SxPs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704790343; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=7gfSPZ2k6XiWOui3SSdn1HLvSQTwF1mryC+n5TfipdA=; b=EPcvGNevFj4JG8qu+ZE+2CPHCnKqR8r9SjvVFnKMy0IgoY7wnlfAPqdqIku64FoQLsWrMK DyaBeJO+UHJ7Yi3Xqp7iBe/NxAlk2sg2y69pmZyvZpzOKiqpZN0eVleL59Bo/T2tWFMHPc FK4lnmCJg9CEVgbb23eQ4hWd8eWR0cY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-248-mgp-No5GOZe2OCwAWIOx9g-1; Tue, 09 Jan 2024 03:52:21 -0500 X-MC-Unique: mgp-No5GOZe2OCwAWIOx9g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 20F933C02B6E for ; Tue, 9 Jan 2024 08:52:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CD546C15E6A; Tue, 9 Jan 2024 08:52:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 4098qIvj1771463 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 9 Jan 2024 09:52:18 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4098qHMf1771462; Tue, 9 Jan 2024 09:52:17 +0100 Date: Tue, 9 Jan 2024 09:52:17 +0100 From: Jakub Jelinek To: "Joseph S. Myers" , Jason Merrill , Marek Polacek Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c-family: copy attribute diagnostic fixes [PR113262] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787602438561860399 X-GMAIL-MSGID: 1787602438561860399 Hi! The copy attributes is allowed on decls as well as types and even has checks whether decl (set to *node) is DECL_P or TYPE_P, but for diagnostics unconditionally uses DECL_SOURCE_LOCATION (decl), which obviously only works if it applies to a decl. The following patch fixes that, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-09 Jakub Jelinek PR c/113262 * c-attribs.cc (handle_copy_attribute): Don't use DECL_SOURCE_LOCATION (decl) if decl is not DECL_P, use input_location instead. Formatting fixes. * gcc.dg/pr113262.c: New test. Jakub --- gcc/c-family/c-attribs.cc.jj 2024-01-03 12:07:02.020736256 +0100 +++ gcc/c-family/c-attribs.cc 2024-01-08 22:10:04.789616664 +0100 @@ -3143,13 +3143,14 @@ handle_copy_attribute (tree *node, tree if (ref == error_mark_node) return NULL_TREE; + location_t loc = input_location; + if (DECL_P (decl)) + loc = DECL_SOURCE_LOCATION (decl); if (TREE_CODE (ref) == STRING_CST) { /* Explicitly handle this case since using a string literal as an argument is a likely mistake. */ - error_at (DECL_SOURCE_LOCATION (decl), - "%qE attribute argument cannot be a string", - name); + error_at (loc, "%qE attribute argument cannot be a string", name); return NULL_TREE; } @@ -3160,10 +3161,8 @@ handle_copy_attribute (tree *node, tree /* Similar to the string case, since some function attributes accept literal numbers as arguments (e.g., alloc_size or nonnull) using one here is a likely mistake. */ - error_at (DECL_SOURCE_LOCATION (decl), - "%qE attribute argument cannot be a constant arithmetic " - "expression", - name); + error_at (loc, "%qE attribute argument cannot be a constant arithmetic " + "expression", name); return NULL_TREE; } @@ -3171,12 +3170,11 @@ handle_copy_attribute (tree *node, tree { /* Another possible mistake (but indirect self-references aren't and diagnosed and shouldn't be). */ - if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, + if (warning_at (loc, OPT_Wattributes, "%qE attribute ignored on a redeclaration " - "of the referenced symbol", - name)) - inform (DECL_SOURCE_LOCATION (node[1]), - "previous declaration here"); + "of the referenced symbol", name) + && DECL_P (node[1])) + inform (DECL_SOURCE_LOCATION (node[1]), "previous declaration here"); return NULL_TREE; } @@ -3196,7 +3194,8 @@ handle_copy_attribute (tree *node, tree ref = TREE_OPERAND (ref, 1); else break; - } while (!DECL_P (ref)); + } + while (!DECL_P (ref)); /* For object pointer expressions, consider those to be requests to copy from their type, such as in: @@ -3228,8 +3227,7 @@ handle_copy_attribute (tree *node, tree to a variable, or variable attributes to a function. */ if (warning (OPT_Wattributes, "%qE attribute ignored on a declaration of " - "a different kind than referenced symbol", - name) + "a different kind than referenced symbol", name) && DECL_P (ref)) inform (DECL_SOURCE_LOCATION (ref), "symbol %qD referenced by %qD declared here", ref, decl); @@ -3279,9 +3277,7 @@ handle_copy_attribute (tree *node, tree } else if (!TYPE_P (decl)) { - error_at (DECL_SOURCE_LOCATION (decl), - "%qE attribute must apply to a declaration", - name); + error_at (loc, "%qE attribute must apply to a declaration", name); return NULL_TREE; } --- gcc/testsuite/gcc.dg/pr113262.c.jj 2024-01-08 22:19:07.414588762 +0100 +++ gcc/testsuite/gcc.dg/pr113262.c 2024-01-08 22:18:51.327815573 +0100 @@ -0,0 +1,6 @@ +/* PR c/113262 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +int [[gnu::copy ("")]] a; /* { dg-error "'copy' attribute argument cannot be a string" } */ +