unicode.h and -Wcast-qual
Stefan Huehner
stefan at huehner.org
Mon Jun 20 15:26:32 CDT 2005
Hi,
i am currently trying to fix the -Wcast-qual warnings. In
include/wine/unicode.h there a 4 function:
static inline int strncmpW( const WCHAR *str1, const WCHAR *str2, int n)
static inline WCHAR *strchrW( const WCHAR *str, WCHAR ch )
static inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
static inline WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
whose signatures forces us to discard the const of an parameter while
returning it as the function value. Compiling with -Wcast-qual gives
(correctly) this warning:
../../include/wine/unicode.h:218: warning: cast discards qualifiers from
pointer target type
Together these four functions account for a total of 1400 (!) warnings.
In samba_4 sourcecode i discovered the following macros, which are
apparently a hack but silences these warnings:
#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
an crude proof of concept is attached as a patch.
I am not sure how portable this solution is...
Any comments are welcome...
Regards,
Stefan
-------------- next part --------------
Index: configure.ac
===================================================================
RCS file: /home/wine/wine/configure.ac,v
retrieving revision 1.366
diff -u -r1.366 configure.ac
--- configure.ac 20 Jun 2005 15:52:16 -0000 1.366
+++ configure.ac 20 Jun 2005 20:16:14 -0000
@@ -1276,6 +1276,7 @@
AC_C_INLINE
AC_CHECK_TYPES([mode_t, off_t, pid_t, size_t, ssize_t, long long, fsblkcnt_t, fsfilcnt_t])
AC_CHECK_TYPES([sigset_t],,,[#include <signal.h>])
+AC_CHECK_TYPES(intptr_t)
AC_CACHE_CHECK([whether linux/input.h is for real],
wine_cv_linux_input_h,
Index: include/wine/unicode.h
===================================================================
RCS file: /home/wine/wine/include/wine/unicode.h,v
retrieving revision 1.32
diff -u -r1.32 unicode.h
--- include/wine/unicode.h 28 Apr 2005 12:01:37 -0000 1.32
+++ include/wine/unicode.h 20 Jun 2005 20:16:14 -0000
@@ -31,6 +31,17 @@
#define WINE_UNICODE_API DECLSPEC_IMPORT
#endif
+#include <stdint.h>
+#include <config.h>
+
+#ifdef HAVE_INTPTR_T
+#define discard_const(ptr) ((void *)((intptr_t)(ptr)))
+#else
+#define discard_const(ptr) ((void *)(ptr))
+#endif
+#define discard_const_p(type, ptr) ((type *)discard_const(ptr))
+
+
/* code page info common to SBCS and DBCS */
struct cp_info
{
@@ -215,20 +226,20 @@
static inline WCHAR *strchrW( const WCHAR *str, WCHAR ch )
{
- for ( ; *str; str++) if (*str == ch) return (WCHAR *)str;
+ for ( ; *str; str++) if (*str == ch) return discard_const_p(WCHAR, str);
return NULL;
}
static inline WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
{
WCHAR *ret = NULL;
- for ( ; *str; str++) if (*str == ch) ret = (WCHAR *)str;
+ for ( ; *str; str++) if (*str == ch) ret = discard_const_p(WCHAR, str);
return ret;
}
static inline WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
{
- for ( ; *str; str++) if (strchrW( accept, *str )) return (WCHAR *)str;
+ for ( ; *str; str++) if (strchrW( accept, *str )) return discard_const_p(WCHAR, str);
return NULL;
}
@@ -263,7 +274,7 @@
static inline WCHAR *memchrW( const WCHAR *ptr, WCHAR ch, size_t n )
{
const WCHAR *end;
- for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) return (WCHAR *)ptr;
+ for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) return discard_const_p(WCHAR, ptr);
return NULL;
}
@@ -271,7 +282,7 @@
{
const WCHAR *end, *ret = NULL;
for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) ret = ptr;
- return (WCHAR *)ret;
+ return discard_const_p(WCHAR , ret);
}
static inline long int atolW( const WCHAR *str )
More information about the wine-devel
mailing list