Bug 42959 - g++ does not emit DW_AT_default_value
Summary: g++ does not emit DW_AT_default_value
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: debug (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Dodji Seketeli
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-04 20:52 UTC by Tom Tromey
Modified: 2014-03-07 14:39 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-03-09 14:07:28


Attachments
Draft patch (5.77 KB, patch)
2010-03-11 16:17 UTC, Dodji Seketeli
Details | Diff
Draf patch (6.33 KB, patch)
2010-03-15 10:45 UTC, Dodji Seketeli
Details | Diff
Draft patch (6.68 KB, patch)
2010-03-23 09:33 UTC, Dodji Seketeli
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2010-02-04 20:52:37 UTC
Currently, g++ does not emit the DW_AT_default_value attribute for
formal parameters that have default values.
I think it should; this is a prerequisite to letting gdb work with
default arguments.
Comment 1 Dodji Seketeli 2010-03-11 16:17:12 UTC
Created attachment 20085 [details]
Draft patch

Please find below a copy/paste of the introductory comment of the attached patch.

Basically the value of DW_AT_default_value -- as explained by the DWARF4
draft specification -- is either a constant if the default argument of
the function is a constant, or a reference to a DIE if the value of the
default argument is a language construct like a variable.

I believe there are cases however where it's hard (impossible yet?) to
represent the value of DW_AT_default_value. For instance, when the
default argument of a function F is an arbitrary expression that
yields temporaries.  In that case, the default argument expression
must be evaluated in the context of the caller of F. Just taking in
account the default argument expression in the context of the F
declaration is not enough.
I am not sure we can represent that in DWARF today, can we?

So this patch is limited to the simple cases were the default argument
is either a constant or a variable that does not involve any temporary.
When a default argument expression involves some temporaries, no debug
info is generated for it.

For a class, it appears that we were calling finish_struct right before
parsing default arguments of the member functions.
finish_struct does the layout of the class. So we do the layout of the
class before parsing the defaut arguments because they can refer to
parts of the struct that need to be constructed before. The problem is
finish_struct also generates debug info for the class. That means, when
we generate debug info for the struct, we don't have any information
ready about the default arguments.

The patch allows finish_struct to not emit debug info systematically. We
can then keep calling finish_struct "early" and emit debug info only after
we parsed the default arguments.
We do something similar at class template instantiation time. We emit debug
info only after the template arguments are substituted into the default
arguments expressions.
Comment 2 Jakub Jelinek 2010-03-11 20:01:35 UTC
I think instead of creating the _1 functions you should just add the attr argument to those 2 functions and modify callers to pass DW_AT_const_value.
There aren't so many callers, and add_location_or_const_value_attribute/add_AT_location_description is an example already.  Perhaps putting attr argument right after die would be better too.
Comment 3 Dodji Seketeli 2010-03-15 10:45:47 UTC
Created attachment 20107 [details]
Draf patch

Thanks Jakub for the review. This updated patch should address your comments.
Comment 4 Jakub Jelinek 2010-03-15 11:19:25 UTC
As for the arbitrary expressions, can't you just add an artificial, nameless, DW_TAG_variable (not sure if DW_TAG_dwarf_procedure would be tollerable instead), probably as child of the DW_TAG_subprogram whose DW_TAG_formal_parameter it is, and set its DW_AT_location?
See add_bounds_info how it creates similar artificial variable if the bound is an arbitrary expression for which we have a location or location list.
Comment 5 Dodji Seketeli 2010-03-18 14:38:24 UTC
(In reply to comment #4)

As we discussed on IRC, it seems we'd need a way to express that we'd want the debugger to create a temporary, initialize it and later destroy it. DWARF can't express as of now. So we'll probably need some kind of extension. I am not sure exactly what extension yet though. I need to think about this.
Comment 6 Dodji Seketeli 2010-03-23 09:33:51 UTC
Created attachment 20167 [details]
Draft patch

This draft patch emits a DW_AT_GNU_default_value_unrepresentable attribute flag when the default argument value cannot be represented because e.g. it involves temporaries.

Tom, if the patch does what you want, I can propose it for discussion/inclusion for GCC 4.6?
Comment 7 Tom Tromey 2010-03-23 16:11:51 UTC
In the case where the default value is an expression,
would it be possible to just emit the expression as a string?
I believe that would be sufficient for gdb's purposes.