This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] fix PR 45038, bad interaction between __DBL_MIN__ and -Wold-style-cast
- From: Nathan Froyd <froydnj at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 10 Nov 2010 08:22:18 -0500
- Subject: Re: [PATCH] fix PR 45038, bad interaction between __DBL_MIN__ and -Wold-style-cast
- References: <20101109221019.GI7991@nightcrawler>
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
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__;
+}