namespace S { int f () { int i = 42; { extern int i; return i; } } } generates DWARF: # Grossly simplified. <1><2d>: Abbrev Number: 2 (DW_TAG_namespace) <2e> DW_AT_name : S <2><36>: Abbrev Number: 3 (DW_TAG_subprogram) <37> DW_AT_external : 1 <38> DW_AT_name : f <3c> DW_AT_MIPS_linkage_name: (indirect string, offset: 0x49): _ZN1S1fEv <61> DW_AT_low_pc : 0x0 <69> DW_AT_high_pc : 0x13 <71> DW_AT_frame_base : 0x0 (location list) <3><75>: Abbrev Number: 7 (DW_TAG_lexical_block) <76> DW_AT_low_pc : 0x4 <7e> DW_AT_high_pc : 0x11 <4><86>: Abbrev Number: 8 (DW_TAG_variable) <87> DW_AT_name : i <8f> DW_AT_location : 2 byte block: 91 6c (DW_OP_fbreg: -20) <4><92>: Abbrev Number: 9 (DW_TAG_lexical_block) <93> DW_AT_low_pc : 0xb <9b> DW_AT_high_pc : 0x11 <2><45>: Abbrev Number: 4 (DW_TAG_variable) <46> DW_AT_name : i <4a> DW_AT_MIPS_linkage_name: (indirect string, offset: 0x40): _ZN1S1iE <52> DW_AT_external : 1 <53> DW_AT_declaration : 1 But the last DIE should have been <5><45> being in inner DW_TAG_lexical_block so that debugger would display external variable S::i in the inner block. (I have inlined the DW_AT_specification reference in the dump above.) Similar case in C works OK: int f (void) { int i = 42; { extern int i; return i; } }
A patch was proposed at http://gcc.gnu.org/ml/gcc-patches/2010-03/msg01473.html
Subject: Bug 43325 Author: dodji Date: Thu Apr 1 18:54:30 2010 New Revision: 157928 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=157928 Log: Fix for PR debug/43325 gcc/ChangeLog: PR debug/43325 * dwarf2out.c (gen_variable_die): Allow debug info for variable re-declaration when it happens in a function. gcc/testsuite/ChangeLog: PR debug/43325 * c-c++-common/dwarf2/redeclaration-1.C: New test. Added: trunk/gcc/testsuite/c-c++-common/dwarf2/redeclaration-1.C Modified: trunk/gcc/ChangeLog trunk/gcc/dwarf2out.c trunk/gcc/testsuite/ChangeLog
Fixed in 4.5 (trunk).