Index: llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -165,7 +165,12 @@ } if (!GV->isDefinition()) + { addFlag(*VariableDIE, dwarf::DW_AT_declaration); + + if (DD->useAllLinkageNames()) + addLinkageName(*VariableDIE, GV->getLinkageName()); + } else addGlobalName(GV->getName(), *VariableDIE, DeclContext); @@ -177,6 +182,7 @@ addTemplateParams(*VariableDIE, DINodeArray(TP)); // Add location. + if (GV->isDefinition()) addLocationAttribute(VariableDIE, GV, GlobalExprs); return VariableDIE; Index: llvm/test/DebugInfo/Generic/debug-info-extern-variable-declaration.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/Generic/debug-info-extern-variable-declaration.ll @@ -0,0 +1,56 @@ +; RUN: %llc_dwarf -O0 -filetype=obj < %s | llvm-dwarfdump -v -debug-info - | FileCheck %s +; Check extern variable debuginfo , declaration attribute + +; CHECK: DW_TAG_variable +; CHECK: DW_AT_name {{.*}} "i" +; CHECK: DW_AT_declaration +; CHECK-NOT: DW_AT_location + +; CHECK: DW_TAG_variable +; CHECK: DW_AT_name {{.*}} "j" +; CHECK: DW_AT_declaration +; CHECK-NOT: DW_AT_location + + +; ModuleID = 'debug-info-extern-variable-declaration.c' +source_filename = "debug-info-extern-variable-declaration.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@i = external dso_local global i32, align 4, !dbg !0 +@j = external dso_local global i32, align 4, !dbg !6 +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @foo() #0 !dbg !13 { +entry: + %0 = load i32, i32* @i, align 4, !dbg !16 + %1 = load i32, i32* @j, align 4, !dbg !17 + %add = add nsw i32 %0, %1, !dbg !18 + ret i32 %add, !dbg !19 +} + +attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!9, !10, !11} +!llvm.ident = !{!12} + +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "i", scope: !2, file: !3, line: 1, type: !8, isLocal: false, isDefinition: false) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 10.0.0 (https://github.com/llvm/llvm-project.git bbcf1c3496ce2bd1ed87e8fb15ad896e279633ce)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "debug-info-extern-variable-declaration.c", directory: "/home/bft/Jaydeep/llvm-project/build_debug_fix/bin") +!4 = !{} +!5 = !{!0, !6} +!6 = !DIGlobalVariableExpression(var: !7, expr: !DIExpression()) +!7 = distinct !DIGlobalVariable(name: "j", scope: !2, file: !3, line: 3, type: !8, isLocal: false, isDefinition: false) +!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!9 = !{i32 7, !"Dwarf Version", i32 4} +!10 = !{i32 2, !"Debug Info Version", i32 3} +!11 = !{i32 1, !"wchar_size", i32 4} +!12 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git bbcf1c3496ce2bd1ed87e8fb15ad896e279633ce)"} +!13 = distinct !DISubprogram(name: "foo", scope: !3, file: !3, line: 2, type: !14, scopeLine: 2, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4) +!14 = !DISubroutineType(types: !15) +!15 = !{!8} +!16 = !DILocation(line: 4, column: 10, scope: !13) +!17 = !DILocation(line: 4, column: 12, scope: !13) +!18 = !DILocation(line: 4, column: 11, scope: !13) +!19 = !DILocation(line: 4, column: 3, scope: !13)