* system.h (CONST_CAST): New.
authorghazi <ghazi@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 10 Aug 2007 17:53:58 +0000 (17:53 +0000)
committerghazi <ghazi@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 10 Aug 2007 17:53:58 +0000 (17:53 +0000)
* c-decl.c (c_make_fname_decl): Use it.
* c-lex.c (cb_ident, lex_string): Likewise.
* c-typeck.c (free_all_tagged_tu_seen_up_to): Likewise.
* gcc.c (set_spec, read_specs, for_each_path, execute, do_spec_1,
give_switch, set_multilib_dir): Likewise.
* gengtype-parse.c (string_seq, typedef_name): Likewise.
* passes.c (execute_one_pass): Likewise.
* prefix.c (update_path): Likewise.
* pretty-print.c (pp_base_destroy_prefix): Likewise.
* tree.c (build_string): Likewise.

cp:
* call.c (name_as_c_string): Use CONST_CAST.
* decl.c (build_decl): Likewise.
* parser.c (cp_parser_string_literal): Likewise.

fortran:
* gfortranspec.c (lang_specific_driver): Use CONST_CAST.
* options.c (gfc_post_options): Likewise.
* parse.c (parse_omp_structured_block): Likewise.
* st.c (gfc_free_statement): Likewise.

java:
* jcf-parse.c (read_class, java_parse_file): Use CONST_CAST.
* jcf.h (JCF_FINISH): Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@127344 138bc75d-0d04-0410-961f-82ee72b054a4

23 files changed:
gcc/ChangeLog
gcc/c-decl.c
gcc/c-lex.c
gcc/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/call.c
gcc/cp/decl.c
gcc/cp/parser.c
gcc/fortran/ChangeLog
gcc/fortran/gfortranspec.c
gcc/fortran/options.c
gcc/fortran/parse.c
gcc/fortran/st.c
gcc/gcc.c
gcc/gengtype-parse.c
gcc/java/ChangeLog
gcc/java/jcf-parse.c
gcc/java/jcf.h
gcc/passes.c
gcc/prefix.c
gcc/pretty-print.c
gcc/system.h
gcc/tree.c

index c120ba6..ab159b5 100644 (file)
@@ -1,3 +1,17 @@
+2007-08-10  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * system.h (CONST_CAST): New.
+       * c-decl.c (c_make_fname_decl): Use it.
+       * c-lex.c (cb_ident, lex_string): Likewise.
+       * c-typeck.c (free_all_tagged_tu_seen_up_to): Likewise.
+       * gcc.c (set_spec, read_specs, for_each_path, execute, do_spec_1,
+       give_switch, set_multilib_dir): Likewise.
+       * gengtype-parse.c (string_seq, typedef_name): Likewise.
+       * passes.c (execute_one_pass): Likewise.
+       * prefix.c (update_path): Likewise.
+       * pretty-print.c (pp_base_destroy_prefix): Likewise.
+       * tree.c (build_string): Likewise.
+
 2007-08-10  Aldy Hernandez  <aldyh@redhat.com>
 
        * tree-flow-inline.h (is_exec_stmt): Remove.
index 0c47d7d..ce93ac5 100644 (file)
@@ -2811,7 +2811,7 @@ c_make_fname_decl (tree id, int type_dep)
   DECL_ARTIFICIAL (decl) = 1;
 
   init = build_string (length + 1, name);
-  free ((char *) name);
+  free (CONST_CAST (name));
   TREE_TYPE (init) = type;
   DECL_INITIAL (decl) = init;
 
index 8dc2a1b..a4e3b0c 100644 (file)
@@ -186,7 +186,7 @@ cb_ident (cpp_reader * ARG_UNUSED (pfile),
       if (cpp_interpret_string (pfile, str, 1, &cstr, false))
        {
          ASM_OUTPUT_IDENT (asm_out_file, (const char *) cstr.text);
-         free ((void *) cstr.text);
+         free (CONST_CAST (cstr.text));
        }
     }
 #endif
@@ -811,7 +811,7 @@ lex_string (const cpp_token *tok, tree *valp, bool objc_string)
       (parse_in, strs, concats + 1, &istr, wide))
     {
       value = build_string (istr.len, (const char *) istr.text);
-      free ((void *) istr.text);
+      free (CONST_CAST (istr.text));
 
       if (c_lex_string_translate == -1)
        {
@@ -832,7 +832,7 @@ lex_string (const cpp_token *tok, tree *valp, bool objc_string)
              *valp = build_string (istr.len, (const char *) istr.text);
              valp = &TREE_CHAIN (*valp);
            }
-         free ((void *) istr.text);
+         free (CONST_CAST (istr.text));
        }
     }
   else
