]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
vla: enable sizeof operator to work with variable length arrays
authorSanimir Agovic <sanimir.agovic@intel.com>
Wed, 9 Oct 2013 14:28:22 +0000 (15:28 +0100)
committerJoel Brobecker <brobecker@adacore.com>
Mon, 14 Apr 2014 16:11:48 +0000 (09:11 -0700)
In C99 the sizeof operator computes the size of a variable length array
at runtime (6.5.3.4 The sizeof operator). This patch reflects the semantic
change in the debugger.

We now are able to get the size of a vla:

1| void foo (size_t n) {
2|   int vla[n];
3| }

(gdb) p sizeof(vla)

yields N * sizeof(int).

gdb/ChangeLog:

* eval.c (evaluate_subexp_for_sizeof) <OP_VAR_VALUE>: If the type
passed to sizeof is dynamic evaluate the argument to compute the length.

gdb/ChangeLog
gdb/eval.c

index cf9a35da3129dd1364fc4b8d49a62be4dbd02986..17556097acea6fd05c31d5e34e6f003d2be5f7ef 100644 (file)
@@ -1,3 +1,8 @@
+2014-04-14  Sanimir Agovic  <sanimir.agovic@intel.com>
+
+       * eval.c (evaluate_subexp_for_sizeof) <OP_VAR_VALUE>: If the type
+       passed to sizeof is dynamic evaluate the argument to compute the length.
+
 2014-04-14  Sanimir Agovic  <sanimir.agovic@intel.com>
            Joel Brobecker  <brobecker@adacore.com>
 
index c1e47e03685e43871b23678f096f4230e5bd0367..85523cd2fd3717fd1c394bc22310cc0e54480da9 100644 (file)
@@ -3040,8 +3040,14 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
       break;
 
     case OP_VAR_VALUE:
-      (*pos) += 4;
       type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
+      if (is_dynamic_type (type))
+       {
+         val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
+         type = value_type (val);
+       }
+      else
+       (*pos) += 4;
       break;
 
     default: