diff mbox

fix PR 45038, bad interaction between __DBL_MIN__ and -Wold-style-cast

Message ID 20101110132218.GK7991@nightcrawler
State New
Headers show

Commit Message

Nathan Froyd Nov. 10, 2010, 1:22 p.m. UTC
On Tue, Nov 09, 2010 at 05:10:20PM -0500, Nathan Froyd wrote:
> Builtin double-precision floating-point constants defined by the
> preprocessor get defined as `((double)VAL)'; this style causes problems
> with -Wold-style-cast.  The patch below tweaks the definition of such
> constants to use static_cast when compiling for C++.

My previous patch was overly complicated.  This one is much simpler.
I used a function style cast as suggested by Gabriel, but I am unsure of
whether that is preferred over static_cast.

Tested on x86_64-unknown-linux-gnu.  OK to commit?

-Nathan

Comments

Gabriel Dos Reis Nov. 10, 2010, 3:46 p.m. UTC | #1
On Wed, Nov 10, 2010 at 7:22 AM, Nathan Froyd <froydnj@codesourcery.com> wrote:
> On Tue, Nov 09, 2010 at 05:10:20PM -0500, Nathan Froyd wrote:
>> Builtin double-precision floating-point constants defined by the
>> preprocessor get defined as `((double)VAL)'; this style causes problems
>> with -Wold-style-cast.  The patch below tweaks the definition of such
>> constants to use static_cast when compiling for C++.
>
> My previous patch was overly complicated.  This one is much simpler.
> I used a function style cast as suggested by Gabriel, but I am unsure of
> whether that is preferred over static_cast.

It certainly is simpler -- and does not qualify as `old-style cast' which
is what you write in C.

The patch is OK with me.  If you don't hear objects in the next 24 hours,
I would suggest to commit.

-- Gaby
diff mbox

Patch

diff --git a/gcc/c-family/c-cppbuiltin.c b/gcc/c-family/c-cppbuiltin.c
index 7b5a14d..ca77964 100644
--- a/gcc/c-family/c-cppbuiltin.c
+++ b/gcc/c-family/c-cppbuiltin.c
@@ -657,9 +657,13 @@  c_cpp_builtins (cpp_reader *pfile)
   /* Cast the double precision constants.  This is needed when single
      precision constants are specified or when pragma FLOAT_CONST_DECIMAL64
      is used.  The correct result is computed by the compiler when using
-     macros that include a cast.  */
-  builtin_define_float_constants ("DBL", "L", "((double)%s)", "",
-				  double_type_node);
+     macros that include a cast.  We use a different cast for C++ to avoid
+     problems with -Wold-style-cast.  */
+  builtin_define_float_constants ("DBL", "L",
+				  (c_dialect_cxx ()
+				   ? "double(%s)"
+				   : "((double)%s)"),
+				  "", double_type_node);
   builtin_define_float_constants ("LDBL", "L", "%s", "L",
 				  long_double_type_node);
 
diff --git a/gcc/testsuite/g++.dg/pr45038.C b/gcc/testsuite/g++.dg/pr45038.C
new file mode 100644
index 0000000..57c0c44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr45038.C
@@ -0,0 +1,9 @@ 
+// PR preprocessor/45038
+// { dg-do compile }
+// { dg-options "-Werror -Wold-style-cast" }
+
+double f(void)
+{
+  // We used to produce old-style casts for this.
+  return __DBL_MIN__;
+}