This fixes an issue with LTO handling DINFO_LEVEL_TERSE where we do not generate DIEs for NAMESPACE_DECLs and thus reconstruction of the DIE tree from DECL_CONTEXT at LTRANS time fails. The fix is to skip NAMESPACE_DECL contexts like we do for TYPE contexts.
[LTO with -g1] Bootstrapped on x86_64-unknown-linux-gnu, testing in progress. Richard. 2018-04-04 Richard Biener <rguent...@suse.de> PR lto/85176 * dwarf2out.c (dwarf2out_register_external_die): Peel namespaces from contexts for DINFO_LEVEL_TERSE and below. * g++.dg/lto/pr85176_0.C: New testcase. Index: gcc/dwarf2out.c =================================================================== --- gcc/dwarf2out.c (revision 259069) +++ gcc/dwarf2out.c (working copy) @@ -5903,8 +5903,13 @@ dwarf2out_register_external_die (tree de } else ctx = DECL_CONTEXT (decl); + /* Peel types in the context stack. */ while (ctx && TYPE_P (ctx)) ctx = TYPE_CONTEXT (ctx); + /* Likewise namespaces in case we do not want to emit DIEs for them. */ + if (debug_info_level <= DINFO_LEVEL_TERSE) + while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) + ctx = DECL_CONTEXT (ctx); if (ctx) { if (TREE_CODE (ctx) == BLOCK) Index: gcc/testsuite/g++.dg/lto/pr85176_0.C =================================================================== --- gcc/testsuite/g++.dg/lto/pr85176_0.C (nonexistent) +++ gcc/testsuite/g++.dg/lto/pr85176_0.C (working copy) @@ -0,0 +1,10 @@ +// { dg-lto-do link } +// { dg-lto-options { { -flto -g1 } } } +// { dg-extra-ld-options "-r -nostdlib" } +namespace a { + template <typename b, typename = b> class c; + template <typename b, typename d> void e(c<b, d> &); + void operator<<(c<char> &f, const char *) { e(f); } + extern c<char> cout; +} +int main() { a::cout << ""; }