]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Make _GLIBCXX_NODISCARD work for C++11 and C++14
authorJonathan Wakely <jwakely@redhat.com>
Mon, 26 Feb 2024 11:40:46 +0000 (11:40 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 14 Nov 2024 17:00:40 +0000 (17:00 +0000)
The _GLIBCXX_NODISCARD macro only expands to [[__nodiscard__]] for C++17
and later, but all supported compilers will allow us to use that for
C++11 and C++14 too. Enable it for those older standards, to give
improved diagnostics for users of those older standards.

libstdc++-v3/ChangeLog:

* include/bits/c++config (_GLIBCXX_NODISCARD): Expand for C++11
and C++14.
* testsuite/22_locale/locale/cons/12438.cc: Adjust dg-warning to
expect nodiscard warnings for C++11 and C++14 as well.
* testsuite/22_locale/locale/operations/2.cc: Likewise.
* testsuite/25_algorithms/equal/debug/1_neg.cc: Likewise.
* testsuite/25_algorithms/equal/debug/2_neg.cc: Likewise.
* testsuite/25_algorithms/equal/debug/3_neg.cc: Likewise.
* testsuite/25_algorithms/find_first_of/concept_check_1.cc:
Likewise.
* testsuite/25_algorithms/is_permutation/2.cc: Likewise.
* testsuite/25_algorithms/lexicographical_compare/71545.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/33613.cc: Likewise.
* testsuite/25_algorithms/lower_bound/debug/irreflexive.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc:
Likewise.
* testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc: Likewise.
* testsuite/25_algorithms/minmax/3.cc: Likewise.
* testsuite/25_algorithms/search/78346.cc: Likewise.
* testsuite/25_algorithms/search_n/58358.cc: Likewise.
* testsuite/25_algorithms/unique/1.cc: Likewise.
* testsuite/25_algorithms/unique/11480.cc: Likewise.
* testsuite/25_algorithms/upper_bound/33613.cc: Likewise.
* testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc:
Likewise.
* testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc: Likewise.
* testsuite/27_io/ios_base/types/fmtflags/bitmask_operators.cc:
Likewise.
* testsuite/27_io/ios_base/types/iostate/bitmask_operators.cc:
Likewise.
* testsuite/27_io/ios_base/types/openmode/bitmask_operators.cc:
Likewise.
* testsuite/ext/concept_checks.cc: Likewise.
* testsuite/ext/is_heap/47709.cc: Likewise.
* testsuite/ext/is_sorted/cxx0x.cc: Likewise.

27 files changed:
libstdc++-v3/include/bits/c++config
libstdc++-v3/testsuite/22_locale/locale/cons/12438.cc
libstdc++-v3/testsuite/22_locale/locale/operations/2.cc
libstdc++-v3/testsuite/25_algorithms/equal/debug/1_neg.cc
libstdc++-v3/testsuite/25_algorithms/equal/debug/2_neg.cc
libstdc++-v3/testsuite/25_algorithms/equal/debug/3_neg.cc
libstdc++-v3/testsuite/25_algorithms/find_first_of/concept_check_1.cc
libstdc++-v3/testsuite/25_algorithms/is_permutation/2.cc
libstdc++-v3/testsuite/25_algorithms/lexicographical_compare/71545.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/33613.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/irreflexive.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_neg.cc
libstdc++-v3/testsuite/25_algorithms/lower_bound/debug/partitioned_pred_neg.cc
libstdc++-v3/testsuite/25_algorithms/minmax/3.cc
libstdc++-v3/testsuite/25_algorithms/search/78346.cc
libstdc++-v3/testsuite/25_algorithms/search_n/58358.cc
libstdc++-v3/testsuite/25_algorithms/unique/1.cc
libstdc++-v3/testsuite/25_algorithms/unique/11480.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/33613.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_neg.cc
libstdc++-v3/testsuite/25_algorithms/upper_bound/debug/partitioned_pred_neg.cc
libstdc++-v3/testsuite/27_io/ios_base/types/fmtflags/bitmask_operators.cc
libstdc++-v3/testsuite/27_io/ios_base/types/iostate/bitmask_operators.cc
libstdc++-v3/testsuite/27_io/ios_base/types/openmode/bitmask_operators.cc
libstdc++-v3/testsuite/ext/concept_checks.cc
libstdc++-v3/testsuite/ext/is_heap/47709.cc
libstdc++-v3/testsuite/ext/is_sorted/cxx0x.cc

index 1076803a86557e4417bf5ff4a7478cf55418ca09..236906d2f79f4409923d688d082ca17083d17273 100644 (file)
 #endif
 
 // Macro to warn about unused results.
-#if __cplusplus >= 201703L
+#if __cplusplus >= 201103L
 # define _GLIBCXX_NODISCARD [[__nodiscard__]]
 #else
 # define _GLIBCXX_NODISCARD
index 4838e1ba6936e07585e6012897ed7c1d4e7adce9..72b6b15adf5eaccd44cc5066407eeb6cfcb9520b 100644 (file)
@@ -45,7 +45,7 @@ void test01(int iters)
          locale loc2 = locale("");
          VERIFY( !has_facet<MyFacet>(loc2) );
          
-         loc1.combine<MyFacet>(loc2); // { dg-warning "nodiscard" "" { target c++17 } }
+         loc1.combine<MyFacet>(loc2); // { dg-warning "nodiscard" "" { target c++11 } }
          VERIFY( false );
        }
       catch (std::runtime_error&)
