]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Enable -Wcast-qual when compiling the valgrind source.
authorFlorian Krohm <florian@eich-krohm.de>
Wed, 22 Oct 2014 12:53:16 +0000 (12:53 +0000)
committerFlorian Krohm <florian@eich-krohm.de>
Wed, 22 Oct 2014 12:53:16 +0000 (12:53 +0000)
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

Makefile.tool-tests.am
configure.ac
coregrind/m_deduppoolalloc.c
coregrind/m_libcbase.c
coregrind/m_libcproc.c
coregrind/m_redir.c
exp-sgcheck/h_intercepts.c
include/pub_tool_basics.h
shared/vg_replace_strmem.c

index 6521afb4ac78ae0b73cec874b79c08fd8bf4ffb4..4ca6f7cc53fb59e3339b956b6839e8079e880543 100644 (file)
@@ -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
index 710accf3b1f77ff0f56cf5acae54c8440a217c64..4b3edd0dd4ff030ebb36af00550a61c7df9eecf9 100644 (file)
@@ -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])
index dd69c7ffd5d535f2f97c65e7adedc95b13bba1f9..1c34ab4377cb5ca7cb178329cdee8bdb84964d14 100644 (file)
@@ -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)
index b6f293dbb9ef998e441068d098646152700967d2..8dd452e7f1793b76ed01666c3f69853d958bff9c 100644 (file)
@@ -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;
 }
index cc285c437a841f408325567791e89baef6dfd9d9..a371ce310ee64a20ac84f38f23cf17f76d683138 100644 (file)
@@ -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);
index 78bed76f4fba361ea09c4a96acb6a9851c3730e2..24403300bfc23c892245686883970f6e06d5e5ea 100644 (file)
@@ -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;
index 1c8f1b12a709e2ff959fc36a19c87577ff0f0ee4..64bc7f7da2f2e3cd3b76da9bb90bd910f29b6865 100644 (file)
@@ -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++; \
       } \
index 26a22e3196982245d3208be3e060a12b5c9654d8..acc766b16170621cb74dc68e32ab5e499da24a93 100644 (file)
@@ -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 */
 
index 78923ebe28bf8c34ceb00ef67d2fedcdfa66ce38..4ef6286f5b3319921508eb7b07962a650701d57f 100644 (file)
@@ -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++; \
       } \
    }