From 23670e5b95fcaf88238239729457393b8cc831ff Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Thu, 24 Mar 2011 20:30:50 +0000 Subject: [PATCH] Keep track of directory namd and fIx regression caused by Rafael's patch r119613. A better approach would be to move source id handling inside MC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@128233 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 39 +++++++++++----- lib/CodeGen/AsmPrinter/DwarfDebug.h | 2 +- test/CodeGen/X86/unknown-location.ll | 2 +- test/DebugInfo/dbg-file-name.ll | 66 +++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 test/DebugInfo/dbg-file-name.ll diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index 58d51ea2ac8..1a84c1f23a1 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -516,7 +516,8 @@ void DwarfDebug::addSourceLine(DIE *Die, DIVariable V) { unsigned Line = V.getLineNumber(); if (Line == 0) return; - unsigned FileID = GetOrCreateSourceID(V.getContext().getFilename()); + unsigned FileID = GetOrCreateSourceID(V.getContext().getFilename(), + V.getContext().getDirectory()); assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); @@ -532,7 +533,8 @@ void DwarfDebug::addSourceLine(DIE *Die, DIGlobalVariable G) { unsigned Line = G.getLineNumber(); if (Line == 0) return; - unsigned FileID = GetOrCreateSourceID(G.getContext().getFilename()); + unsigned FileID = GetOrCreateSourceID(G.getContext().getFilename(), + G.getContext().getDirectory()); assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); @@ -551,7 +553,7 @@ void DwarfDebug::addSourceLine(DIE *Die, DISubprogram SP) { unsigned Line = SP.getLineNumber(); if (!SP.getContext().Verify()) return; - unsigned FileID = GetOrCreateSourceID(SP.getFilename()); + unsigned FileID = GetOrCreateSourceID(SP.getFilename(), SP.getDirectory()); assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); @@ -567,7 +569,7 @@ void DwarfDebug::addSourceLine(DIE *Die, DIType Ty) { unsigned Line = Ty.getLineNumber(); if (Line == 0 || !Ty.getContext().Verify()) return; - unsigned FileID = GetOrCreateSourceID(Ty.getFilename()); + unsigned FileID = GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory()); assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); @@ -585,7 +587,7 @@ void DwarfDebug::addSourceLine(DIE *Die, DINameSpace NS) { return; StringRef FN = NS.getFilename(); - unsigned FileID = GetOrCreateSourceID(FN); + unsigned FileID = GetOrCreateSourceID(FN, NS.getDirectory()); assert(FileID && "Invalid file id"); addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID); addUInt(Die, dwarf::DW_AT_decl_line, 0, Line); @@ -1859,10 +1861,21 @@ DIE *DwarfDebug::constructScopeDIE(DbgScope *Scope) { /// in the SourceIds map. This can update DirectoryNames and SourceFileNames /// maps as well. -unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName){ +unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName, + StringRef DirName) { // If FE did not provide a file name, then assume stdin. if (FileName.empty()) - return GetOrCreateSourceID(""); + return GetOrCreateSourceID("", StringRef()); + + // MCStream expects full path name as filename. + if (!DirName.empty() && !FileName.startswith("/")) { + std::string FullPathName(DirName.data()); + if (!DirName.endswith("/")) + FullPathName += "/"; + FullPathName += FileName.data(); + // Here FullPathName will be copied into StringMap by GetOrCreateSourceID. + return GetOrCreateSourceID(StringRef(FullPathName), StringRef()); + } StringMapEntry &Entry = SourceIdMap.GetOrCreateValue(FileName); if (Entry.getValue()) @@ -1872,7 +1885,7 @@ unsigned DwarfDebug::GetOrCreateSourceID(StringRef FileName){ Entry.setValue(SrcId); // Print out a .file directive to specify files for .loc directives. - Asm->OutStreamer.EmitDwarfFileDirective(SrcId, FileName); + Asm->OutStreamer.EmitDwarfFileDirective(SrcId, Entry.getKey()); return SrcId; } @@ -1898,7 +1911,7 @@ void DwarfDebug::constructCompileUnit(const MDNode *N) { DICompileUnit DIUnit(N); StringRef FN = DIUnit.getFilename(); StringRef Dir = DIUnit.getDirectory(); - unsigned ID = GetOrCreateSourceID(FN); + unsigned ID = GetOrCreateSourceID(FN, Dir); DIE *Die = new DIE(dwarf::DW_TAG_compile_unit); addString(Die, dwarf::DW_AT_producer, dwarf::DW_FORM_string, @@ -3102,7 +3115,7 @@ DbgScope *DwarfDebug::findDbgScope(const MachineInstr *MInsn) { MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S) { StringRef Fn; - + StringRef Dir; unsigned Src = 1; if (S) { DIDescriptor Scope(S); @@ -3110,19 +3123,23 @@ MCSymbol *DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, if (Scope.isCompileUnit()) { DICompileUnit CU(S); Fn = CU.getFilename(); + Dir = CU.getDirectory(); } else if (Scope.isFile()) { DIFile F(S); Fn = F.getFilename(); + Dir = F.getDirectory(); } else if (Scope.isSubprogram()) { DISubprogram SP(S); Fn = SP.getFilename(); + Dir = SP.getDirectory(); } else if (Scope.isLexicalBlock()) { DILexicalBlock DB(S); Fn = DB.getFilename(); + Dir = DB.getDirectory(); } else assert(0 && "Unexpected scope info"); - Src = GetOrCreateSourceID(Fn); + Src = GetOrCreateSourceID(Fn, Dir); } Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, DWARF2_FLAG_IS_STMT, diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.h b/lib/CodeGen/AsmPrinter/DwarfDebug.h index e59ab5bd821..207542dfd3f 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -518,7 +518,7 @@ private: /// GetOrCreateSourceID - Look up the source id with the given directory and /// source file names. If none currently exists, create a new id and insert it /// in the SourceIds map. - unsigned GetOrCreateSourceID(StringRef FullName); + unsigned GetOrCreateSourceID(StringRef DirName, StringRef FullName); /// constructCompileUnit - Create new CompileUnit for the given /// metadata node with tag DW_TAG_compile_unit. diff --git a/test/CodeGen/X86/unknown-location.ll b/test/CodeGen/X86/unknown-location.ll index 09431b5564a..7d101bf95cf 100644 --- a/test/CodeGen/X86/unknown-location.ll +++ b/test/CodeGen/X86/unknown-location.ll @@ -9,7 +9,7 @@ ; CHECK-NEXT: Ltmp ; CHECK-NEXT: cltd ; CHECK-NEXT: idivl %r8d -; CHECK-NEXT: .loc 1 4 3 +; CHECK-NEXT: .loc 2 4 3 ; CHECK-NEXT: Ltmp ; CHECK-NEXT: addl %ecx, %eax ; CHECK-NEXT: ret diff --git a/test/DebugInfo/dbg-file-name.ll b/test/DebugInfo/dbg-file-name.ll new file mode 100644 index 00000000000..79cc33484a2 --- /dev/null +++ b/test/DebugInfo/dbg-file-name.ll @@ -0,0 +1,66 @@ +; RUN: llc -O0 < %s | FileCheck %s +; Radar 8884898 +; CHECK: file 1 "/Users/manav/one/two/simple.c" + +@.str = private unnamed_addr constant [8 x i8] c"i = %d\0A\00", align 4 +@.str1 = private unnamed_addr constant [12 x i8] c"i + 1 = %d\0A\00", align 4 + +define void @foo(i32 %i) nounwind { +entry: + %i_addr = alloca i32, align 4 + %"alloca point" = bitcast i32 0 to i32 + call void @llvm.dbg.declare(metadata !{i32* %i_addr}, metadata !9), !dbg !10 + store i32 %i, i32* %i_addr + %0 = load i32* %i_addr, align 4, !dbg !11 + %1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 %0) nounwind, !dbg !11 + %2 = load i32* %i_addr, align 4, !dbg !13 + %3 = add nsw i32 %2, 1, !dbg !13 + %4 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str1, i32 0, i32 0), i32 %3) nounwind, !dbg !13 + br label %return, !dbg !14 + +return: ; preds = %entry + ret void, !dbg !14 +} + +declare void @llvm.dbg.declare(metadata, metadata) nounwind readnone + +declare i32 @printf(i8*, ...) nounwind + +define i32 @main() nounwind { +entry: + %retval = alloca i32 + %0 = alloca i32 + %"alloca point" = bitcast i32 0 to i32 + call void @foo(i32 2) nounwind, !dbg !15 + call void @foo(i32 4) nounwind, !dbg !17 + store i32 0, i32* %0, align 4, !dbg !18 + %1 = load i32* %0, align 4, !dbg !18 + store i32 %1, i32* %retval, align 4, !dbg !18 + br label %return, !dbg !18 + +return: ; preds = %entry + %retval1 = load i32* %retval, !dbg !18 + ret i32 %retval1, !dbg !18 +} + +!llvm.dbg.sp = !{!0, !6} + +!0 = metadata !{i32 589870, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"foo", metadata !1, i32 4, metadata !3, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, void (i32)* @foo} ; [ DW_TAG_subprogram ] +!1 = metadata !{i32 589865, metadata !"simple.c", metadata !"/Users/manav/one/two", metadata !2} ; [ DW_TAG_file_type ] +!2 = metadata !{i32 589841, i32 0, i32 1, metadata !"simple.c", metadata !"/Users/manav/one/two", metadata !"LLVM build 00", i1 true, i1 false, metadata !"", i32 0} ; [ DW_TAG_compile_unit ] +!3 = metadata !{i32 589845, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !4, i32 0, null} ; [ DW_TAG_subroutine_type ] +!4 = metadata !{null, metadata !5} +!5 = metadata !{i32 589860, metadata !1, metadata !"int", metadata !1, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] +!6 = metadata !{i32 589870, i32 0, metadata !1, metadata !"main", metadata !"main", metadata !"main", metadata !1, i32 9, metadata !7, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 ()* @main} ; [ DW_TAG_subprogram ] +!7 = metadata !{i32 589845, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !8, i32 0, null} ; [ DW_TAG_subroutine_type ] +!8 = metadata !{metadata !5} +!9 = metadata !{i32 590081, metadata !0, metadata !"i", metadata !1, i32 4, metadata !5, i32 0} ; [ DW_TAG_arg_variable ] +!10 = metadata !{i32 4, i32 0, metadata !0, null} +!11 = metadata !{i32 5, i32 0, metadata !12, null} +!12 = metadata !{i32 589835, metadata !0, i32 4, i32 0, metadata !1, i32 0} ; [ DW_TAG_lexical_block ] +!13 = metadata !{i32 6, i32 0, metadata !12, null} +!14 = metadata !{i32 7, i32 0, metadata !12, null} +!15 = metadata !{i32 10, i32 0, metadata !16, null} +!16 = metadata !{i32 589835, metadata !6, i32 9, i32 0, metadata !1, i32 1} ; [ DW_TAG_lexical_block ] +!17 = metadata !{i32 11, i32 0, metadata !16, null} +!18 = metadata !{i32 12, i32 0, metadata !16, null}