index 917adecac56ef485c7c3df9ba4a02a19d9d6c3e6..c0a6613eb74f05c0f658c53c25761879af923525 100644 (file)
@@ -48,7 +48,7 @@ void test02()
   // Derivation, MF check.
   locale loc_gnu(loc_c, new gnu_collate);
   gnu_count = 0;
-  loc_gnu(s01, s02); // { dg-warning "nodiscard" "" { target c++17 } }
+  loc_gnu(s01, s02); // { dg-warning "nodiscard" "" { target c++11 } }
   VERIFY( gnu_count == 1 );
 }
 
index f9328599a5584477353f861d1e43bdd42a7cb668..38060b1bea41f1addd1d06dc89b26b6f672881e6 100644 (file)
@@ -27,7 +27,7 @@ test01()
   std::vector<int> vect;
   vect.push_back(1);
   std::equal(vect.end(), vect.begin(), vect.begin());
-  // { dg-warning "ignoring return value" "" { target c++17 } 29 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 29 }
 }
 
 int
index 2e9134a74ea6eb3ccac8a4d32ff9d466f37c2af6..5148571fa2196d88c80a5a462afc5c2bc382e9fa 100644 (file)
@@ -27,7 +27,7 @@ test01()
   std::vector<int> v1, v2;
   v1.push_back(1);
   std::equal(v1.begin(), v1.end(), v2.begin());
-  // { dg-warning "ignoring return value" "" { target c++17 } 29 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 29 }
 }
 
 int
index 425e7aa78c66d6e7304f957975f52c3559057a4a..5a176306c2b86aa837df1a870d4d5568aef73da2 100644 (file)
@@ -33,7 +33,7 @@ test01()
   l2.push_back(2);
 
   std::equal(++l1.begin(), l1.end(), ++l2.begin());
-  // { dg-warning "ignoring return value" "" { target c++17 } 35 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 35 }
 }
 
 int
index a970cd4bd61c01be955531970c057183469c5725..33f3b42b9fbe571be2ee450ecfb9ef160ae61055 100644 (file)
@@ -37,7 +37,7 @@ class2 b;
 void test01()
 {
   std::find_first_of(&a, &a, &b, &b, comp);
-  // { dg-warning "ignoring return value" "" { target c++17 } 39 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 39 }
 }
 
 int main()
index ab0610c329557f79639e9df1a401051a4a59d217..3561c2e6fbfb03e7c60da455165a43c33d3f9347 100644 (file)
@@ -103,7 +103,7 @@ void test02()
   int arr[] = { 11, 22, 33 };
   using namespace std;
   is_permutation(begin(arr0), end(arr0), begin(arr), end(arr), thrower);
-  // { dg-warning "ignoring return value" "" { target c++17 } 105 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 105 }
 }
 
 int main()
index 9f666caa68b74b70ce3b9f1f72484919cd7cd803..9f50714e3c90299cec374ecaa9eb23f056769294 100644 (file)
@@ -33,5 +33,5 @@ int main()
   X x[1];
   int i[1];
   std::lexicographical_compare(x, x+1, i, i+1);
-  // { dg-warning "ignoring return value" "" { target c++17 } 35 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 35 }
 }
index fa42b82b72132d19022d994c0189c508606ee163..adb04d3f0a138e2c07deea9a26d82f72b8b7decf 100644 (file)
@@ -33,5 +33,5 @@ bool ab(A, B);
 void test01(A* a, B b)
 {
   std::lower_bound(a, a, b, ab);
-  // { dg-warning "ignoring return value" "" { target c++17 } 35 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 35 }
 }
