Skip to content

[Debug Info] Store the source code for macro expansions in the debug … #70132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions lib/IRGen/IRGenDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringRef> 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;
}

Expand All @@ -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;
}

Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -457,7 +483,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {

#endif

llvm::DIFile *getOrCreateFile(StringRef Filename) {
llvm::DIFile *getOrCreateFile(StringRef Filename,
llvm::Optional<StringRef> Source) {
if (Filename.empty())
Filename = SILLocation::getCompilerGeneratedLoc()->filename;

Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
)
Expand Down