From patchwork Tue Feb 1 18:53:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1587369 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=jym5x6yz; dkim-atps=neutral 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+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) 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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4JpDdk1wvnz9s1l for ; Wed, 2 Feb 2022 05:55:20 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2AFD2385AC31 for ; Tue, 1 Feb 2022 18:55:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2AFD2385AC31 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643741718; bh=Hoi4UEH2YS8YOWxAoWwyyTULRglkhppGOz9YKsRNEIU=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jym5x6yzAM8zErznarn+9VdkZxxvQy1eGkqtWplnVifQryimHAqa4bY3UOkgQP+/g VswdP3XPyM+pN9EnPwaRwSH+VcaqzJUgZrebhVVicK71DJVUrgYLvoWLabbzfgLh7e PAL+eNbz8ybm0QVG2SmwB9QfbNfCzCMbRD3zAKsc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id 63D9A385AC1A; Tue, 1 Feb 2022 18:54:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 63D9A385AC1A Received: by mail-io1-xd33.google.com with SMTP id d188so22414141iof.7; Tue, 01 Feb 2022 10:54:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:from:subject; bh=Hoi4UEH2YS8YOWxAoWwyyTULRglkhppGOz9YKsRNEIU=; b=gmLlYMzGiPAIW+4MsyMek/AKex3LWUCHdLYVeI1FNNO3KOU9YXeClXPJT0pPxXdhz6 ZO2AiioWr+0XOAT+1YAz+LUWiqGFVj+kvSRo8mi4r1x4lmJeLTmrb6n4S/FyZ+GD7QZk KOcGxwML5oZ+ghNakhgJ6cDF9ahYan5RgH+995Q8FJpe5yTjs4hZ6vbi93Dcc5ZeFTpw xYh5+Rco+zKIH7G0xT+4SGIX97uFzySKHu8jvIetTd8rRWa9N953v0pFX7hjHuhs+eYS +5vsuchD/kdsFvMf67MghK+urHFBaKfpUWkDkIyXN5HP8kYXE1RiFEkI9qucF0UwCGbp 3yTg== X-Gm-Message-State: AOAM5306C0+cDaEa4XVo/ShYXrX/onSK6Aa59gPdM6wL8Es5ZxQSD9XF 57ZqjdkxQGDF21jtiXhFmBzklRdl6wg= X-Google-Smtp-Source: ABdhPJyhKySnh3BRd8IBsYtpWtwgZ9clC0C0/57Uw8xNXP1kDv45Npx3YkSo/kd2s+CzOZ/lM7DwMQ== X-Received: by 2002:a05:6638:1683:: with SMTP id f3mr9339427jat.205.1643741640662; Tue, 01 Feb 2022 10:54:00 -0800 (PST) Received: from [192.168.0.41] (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.gmail.com with ESMTPSA id l13sm5607414ilj.24.2022.02.01.10.54.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 01 Feb 2022 10:54:00 -0800 (PST) Message-ID: <2c589ec7-41ec-e581-29ba-1e35825b2d51@gmail.com> Date: Tue, 1 Feb 2022 11:53:59 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Content-Language: en-US To: libstdc++@gcc.gnu.org, gcc-patches Subject: [PATCH] declare std::array members attribute const [PR101831] X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Passing an uninitialized object to a function that takes its argument by const reference is diagnosed by -Wmaybe-uninitialized because most such functions read the argument. The exceptions are functions that don't access the object but instead use its address to compute a result. This includes a number of std::array member functions such as std::array::size() which returns the template argument N. Such functions may be candidates for attribute const which also avoids the warning. The attribute typically only benefits extern functions that IPA cannot infer the property from, but in this case it helps avoid the warning which runs very early on, even without optimization or inlining. The attached patch adds the attribute to a subset of those member functions of std::array. (It doesn't add it to const member functions like cbegin() or front() that return a const_iterator or const reference to the internal data.) It might be possible to infer this property from inline functions earlier on than during IPA and avoid having to annotate them explicitly. That seems like an enhancement worth considering in the future. Tested on x86_64-linux. Martin Declare std::array members with attribute const [PR101831]. Resolves: PR libstdc++/101831 - Spurious maybe-uninitialized warning on std::array::size libstdc++-v3/ChangeLog: * include/std/array (begin): Declare const member function attribute const. (end, rbegin, rend, size, max_size, empty, data): Same. * testsuite/23_containers/array/capacity/empty.cc: Add test cases. * testsuite/23_containers/array/capacity/max_size.cc: Same. * testsuite/23_containers/array/capacity/size.cc: Same. * testsuite/23_containers/array/iterators/begin_end.cc: New test. diff --git a/libstdc++-v3/include/std/array b/libstdc++-v3/include/std/array index b4d8fc81a52..e45143fb329 100644 --- a/libstdc++-v3/include/std/array +++ b/libstdc++-v3/include/std/array @@ -127,7 +127,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { std::swap_ranges(begin(), end(), __other.begin()); } // Iterators. - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] _GLIBCXX17_CONSTEXPR iterator begin() noexcept { return iterator(data()); } @@ -137,7 +137,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION begin() const noexcept { return const_iterator(data()); } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] _GLIBCXX17_CONSTEXPR iterator end() noexcept { return iterator(data() + _Nm); } @@ -147,7 +147,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION end() const noexcept { return const_iterator(data() + _Nm); } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] _GLIBCXX17_CONSTEXPR reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } @@ -157,7 +157,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION rbegin() const noexcept { return const_reverse_iterator(end()); } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] _GLIBCXX17_CONSTEXPR reverse_iterator rend() noexcept { return reverse_iterator(begin()); } @@ -188,15 +188,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return const_reverse_iterator(begin()); } // Capacity. - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] constexpr size_type size() const noexcept { return _Nm; } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] constexpr size_type max_size() const noexcept { return _Nm; } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] constexpr bool empty() const noexcept { return size() == 0; } @@ -278,7 +278,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _AT_Type::_S_ref(_M_elems, 0); } - [[__nodiscard__]] + [[__gnu__::__const__, __nodiscard__]] _GLIBCXX17_CONSTEXPR pointer data() noexcept { return _AT_Type::_S_ptr(_M_elems); } diff --git a/libstdc++-v3/testsuite/23_containers/array/capacity/empty.cc b/libstdc++-v3/testsuite/23_containers/array/capacity/empty.cc index 3f3f282ad9d..cecbae39e45 100644 --- a/libstdc++-v3/testsuite/23_containers/array/capacity/empty.cc +++ b/libstdc++-v3/testsuite/23_containers/array/capacity/empty.cc @@ -40,8 +40,26 @@ test01() } } +#pragma GCC push_options +#pragma GCC optimize "0" + +void +test02() +{ + { + const size_t len = 3; + typedef std::array array_type; + array_type a; + + VERIFY( a.empty() == false ); // { dg-bogus "-Wmaybe-uninitialized" + } +} + +#pragma GCC pop_options + int main() { test01(); + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/array/capacity/max_size.cc b/libstdc++-v3/testsuite/23_containers/array/capacity/max_size.cc index 0e000258530..4629316161d 100644 --- a/libstdc++-v3/testsuite/23_containers/array/capacity/max_size.cc +++ b/libstdc++-v3/testsuite/23_containers/array/capacity/max_size.cc @@ -40,8 +40,26 @@ test01() } } +#pragma GCC push_options +#pragma GCC optimize "0" + +void +test02() +{ + { + const size_t len = 3; + typedef std::array array_type; + array_type a; + + VERIFY( a.max_size() == len ); // { dg-bogus "-Wmaybe-uninitialized" + } +} + +#pragma GCC pop_options + int main() { test01(); + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/array/capacity/size.cc b/libstdc++-v3/testsuite/23_containers/array/capacity/size.cc index 3e4aa7143dc..dddd909a0ac 100644 --- a/libstdc++-v3/testsuite/23_containers/array/capacity/size.cc +++ b/libstdc++-v3/testsuite/23_containers/array/capacity/size.cc @@ -40,8 +40,26 @@ test01() } } +#pragma GCC push_options +#pragma GCC optimize "0" + +void +test02() +{ + { + const size_t len = 3; + typedef std::array array_type; + array_type a; + + VERIFY( a.size() == len ); // { dg-bogus "-Wmaybe-uninitialized" + } +} + +#pragma GCC pop_options + int main() { test01(); + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/23_containers/array/iterators/begin_end.cc b/libstdc++-v3/testsuite/23_containers/array/iterators/begin_end.cc new file mode 100644 index 00000000000..b7743adf3c9 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/array/iterators/begin_end.cc @@ -0,0 +1,56 @@ +// { dg-do compile { target c++11 } } +// +// Copyright (C) 2011-2022 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include + +#pragma GCC push_options +#pragma GCC optimize "0" + +extern void +sink (const void*, ...); + +void +test01() +{ + { + const std::size_t len = 1; + typedef std::array array_type; + typedef array_type::iterator iterator;; + array_type a; + + iterator b = a.begin(); // { dg-bogus "-Wmaybe-uninitialized" } + iterator e = a.end(); // { dg-bogus "-Wmaybe-uninitialized" } + + sink(&b, &e); + } + + { + const std::size_t len = 3; + typedef std::array array_type; + typedef array_type::reverse_iterator reverse_iterator; + array_type a; + + reverse_iterator b = a.rbegin(); // { dg-bogus "-Wmaybe-uninitialized" } + reverse_iterator e = a.rend(); // { dg-bogus "-Wmaybe-uninitialized" } + + sink(&b, &e); + } +} + +#pragma GCC pop_options