From: Florian Krohm Date: Wed, 22 Oct 2014 12:53:16 +0000 (+0000) Subject: Enable -Wcast-qual when compiling the valgrind source. X-Git-Tag: svn/VALGRIND_3_11_0~896 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=070ad41c3bd5c22d695904e19db85394b9d03518;p=thirdparty%2Fvalgrind.git Enable -Wcast-qual when compiling the valgrind source. Testcases are not compiled with -Wcast-qual. Introduce CONST_CAST macro to work around in the few spots where a cast that drops type qualifiers is needed. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14652 --- diff --git a/Makefile.tool-tests.am b/Makefile.tool-tests.am index 6521afb4ac..4ca6f7cc53 100644 --- a/Makefile.tool-tests.am +++ b/Makefile.tool-tests.am @@ -39,6 +39,10 @@ CFLAGS += -Wno-string-plus-int # drd/tests/annotate_ignore_rw.c CXXFLAGS += -Wno-unused-private-field # drd/tests/tsan_unittest.cpp endif +# Compile testcases without -Wcast-qual +CFLAGS += -Wno-cast-qual +CXXFLAGS += -Wno-cast-qual + check-local: build-noinst_DSYMS clean-local: clean-noinst_DSYMS diff --git a/configure.ac b/configure.ac index 710accf3b1..4b3edd0dd4 100644 --- a/configure.ac +++ b/configure.ac @@ -1735,6 +1735,30 @@ AC_MSG_RESULT([-Wextra]) ]) CFLAGS=$safe_CFLAGS +# does this compiler support -Wcast-qual ? +AC_MSG_CHECKING([if gcc accepts -Wcast-qual]) + +safe_CFLAGS=$CFLAGS +CFLAGS="-Wcast-qual" + +AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[ + return 0; +]])], [ +cast_qual=yes +AC_MSG_RESULT([yes]) +], [ +cast_qual=no +AC_MSG_RESULT([no]) +]) +CFLAGS=$safe_CFLAGS + +AM_CONDITIONAL(HAS_CAST_QUAL_WARNING, test x$cast_qual = xyes) + +if test x$cast_qual = xyes; then + CFLAGS="$CFLAGS -Wcast-qual" + CXXFLAGS="$CXXFLAGS -Wcast-qual" +fi + # does this compiler support -fno-stack-protector ? AC_MSG_CHECKING([if gcc accepts -fno-stack-protector]) diff --git a/coregrind/m_deduppoolalloc.c b/coregrind/m_deduppoolalloc.c index dd69c7ffd5..1c34ab4377 100644 --- a/coregrind/m_deduppoolalloc.c +++ b/coregrind/m_deduppoolalloc.c @@ -252,7 +252,7 @@ void* VG_(allocEltDedupPA) (DedupPoolAlloc *ddpa, SizeT eltSzB, const void *elt) ht_elt.key = VG_(adler32) (ht_elt.key, elt, eltSzB); ht_elt.eltSzB = eltSzB; - ht_elt.elt = (void *)elt; + ht_elt.elt = CONST_CAST(void *,elt); ht_ins = VG_(HT_gen_lookup) (ddpa->ht_elements, &ht_elt, cmp_pool_elt); if (ht_ins) diff --git a/coregrind/m_libcbase.c b/coregrind/m_libcbase.c index b6f293dbb9..8dd452e7f1 100644 --- a/coregrind/m_libcbase.c +++ b/coregrind/m_libcbase.c @@ -111,7 +111,8 @@ Long VG_(strtoll10) ( const HChar* str, HChar** endptr ) if (!converted) str = str0; // If nothing converted, endptr points to if (neg) n = -n; // the start of the string. - if (endptr) *endptr = (HChar *)str; // Record first failing character. + if (endptr) + *endptr = CONST_CAST(HChar *,str); // Record first failing character. return n; } @@ -136,7 +137,8 @@ ULong VG_(strtoull10) ( const HChar* str, HChar** endptr ) if (!converted) str = str0; // If nothing converted, endptr points to // the start of the string. - if (endptr) *endptr = (HChar *)str; // Record first failing character. + if (endptr) + *endptr = CONST_CAST(HChar *,str); // Record first failing character. return n; } @@ -169,7 +171,8 @@ Long VG_(strtoll16) ( const HChar* str, HChar** endptr ) if (!converted) str = str0; // If nothing converted, endptr points to if (neg) n = -n; // the start of the string. - if (endptr) *endptr = (HChar *)str; // Record first failing character. + if (endptr) + *endptr = CONST_CAST(HChar *,str); // Record first failing character. return n; } @@ -202,7 +205,8 @@ ULong VG_(strtoull16) ( const HChar* str, HChar** endptr ) if (!converted) str = str0; // If nothing converted, endptr points to // the start of the string. - if (endptr) *endptr = (HChar *)str; // Record first failing character. + if (endptr) + *endptr = CONST_CAST(HChar *,str); // Record first failing character. return n; } @@ -235,7 +239,8 @@ double VG_(strtod) ( const HChar* str, HChar** endptr ) n += frac; if (neg) n = -n; - if (endptr) *endptr = (HChar *)str; // Record first failing character. + if (endptr) + *endptr = CONST_CAST(HChar *,str); // Record first failing character. return n; } @@ -284,7 +289,7 @@ HChar* VG_(strpbrk) ( const HChar* s, const HChar* accpt ) a = accpt; while (*a) if (*a++ == *s) - return (HChar *)s; + return CONST_CAST(HChar *,s); s++; } return NULL; @@ -400,7 +405,7 @@ HChar* VG_(strstr) ( const HChar* haystack, const HChar* needle ) if (haystack[0] == 0) return NULL; if (VG_(strncmp)(haystack, needle, n) == 0) - return (HChar*)haystack; + return CONST_CAST(HChar *,haystack); haystack++; } } @@ -415,7 +420,7 @@ HChar* VG_(strcasestr) ( const HChar* haystack, const HChar* needle ) if (haystack[0] == 0) return NULL; if (VG_(strncasecmp)(haystack, needle, n) == 0) - return (HChar*)haystack; + return CONST_CAST(HChar *,haystack); haystack++; } } @@ -423,7 +428,7 @@ HChar* VG_(strcasestr) ( const HChar* haystack, const HChar* needle ) HChar* VG_(strchr) ( const HChar* s, HChar c ) { while (True) { - if (*s == c) return (HChar *)s; + if (*s == c) return CONST_CAST(HChar *,s); if (*s == 0) return NULL; s++; } @@ -433,7 +438,7 @@ HChar* VG_(strrchr) ( const HChar* s, HChar c ) { Int n = VG_(strlen)(s); while (--n > 0) { - if (s[n] == c) return (HChar *)s + n; + if (s[n] == c) return CONST_CAST(HChar *,s) + n; } return NULL; } diff --git a/coregrind/m_libcproc.c b/coregrind/m_libcproc.c index cc285c437a..a371ce310e 100644 --- a/coregrind/m_libcproc.c +++ b/coregrind/m_libcproc.c @@ -350,7 +350,7 @@ Int VG_(system) ( const HChar* cmd ) if (pid == 0) { /* child */ const HChar* argv[4] = { "/bin/sh", "-c", cmd, 0 }; - VG_(execv)(argv[0], (HChar **)argv); + VG_(execv)(argv[0], CONST_CAST(HChar **,argv)); /* If we're still alive here, execv failed. */ VG_(exit)(1); diff --git a/coregrind/m_redir.c b/coregrind/m_redir.c index 78bed76f4f..24403300bf 100644 --- a/coregrind/m_redir.c +++ b/coregrind/m_redir.c @@ -1181,8 +1181,8 @@ static void add_hardwired_spec (const HChar* sopatt, const HChar* fnpatt, vg_assert(topSpecs->next == NULL); vg_assert(topSpecs->seginfo == NULL); /* FIXED PARTS */ - spec->from_sopatt = (HChar *)sopatt; - spec->from_fnpatt = (HChar *)fnpatt; + spec->from_sopatt = CONST_CAST(HChar *,sopatt); + spec->from_fnpatt = CONST_CAST(HChar *,fnpatt); spec->to_addr = to_addr; spec->isWrap = False; spec->mandatory = mandatory; diff --git a/exp-sgcheck/h_intercepts.c b/exp-sgcheck/h_intercepts.c index 1c8f1b12a7..64bc7f7da2 100644 --- a/exp-sgcheck/h_intercepts.c +++ b/exp-sgcheck/h_intercepts.c @@ -54,7 +54,7 @@ const HChar* last = NULL; \ while (True) { \ if (*p == ch) last = p; \ - if (*p == 0) return (HChar *)last; \ + if (*p == 0) return CONST_CAST(HChar *,last); \ p++; \ } \ } @@ -78,7 +78,7 @@ STRRCHR(VG_Z_DYLD, rindex) HChar ch = (HChar)c ; \ const HChar* p = s; \ while (True) { \ - if (*p == ch) return (HChar *)p; \ + if (*p == ch) return CONST_CAST(HChar *,p); \ if (*p == 0) return NULL; \ p++; \ } \ @@ -215,9 +215,9 @@ STRCMP(VG_Z_LD64_SO_1, strcmp) { \ SizeT i; \ UChar c0 = (UChar)c; \ - const UChar* p = (const UChar*)s; \ + const UChar* p = s; \ for (i = 0; i < n; i++) \ - if (p[i] == c0) return (void*)(&p[i]); \ + if (p[i] == c0) return CONST_CAST(void *,&p[i]); \ return NULL; \ } @@ -331,7 +331,7 @@ STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) UChar c = (UChar)c_in; \ const UChar* char_ptr = s; \ while (1) { \ - if (*char_ptr == c) return (void *)char_ptr; \ + if (*char_ptr == c) return CONST_CAST(void *,char_ptr); \ char_ptr++; \ } \ } @@ -356,7 +356,7 @@ GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) while (n[nlen]) nlen++; \ \ /* if n is the empty string, match immediately. */ \ - if (nlen == 0) return (HChar *)h; \ + if (nlen == 0) return CONST_CAST(HChar *,h); \ \ /* assert(nlen >= 1); */ \ HChar n0 = n[0]; \ @@ -373,7 +373,7 @@ GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) } \ /* assert(i >= 0 && i <= nlen); */ \ if (i == nlen) \ - return (HChar *)h; \ + return CONST_CAST(HChar *,h); \ \ h++; \ } \ @@ -408,7 +408,7 @@ STRSTR(VG_Z_LIBC_SONAME, strstr) break; \ for (i = 0; i < nacc; i++) { \ if (sc == accept[i]) \ - return (HChar *)s; \ + return CONST_CAST(HChar *,s); \ } \ s++; \ } \ diff --git a/include/pub_tool_basics.h b/include/pub_tool_basics.h index 26a22e3196..acc766b161 100644 --- a/include/pub_tool_basics.h +++ b/include/pub_tool_basics.h @@ -339,6 +339,16 @@ static inline Bool sr_EQ ( SysRes sr1, SysRes sr2 ) { #define PRINTF_CHECK(x, y) #endif +// Macro to "cast" away constness (from type const T to type T) without +// GCC complaining about it. This macro should be used RARELY. +// x is expected to have type const T +#define CONST_CAST(T,x) \ + ({ \ + union { \ + const T in; \ + T out; \ + } var = { .in = x }; var.out; \ + }) #endif /* __PUB_TOOL_BASICS_H */ diff --git a/shared/vg_replace_strmem.c b/shared/vg_replace_strmem.c index 78923ebe28..4ef6286f5b 100644 --- a/shared/vg_replace_strmem.c +++ b/shared/vg_replace_strmem.c @@ -179,7 +179,7 @@ static inline void my_exit ( int x ) const HChar* last = NULL; \ while (True) { \ if (*p == ch) last = p; \ - if (*p == 0) return (HChar *)last; \ + if (*p == 0) return CONST_CAST(HChar *,last); \ p++; \ } \ } @@ -220,7 +220,7 @@ static inline void my_exit ( int x ) HChar ch = (HChar)c ; \ const HChar* p = s; \ while (True) { \ - if (*p == ch) return (HChar *)p; \ + if (*p == ch) return CONST_CAST(HChar *,p); \ if (*p == 0) return NULL; \ p++; \ } \ @@ -785,9 +785,9 @@ static inline void my_exit ( int x ) { \ SizeT i; \ UChar c0 = (UChar)c; \ - UChar* p = (UChar*)s; \ + const UChar* p = s; \ for (i = 0; i < n; i++) \ - if (p[i] == c0) return (void*)(&p[i]); \ + if (p[i] == c0) return CONST_CAST(void *,&p[i]); \ return NULL; \ } @@ -814,9 +814,9 @@ static inline void my_exit ( int x ) { \ SizeT i; \ UChar c0 = (UChar)c; \ - UChar* p = (UChar*)s; \ + const UChar* p = s; \ for (i = 0; i < n; i++) \ - if (p[n-1-i] == c0) return (void*)(&p[n-1-i]); \ + if (p[n-1-i] == c0) return CONST_CAST(void *,&p[n-1-i]); \ return NULL; \ } @@ -1208,11 +1208,11 @@ static inline void my_exit ( int x ) char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ (const char* s, int c_in) \ { \ - UChar c = (UChar) c_in; \ - UChar* char_ptr = (UChar *)s; \ + HChar c = (HChar) c_in; \ + const HChar* char_ptr = s; \ while (1) { \ - if (*char_ptr == 0) return (HChar *)char_ptr; \ - if (*char_ptr == c) return (HChar *)char_ptr; \ + if (*char_ptr == 0) return CONST_CAST(HChar *,char_ptr); \ + if (*char_ptr == c) return CONST_CAST(HChar *,char_ptr); \ char_ptr++; \ } \ } @@ -1237,7 +1237,7 @@ static inline void my_exit ( int x ) UChar c = (UChar) c_in; \ const UChar* char_ptr = s; \ while (1) { \ - if (*char_ptr == c) return (void *)char_ptr; \ + if (*char_ptr == c) return CONST_CAST(void *,char_ptr); \ char_ptr++; \ } \ } @@ -1429,7 +1429,7 @@ static inline void my_exit ( int x ) while (n[nlen]) nlen++; \ \ /* if n is the empty string, match immediately. */ \ - if (nlen == 0) return (HChar *)h; \ + if (nlen == 0) return CONST_CAST(HChar *,h); \ \ /* assert(nlen >= 1); */ \ HChar n0 = n[0]; \ @@ -1446,7 +1446,7 @@ static inline void my_exit ( int x ) } \ /* assert(i >= 0 && i <= nlen); */ \ if (i == nlen) \ - return (HChar *)h; \ + return CONST_CAST(HChar *,h); \ \ h++; \ } \ @@ -1488,7 +1488,7 @@ static inline void my_exit ( int x ) break; \ for (i = 0; i < nacc; i++) { \ if (sc == accept[i]) \ - return (HChar *)s; \ + return CONST_CAST(HChar *,s); \ } \ s++; \ } \ @@ -1608,7 +1608,7 @@ static inline void my_exit ( int x ) while (n[nlen]) nlen++; \ \ /* if n is the empty string, match immediately. */ \ - if (nlen == 0) return (HChar *)h; \ + if (nlen == 0) return CONST_CAST(HChar *,h); \ \ /* assert(nlen >= 1); */ \ UChar n0 = tolower(n[0]); \ @@ -1625,7 +1625,7 @@ static inline void my_exit ( int x ) } \ /* assert(i >= 0 && i <= nlen); */ \ if (i == nlen) \ - return (HChar *)h; \ + return CONST_CAST(HChar *,h); \ \ h++; \ } \ @@ -1742,9 +1742,9 @@ static inline void my_exit ( int x ) Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ); \ Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ) \ { \ - Int* p = (Int*)s; \ + const Int* p = s; \ while (True) { \ - if (*p == c) return p; \ + if (*p == c) return CONST_CAST(Int *,p); \ if (*p == 0) return NULL; \ p++; \ } \ @@ -1763,11 +1763,11 @@ static inline void my_exit ( int x ) Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ); \ Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ) \ { \ - Int* p = (Int*) s; \ - Int* last = NULL; \ + const Int* p = s; \ + const Int* last = NULL; \ while (True) { \ if (*p == c) last = p; \ - if (*p == 0) return last; \ + if (*p == 0) return CONST_CAST(Int *,last); \ p++; \ } \ }