diff options
author | Pedro Alves <palves@redhat.com> | 2017-09-04 20:21:16 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-09-04 20:21:16 +0100 |
commit | 3693fdb3c8ec14bd8ecb4ebb39e4384b330a2999 (patch) | |
tree | 595184fd6316fdadcc281f8cd5f7e48d1868b052 /gdb/eval.c | |
parent | e68cb8e00166d2f8a9e23c60f68bec04ef0a90f0 (diff) | |
download | gdb-3693fdb3c8ec14bd8ecb4ebb39e4384b330a2999.zip gdb-3693fdb3c8ec14bd8ecb4ebb39e4384b330a2999.tar.gz gdb-3693fdb3c8ec14bd8ecb4ebb39e4384b330a2999.tar.bz2 |
Make "p S::method() const::static_var" work too
Trying to print a function local static variable of a const-qualified
method still doesn't work after the previous fixes:
(gdb) p 'S::method() const'::static_var
$1 = {i1 = 1, i2 = 2, i3 = 3}
(gdb) p S::method() const::static_var
No symbol "static_var" in specified context.
The reason is that the expression parser/evaluator loses the "const",
and the above unquoted case is just like trying to print a variable of
the non-const overload, if it exists, even. As if the above unquoted
case had been written as:
(gdb) p S::method()::static_var
No symbol "static_var" in specified context.
We can see the problem without static vars in the picture. With:
struct S
{
void method ();
void method () const;
};
Compare:
(gdb) print 'S::method(void) const'
$1 = {void (const S * const)} 0x400606 <S::method() const>
(gdb) print S::method(void) const
$2 = {void (S * const)} 0x4005d8 <S::method()> # wrong method!
That's what we need to fix. If we fix that, the function local static
case starts working.
The grammar production for function/method types is this one:
exp: exp '(' parameter_typelist ')' const_or_volatile
This results in a TYPE_INSTANCE expression evaluator operator. For
the example above, we get something like this ("set debug expression 1"):
...
0 TYPE_INSTANCE 1 TypeInstance: Type @0x560fda958be0 (void)
5 OP_SCOPE Type @0x560fdaa544d8 (S) Field name: `method'
...
While evaluating TYPE_INSTANCE, we end up in
value_struct_elt_for_reference, trying to find the method named
"method" that has the prototype recorded in TYPE_INSTANCE. In this
case, TYPE_INSTANCE says that we're looking for a method that has
"(void)" as parameters (that's what "1 TypeInstance: Type
@0x560fda958be0 (void)" above means. The trouble is that nowhere in
this mechanism do we communicate to value_struct_elt_for_reference
that we're looking for the _const_ overload.
value_struct_elt_for_reference only compared parameters, and the
non-const "method()" overload has matching parameters, so it's
considered the right match...
Conveniently, the "const_or_volatile" production in the grammar
already records "const" and "volatile" info in the type stack. The
type stack is not used in this code path, but we can borrow the
information. The patch converts the info in the type stack to an
"instance flags" enum, and adds that as another element in
TYPE_INSTANCE operators. This type instance flags is then applied to
the temporary type that is passed to value_struct_elt_for_reference
for matching.
The other side of the problem is that methods in the debug info aren't
marked const/volatile, so with that in place, the matching never finds
const/volatile-qualified methods.
The problem is that in the DWARF, there's no indication at all whether
a method is const/volatile qualified... For example (c++filt applied
to the linkage name for convenience):
<2><d3>: Abbrev Number: 6 (DW_TAG_subprogram)
<d4> DW_AT_external : 1
<d4> DW_AT_name : (indirect string, offset: 0x3df): method
<d8> DW_AT_decl_file : 1
<d9> DW_AT_decl_line : 58
<da> DW_AT_linkage_name: (indirect string, offset: 0x5b2): S::method() const
<de> DW_AT_declaration : 1
<de> DW_AT_object_pointer: <0xe6>
<e2> DW_AT_sibling : <0xec>
I see the same with both GCC and Clang. The patch works around this
by extracting the cv qualification from the "const" and "volatile" in
the demangled name. This will need further tweaking for "&" and
"const &" overloads, but we don't support them in the parser yet,
anyway.
The TYPE_CONST changes were necessary otherwise the comparisons in valops.c:
if (TYPE_CONST (intype) != TYPE_FN_FIELD_CONST (f, j))
continue;
would fail, because when both TYPE_CONST() TYPE_FN_FIELD_CONST() were
true, their values were different.
BTW, I'm recording the const/volatile-ness of methods in the
TYPE_FN_FIELD info because #1 - I'm not sure it's kosher to change the
method's type directly (vs having to call make_cv_type to create a new
type), and #2 it's what stabsread.c does:
...
case 'A': /* Normal functions. */
new_sublist->fn_field.is_const = 0;
new_sublist->fn_field.is_volatile = 0;
(*pp)++;
break;
case 'B': /* `const' member functions. */
new_sublist->fn_field.is_const = 1;
new_sublist->fn_field.is_volatile = 0;
...
After all this, this finally all works:
print S::method(void) const
$1 = {void (const S * const)} 0x400606 <S::method() const>
(gdb) p S::method() const::static_var
$2 = {i1 = 1, i2 = 2, i3 = 3}
gdb/ChangeLog:
2017-09-04 Pedro Alves <palves@redhat.com>
* c-exp.y (function_method, function_method_void): Add current
instance flags to TYPE_INSTANCE.
* dwarf2read.c (check_modifier): New.
(compute_delayed_physnames): Assert that only C++ adds delayed
physnames. Mark fn_fields as const/volatile depending on
physname.
* eval.c (make_params): New type_instance_flags parameter. Use
it as the new type's instance flags.
(evaluate_subexp_standard) <TYPE_INSTANCE>: Extract the instance
flags element and pass it to make_params.
* expprint.c (print_subexp_standard) <TYPE_INSTANCE>: Handle
instance flags element.
(dump_subexp_body_standard) <TYPE_INSTANCE>: Likewise.
* gdbtypes.h: Include "enum-flags.h".
(type_instance_flags): New enum-flags type.
(TYPE_CONST, TYPE_VOLATILE, TYPE_RESTRICT, TYPE_ATOMIC)
(TYPE_CODE_SPACE, TYPE_DATA_SPACE): Return boolean.
* parse.c (operator_length_standard) <TYPE_INSTANCE>: Adjust.
(follow_type_instance_flags): New function.
(operator_check_standard) <TYPE_INSTANCE>: Adjust.
* parser-defs.h (follow_type_instance_flags): Declare.
* valops.c (value_struct_elt_for_reference): const/volatile must
match too.
gdb/testsuite/ChangeLog:
2017-09-04 Pedro Alves <palves@redhat.com>
* gdb.base/func-static.c (S::method const, S::method volatile)
(S::method volatile const): New methods.
(c_s, v_s, cv_s): New instances.
(main): Call method() on them.
* gdb.base/func-static.exp (syntax_re, cannot_resolve_re): New variables.
(cannot_resolve): New procedure.
(cxx_scopes_list): Test cv methods. Add print-scope-quote and
print-quote-unquoted columns.
(do_test): Test printing each scope too.
Diffstat (limited to 'gdb/eval.c')
-rw-r--r-- | gdb/eval.c | 40 |
1 files changed, 24 insertions, 16 deletions
@@ -642,18 +642,22 @@ ptrmath_type_p (const struct language_defn *lang, struct type *type) } } -/* Constructs a fake method with the given parameter types. - This function is used by the parser to construct an "expected" - type for method overload resolution. */ +/* Constructs a fake method with the given parameter types. This + function is used by the parser to construct an "expected" type for + method overload resolution. FLAGS is used as instance flags of the + new type, in order to be able to make the new type represent a + const/volatile overload. */ static struct type * -make_params (int num_types, struct type **param_types) +make_params (type_instance_flags flags, + int num_types, struct type **param_types) { struct type *type = XCNEW (struct type); TYPE_MAIN_TYPE (type) = XCNEW (struct main_type); TYPE_LENGTH (type) = 1; TYPE_CODE (type) = TYPE_CODE_METHOD; TYPE_CHAIN (type) = type; + TYPE_INSTANCE_FLAGS (type) = flags; if (num_types > 0) { if (param_types[num_types - 1] == NULL) @@ -2039,18 +2043,22 @@ evaluate_subexp_standard (struct type *expect_type, } case TYPE_INSTANCE: - nargs = longest_to_int (exp->elts[pc + 1].longconst); - arg_types = (struct type **) alloca (nargs * sizeof (struct type *)); - for (ix = 0; ix < nargs; ++ix) - arg_types[ix] = exp->elts[pc + 1 + ix + 1].type; - - expect_type = make_params (nargs, arg_types); - *(pos) += 3 + nargs; - arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside); - xfree (TYPE_FIELDS (expect_type)); - xfree (TYPE_MAIN_TYPE (expect_type)); - xfree (expect_type); - return arg1; + { + type_instance_flags flags + = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst); + nargs = longest_to_int (exp->elts[pc + 2].longconst); + arg_types = (struct type **) alloca (nargs * sizeof (struct type *)); + for (ix = 0; ix < nargs; ++ix) + arg_types[ix] = exp->elts[pc + 2 + ix + 1].type; + + expect_type = make_params (flags, nargs, arg_types); + *(pos) += 4 + nargs; + arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside); + xfree (TYPE_FIELDS (expect_type)); + xfree (TYPE_MAIN_TYPE (expect_type)); + xfree (expect_type); + return arg1; + } case BINOP_CONCAT: arg1 = evaluate_subexp_with_coercion (exp, pos, noside); |