]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: constify parameter of value_copy
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 31 Jan 2022 20:57:58 +0000 (15:57 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Sun, 6 Mar 2022 16:33:23 +0000 (11:33 -0500)
In a following patch, I have a const value I want to copy using a
value_copy.  However, value_copy takes a non-const source value, at the
moment.  Change the paramter to be const,

If the source value is not lazy, we currently call
value_contents_all_raw, which calls allocate_value_contents, to get a
view on the contents.  They both take a non-const value, that's a
problem.  My first attempt at solving it was to add a const version of
value_contents_all_raw, make allocate_value_contents take a const value,
and either:

 - make value::contents mutable
 - make allocate_value_contents cast away the const

The idea being that allocating the value contents buffer does modify the
value at the bit level, but logically that doesn't change its state.

That was getting a bit complicated, so what I ended up doing is make
value_copy not call value_contents_all_raw.  We know at this point that
the value is not lazy, so value::contents must have been allocate
already.

Change-Id: I3741ab362bce14315f712ec24064ccc17e3578d4

gdb/value.c
gdb/value.h

index 080af487c56755882aa0f1df6ae83e6c00cd8912..a57a83207e29ef7a5ffe6005037dd771e66dce84 100644 (file)
@@ -1703,7 +1703,7 @@ value_release_to_mark (const struct value *mark)
    but it's a different block of storage.  */
 
 struct value *
-value_copy (struct value *arg)
+value_copy (const value *arg)
 {
   struct type *encl_type = value_enclosing_type (arg);
   struct value *val;
@@ -1713,7 +1713,7 @@ value_copy (struct value *arg)
   else
     val = allocate_value (encl_type);
   val->type = arg->type;
-  VALUE_LVAL (val) = VALUE_LVAL (arg);
+  VALUE_LVAL (val) = arg->lval;
   val->location = arg->location;
   val->offset = arg->offset;
   val->bitpos = arg->bitpos;
@@ -1727,8 +1727,13 @@ value_copy (struct value *arg)
   val->initialized = arg->initialized;
 
   if (!value_lazy (val))
-    copy (value_contents_all_raw (arg),
-         value_contents_all_raw (val));
+    {
+      gdb_assert (arg->contents != nullptr);
+      ULONGEST length = TYPE_LENGTH (value_enclosing_type (arg));
+      const auto &arg_view
+       = gdb::make_array_view (arg->contents.get (), length);
+      copy (arg_view, value_contents_all_raw (val));
+    }
 
   val->unavailable = arg->unavailable;
   val->optimized_out = arg->optimized_out;
index f7b29022d37cf445cc10807ca79982ff6ec5dfe1..4cd2044ac7cff23054ed0f23c106a878f2c455f0 100644 (file)
@@ -1127,7 +1127,7 @@ extern void preserve_values (struct objfile *);
 
 /* From values.c */
 
-extern struct value *value_copy (struct value *);
+extern struct value *value_copy (const value *);
 
 extern struct value *value_non_lval (struct value *);