From patchwork Fri Nov 4 15:05:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 15628 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp458700wru; Fri, 4 Nov 2022 08:06:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5ThfhcxpfYve2DhCWJuz0iD23+me174TAR8LEy+Xyki1rtPVd9XZz69TPT11bOM8MT8RGM X-Received: by 2002:a17:907:6e1b:b0:78e:15a3:5be6 with SMTP id sd27-20020a1709076e1b00b0078e15a35be6mr35046369ejc.750.1667574388695; Fri, 04 Nov 2022 08:06:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667574388; cv=none; d=google.com; s=arc-20160816; b=nfUuNnGC0+AuqEHG+voMAqDCTuolTS/41H2XuelPhOuJqI0rFTl56hd5wZ5hILDAj6 3cv9k3vPhnkZi8vU6wiqGB6DRLuFgnrStgMv8sXevZUrSAp0JSb43lO8TN/XuKxYqaxx K7Td20DHroffjkTiXxVvcYT/l93Gp91LGDMdVxVcDZdcJZl3qtL7iPD9l8XvCLdd7GM+ GyPF1uuDv2OpQNDuTLgLNjEEZ5L+vwEboduoxbBU9tbv1mceeJzgNiOP5/0r09mf3UDE 4vOB2/EQIS17OQro0je2B3OH3I1wvl408zXo2vp+BYK9W9IfZxnjQ+L56AnqO3UTRzCl AuLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=SiHoEGlKNa6IhUXGZql+tHZGKoj6aaAKkiE873CTqPo=; b=Uuwc6UnAIVlDylormcKwEag77jubcRve7eKJW1fdpst46GQ2x0DStowBWXNw8pzWGV zXJNQ9x1ixGQ9RLYDjCmhaZop7PBiFa81jVGxu/bvD5SDme3PuaaJ2FgJQgD0QNKFZZW FA0RWIk52OMlEvxjZ7FF0pN0VoZIpIJPmNXce/nq53u5nAYm6Sk/fnX+bQQbCgNLVucM bWWdzNwEmbrKca3RIGeiuMXPlyY3oc3Gioo30hjUn8RpK0ji09J5JoE3I4ibGEcjDDyK ekjAa0wmh4WUcn0EBaXr1ByxcAXfbbkFjrsg4xkJSzO8JyTRDdC3/V31OUKFrZoolQop /1Dw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="FvyOBFc/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ht11-20020a170907608b00b007ae2368c8b3si5795851ejc.730.2022.11.04.08.06.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 08:06:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="FvyOBFc/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 42AD238582A4 for ; Fri, 4 Nov 2022 15:06:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42AD238582A4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667574380; bh=SiHoEGlKNa6IhUXGZql+tHZGKoj6aaAKkiE873CTqPo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=FvyOBFc/196UUGhM4pZaI7nKldMNqFtHoxpGO0dzT4BK7Pz0Q5fXeD3hl2jXHjcq/ S3yAOtT8LCdz4ZfMwsd0Fb6rl6ZLrLNP06mdf67ewVcNPRO1ZiK05LmDdvEAT/vZyV 97onmsi8EuXwZigKjda6qUUeFCPZUqtenZ7EXPdU= 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 11F743858420 for ; Fri, 4 Nov 2022 15:05:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 11F743858420 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-356-DcIgv4drPMyXeoFmjknFyA-1; Fri, 04 Nov 2022 11:05:31 -0400 X-MC-Unique: DcIgv4drPMyXeoFmjknFyA-1 Received: by mail-qv1-f69.google.com with SMTP id z18-20020a0cfed2000000b004bc28af6f7dso3415850qvs.4 for ; Fri, 04 Nov 2022 08:05:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SiHoEGlKNa6IhUXGZql+tHZGKoj6aaAKkiE873CTqPo=; b=d+UvdlPjkslTsoGFveM1ybTSu+hsHilbQR7ivdJPReRMarda1db/hfm7V0p4OmI8tm JiLOys9C02UVjvD0hhIgsQqLN/6EIt44GORsYIMc4RHSWtxEKlxTA0RUZux51tIhdTKT gGytt1fpB6dt572LgU63tCm1o8eSDrl0Cmqh8HvZHK2WowJIgc0AwuUogrn8Zo5l524j C9Pccofe4D7YeWzaDzru1ZXcb5UxGs6BSAuURrgYHKg2yQc4qRIgbNaVesUVGVs9Q9B1 2Ovrrro0gj5DyEwDYkhOQo8GAvBQpQKrAwgPUI9oduS/3PhChu0ux2g2iVtZFgEG8+0u +0eA== X-Gm-Message-State: ACrzQf04is5AhSXX840YHSSRvSiEgoveA0o80aMp5KQYsNqHF1yby67P TwCBy6m4fH4c+/qkPnsdvuVTJ+qP7N6ZhfmyQCfCIzxJhfoPlkMjszD1j6KP23U1jykgQc+AJx+ I5vKCta1vhkPnh8hN68UmzMmtaXKUOE1v5z+BJuB5EjVsvj1i5RHn02UgEFLGu9lNXto= X-Received: by 2002:a05:620a:2b93:b0:6fa:307f:e074 with SMTP id dz19-20020a05620a2b9300b006fa307fe074mr19806843qkb.523.1667574329992; Fri, 04 Nov 2022 08:05:29 -0700 (PDT) X-Received: by 2002:a05:620a:2b93:b0:6fa:307f:e074 with SMTP id dz19-20020a05620a2b9300b006fa307fe074mr19806808qkb.523.1667574329632; Fri, 04 Nov 2022 08:05:29 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id cx26-20020a05620a51da00b006fa12a74c53sm3018498qkb.61.2022.11.04.08.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 08:05:29 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] c++: correct __has_attribute(init_priority) Date: Fri, 4 Nov 2022 11:05:24 -0400 Message-Id: <20221104150525.2968778-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.385.g3b08839926 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.3 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable 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.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 Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748578481855677472?= X-GMAIL-MSGID: =?utf-8?q?1748578481855677472?= Currently __has_attribute(init_priority) always returns true, even on targets that don't actually support init priorities, and when using the attribute on such targets, we issue a hard error that init priorities are unsupported. This makes it impossible to conditionally use the attribute by querying __has_attribute. This patch fixes this by adding the attribute to the attribute table only if the target supports init priorities, so that __has_attribute returns false appropriately. Thus on such targets we'll now treat it as just another unrecognized attribute, so using it gives a -Wattribute warning instead of an error. gcc/cp/ChangeLog: * tree.cc (cxx_attribute_table): Add entry for init_priority only if SUPPORTS_INIT_PRIORITY. (handle_init_priority_attribute): Assert SUPPORTS_INIT_PRIORITY is true. gcc/testsuite/ChangeLog: * g++.dg/special/initpri3.C: New test. --- gcc/cp/tree.cc | 20 +++++++------------- gcc/testsuite/g++.dg/special/initpri3.C | 9 +++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/special/initpri3.C diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 45348c58bb6..c30bbeb0839 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -5010,8 +5010,10 @@ const struct attribute_spec cxx_attribute_table[] = { /* { name, min_len, max_len, decl_req, type_req, fn_type_req, affects_type_identity, handler, exclude } */ +#if SUPPORTS_INIT_PRIORITY { "init_priority", 1, 1, true, false, false, false, handle_init_priority_attribute, NULL }, +#endif { "abi_tag", 1, -1, false, false, false, true, handle_abi_tag_attribute, NULL }, { NULL, 0, 0, false, false, false, false, NULL, NULL } @@ -5039,7 +5041,7 @@ const struct attribute_spec std_attribute_table[] = /* Handle an "init_priority" attribute; arguments as in struct attribute_spec.handler. */ -static tree +ATTRIBUTE_UNUSED static tree handle_init_priority_attribute (tree* node, tree name, tree args, @@ -5103,18 +5105,10 @@ handle_init_priority_attribute (tree* node, pri); } - if (SUPPORTS_INIT_PRIORITY) - { - SET_DECL_INIT_PRIORITY (decl, pri); - DECL_HAS_INIT_PRIORITY_P (decl) = 1; - return NULL_TREE; - } - else - { - error ("%qE attribute is not supported on this platform", name); - *no_add_attrs = true; - return NULL_TREE; - } + gcc_assert (SUPPORTS_INIT_PRIORITY); + SET_DECL_INIT_PRIORITY (decl, pri); + DECL_HAS_INIT_PRIORITY_P (decl) = 1; + return NULL_TREE; } /* DECL is being redeclared; the old declaration had the abi tags in OLD, diff --git a/gcc/testsuite/g++.dg/special/initpri3.C b/gcc/testsuite/g++.dg/special/initpri3.C new file mode 100644 index 00000000000..a181abdd0b1 --- /dev/null +++ b/gcc/testsuite/g++.dg/special/initpri3.C @@ -0,0 +1,9 @@ +// Verify __has_attribute(init_priority) is false whenever the platform +// doesn't support it, and is treated as an unrecognized attribute. + +#if !__has_attribute(init_priority) +#error init_priority /* { dg-error "" "" { target { ! init_priority } } } */ +#endif + +struct A { A(); } a __attribute__((init_priority(500))); +// { dg-warning "attribute directive ignored" "" { target { ! init_priority } } .-1 } From patchwork Fri Nov 4 15:05:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 15629 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp459403wru; Fri, 4 Nov 2022 08:07:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5wkihICXQ4ZmZ8/vLwgoW1NdJCw0bqq1t05TYjHJNM+IZWHV0p6RPXN41f4DynMQlSfaaI X-Received: by 2002:a05:6402:5507:b0:452:183f:16d1 with SMTP id fi7-20020a056402550700b00452183f16d1mr37121898edb.96.1667574448623; Fri, 04 Nov 2022 08:07:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667574448; cv=none; d=google.com; s=arc-20160816; b=ZdQg02XztnTRyk3gDzELL8qePPSouMURus3VSGXAIm9naI9QPrwybnj5AZvNIKpAWl D8FFiiAMEo5KNldCLJBSWRSUMt0cHzOFaVX+cIB4RQUUAIiGDC3cR1Avjk2d3pofiKfu V9b4Gf+//1PTiMhZ3TWxV2Rj80n9rLDh9NACp49oFRngLP3Dj6syOMWl+lschjkdAnZz PPKsfNlZ8XwLiCQII/ePVHz4/hXRE/2uC+UtNBLkodMT6v6kmet7TrWrw5NIvyuu5kwr ihlB80B2DXYyV4N93sK9un75pqDxeM2YMidPRqJjki5lzjNKASJ216XsOjNljHciqyu3 q9dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=rmgUs23lKPlZ1uNaHMio3kywczEnXxr8ui/LdLG6yJE=; b=MXrn4syWRpOsAbhLRltEXMbAq3QjmOxxj3yW9wrVtm2Xe9txThEdYszBTsC5i6AuMS 1DUCGI4An9IMKbhXV57Rl8NOHfU6ECBlS8LhCMULUFHjtVIit7XK40PfgnpQTs5ROB5V YjlP/1v0UFeo02wJVB64v0ZuHtLUzX8zEnmOyBVxz8XsnP7QvcXQBoJv2uwERALtkDuK Jj+X0xUZhhi3VOtW3RWw+tkSvvyEDDQwXLzqNMdFqkAMctYfCQV8/2pHSSArrw223Y1i J460XN+XUH8ix+s2TsZJ+TQ9B/km3zPZTaeQzc63jJ8dXaDeSbm0Q0IGK/DYLXpHhSuv 3H7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MiMQwhrW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y8-20020a056402270800b004617c3c9297si5587535edd.252.2022.11.04.08.07.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 08:07:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MiMQwhrW; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6AA73385843F for ; Fri, 4 Nov 2022 15:07:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6AA73385843F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667574447; bh=rmgUs23lKPlZ1uNaHMio3kywczEnXxr8ui/LdLG6yJE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=MiMQwhrWZTURKJrbHt7V0Y5dAyzdgsPty01vOPXxbkN36iUgo+VGdHipxME5CIuk2 52LajXUQjXCZgFEnLlhkBTWGmByZXciqQUf0+mVFapSdyxgo534l87SRZtBYT+ez86 pjcceW2bk68G4Xwmm7EA6CWj3b8+LTK5FO7PPaCw= 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 5F6463858422 for ; Fri, 4 Nov 2022 15:05:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5F6463858422 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-92-47wK7GDqMl2Pjt3F1hElSA-1; Fri, 04 Nov 2022 11:05:33 -0400 X-MC-Unique: 47wK7GDqMl2Pjt3F1hElSA-1 Received: by mail-qk1-f199.google.com with SMTP id de21-20020a05620a371500b006eed31abb72so4593957qkb.6 for ; Fri, 04 Nov 2022 08:05:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rmgUs23lKPlZ1uNaHMio3kywczEnXxr8ui/LdLG6yJE=; b=YiTJ5d60WH8kySwaxTPR4I4qzYyqo1vUJOd4edRij7+13BjsdMoso2YHqTVliMvKh3 T0slV1KCwzO+0GmoLt6yuMt0oOBt54Ca6GK8yHdJrKEZKbTpOKZ65f+5m7SQU0NonzOf 6TWwS9/qxxbONUryK7OTFTYG1/Svi5bMOxWsYitjg5m28Jk7p0Zen6GNx7vZryBFgwr+ jOXjlL6QhNOi7JBMXP0qYUXgaK6Dm99VFIjR8D6CigmWx9FM2/PPCB6T86vKizHMq2WH 9/zZSr00H/8YO/2KSy357qZvXMcVR2DTCO9F1iKYwdgZq8PFuxhCjDH8wxaOTe4mCD/m Fefw== X-Gm-Message-State: ACrzQf0ww4caNVOQTSezlES3gKZx3pEzaEXuBiWZRnPhd7s9tWiU+98s 6RlKxLQXatPNytifvEa/eNb+OH8cRPCQO4P3OMZ4+NBUkAVS64PKNpJ8hbCtp6jraCt4w/1RLbT 9J3fdI9P37Ker1bWHyxWFABxKsxHukya7+P0BAJCcx5lyHRwz9uZUn89LQ2lkc835tnk= X-Received: by 2002:a05:620a:f03:b0:6cf:c0a1:20bc with SMTP id v3-20020a05620a0f0300b006cfc0a120bcmr26069047qkl.663.1667574331632; Fri, 04 Nov 2022 08:05:31 -0700 (PDT) X-Received: by 2002:a05:620a:f03:b0:6cf:c0a1:20bc with SMTP id v3-20020a05620a0f0300b006cfc0a120bcmr26069020qkl.663.1667574331312; Fri, 04 Nov 2022 08:05:31 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id cx26-20020a05620a51da00b006fa12a74c53sm3018498qkb.61.2022.11.04.08.05.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Nov 2022 08:05:30 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Move stream initialization into compiled library [PR44952] Date: Fri, 4 Nov 2022 11:05:25 -0400 Message-Id: <20221104150525.2968778-2-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.385.g3b08839926 In-Reply-To: <20221104150525.2968778-1-ppalka@redhat.com> References: <20221104150525.2968778-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.3 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_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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.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 Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748578544571390380?= X-GMAIL-MSGID: =?utf-8?q?1748578544571390380?= This patch moves the global object for constructing the standard streams out from and into the compiled library on targets that support the init_priority attribute. This means that no longer introduces a separate global constructor in each TU that includes it. We can do this only if the init_priority attribute is supported because we need to force that the stream initialization runs first before any user-defined global initializer in programs that that use a static libstdc++.a. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look right? Unfortunately I don't have access to a system that truly doesn't support init priorities, so I instead tested that situation by artificially disabling the init_priority attribute on x86_64. PR libstdc++/44952 PR libstdc++/98108 libstdc++-v3/ChangeLog: * include/bits/c++config (_GLIBCXX_HAS_ATTRIBUTE): Define. (_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY): Define. * include/std/iostream (__ioinit): Define only if init_priority attribute isn't usable. * src/c++98/ios_init.cc (__ioinit): Define here instead if the init_priority is usable. * src/c++98/ios_base_init.h: New file. --- libstdc++-v3/include/bits/c++config | 12 ++++++++++++ libstdc++-v3/include/std/iostream | 4 ++++ libstdc++-v3/src/c++98/ios_base_init.h | 9 +++++++++ libstdc++-v3/src/c++98/ios_init.cc | 4 ++++ 4 files changed, 29 insertions(+) create mode 100644 libstdc++-v3/src/c++98/ios_base_init.h diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 50406066afe..f93076191d9 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -837,6 +837,18 @@ namespace __gnu_cxx #undef _GLIBCXX_HAS_BUILTIN +#ifdef __has_attribute +# define _GLIBCXX_HAS_ATTRIBUTE(B) __has_attribute(B) +#else +# define _GLIBCXX_HAS_ATTRIBUTE(B) 0 +#endif + +#if _GLIBCXX_HAS_ATTRIBUTE(init_priority) +# define _GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY 1 +#endif + +#undef _GLIBCXX_HAS_ATTRIBUTE + // Mark code that should be ignored by the compiler, but seen by Doxygen. #define _GLIBCXX_DOXYGEN_ONLY(X) diff --git a/libstdc++-v3/include/std/iostream b/libstdc++-v3/include/std/iostream index 70318a45891..5eaa9755d9a 100644 --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -73,7 +73,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ///@} // For construction of filebuffers for cout, cin, cerr, clog et. al. + // When the init_priority attribute is usable, we do this initialization + // in the compiled library instead (see src/c++98/ios_init.cc). +#if !_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY static ios_base::Init __ioinit; +#endif _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/src/c++98/ios_base_init.h b/libstdc++-v3/src/c++98/ios_base_init.h new file mode 100644 index 00000000000..f3087d1da3c --- /dev/null +++ b/libstdc++-v3/src/c++98/ios_base_init.h @@ -0,0 +1,9 @@ +// This is only in a header so we can use the system_header pragma, +// to suppress the warning caused by using a reserved init_priority. +#pragma GCC system_header + +#if !_GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY +# error "This file should not be included for this build" +#endif + +static ios_base::Init __ioinit __attribute__((init_priority(90))); diff --git a/libstdc++-v3/src/c++98/ios_init.cc b/libstdc++-v3/src/c++98/ios_init.cc index 1b5132f1c2d..954fa9f29cf 100644 --- a/libstdc++-v3/src/c++98/ios_init.cc +++ b/libstdc++-v3/src/c++98/ios_init.cc @@ -75,6 +75,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION extern wostream wclog; #endif +#if _GLIBCXX_HAVE_ATTRIBUTE_INIT_PRIORITY +# include "ios_base_init.h" +#endif + ios_base::Init::Init() { if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)