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.
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.
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.
Created attachment 20107 [details] Draf patch Thanks Jakub for the review. This updated patch should address your comments.
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.
(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.
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?
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.