index 41a6028e4041e6cff8b5c625508db5b410391fb2..d85459a57aafa620efc73311a2a3b5c61dbb1a85 100644 (file)
@@ -44,5 +44,5 @@ void test01()
 {
   A as[] = { 0, 1, 2, 3 };
   std::lower_bound(as, as + 4, 1, A_int_comparer());
-  // { dg-warning "ignoring return value" "" { target c++17 } 46 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 46 }
 }
index bc01d8147fad114bfb75e68b63ff9d411ca7ff27..34a6dbc2531a196ff51228728528fce296d57db7 100644 (file)
@@ -36,7 +36,7 @@ void test01()
 {
   A as[] = { 0, 1, 2, 0, 2, 3 };
   std::lower_bound(as, as + 6, A(1));
-  // { dg-warning "ignoring return value" "" { target c++17 } 38 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 38 }
 }
 
 int
index f860df939404029c631c0cb72e3f1758658c1343..2e7dec02540bfd4012a01bd75c8e989fe991ab1a 100644 (file)
@@ -24,7 +24,7 @@ void test01()
 {
   int as[] = { 0, 1, 0, 2, 3 };
   std::lower_bound(as, as + 5, 1, std::less<int>());
-  // { dg-warning "ignoring return value" "" { target c++17 } 26 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 26 }
 }
 
 
index a6620b9fa0d8acbc10a1ff8f6c4fdea6e2ed2082..63607cd469447f0376bb07737a326392d0ac38a4 100644 (file)
@@ -43,7 +43,7 @@ int compare_counter::count = 0;
 void test01()
 {
   std::minmax({1, 2, 3, 4, 5, 6, 7, 8}, compare_counter());
-  // { dg-warning "ignoring return value" "" { target c++17 } 45 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 45 }
 
   // If N is the number of arguments in the minmax function call, 
   // 25.3.7 specifies that at most 3N/2 comparisons are allowed.
index e6f3956b252db37b9f1b1c0ad691598b1fba9e8d..18fc946b2f3e171f7fcd86cb1c46a920c047e236 100644 (file)
@@ -109,7 +109,7 @@ test01()
 {
   value s[] = { 0, 1, 2, 3, 4, 5 };
   std::search(s, s+6, stashing_iterator(s), stashing_iterator(s+4));
-  // { dg-warning "ignoring return value" "" { target c++17 } 111 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 111 }
 }
 
 int
index 651fd86fc9c122277f622aaf26b3b52eddbfadec..b16e8c186a292812771c2f15d9af2c4c6ad5aada 100644 (file)
@@ -29,7 +29,7 @@ void test01()
   int count = 0;
   std::search_n(a.begin(), a.end(), 10, 1,
                [&count](int t, int u) { ++count; return t == u; });
-  // { dg-warning "ignoring return value" "" { target c++17 } 30 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 30 }
   VERIFY( count <= 11 );
 }
 
index 698c370e22995b412e7ba61d988416a9770ec1dc..f5e3c976a6301feb1a810746fc82b44e8f358dd4 100644 (file)
@@ -27,6 +27,6 @@ int main()
   using namespace std;
   list<int> menge;
   unique (menge.begin(), menge.end());
-  // { dg-warning "ignoring return value" "" { target c++17 } 29 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 29 }
   return 0;
 }
index a008fc57f549d0095f1f4883124d6edc1338830a..8c11b4c429d7ef91007d3a75c0dfd741f8731d17 100644 (file)
@@ -31,7 +31,7 @@ bool compare(int a, int b)
 // libstdc++/11480
 void test01()
 {
-  std::unique(a, a+10, compare); // { dg-warning "ignoring return value" "" { target c++17 } }
+  std::unique(a, a+10, compare); // { dg-warning "ignoring return value" "" { target c++11 } }
   VERIFY( compare_count == 9 );
 }
 
index 19453a38f51436bb2e0bc04394483ab801a097e6..e9eabef5a5337a40bcfda413761d91ee60719e68 100644 (file)
@@ -33,5 +33,5 @@ bool ba(B, A);
 void test01(A* a, B b)
 {
   std::upper_bound(a, a, b, ba);
-  // { dg-warning "ignoring return value" "" { target c++17 } 35 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 35 }
 }
