Hi! Starting with GCC6 we emit for C (C++ is correct) incorrect DW_AT_decl_* location for structs which are forward declared, then have some incomplete var defined with it and finally defined.
The problem is that we process incomplete_vars before actually updating DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) with the location of the definition, we find the type is onw complete and fill in debug info for it using the old locus. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/release branches? 2017-12-22 Jakub Jelinek <ja...@redhat.com> PR debug/83550 * c-decl.c (finish_struct): Set DECL_SOURCE_LOCATION on TYPE_STUB_DECL and call rest_of_type_compilation before processing incomplete vars rather than after it. * c-c++-common/dwarf2/pr83550.c: New test. --- gcc/c/c-decl.c.jj 2017-12-19 18:09:05.000000000 +0100 +++ gcc/c/c-decl.c 2017-12-22 13:08:58.063861471 +0100 @@ -8199,6 +8199,14 @@ finish_struct (location_t loc, tree t, t warning_at (loc, 0, "union cannot be made transparent"); } + /* Update type location to the one of the definition, instead of e.g. + a forward declaration. */ + if (TYPE_STUB_DECL (t)) + DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc; + + /* Finish debugging output for this type. */ + rest_of_type_compilation (t, toplevel); + /* If this structure or union completes the type of any previous variable declaration, lay it out and output its rtl. */ for (x = incomplete_vars; x; x = TREE_CHAIN (x)) @@ -8215,14 +8223,6 @@ finish_struct (location_t loc, tree t, t } } - /* Update type location to the one of the definition, instead of e.g. - a forward declaration. */ - if (TYPE_STUB_DECL (t)) - DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc; - - /* Finish debugging output for this type. */ - rest_of_type_compilation (t, toplevel); - /* If we're inside a function proper, i.e. not file-scope and not still parsing parameters, then arrange for the size of a variable sized type to be bound now. */ --- gcc/testsuite/c-c++-common/dwarf2/pr83550.c.jj 2017-12-22 13:11:03.205257187 +0100 +++ gcc/testsuite/c-c++-common/dwarf2/pr83550.c 2017-12-22 13:12:26.144193929 +0100 @@ -0,0 +1,10 @@ +/* PR debug/83550 */ +/* { dg-do compile } */ +/* { dg-options "-gdwarf -dA -fno-merge-debug-strings" } */ + +struct my_struct; +extern struct my_struct s; +struct my_struct { int a, b; }; +struct my_struct q; + +/* { dg-final { scan-assembler "DW_TAG_structure_type\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*\"my_struct\[^\\r\\n\]*DW_AT_name(\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*DW_AT_)*\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*\[^0-9a-fA-FxX](0x)?7\[^0-9a-fA-FxX]\[^\\r\\n\]*DW_AT_decl_line" } } */ Jakub