Index: llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -113,7 +113,8 @@ // extend .file to support this. unsigned CUID = Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID(); if (!File) - return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, CUID); + return Asm->OutStreamer->EmitDwarfFileDirective(0, "", "", None, None, + CUID); return Asm->OutStreamer->EmitDwarfFileDirective( 0, File->getDirectory(), File->getFilename(), getMD5AsBytes(File), File->getSource(), CUID); @@ -134,7 +135,7 @@ // case such construction creates the DIE. auto *CB = GVContext ? dyn_cast(GVContext) : nullptr; DIE *ContextDIE = CB ? getOrCreateCommonBlock(CB, GlobalExprs) - : getOrCreateContextDIE(GVContext); + : getOrCreateContextDIE(GVContext); // Add to map. DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); @@ -164,9 +165,12 @@ addSourceLine(*VariableDIE, GV); } - if (!GV->isDefinition()) + if (!GV->isDefinition()) { addFlag(*VariableDIE, dwarf::DW_AT_declaration); - else + + if (DD->useAllLinkageNames()) + addLinkageName(*VariableDIE, GV->getLinkageName()); + } else addGlobalName(GV->getName(), *VariableDIE, DeclContext); if (uint32_t AlignInBytes = GV->getAlignInBytes()) @@ -177,13 +181,15 @@ addTemplateParams(*VariableDIE, DINodeArray(TP)); // Add location. - addLocationAttribute(VariableDIE, GV, GlobalExprs); + if (GV->isDefinition()) + addLocationAttribute(VariableDIE, GV, GlobalExprs); return VariableDIE; } -void DwarfCompileUnit::addLocationAttribute( - DIE *VariableDIE, const DIGlobalVariable *GV, ArrayRef GlobalExprs) { +void DwarfCompileUnit::addLocationAttribute(DIE *VariableDIE, + const DIGlobalVariable *GV, + ArrayRef GlobalExprs) { bool addToAccelTable = false; DIELoc *Loc = nullptr; Optional NVPTXAddressSpace; @@ -337,8 +343,7 @@ // emitted into and the subprogram was contained within. If these are the // same then extend our current range, otherwise add this as a new range. if (CURanges.empty() || !SameAsPrevCU || - (&CURanges.back().End->getSection() != - &Range.End->getSection())) { + (&CURanges.back().End->getSection() != &Range.End->getSection())) { CURanges.push_back(Range); return; } @@ -915,13 +920,16 @@ // Passing null as the associated node because the abstract definition // shouldn't be found by lookup. - AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); + AbsDef = &ContextCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, + nullptr); ContextCU->applySubprogramAttributesToDefinition(SP, *AbsDef); if (!ContextCU->includeMinimalInlineScopes()) - ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); + ContextCU->addUInt(*AbsDef, dwarf::DW_AT_inline, None, + dwarf::DW_INL_inlined); if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren(Scope, *AbsDef)) - ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); + ContextCU->addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, + *ObjectPointer); } /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location atom. @@ -1118,12 +1126,13 @@ assert(Scope && Scope->isAbstractScope()); auto &Entity = getAbstractEntities()[Node]; if (isa(Node)) { - Entity = std::make_unique( - cast(Node), nullptr /* IA */);; + Entity = std::make_unique(cast(Node), + nullptr /* IA */); + ; DU->addScopeVariable(Scope, cast(Entity.get())); } else if (isa(Node)) { - Entity = std::make_unique( - cast(Node), nullptr /* IA */); + Entity = + std::make_unique(cast(Node), nullptr /* IA */); DU->addScopeLabel(Scope, cast(Entity.get())); } } @@ -1271,9 +1280,9 @@ unsigned Index) { dwarf::Form Form = dwarf::DW_FORM_data4; if (DD->getDwarfVersion() == 4) - Form =dwarf::DW_FORM_sec_offset; + Form = dwarf::DW_FORM_sec_offset; if (DD->getDwarfVersion() >= 5) - Form =dwarf::DW_FORM_loclistx; + Form = dwarf::DW_FORM_loclistx; Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); } @@ -1321,7 +1330,8 @@ return DD->useSplitDwarf() && Skeleton; } -void DwarfCompileUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) { +void DwarfCompileUnit::finishNonUnitTypeDIE(DIE &D, + const DICompositeType *CTy) { constructTypeDIE(D, CTy); } @@ -1351,11 +1361,12 @@ // child list. for (auto &Btr : reverse(ExprRefedBaseTypes)) { DIE &Die = getUnitDie().addChildFront( - DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); + DIE::get(DIEValueAllocator, dwarf::DW_TAG_base_type)); SmallString<32> Str; addString(Die, dwarf::DW_AT_name, - Twine(dwarf::AttributeEncodingString(Btr.Encoding) + - "_" + Twine(Btr.BitSize)).toStringRef(Str)); + Twine(dwarf::AttributeEncodingString(Btr.Encoding) + "_" + + Twine(Btr.BitSize)) + .toStringRef(Str)); addUInt(Die, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, Btr.Encoding); addUInt(Die, dwarf::DW_AT_byte_size, None, Btr.BitSize / 8); Index: test/DebugInfo/Generic/debug-info-extern-variable-declaration.ll =================================================================== --- /dev/null +++ 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 } + +!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)