Declare std::array members with attribute const [PR101831].
authorMartin Sebor <msebor@redhat.com>
Wed, 2 Feb 2022 00:19:11 +0000 (17:19 -0700)
committerMartin Sebor <msebor@redhat.com>
Wed, 2 Feb 2022 00:21:49 +0000 (17:21 -0700)
Resolves:
PR libstdc++/101831 - Spurious maybe-uninitialized warning on std::array::size

libstdc++-v3/ChangeLog:

PR libstdc++/101831
* 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.

libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/capacity/empty.cc
libstdc++-v3/testsuite/23_containers/array/capacity/max_size.cc
libstdc++-v3/testsuite/23_containers/array/capacity/size.cc
libstdc++-v3/testsuite/23_containers/array/iterators/begin_end.cc [new file with mode: 0644]

index b4d8fc8..e45143f 100644 (file)
@@ -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); }
index 3f3f282..cecbae3 100644 (file)
@@ -40,8 +40,26 @@ test01()
   }
 }
 
+#pragma GCC push_options
+#pragma GCC optimize "0"
+
+void
+test02()
+{
+  {
+    const size_t len = 3;
+    typedef std::array<int, len> array_type;
+    array_type a;
+
+    VERIFY( a.empty() == false );    // { dg-bogus "-Wmaybe-uninitialized"
+  }
+}
+
+#pragma GCC pop_options
+
 int main()
 {
   test01();
+  test02();
   return 0;
 }
index 0e00025..4629316 100644 (file)
@@ -40,8 +40,26 @@ test01()
   }
 }
 
+#pragma GCC push_options
+#pragma GCC optimize "0"
+
+void
+test02()
+{
+  {
+    const size_t len = 3;
+    typedef std::array<int, len> array_type;
+    array_type a;
+
+    VERIFY( a.max_size() == len );  // { dg-bogus "-Wmaybe-uninitialized"
+  }
+}
+
+#pragma GCC pop_options
+
 int main()
 {
   test01();
+  test02();
   return 0;
 }
index 3e4aa71..dddd909 100644 (file)
@@ -40,8 +40,26 @@ test01()
   }
 }
 
+#pragma GCC push_options
+#pragma GCC optimize "0"
+
+void
+test02()
+{
+  {
+    const size_t len = 3;
+    typedef std::array<int, len> 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 (file)
index 0000000..69594a1
--- /dev/null
@@ -0,0 +1,39 @@
+// { dg-do compile { target c++11 } }
+
+#include <array>
+
+#pragma GCC push_options
+#pragma GCC optimize "0"
+
+extern void
+sink (const void*, ...);
+
+void
+test01()
+{
+  {
+    const std::size_t len = 1;
+    typedef std::array<int, len> 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<int, len> 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