index 6964e75..7e6da0f 100644 (file)
@@ -1026,7 +1026,7 @@ free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
       const struct tagged_tu_seen_cache *const tu1
        = (const struct tagged_tu_seen_cache *) tu;
       tu = tu1->next;
-      free ((void *)tu1);
+      free (CONST_CAST (tu1));
     }
   tagged_tu_seen_base = tu_til;
 }
index 876939b..38f489c 100644 (file)
@@ -1,3 +1,9 @@
+2007-08-10  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * call.c (name_as_c_string): Use CONST_CAST.
+       * decl.c (build_decl): Likewise.
+       * parser.c (cp_parser_string_literal): Likewise.
+
 2007-08-10  Paolo Carlini  <pcarlini@suse.de>
 
        PR c++/17763
index 664d093..79b8cd5 100644 (file)
@@ -5388,7 +5388,7 @@ name_as_c_string (tree name, tree type, bool *free_p)
   if (IDENTIFIER_CTOR_OR_DTOR_P (name))
     {
       pretty_name
-       = (char *) IDENTIFIER_POINTER (constructor_name (type));
+       = (char *) CONST_CAST (IDENTIFIER_POINTER (constructor_name (type)));
       /* For a destructor, add the '~'.  */
       if (name == complete_dtor_identifier
          || name == base_dtor_identifier
@@ -5409,7 +5409,7 @@ name_as_c_string (tree name, tree type, bool *free_p)
       *free_p = true;
     }
   else
-    pretty_name = (char *) IDENTIFIER_POINTER (name);
+    pretty_name = (char *) CONST_CAST (IDENTIFIER_POINTER (name));
 
   return pretty_name;
 }
index 1740ed3..6edb79d 100644 (file)
@@ -3368,7 +3368,7 @@ cp_make_fname_decl (tree id, int type_dep)
   tree decl = build_decl (VAR_DECL, id, type);
 
   if (name)
-    free ((char *) name);
+    free (CONST_CAST (name));
 
   /* As we're using pushdecl_with_scope, we must set the context.  */
   DECL_CONTEXT (decl) = current_function_decl;
index a4ae997..b4ffd2f 100644 (file)
@@ -2922,8 +2922,8 @@ cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
       (parse_in, strs, count, &istr, wide))
     {
-      value = build_string (istr.len, (char *)istr.text);
-      free ((void *)istr.text);
+      value = build_string (istr.len, (const char *)istr.text);
+      free (CONST_CAST (istr.text));
 
       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
       value = fix_string_type (value);
index 77697a7..d9a7580 100644 (file)
@@ -1,3 +1,10 @@
+2007-08-10  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * gfortranspec.c (lang_specific_driver): Use CONST_CAST.
+       * options.c (gfc_post_options): Likewise.
+       * parse.c (parse_omp_structured_block): Likewise.
+       * st.c (gfc_free_statement): Likewise.
+
 2007-08-10  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
 
        PR fortran/32933
index 265f8d6..ca5da29 100644 (file)
@@ -302,7 +302,7 @@ lang_specific_driver (int *in_argc, const char *const **in_argv,
   g77_xargc = argc;
   g77_xargv = argv;
   g77_newargc = 0;
-  g77_newargv = (const char **) argv;
+  g77_newargv = (const char **) CONST_CAST (argv);
 
   /* First pass through arglist.
 
index 99bb719..ad639ee 100644 (file)
@@ -239,7 +239,7 @@ gfc_post_options (const char **pfilename)
     gfc_add_include_path (".", true);
 
   if (canon_source_file != gfc_source_file)
-    gfc_free ((void *) canon_source_file);
+    gfc_free (CONST_CAST (canon_source_file));
 
   /* Decide which form the file will be read in as.  */
 
index 10476f6..4e7e8e1 100644 (file)
@@ -2625,7 +2625,7 @@ parse_omp_structured_block (gfc_statement omp_st, bool workshare_stmts_only)
              && strcmp (cp->ext.omp_name, new_st.ext.omp_name) != 0))
        gfc_error ("Name after !$omp critical and !$omp end critical does "
                   "not match at %C");
-      gfc_free ((char *) new_st.ext.omp_name);
+      gfc_free (CONST_CAST (new_st.ext.omp_name));
       break;
     case EXEC_OMP_END_SINGLE:
       cp->ext.omp_clauses->lists[OMP_LIST_COPYPRIVATE]
index 0717615..8084453 100644 (file)
@@ -173,7 +173,7 @@ gfc_free_statement (gfc_code *p)
       break;
 
     case EXEC_OMP_CRITICAL:
-      gfc_free ((char *) p->ext.omp_name);
+      gfc_free (CONST_CAST (p->ext.omp_name));
       break;
 
     case EXEC_OMP_FLUSH:
