changeset 3067:bcd41b269091

Fixed bug #537 Description From esigra 2008-01-07 16:20:21 (-) [reply] I try to get a clean compile for a project using SDL. But I get warnings from SDL headers when I use -Wold-style-cast. This is especially bad because I plan to build the software with -Werror=old-style-cast when we have switched over to GCC 4.2, which has that option. But this problem can be fixed in the SDL headers. I checked out the SVN version and made some patches. See the attached patch.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 17 Feb 2009 04:57:39 +0000
parents 8cbf68bdf81b
children b21348d47cab
files include/SDL_endian.h include/SDL_stdinc.h
diffstat 2 files changed, 18 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_endian.h	Sun Feb 15 06:12:52 2009 +0000
+++ b/include/SDL_endian.h	Tue Feb 17 04:57:39 2009 +0000
@@ -174,9 +174,9 @@
     Uint32 hi, lo;
 
     /* Separate into high and low 32-bit values and swap them */
-    lo = (Uint32) (x & 0xFFFFFFFF);
+    lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
     x >>= 32;
-    hi = (Uint32) (x & 0xFFFFFFFF);
+    hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
     x = SDL_Swap32(lo);
     x <<= 32;
     x |= SDL_Swap32(hi);
--- a/include/SDL_stdinc.h	Sun Feb 15 06:12:52 2009 +0000
+++ b/include/SDL_stdinc.h	Tue Feb 17 04:57:39 2009 +0000
@@ -83,6 +83,16 @@
 #define SDL_arraysize(array)	(sizeof(array)/sizeof(array[0]))
 #define SDL_TABLESIZE(table)	SDL_arraysize(table)
 
+/* Use proper C++ casts when compiled as C++ to be compatible with the option
+ -Wold-style-cast of GCC (and -Werror=old-style-cast in GCC 4.2 and above. */
+#ifdef __cplusplus
+#define SDL_reinterpret_cast(type, expression) reinterpret_cast<type>(expression)
+#define SDL_static_cast(type, expression) static_cast<type>(expression)
+#else
+#define SDL_reinterpret_cast(type, expression) ((type)(expression))
+#define SDL_static_cast(type, expression) ((type)(expression))
+#endif
+
 /* Basic data types */
 typedef enum SDL_bool
 {
@@ -298,7 +308,7 @@
 		"cld\n\t"					\
 		"rep ; stosl\n\t"				\
 		: "=&D" (u0), "=&a" (u1), "=&c" (u2)		\
-		: "0" (dst), "1" (val), "2" ((Uint32)(len))	\
+		: "0" (dst), "1" (val), "2" (SDL_static_cast(Uint32, len))	\
 		: "memory" );					\
 } while(0)
 #endif
@@ -307,7 +317,7 @@
 do {						\
 	unsigned _count = (len);		\
 	unsigned _n = (_count + 3) / 4;		\
-	Uint32 *_p = (Uint32 *)(dst);		\
+	Uint32 *_p = SDL_static_cast(Uint32 *, dst);		\
 	Uint32 _val = (val);			\
         switch (_count % 4) {			\
         case 0: do {    *_p++ = _val;		\
@@ -337,7 +347,7 @@
 		"movsb\n"						  \
 		"2:"							  \
 		: "=&c" (u0), "=&D" (u1), "=&S" (u2)			  \
-		: "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
+		: "0" (SDL_static_cast(unsigned, len)/4), "q" (len), "1" (dst),"2" (src) \
 		: "memory" );						  \
 } while(0)
 #endif
@@ -363,7 +373,7 @@
 		"cld\n\t"					\
 		"rep ; movsl"					\
 		: "=&c" (ecx), "=&D" (edi), "=&S" (esi)		\
-		: "0" ((unsigned)(len)), "1" (dst), "2" (src)	\
+		: "0" (SDL_static_cast(unsigned, len)), "1" (dst), "2" (src)	\
 		: "memory" );					\
 } while(0)
 #endif
@@ -375,8 +385,8 @@
 #define SDL_revcpy(dst, src, len)			\
 do {							\
 	int u0, u1, u2;					\
-	char *dstp = (char *)(dst);			\
-	char *srcp = (char *)(src);			\
+	char *dstp = SDL_static_cast(char *, dst);			\
+	char *srcp = SDL_static_cast(char *, src);			\
 	int n = (len);					\
 	if ( n >= 4 ) {					\
 	__asm__ __volatile__ (				\