From c168eab92cc42d3c995cc0a1188e1554ebbcd63a Mon Sep 17 00:00:00 2001
From: Ulrich Drepper <drepper@gmail.com>
Date: Thu, 11 Dec 2014 04:31:52 +0000
Subject: [PATCH] Minor interface cleanups of libgccjit

        Minor interface cleanups of libgccjit
        * jit/jit-playback.c (convert_to_dso): Use auto_vec instead
        of automatic array to build up command line.
        * jit/jit-recording.c (recording::context::set_str_option):
        Make copy of the string.
        (recording::context::~context): Free string options.
        * jit/jit-recording.h (recording::context): Adjust type
        of m_str_options member.
        * jit/libgccjit.h: Adjust comment about
        gcc_jit_context_set_str_option parameter begin used after
        the call.
        Update comment now that all interfaces are copy strings
        if necessary.
        * jit/libgccjit++.h (gccjit::context): Add set_str_option
        member function.

From-SVN: r218617
---
 gcc/ChangeLog           | 18 ++++++++++++++++++
 gcc/jit/jit-playback.c  | 42 +++++++++++++++++++++--------------------
 gcc/jit/jit-recording.c | 26 ++++++++++++++-----------
 gcc/jit/jit-recording.h |  3 +--
 gcc/jit/libgccjit++.h   | 11 +++++++++++
 gcc/jit/libgccjit.h     | 24 +++++++++++------------
 6 files changed, 79 insertions(+), 45 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index dfff862a8220..2d0f06e3ff24 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,21 @@
+2014-12-10  Ulrich Drepper  <drepper@gmail.com>
+
+	Minor interface cleanups of libgccjit
+	* jit/jit-playback.c (convert_to_dso): Use auto_vec instead
+	of automatic array to build up command line.
+	* jit/jit-recording.c (recording::context::set_str_option):
+	Make copy of the string.
+	(recording::context::~context): Free string options.
+	* jit/jit-recording.h (recording::context): Adjust type
+	of m_str_options member.
+	* jit/libgccjit.h: Adjust comment about
+	gcc_jit_context_set_str_option parameter begin used after
+	the call.
+	Update comment now that all interfaces are copy strings
+	if necessary.
+	* jit/libgccjit++.h (gccjit::context): Add set_str_option
+	member function.
+
 2014-12-10  Aldy Hernandez  <aldyh@redhat.com>
 
 	* gdbhooks.py (class DWDieRefPrinter): New class.
diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
index 84989005b446..57ff50efa65e 100644
--- a/gcc/jit/jit-playback.c
+++ b/gcc/jit/jit-playback.c
@@ -454,8 +454,8 @@ new_function (location *loc,
 playback::lvalue *
 playback::context::
 new_global (location *loc,
-            type *type,
-            const char *name)
+	    type *type,
+	    const char *name)
 {
   gcc_assert (type);
   gcc_assert (name);
@@ -943,7 +943,7 @@ new_array_access (location *loc,
       tree t_result = build4 (ARRAY_REF, t_type_star_ptr, t_ptr, t_index,
 			      NULL_TREE, NULL_TREE);
       if (loc)
-        set_tree_location (t_result, loc);
+	set_tree_location (t_result, loc);
       return new lvalue (this, t_result);
     }
   else
@@ -958,12 +958,12 @@ new_array_access (location *loc,
 
       tree t_indirection = build1 (INDIRECT_REF, t_type_star_ptr, t_address);
       if (loc)
-        {
-          set_tree_location (t_sizeof, loc);
-          set_tree_location (t_offset, loc);
-          set_tree_location (t_address, loc);
-          set_tree_location (t_indirection, loc);
-        }
+	{
+	  set_tree_location (t_sizeof, loc);
+	  set_tree_location (t_offset, loc);
+	  set_tree_location (t_address, loc);
+	  set_tree_location (t_indirection, loc);
+	}
 
       return new lvalue (this, t_indirection);
     }
@@ -1331,8 +1331,8 @@ add_assignment (location *loc,
   if (TREE_TYPE (t_rvalue) != TREE_TYPE (t_lvalue))
     {
       t_rvalue = build1 (CONVERT_EXPR,
-		         TREE_TYPE (t_lvalue),
-		         t_rvalue);
+			 TREE_TYPE (t_lvalue),
+			 t_rvalue);
       if (loc)
 	set_tree_location (t_rvalue, loc);
     }
@@ -1818,18 +1818,19 @@ convert_to_dso (const char *ctxt_progname)
      TV_ASSEMBLE.  */
   auto_timevar assemble_timevar (TV_ASSEMBLE);
   const char *errmsg;
-  const char *argv[7];
+  auto_vec <const char *> argvec;
+#define ADD_ARG(arg) argvec.safe_push (arg)
   int exit_status = 0;
   int err = 0;
   const char *gcc_driver_name = GCC_DRIVER_NAME;
 
-  argv[0] = gcc_driver_name;
-  argv[1] = "-shared";
+  ADD_ARG (gcc_driver_name);
+  ADD_ARG ("-shared");
   /* The input: assembler.  */
-  argv[2] = m_tempdir->get_path_s_file ();
+  ADD_ARG (m_tempdir->get_path_s_file ());
   /* The output: shared library.  */
-  argv[3] = "-o";
-  argv[4] = m_tempdir->get_path_so_file ();
+  ADD_ARG ("-o");
+  ADD_ARG (m_tempdir->get_path_so_file ());
 
   /* Don't use the linker plugin.
      If running with just a "make" and not a "make install", then we'd
@@ -1838,17 +1839,17 @@ convert_to_dso (const char *ctxt_progname)
      libto_plugin is a .la at build time, with it becoming installed with
      ".so" suffix: i.e. it doesn't exist with a .so suffix until install
      time.  */
-  argv[5] = "-fno-use-linker-plugin";
+  ADD_ARG ("-fno-use-linker-plugin");
 
   /* pex argv arrays are NULL-terminated.  */
-  argv[6] = NULL;
+  ADD_ARG (NULL);
 
   /* pex_one's error-handling requires pname to be non-NULL.  */
   gcc_assert (ctxt_progname);
 
   errmsg = pex_one (PEX_SEARCH, /* int flags, */
 		    gcc_driver_name,
-		    const_cast<char * const *> (argv),
+		    const_cast <char *const *> (argvec.address ()),
 		    ctxt_progname, /* const char *pname */
 		    NULL, /* const char *outname */
 		    NULL, /* const char *errname */
@@ -1875,6 +1876,7 @@ convert_to_dso (const char *ctxt_progname)
 		 getenv ("PATH"));
       return;
     }
+#undef ADD_ARG
 }
 
 /* Dynamically-link the built DSO file into this process, using dlopen.
diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c
index bea2360ae5d3..9d6d26a92a1b 100644
--- a/gcc/jit/jit-recording.c
+++ b/gcc/jit/jit-recording.c
@@ -182,16 +182,16 @@ recording::context::context (context *parent_ctxt)
   if (parent_ctxt)
     {
       /* Inherit options from parent.
-         Note that the first memcpy means copying pointers to strings.  */
+	 Note that the first memcpy means copying pointers to strings.  */
       memcpy (m_str_options,
-              parent_ctxt->m_str_options,
-              sizeof (m_str_options));
+	      parent_ctxt->m_str_options,
+	      sizeof (m_str_options));
       memcpy (m_int_options,
-              parent_ctxt->m_int_options,
-              sizeof (m_int_options));
+	      parent_ctxt->m_int_options,
+	      sizeof (m_int_options));
       memcpy (m_bool_options,
-              parent_ctxt->m_bool_options,
-              sizeof (m_bool_options));
+	      parent_ctxt->m_bool_options,
+	      sizeof (m_bool_options));
     }
   else
     {
@@ -215,6 +215,9 @@ recording::context::~context ()
       delete m;
     }
 
+  for (i = 0; i < GCC_JIT_NUM_STR_OPTIONS; ++i)
+    free (m_str_options[i]);
+
   if (m_builtins_manager)
     delete m_builtins_manager;
 