index fc85e2b..02c365b 100644 (file)
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1881,7 +1881,7 @@ set_spec (const char *name, const char *spec)
 
   /* Free the old spec.  */
   if (old_spec && sl->alloc_p)
-    free ((void *) old_spec);
+    free (CONST_CAST(old_spec));
 
   sl->alloc_p = 1;
 }
@@ -2186,7 +2186,7 @@ read_specs (const char *filename, int main_p)
 
              set_spec (p2, *(sl->ptr_spec));
              if (sl->alloc_p)
-               free ((void *) *(sl->ptr_spec));
+               free (CONST_CAST (*(sl->ptr_spec)));
 
              *(sl->ptr_spec) = "";
              sl->alloc_p = 0;
@@ -2536,18 +2536,18 @@ for_each_path (const struct path_prefix *paths,
         Don't repeat any we have already seen.  */
       if (multi_dir)
        {
-         free ((char *) multi_dir);
+         free (CONST_CAST (multi_dir));
          multi_dir = NULL;
-         free ((char *) multi_suffix);
+         free (CONST_CAST (multi_suffix));
          multi_suffix = machine_suffix;
-         free ((char *) just_multi_suffix);
+         free (CONST_CAST (just_multi_suffix));
          just_multi_suffix = just_machine_suffix;
        }
       else
        skip_multi_dir = true;
       if (multi_os_dir)
        {
-         free ((char *) multi_os_dir);
+         free (CONST_CAST (multi_os_dir));
          multi_os_dir = NULL;
        }
       else
@@ -2556,12 +2556,12 @@ for_each_path (const struct path_prefix *paths,
 
   if (multi_dir)
     {
-      free ((char *) multi_dir);
-      free ((char *) multi_suffix);
-      free ((char *) just_multi_suffix);
+      free (CONST_CAST (multi_dir));
+      free (CONST_CAST (multi_suffix));
+      free (CONST_CAST (just_multi_suffix));
     }
   if (multi_os_dir)
-    free ((char *) multi_os_dir);
+    free (CONST_CAST (multi_os_dir));
   if (ret != path)
     free (path);
   return ret;
@@ -2968,7 +2968,7 @@ execute (void)
       errmsg = pex_run (pex,
                        ((i + 1 == n_commands ? PEX_LAST : 0)
                         | (string == commands[i].prog ? PEX_SEARCH : 0)),
-                       string, (char * const *) commands[i].argv,
+                       string, (char * const *) CONST_CAST (commands[i].argv),
                        NULL, NULL, &err);
       if (errmsg != NULL)
        {
@@ -2982,7 +2982,7 @@ execute (void)
        }
 
       if (string != commands[i].prog)
-       free ((void *) string);
+       free (CONST_CAST (string));
     }
 
   execution_count++;
@@ -5023,7 +5023,7 @@ do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
                   for (i = 0, j = 0; i < max; i++)
                     if (outfiles[i])
                       {
-                        argv[j] = (char *) outfiles[i];
+                        argv[j] = (char *) CONST_CAST (outfiles[i]);
                         j++;
                       }
                   argv[j] = NULL;
@@ -5979,13 +5979,13 @@ give_switch (int switchnum, int omit_first_word)
              while (length-- && !IS_DIR_SEPARATOR (arg[length]))
                if (arg[length] == '.')
                  {
-                   ((char *)arg)[length] = 0;
+                   ((char *)CONST_CAST(arg))[length] = 0;
                    dot = 1;
                    break;
                  }
              do_spec_1 (arg, 1, NULL);
              if (dot)
-               ((char *)arg)[length] = '.';
+               ((char *)CONST_CAST(arg))[length] = '.';
              do_spec_1 (suffix_subst, 1, NULL);
            }
          else
@@ -7438,7 +7438,7 @@ set_multilib_dir (void)
   if (multilib_dir == NULL && multilib_os_dir != NULL
       && strcmp (multilib_os_dir, ".") == 0)
     {
-      free ((char *) multilib_os_dir);
+      free (CONST_CAST (multilib_os_dir));
       multilib_os_dir = NULL;
     }
   else if (multilib_dir != NULL && multilib_os_dir == NULL)
index 7cd66cf..4d546f3 100644 (file)
@@ -197,9 +197,9 @@ string_seq (void)
 
       l1 = strlen (s1);
       l2 = strlen (s2);
-      buf = XRESIZEVEC (char, s1, l1 + l2 + 1);
+      buf = XRESIZEVEC (char, CONST_CAST(s1), l1 + l2 + 1);
       memcpy (buf + l1, s2, l2 + 1);
