diff --git a/lib/IRGen/IRGenDebugInfo.cpp b/lib/IRGen/IRGenDebugInfo.cpp index 4e922fffc2661..64b14e6f9ed55 100644 --- a/lib/IRGen/IRGenDebugInfo.cpp +++ b/lib/IRGen/IRGenDebugInfo.cpp @@ -268,10 +268,36 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { /// Decode (and cache) a SourceLoc. FileAndLocation decodeSourceLoc(SourceLoc SL) { auto &Cached = FileAndLocationCache[SL.getOpaquePointerValue()]; - if (!Cached.File) { - Cached = getFileAndLocation( - SILLocation::decode(SL, SM, /*ForceGeneratedSourceToDisk=*/true)); + if (Cached.File) + return Cached; + + if (!SL.isValid()) { + Cached.File = CompilerGeneratedFile; + return Cached; + } + + // If the source buffer is a macro, extract its full text. + llvm::Optional Source; + bool ForceGeneratedSourceToDisk = Opts.DWARFVersion < 5; + if (!ForceGeneratedSourceToDisk) { + auto BufferID = SM.findBufferContainingLoc(SL); + if (auto generatedInfo = SM.getGeneratedSourceInfo(BufferID)) { + // We only care about macros, so skip everything else. + if (generatedInfo->kind != GeneratedSourceInfo::ReplacedFunctionBody && + generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted) + if (auto *MemBuf = SM.getLLVMSourceMgr().getMemoryBuffer(BufferID)) + Source = MemBuf->getBuffer(); + } } + Cached.File = getOrCreateFile( + SM.getDisplayNameForLoc(SL, !ForceGeneratedSourceToDisk), Source); + std::tie(Cached.Line, Cached.Column) = + SM.getPresumedLineAndColumnForLoc(SL); + // When WinDbg finds two locations with the same line but different + // columns, the user must select an address when they break on that + // line. Also, clang does not emit column locations in CodeView for C++. + if (Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView) + Cached.Column = 0; return Cached; } @@ -295,7 +321,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { const DeclContext *DC = D->getDeclContext()->getModuleScopeContext(); StringRef Filename = getFilenameFromDC(DC); if (!Filename.empty()) - L.File = getOrCreateFile(Filename); + L.File = getOrCreateFile(Filename, {}); return L; } @@ -306,7 +332,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { // line. Also, clang does not emit column locations in CodeView for C++. bool CodeView = Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView; return {FL.line, CodeView ? (uint16_t)0 : FL.column, - getOrCreateFile(FL.filename)}; + getOrCreateFile(FL.filename, {})}; } /// Use the Swift SM to figure out the actual line/column of a SourceLoc. @@ -345,7 +371,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { return L; L.Line = PresumedLoc.getLine(); L.Column = PresumedLoc.getColumn(); - L.File = getOrCreateFile(PresumedLoc.getFilename()); + L.File = getOrCreateFile(PresumedLoc.getFilename(), {}); return L; } return getSwiftFileAndLocation(D, End); @@ -457,7 +483,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { #endif - llvm::DIFile *getOrCreateFile(StringRef Filename) { + llvm::DIFile *getOrCreateFile(StringRef Filename, + llvm::Optional Source) { if (Filename.empty()) Filename = SILLocation::getCompilerGeneratedLoc()->filename; @@ -486,7 +513,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo { } } - return createFile(Filename, llvm::None, llvm::None); + return createFile(Filename, llvm::None, Source); } /// This is effectively \p clang::CGDebugInfo::createFile(). @@ -2077,7 +2104,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts, MainFile = (RelFile && RelDir) ? createFile(SourcePath, {}, {}) : DBuilder.createFile(RemappedFile, RemappedDir); - CompilerGeneratedFile = getOrCreateFile(""); + CompilerGeneratedFile = getOrCreateFile("", {}); StringRef Sysroot = IGM.Context.SearchPathOpts.getSDKPath(); StringRef SDK; @@ -2287,7 +2314,7 @@ void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder, else { std::string FuncName = "Swift runtime failure: "; FuncName += failureMsg; - llvm::DIFile *File = getOrCreateFile({}); + llvm::DIFile *File = getOrCreateFile({}, {}); TrapSP = DBuilder.createFunction( File, FuncName, StringRef(), File, 0, DIFnTy, 0, llvm::DINode::FlagArtificial, diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index 57607615961bb..269be21e2843b 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -24,7 +24,7 @@ // RUN: %target-swift-frontend -swift-version 5 -emit-sil -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-SIL %s // Debug info IR testing -// RUN: %target-swift-frontend -swift-version 5 -emit-ir -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s +// RUN: %target-swift-frontend -swift-version 5 -dwarf-version=5 -emit-ir -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s // Execution testing // RUN: %target-build-swift -swift-version 5 -g -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -Xfrontend -emit-dependencies-path -Xfrontend %t/main.d -Xfrontend -emit-reference-dependencies-path -Xfrontend %t/main.swiftdeps @@ -163,10 +163,10 @@ func testFileID(a: Int, b: Int) { // CHECK-AST: macro_expansion_expr type='String'{{.*}}name=line print("Builtin result is \(#fileID)") print( - /// CHECK-IR-DAG: ![[L1:[0-9]+]] = distinct !DILocation(line: [[@LINE+1]], column: 5 + // CHECK-IR-DAG: ![[L1:[0-9]+]] = distinct !DILocation(line: [[@LINE+3]], column: 5 + // CHECK-IR-DAG: ![[L2:[0-9]+]] = distinct !DILocation({{.*}}inlinedAt: ![[L1]]) + // CHECK-IR-DAG: !DIFile(filename: "{{.*}}@__swiftmacro_9MacroUser10testFileID1a1bySi_SitF06customdE0fMf_.swift", {{.*}}source: "{{.*}}MacroUser/macro_expand.swift{{.*}}") #addBlocker( - /// CHECK-IR-DAG: ![[L2:[0-9]+]] = distinct !DILocation({{.*}}inlinedAt: ![[L1]]) - /// CHECK-IR-DAG: ![[L3:[0-9]+]] = !DILocation({{.*}}inlinedAt: ![[L2]]) #stringify(a - b) ) )