public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/trunk] c++: Don't hide narrowing errors in system headers
@ 2021-07-16 13:29 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2021-07-16 13:29 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:e32234536f361796e7cad8ed69a1c0bb46ee55de

commit e32234536f361796e7cad8ed69a1c0bb46ee55de
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jul 15 18:45:49 2021 -0400

    c++: Don't hide narrowing errors in system headers
    
    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.

Diff:
---
 gcc/cp/typeck2.c                                                    | 1 +
 gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C                            | 4 ++++
 gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h                            | 2 ++
 libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc | 2 ++
 4 files changed, 9 insertions(+)

diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 6679e247816..dcfdff2f905 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -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
index 00000000000..048d484f46f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.C
@@ -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
index 00000000000..7dafa51af14
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/Wnarrowing2.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+constexpr unsigned f() { constexpr int n = -1; return unsigned{n}; }
diff --git a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc
index 47d3c3a037e..f120e599a33 100644
--- a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc
@@ -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" }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-07-16 13:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-16 13:29 [gcc/trunk] c++: Don't hide narrowing errors in system headers Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).