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 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/Makefile.tool-tests.am b/Makefile.tool-tests.am
index 6521afb..4ca6f7c 100644
--- a/Makefile.tool-tests.am
+++ b/Makefile.tool-tests.am
@@ -39,6 +39,10 @@
 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 710accf..4b3edd0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1735,6 +1735,30 @@
 ])
 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 dd69c7f..1c34ab4 100644
--- a/coregrind/m_deduppoolalloc.c
+++ b/coregrind/m_deduppoolalloc.c
@@ -252,7 +252,7 @@
    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 b6f293d..8dd452e 100644
--- a/coregrind/m_libcbase.c
+++ b/coregrind/m_libcbase.c
@@ -111,7 +111,8 @@
 
    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 @@
 
    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 @@
 
    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 @@
 
    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 @@
 
    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 @@
       a = accpt;
       while (*a)
          if (*a++ == *s)
-           return (HChar *)s;
+            return CONST_CAST(HChar *,s);
       s++;
    }
    return NULL;
@@ -400,7 +405,7 @@
       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 @@
       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_(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 @@
 {
    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 cc285c4..a371ce3 100644
--- a/coregrind/m_libcproc.c
+++ b/coregrind/m_libcproc.c
@@ -350,7 +350,7 @@
    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 78bed76..2440330 100644
--- a/coregrind/m_redir.c
+++ b/coregrind/m_redir.c
@@ -1181,8 +1181,8 @@
    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 1c8f1b1..64bc7f7 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 @@
       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 @@
    { \
       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 @@
       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 @@
       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 @@
          } \
          /* assert(i >= 0 && i <= nlen); */ \
          if (i == nlen) \
-           return (HChar *)h;                   \
+            return CONST_CAST(HChar *,h);          \
          \
          h++; \
       } \
@@ -408,7 +408,7 @@
             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 26a22e3..acc766b 100644
--- a/include/pub_tool_basics.h
+++ b/include/pub_tool_basics.h
@@ -339,6 +339,16 @@
 #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 78923eb..4ef6286 100644
--- a/shared/vg_replace_strmem.c
+++ b/shared/vg_replace_strmem.c
@@ -179,7 +179,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++; \
       } \
    }
@@ -220,7 +220,7 @@
       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 @@
    { \
       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 @@
    { \
       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 @@
    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 @@
       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 @@
       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 @@
          } \
          /* assert(i >= 0 && i <= nlen); */ \
          if (i == nlen) \
-           return (HChar *)h;                   \
+           return CONST_CAST(HChar *,h);          \
          \
          h++; \
       } \
@@ -1488,7 +1488,7 @@
             break; \
          for (i = 0; i < nacc; i++) { \
             if (sc == accept[i]) \
-              return (HChar *)s; \
+              return CONST_CAST(HChar *,s);       \
          } \
          s++; \
       } \
@@ -1608,7 +1608,7 @@
       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 @@
          } \
          /* assert(i >= 0 && i <= nlen); */ \
          if (i == nlen) \
-           return (HChar *)h;                   \
+           return CONST_CAST(HChar *,h);    \
          \
          h++; \
       } \
@@ -1742,9 +1742,9 @@
    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 @@
    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++; \
       } \
    }