]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Don't hide narrowing errors in system headers
authorMarek Polacek <polacek@redhat.com>
Thu, 15 Jul 2021 22:45:49 +0000 (18:45 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 16 Jul 2021 13:28:37 +0000 (09:28 -0400)
Jonathan pointed me at this issue where

  constexpr unsigned f() { constexpr int n = -1; return unsigned{n}; }

is accepted in system headers, despite the narrowing conversion from
a constant.  I suspect that whereas narrowing warnings should be
disabled, ill-formed narrowing of constants should be a hard error
(which can still be disabled by -Wno-narrowing).

gcc/cp/ChangeLog:

* typeck2.c (check_narrowing): Don't suppress the pedantic error
in system headers.

libstdc++-v3/ChangeLog:

* testsuite/20_util/ratio/operations/ops_overflow_neg.cc: Add
dg-error.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/Wnarrowing2.C: New test.
* g++.dg/cpp1y/Wnarrowing2.h: New test.

gcc/cp/typeck2.c
gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc

index 6679e247816412cf6e43602751a0512a122e8c88..dcfdff2f90553b01ea1835a0e73271636edb4ca7 100644 (file)
@@ -986,6 +986,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain,
        {
          int savederrorcount = errorcount;
          global_dc->pedantic_errors = 1;
+         auto s = make_temp_override (global_dc->dc_warn_system_headers, true);
          pedwarn (loc, OPT_Wnarrowing,
                   "narrowing conversion of %qE from %qH to %qI",
                   init, ftype, type);
diff --git a/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C
new file mode 100644 (file)
index 0000000..048d484
--- /dev/null
@@ -0,0 +1,4 @@
+// { dg-do compile { target c++14 } }
+
+#include "Wnarrowing2.h"
+// { dg-error "narrowing conversion" "" { target *-*-* } 0 }
diff --git a/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h
new file mode 100644 (file)
index 0000000..7dafa51
--- /dev/null
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+constexpr unsigned f() { constexpr int n = -1; return unsigned{n}; }
index 47d3c3a037e2575ae865e5aa8e9ecb994670fac1..f120e599a330407886aefd5b3468a643d035c75e 100644 (file)
@@ -39,6 +39,7 @@ test02()
 }
 
 // { dg-error "required from here" "" { target *-*-* } 28 }
+// { dg-error "expected initializer" "" { target *-*-* } 28 }
 // { dg-error "expected initializer" "" { target *-*-* } 35 }
 // { dg-error "expected initializer" "" { target *-*-* } 37 }
 // { dg-error "overflow in addition" "" { target *-*-* } 0 }
@@ -46,5 +47,6 @@ test02()
 // { dg-error "overflow in multiplication" "" { target *-*-* } 100 }
 // { dg-error "overflow in multiplication" "" { target *-*-* } 102 }
 // { dg-error "overflow in constant expression" "" { target *-*-* } 0 }
+// { dg-error "narrowing conversion" "" { target *-*-* } 0 }
 // { dg-prune-output "out of range" }
 // { dg-prune-output "not usable in a constant expression" }