@@ -827,7 +830,8 @@ recording::context::set_str_option (enum gcc_jit_str_option opt,
 		 "unrecognized (enum gcc_jit_str_option) value: %i", opt);
       return;
     }
-  m_str_options[opt] = value;
+  free (m_str_options[opt]);
+  m_str_options[opt] = xstrdup (value);
 }
 
 /* Set the given integer option for this context, or add an error if
@@ -1950,10 +1954,10 @@ recording::fields::replay_into (replayer *)
    declaration of this form:
 
       struct/union NAME {
-        TYPE_1 NAME_1;
-        TYPE_2 NAME_2;
+	TYPE_1 NAME_1;
+	TYPE_2 NAME_2;
 	....
-        TYPE_N NAME_N;
+	TYPE_N NAME_N;
       };
 
     to the dump.  */
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index d2f5ffb58ee6..b3e52dc0f4f4 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -260,7 +260,7 @@ private:
   char *m_first_error_str;
   bool m_owns_first_error_str;
 
-  const char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
+  char *m_str_options[GCC_JIT_NUM_STR_OPTIONS];
   int m_int_options[GCC_JIT_NUM_INT_OPTIONS];
   bool m_bool_options[GCC_JIT_NUM_BOOL_OPTIONS];
 
@@ -1601,4 +1601,3 @@ private:
 } // namespace gcc
 
 #endif /* JIT_RECORDING_H */
-
diff --git a/gcc/jit/libgccjit++.h b/gcc/jit/libgccjit++.h
index 7f6718bf7797..baed99ef1d1b 100644
--- a/gcc/jit/libgccjit++.h
+++ b/gcc/jit/libgccjit++.h
@@ -102,6 +102,9 @@ namespace gccjit
     void dump_to_file (const std::string &path,
 		       bool update_locations);
 
+    void set_str_option (enum gcc_jit_str_option opt,
+			 const char *value);
+
     void set_int_option (enum gcc_jit_int_option opt,
 			 int value);
 
@@ -537,6 +540,14 @@ context::dump_to_file (const std::string &path,
 				update_locations);
 }
 
+inline void
+context::set_str_option (enum gcc_jit_str_option opt,
+			 const char *value)
+{
+  gcc_jit_context_set_str_option (m_inner_ctxt, opt, value);
+
+}
+
 inline void
 context::set_int_option (enum gcc_jit_int_option opt,
 			 int value)
diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h
index 71628e031219..7fc4928fcfc9 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -50,15 +50,15 @@ typedef struct gcc_jit_result gcc_jit_result;
    The class hierarchy looks like this:
 
      +- gcc_jit_object
-         +- gcc_jit_location
-         +- gcc_jit_type
+	 +- gcc_jit_location
+	 +- gcc_jit_type
 	    +- gcc_jit_struct
-         +- gcc_jit_field
-         +- gcc_jit_function
-         +- gcc_jit_block
-         +- gcc_jit_rvalue
-             +- gcc_jit_lvalue
-                 +- gcc_jit_param
+	 +- gcc_jit_field
+	 +- gcc_jit_function
+	 +- gcc_jit_block
+	 +- gcc_jit_rvalue
+	     +- gcc_jit_lvalue
+		 +- gcc_jit_param
 */
 typedef struct gcc_jit_object gcc_jit_object;
 
@@ -213,8 +213,9 @@ enum gcc_jit_bool_option
 
 /* Set a string option on the given context.
 
-   The context directly stores the (const char *), so the passed string
-   must outlive the context.  */
+   The context takes a copy of the string, so the
+   (const char *) buffer is not needed anymore after the call
+   returns.  */
 extern void
 gcc_jit_context_set_str_option (gcc_jit_context *ctxt,
 				enum gcc_jit_str_option opt,
@@ -288,8 +289,7 @@ gcc_jit_result_release (gcc_jit_result *result);
  released their context.
 
  All (const char *) string arguments passed to these functions are
- copied, so you don't need to keep them around.  Note that this *isn't*
- the case for other parts of the API.
+ copied, so you don't need to keep them around.
 
  You create code by adding a sequence of statements to blocks.
 **********************************************************************/