-      XDELETE (s2);
+      XDELETE (CONST_CAST (s2));
       s1 = buf;
     }
   return s1;
@@ -221,8 +221,8 @@ typedef_name (void)
       c2 = require (ID);
       require (')');
       r = concat ("VEC_", c1, "_", c2, (char *)0);
-      free ((void *)c1);
-      free ((void *)c2);
+      free (CONST_CAST (c1));
+      free (CONST_CAST (c2));
       return r;
     }
   else
index 102fdc2..93428ad 100644 (file)
@@ -1,3 +1,8 @@
+2007-08-10  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * jcf-parse.c (read_class, java_parse_file): Use CONST_CAST.
+       * jcf.h (JCF_FINISH): Likewise.
+
 2007-07-31  Nick Clifton  <nickc@redhat.com>
 
        * java-gimplify.c: Change copyright header to refer to version 3
index 5660431..c31645f 100644 (file)
@@ -1301,7 +1301,7 @@ read_class (tree name)
       if (path_name == 0)
        return 0;
       else
-       free((char *) path_name);
+       free(CONST_CAST (path_name));
     }
 
   current_jcf = jcf;
@@ -1783,7 +1783,7 @@ java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
       file_list = list;
     }
   else
-    list = (char *) main_input_filename;
+    list = (char *) CONST_CAST (main_input_filename);
 
   while (list)
     {
index b4e718d..af9dadd 100644 (file)
@@ -164,8 +164,8 @@ typedef struct JCF GTY(()) {
 #define JCF_FINISH(JCF) { \
   CPOOL_FINISH(&(JCF)->cpool); \
   if ((JCF)->buffer) free ((JCF)->buffer); \
-  if ((JCF)->filename) free ((char *) (JCF)->filename); \
-  if ((JCF)->classname) free ((char *) (JCF)->classname); \
+  if ((JCF)->filename) free (CONST_CAST ((JCF)->filename)); \
+  if ((JCF)->classname) free (CONST_CAST ((JCF)->classname)); \
   (JCF)->finished = 1; }
   
 #define CPOOL_INIT(CPOOL) \
index 8258ca2..2d26a90 100644 (file)
@@ -1147,7 +1147,7 @@ execute_one_pass (struct tree_opt_pass *pass)
   /* Flush and close dump file.  */
   if (dump_file_name)
     {
-      free ((char *) dump_file_name);
+      free (CONST_CAST (dump_file_name));
       dump_file_name = NULL;
     }
 
index 32348bd..07d8782 100644 (file)
@@ -266,7 +266,7 @@ update_path (const char *path, const char *key)
 
       result = concat (key, &path[len], NULL);
       if (free_key)
-       free ((char *) key);
+       free (CONST_CAST (key));
       result = translate_name (result);
     }
   else
index 1c217b1..2b04dc7 100644 (file)
@@ -633,7 +633,7 @@ pp_base_destroy_prefix (pretty_printer *pp)
 {
   if (pp->prefix != NULL)
     {
-      free ((char *) pp->prefix);
+      free (CONST_CAST (pp->prefix));
       pp->prefix = NULL;
     }
 }
index b96205b..46da381 100644 (file)
@@ -766,4 +766,18 @@ extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
 
 #endif /* GCC >= 3.0 */
 
+/* This macro allows casting away const-ness to pass -Wcast-qual
+   warnings.  DO NOT USE THIS UNLESS YOU REALLY HAVE TO!  It should
+   only be used in certain specific cases.  One valid case is where
+   the C standard definitions or prototypes force you to.  E.g. if you
+   need to free a const object, or if you pass a const string to
+   execv, et al.  Another valid use would be in an allocation function
+   that creates const objects that need to be initialized.  Most other
+   cases should be viewed with extreme caution.  */
+#ifdef __GNUC__
+#define CONST_CAST(X) ((__extension__(union {__typeof(X)_q; void *_v;})(X))._v)
+#else
+#define CONST_CAST(X) ((void*)(X))
+#endif
+
 #endif /* ! GCC_SYSTEM_H */
index af1e1bd..3ca006e 100644 (file)
@@ -1194,8 +1194,8 @@ build_string (int len, const char *str)
   TREE_CONSTANT (s) = 1;
   TREE_INVARIANT (s) = 1;
   TREE_STRING_LENGTH (s) = len;
-  memcpy ((char *) TREE_STRING_POINTER (s), str, len);
-  ((char *) TREE_STRING_POINTER (s))[len] = '\0';
+  memcpy (CONST_CAST (TREE_STRING_POINTER (s)), str, len);
+  ((char *) CONST_CAST (TREE_STRING_POINTER (s)))[len] = '\0';
 
   return s;
 }