This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch to pedwarn for out-of-range floating constants


The diagnostic for an out-of-range floating-point constant was a
warning rather than a pedwarn, with the comment

  /* A diagnostic is required for "soft" overflow by some ISO C
     testsuites.  This is not pedwarn, because some people don't want
     an error for this.
     ??? That's a dubious reason... is this a mandatory diagnostic or
     isn't it?   -- zw, 2001-08-21.  */

C90, C99 and C++03 all require a diagnostic here, so it should be a
pedwarn: if you want an infinity, use __builtin_inf*.  This patch
makes it a pedwarn.  Indeed, I wonder whether it should actually be a
mandatory pedwarn rather than a pedwarn-if-pedantic, since
__builtin_inf* exist, users should be using the C99 macro INFINITY and
system headers can be fixincluded to use __builtin_inf* if needed.

(The pedwarn for __builtin_inf* if the target does not support
infinities is also required by the definition of INFINITY in C99;
there's even a footnote pointing to the relevant constraint; and 
__builtin_inf* are meant to be usable to define INFINITY for all targets.)

Bootstrapped with no regressions on i686-pc-linux-gnu.  OK to commit
(the builtins.c change)?

-- 
Joseph S. Myers               http://www.srcf.ucam.org/~jsm28/gcc/
    jsm@polyomino.org.uk (personal mail)
    joseph@codesourcery.com (CodeSourcery mail)
    jsm28@gcc.gnu.org (Bugzilla assignments and CCs)

2004-10-08  Joseph S. Myers  <jsm@polyomino.org.uk>

	* c-lex.c (interpret_float): Give a pedwarn rather than a warning
	for an out-of-range floating point constant.
	* builtins.c (fold_builtin_inf): Give a pedwarn rather than a
	warning if the target format does not support infinities.

testsuite:
2004-10-08  Joseph S. Myers  <jsm@polyomino.org.uk>

	* gcc.dg/float-range-1.c, gcc.dg/float-range-2.c: New tests.

diff -rupN GCC.orig/gcc/builtins.c GCC/gcc/builtins.c
--- GCC.orig/gcc/builtins.c	2004-10-06 22:01:34.000000000 +0000
+++ GCC/gcc/builtins.c	2004-10-08 15:10:24.000000000 +0000
@@ -5873,7 +5873,7 @@ fold_builtin_inf (tree type, int warn)
   REAL_VALUE_TYPE real;
 
   if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) && warn)
-    warning ("target format does not support infinity");
+    pedwarn ("target format does not support infinity");
 
   real_inf (&real);
   return build_real (type, real);
diff -rupN GCC.orig/gcc/c-lex.c GCC/gcc/c-lex.c
--- GCC.orig/gcc/c-lex.c	2004-10-03 20:50:28.000000000 +0000
+++ GCC/gcc/c-lex.c	2004-10-08 14:51:11.000000000 +0000
@@ -653,13 +653,13 @@ interpret_float (const cpp_token *token,
   real_from_string (&real, copy);
   real_convert (&real, TYPE_MODE (type), &real);
 
-  /* A diagnostic is required for "soft" overflow by some ISO C
-     testsuites.  This is not pedwarn, because some people don't want
-     an error for this.
-     ??? That's a dubious reason... is this a mandatory diagnostic or
-     isn't it?   -- zw, 2001-08-21.  */
+  /* Both C and C++ require a diagnostic for a floating constant
+     outside the range of representable values of its type.  Since we
+     have __builtin_inf* to produce an infinity, it might now be
+     appropriate for this to be a mandatory pedwarn rather than
+     conditioned on -pedantic.  */
   if (REAL_VALUE_ISINF (real) && pedantic)
-    warning ("floating constant exceeds range of %<%s%>", type_name);
+    pedwarn ("floating constant exceeds range of %<%s%>", type_name);
 
   /* Create a node with determined type and value.  */
   value = build_real (type, real);
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/float-range-1.c GCC/gcc/testsuite/gcc.dg/float-range-1.c
--- GCC.orig/gcc/testsuite/gcc.dg/float-range-1.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/float-range-1.c	2004-10-08 14:54:02.000000000 +0000
@@ -0,0 +1,13 @@
+/* Floating constants outside the range of their type should receive a
+   pedwarn, not a warning.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile } */
+/* { dg-options "-ansi -pedantic-errors" } */
+
+void
+f (void)
+{
+  float a = 1e+100000000f; /* { dg-error "error: floating constant exceeds range of 'float'" } */
+  double b = 1e+100000000; /* { dg-error "error: floating constant exceeds range of 'double'" } */
+  long double c = 1e+100000000l; /* { dg-error "error: floating constant exceeds range of 'long double'" } */
+}
diff -rupN GCC.orig/gcc/testsuite/gcc.dg/float-range-2.c GCC/gcc/testsuite/gcc.dg/float-range-2.c
--- GCC.orig/gcc/testsuite/gcc.dg/float-range-2.c	1970-01-01 00:00:00.000000000 +0000
+++ GCC/gcc/testsuite/gcc.dg/float-range-2.c	2004-10-08 15:08:53.000000000 +0000
@@ -0,0 +1,14 @@
+/* Floating constants outside the range of their type should receive a
+   pedwarn, not a warning.  This includes INFINITY if the target does
+   not support infinities.  */
+/* Origin: Joseph Myers <jsm@polyomino.org.uk> */
+/* { dg-do compile { target vax-*-* pdp11-*-* } } */
+/* { dg-options "-ansi -pedantic-errors" } */
+
+void
+f (void)
+{
+  float a = __builtin_inff (); /* { dg-error "error: target format does not support infinity" } */
+  double b = __builtin_inf (); /* { dg-error "error: target format does not support infinity" } */
+  long double c = __builtin_infl (); /* { dg-error "error: target format does not support infinity" } */
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]