Index: lib/CodeGen/AsmPrinter/DwarfUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1207,12 +1207,21 @@ DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); StringRef Name = NS.getName(); - if (!Name.empty()) + bool IsAnonymous = Name.empty(); + if (!IsAnonymous) addString(NDie, dwarf::DW_AT_name, NS.getName()); else Name = "(anonymous namespace)"; DD->addAccelNamespace(Name, NDie); addGlobalName(Name, NDie, NS.getContext()); + if (IsAnonymous) { + // For anonymous namespaces, add a DW_TAG_imported_module tag + // containing a DW_AT_import attribute with the reference to this + // namespace entry. + DIE &IDie = createAndAddDIE(dwarf::DW_TAG_imported_module, *ContextDIE); + addDIEEntry(IDie, dwarf::DW_AT_import, NDie); + addFlag(IDie, dwarf::DW_AT_artificial); + } addSourceLine(NDie, NS); return &NDie; } Index: test/DebugInfo/X86/gnu-public-names.ll =================================================================== --- test/DebugInfo/X86/gnu-public-names.ll +++ test/DebugInfo/X86/gnu-public-names.ll @@ -127,6 +127,8 @@ ; in clang. ; CHECK: DW_TAG_imported_module ; CHECK-NOT: {{DW_TAG|NULL}} +; CHECK: DW_TAG_imported_module +; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: NULL ; CHECK-NOT: {{DW_TAG|NULL}} @@ -147,6 +149,7 @@ ; CHECK: DW_AT_name {{.*}} "i" ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: NULL +; CHECK: DW_TAG_imported_module ; CHECK-NOT: {{DW_TAG|NULL}} ; CHECK: [[MEM_FUNC:0x[0-9a-f]+]]: DW_TAG_subprogram Index: test/DebugInfo/anonymous-namespace.ll =================================================================== --- test/DebugInfo/anonymous-namespace.ll +++ test/DebugInfo/anonymous-namespace.ll @@ -0,0 +1,37 @@ +; RUN: llc -O0 < %s | FileCheck %s +; If the namespace is anonymous, add a DW_TAG_imported_module tag +; containing DW_AT_import attribute with the reference to this namespace +; entry. + +; namespace +; { +; int a = 5; +; } +; int *b = &a; + +; ModuleID = 'anon.cpp' + +@_ZN12_GLOBAL__N_11aE = internal global i32 5, align 4 +@b = global i32* @_ZN12_GLOBAL__N_11aE, align 8 + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!10, !11} +!llvm.ident = !{!12} + +!0 = !{!"0x11\004\00clang version 3.7.0 (trunk 230293) (llvm/trunk 230292:230313M)\000\00\000\00\001", !1, !2, !2, !2, !3, !2} ; [ DW_TAG_compile_unit ] [/home/kromanova/anon.cpp] [DW_LANG_C_plus_plus] +!1 = !{!"anon.cpp", !"/home/kromanova"} +!2 = !{} +!3 = !{!4, !8} +!4 = !{!"0x34\00b\00b\00\005\000\001", null, !5, !6, i32** @b, null} ; [ DW_TAG_variable ] [b] [line 5] [def] +!5 = !{!"0x29", !1} ; [ DW_TAG_file_type ] [/home/kromanova/anon.cpp] +!6 = !{!"0xf\00\000\0064\0064\000\000", null, null, !7} ; [ DW_TAG_pointer_type ] [line 0, size 64, align 64, offset 0] [from int] +!7 = !{!"0x24\00int\000\0032\0032\000\000\005", null, null} ; [ DW_TAG_base_type ] [int] [line 0, size 32, align 32, offset 0, enc DW_ATE_signed] +!8 = !{!"0x34\00a\00a\00_ZN12_GLOBAL__N_11aE\003\001\001", !9, !5, !7, i32* @_ZN12_GLOBAL__N_11aE, null} ; [ DW_TAG_variable ] [a] [line 3] [local] [def] +!9 = !{!"0x39\00\002", !1, null} ; [ DW_TAG_namespace ] [line 2] +!10 = !{i32 2, !"Dwarf Version", i32 4} +!11 = !{i32 2, !"Debug Info Version", i32 2} +!12 = !{!"clang version 3.7.0 (trunk 230293) (llvm/trunk 230292:230313M)"} + +;CHECK: DW_TAG_imported_module +;CHECK-NEXT: DW_AT_import +;CHECK-NEXT: DW_AT_artificial