index 630357d6a4560dfd13cf8f5ee35ab2ec5fd0b67a..a48a8261ef6f0cb2ab46a237a559129bbab6dd8d 100644 (file)
@@ -36,7 +36,7 @@ void test01()
 {
   A as[] = { 0, 2, 1, 3, 4, 5 };
   std::upper_bound(as, as + 6, A(1));
-  // { dg-warning "ignoring return value" "" { target c++17 } 38 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 38 }
 }
 
 int
index 5beab7d22d74252106d8500cdd1cebbf9a5a1dd8..3e69a24af026eb1263ec5310c6ac2ea14c72cb16 100644 (file)
@@ -24,7 +24,7 @@ void test01()
 {
   int as[] = { 0, 2, 1, 3, 4 };
   std::upper_bound(as, as + 5, 1, std::less<int>());
-  // { dg-warning "ignoring return value" "" { target c++17 } 26 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 26 }
 }
 
 
index 604d2aa6b5a2675a7e7a9e9d065a7d1471a66137..3cf8ebe9c469e83210a9a4d77c2a05c68c91cdd3 100644 (file)
@@ -27,7 +27,7 @@ int main()
 {
   __gnu_test::bitmask_operators<std::ios_base::fmtflags>();
 }
-// { dg-warning "ignoring return value.*operator\\|" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator&" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator\\^" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator~" "" { target c++17 } 0 }
+// { dg-warning "ignoring return value.*operator\\|" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator&" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator\\^" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator~" "" { target c++11 } 0 }
index a98435867ed442726ca67d02646210f2d0bd5cbe..d6580bb0e709cd5ab476aa3684f10df8ffff4e05 100644 (file)
@@ -27,7 +27,7 @@ int main()
 {
   __gnu_test::bitmask_operators<std::ios_base::iostate>();
 }
-// { dg-warning "ignoring return value.*operator\\|" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator&" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator\\^" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator~" "" { target c++17 } 0 }
+// { dg-warning "ignoring return value.*operator\\|" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator&" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator\\^" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator~" "" { target c++11 } 0 }
index 6d02421736288f5533ea413fcf9fa7e1770294fb..9a94d9b145a435a0851ecb73c1806e6311d0f16c 100644 (file)
@@ -27,7 +27,7 @@ int main()
 {
   __gnu_test::bitmask_operators<std::ios_base::openmode>();
 }
-// { dg-warning "ignoring return value.*operator\\|" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator&" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator\\^" "" { target c++17 } 0 }
-// { dg-warning "ignoring return value.*operator~" "" { target c++17 } 0 }
+// { dg-warning "ignoring return value.*operator\\|" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator&" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator\\^" "" { target c++11 } 0 }
+// { dg-warning "ignoring return value.*operator~" "" { target c++11 } 0 }
index abb43a4fadeaf56708357873c6a585dccff27163..016600461bf2ce5306661de574bf0e69d80b2959 100644 (file)
@@ -73,10 +73,10 @@ test2054( )
   upper_bound(Index.begin(), Index.end(), SearchTerm, aComparison);
   equal_range(Index.begin(), Index.end(), SearchTerm, aComparison);
   binary_search(Index.begin(), Index.end(), SearchTerm, aComparison);
-  // { dg-warning "ignoring return value" "" { target c++17 } 72 }
-  // { dg-warning "ignoring return value" "" { target c++17 } 73 }
-  // { dg-warning "ignoring return value" "" { target c++17 } 74 }
-  // { dg-warning "ignoring return value" "" { target c++17 } 75 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 72 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 73 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 74 }
+  // { dg-warning "ignoring return value" "" { target c++11 } 75 }
 }
 
 int main()
index 50bbccaf7251a9fb9079984e165d73a670abf991..64966d60317d96f33e5952273abfb90e579668ca 100644 (file)
@@ -24,6 +24,5 @@
 void foo()
 {
   std::vector<int> v;
-  is_heap(v.begin(), v.end());
-  // { dg-warning "ignoring return value" "" { target c++17 } 27 }
+  is_heap(v.begin(), v.end()); // { dg-warning "ignoring return value" }
 }
index 3592d68bfcf6a53174332d01662666b344bddefd..31672481ad870d8bb1845c7938dc540c1c78fdd3 100644 (file)
@@ -23,6 +23,5 @@
 void foo()
 {
   std::vector<int> v;
-  is_sorted(v.begin(), v.end());
-  // { dg-warning "ignoring return value" "" { target c++17 } 26 }
+  is_sorted(v.begin(), v.end()); // { dg-warning "ignoring return value" }
 }