Skip to content

Commit 0d6cdd4

Browse files
authored
Merge pull request swiftlang#22456 from adrian-prantl/typdefscopes
2 parents a17bc73 + 711e1f3 commit 0d6cdd4

File tree

5 files changed

+95
-56
lines changed

5 files changed

+95
-56
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -193,43 +193,63 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
193193
return StringRef();
194194
}
195195

196-
SILLocation::DebugLoc getDeserializedLoc(Pattern *) { return {}; }
197-
SILLocation::DebugLoc getDeserializedLoc(Expr *) { return {}; }
198-
SILLocation::DebugLoc getDeserializedLoc(Stmt *) { return {}; }
199-
SILLocation::DebugLoc getDeserializedLoc(Decl *D) {
200-
SILLocation::DebugLoc L;
196+
using DebugLoc = SILLocation::DebugLoc;
197+
DebugLoc getDeserializedLoc(Pattern *) { return {}; }
198+
DebugLoc getDeserializedLoc(Expr *) { return {}; }
199+
DebugLoc getDeserializedLoc(Decl *D) {
200+
DebugLoc L;
201201
const DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
202202
StringRef Filename = getFilenameFromDC(DC);
203203
if (!Filename.empty())
204204
L.Filename = Filename;
205205
return L;
206206
}
207207

208-
/// Use the SM to figure out the actual line/column of a SourceLoc.
208+
/// Use the Swift SM to figure out the actual line/column of a SourceLoc.
209209
template <typename WithLoc>
210-
SILLocation::DebugLoc getDebugLoc(IRGenDebugInfo &DI, WithLoc *S,
211-
bool End = false) {
212-
SILLocation::DebugLoc L;
213-
if (S == nullptr)
214-
return L;
210+
DebugLoc getSwiftDebugLoc(IRGenDebugInfo &DI, WithLoc *ASTNode, bool End) {
211+
if (!ASTNode)
212+
return {};
215213

216-
SourceLoc Loc = End ? S->getEndLoc() : S->getStartLoc();
214+
SourceLoc Loc = End ? ASTNode->getEndLoc() : ASTNode->getStartLoc();
217215
if (Loc.isInvalid())
218216
// This may be a deserialized or clang-imported decl. And modules
219217
// don't come with SourceLocs right now. Get at least the name of
220218
// the module.
221-
return getDeserializedLoc(S);
219+
return getDeserializedLoc(ASTNode);
222220

223221
return DI.decodeSourceLoc(Loc);
224222
}
225223

226-
SILLocation::DebugLoc getStartLocation(Optional<SILLocation> OptLoc) {
224+
DebugLoc getDebugLoc(IRGenDebugInfo &DI, Pattern *P, bool End = false) {
225+
return getSwiftDebugLoc(DI, P, End);
226+
}
227+
DebugLoc getDebugLoc(IRGenDebugInfo &DI, Expr *E, bool End = false) {
228+
return getSwiftDebugLoc(DI, E, End);
229+
}
230+
DebugLoc getDebugLoc(IRGenDebugInfo &DI, Decl *D, bool End = false) {
231+
DebugLoc L;
232+
if (!D)
233+
return L;
234+
235+
if (auto *ClangDecl = D->getClangDecl()) {
236+
auto ClangSrcLoc = ClangDecl->getLocStart();
237+
clang::SourceManager &ClangSM =
238+
CI.getClangASTContext().getSourceManager();
239+
L.Line = ClangSM.getPresumedLineNumber(ClangSrcLoc);
240+
L.Filename = ClangSM.getBufferName(ClangSrcLoc);
241+
return L;
242+
}
243+
return getSwiftDebugLoc(DI, D, End);
244+
}
245+
246+
DebugLoc getStartLocation(Optional<SILLocation> OptLoc) {
227247
if (!OptLoc)
228248
return {};
229249
return decodeSourceLoc(OptLoc->getStartSourceLoc());
230250
}
231251

232-
SILLocation::DebugLoc sanitizeCodeViewDebugLoc(SILLocation::DebugLoc DLoc) {
252+
DebugLoc sanitizeCodeViewDebugLoc(DebugLoc DLoc) {
233253
if (Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
234254
// When WinDbg finds two locations with the same line but different
235255
// columns, the user must select an address when they break on that
@@ -238,13 +258,13 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
238258
return DLoc;
239259
}
240260

241-
SILLocation::DebugLoc decodeDebugLoc(SILLocation Loc) {
261+
DebugLoc decodeDebugLoc(SILLocation Loc) {
242262
if (Loc.isDebugInfoLoc())
243263
return sanitizeCodeViewDebugLoc(Loc.getDebugInfoLoc());
244264
return decodeSourceLoc(Loc.getDebugSourceLoc());
245265
}
246266

247-
SILLocation::DebugLoc getDebugLocation(Optional<SILLocation> OptLoc) {
267+
DebugLoc getDebugLocation(Optional<SILLocation> OptLoc) {
248268
if (!OptLoc || (Opts.DebugInfoFormat != IRGenDebugInfoFormat::CodeView &&
249269
OptLoc->isInPrologue()))
250270
return {};
@@ -1187,13 +1207,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
11871207
auto *StructTy = BaseTy->castTo<StructType>();
11881208
auto *Decl = StructTy->getDecl();
11891209
auto L = getDebugLoc(*this, Decl);
1190-
if (auto *ClangDecl = Decl->getClangDecl()) {
1191-
auto ClangSrcLoc = ClangDecl->getLocStart();
1192-
clang::SourceManager &ClangSM =
1193-
CI.getClangASTContext().getSourceManager();
1194-
L.Line = ClangSM.getPresumedLineNumber(ClangSrcLoc);
1195-
L.Filename = ClangSM.getBufferName(ClangSrcLoc);
1196-
}
11971210
auto *File = getOrCreateFile(L.Filename);
11981211
if (Opts.DebugInfoLevel > IRGenDebugInfoLevel::ASTTypes)
11991212
return createStructType(DbgTy, Decl, StructTy, Scope, File, L.Line,
@@ -1212,13 +1225,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
12121225
auto *ClassTy = BaseTy->castTo<ClassType>();
12131226
auto *Decl = ClassTy->getDecl();
12141227
auto L = getDebugLoc(*this, Decl);
1215-
if (auto *ClangDecl = Decl->getClangDecl()) {
1216-
auto ClangSrcLoc = ClangDecl->getLocStart();
1217-
clang::SourceManager &ClangSM =
1218-
CI.getClangASTContext().getSourceManager();
1219-
L.Line = ClangSM.getPresumedLineNumber(ClangSrcLoc);
1220-
L.Filename = ClangSM.getBufferName(ClangSrcLoc);
1221-
}
12221228
assert(SizeInBits == CI.getTargetInfo().getPointerWidth(0));
12231229
return createPointerSizedStruct(Scope, Decl->getNameStr(),
12241230
getOrCreateFile(L.Filename), L.Line,
@@ -1414,13 +1420,13 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
14141420
auto L = getDebugLoc(*this, Decl);
14151421
auto AliasedTy = TypeAliasTy->getSinglyDesugaredType();
14161422
auto File = getOrCreateFile(L.Filename);
1417-
// For TypeAlias types, the DeclContext for the aliasED type is
1423+
// For TypeAlias types, the DeclContext for the aliased type is
14181424
// in the decl of the alias type.
14191425
DebugTypeInfo AliasedDbgTy(
14201426
DbgTy.getDeclContext(), DbgTy.getGenericEnvironment(), AliasedTy,
14211427
DbgTy.StorageType, DbgTy.size, DbgTy.align, DbgTy.DefaultAlignment);
14221428
return DBuilder.createTypedef(getOrCreateType(AliasedDbgTy), MangledName,
1423-
File, L.Line, File);
1429+
File, L.Line, Scope);
14241430
}
14251431

14261432
case TypeKind::Paren: {
@@ -1529,26 +1535,35 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
15291535
// FIXME: Builtin and qualified types in LLVM have no parent
15301536
// scope. TODO: This can be fixed by extending DIBuilder.
15311537
llvm::DIScope *Scope = nullptr;
1532-
DeclContext *Context = DbgTy.getType()->getNominalOrBoundGenericNominal();
1533-
if (Context) {
1534-
if (auto *D = Context->getSelfNominalTypeDecl())
1535-
if (auto *ClangDecl = D->getClangDecl()) {
1536-
clang::ASTReader &Reader = *CI.getClangInstance().getModuleManager();
1537-
auto Idx = ClangDecl->getOwningModuleID();
1538-
auto SubModuleDesc = Reader.getSourceDescriptor(Idx);
1539-
auto TopLevelModuleDesc = getClangModule(*D->getModuleContext());
1540-
if (SubModuleDesc && TopLevelModuleDesc) {
1541-
// Describe the submodule, but substitute the cached ASTFile from
1542-
// the toplevel module. The ASTFile pointer in SubModule may be
1543-
// dangling and cant be trusted.
1544-
Scope = getOrCreateModule({SubModuleDesc->getModuleName(),
1545-
SubModuleDesc->getPath(),
1546-
TopLevelModuleDesc->getASTFile(),
1547-
TopLevelModuleDesc->getSignature()},
1548-
SubModuleDesc->getModuleOrNull());
1549-
}
1550-
}
1551-
Context = Context->getParent();
1538+
// Make sure to retrieve the context of the type alias, not the pointee.
1539+
DeclContext *Context = nullptr;
1540+
const Decl *TypeDecl = nullptr;
1541+
const clang::Decl *ClangDecl = nullptr;
1542+
if (auto Alias = dyn_cast<TypeAliasType>(DbgTy.getType())) {
1543+
TypeAliasDecl *AliasDecl = Alias->getDecl();
1544+
TypeDecl = AliasDecl;
1545+
Context = AliasDecl->getParent();
1546+
ClangDecl = AliasDecl->getClangDecl();
1547+
} else if (auto *ND = DbgTy.getType()->getNominalOrBoundGenericNominal()) {
1548+
TypeDecl = ND;
1549+
Context = ND->getParent();
1550+
ClangDecl = ND->getClangDecl();
1551+
}
1552+
if (ClangDecl) {
1553+
clang::ASTReader &Reader = *CI.getClangInstance().getModuleManager();
1554+
auto Idx = ClangDecl->getOwningModuleID();
1555+
auto SubModuleDesc = Reader.getSourceDescriptor(Idx);
1556+
auto TopLevelModuleDesc = getClangModule(*TypeDecl->getModuleContext());
1557+
if (SubModuleDesc && TopLevelModuleDesc) {
1558+
// Describe the submodule, but substitute the cached ASTFile from
1559+
// the toplevel module. The ASTFile pointer in SubModule may be
1560+
// dangling and cant be trusted.
1561+
Scope = getOrCreateModule({SubModuleDesc->getModuleName(),
1562+
SubModuleDesc->getPath(),
1563+
TopLevelModuleDesc->getASTFile(),
1564+
TopLevelModuleDesc->getSignature()},
1565+
SubModuleDesc->getModuleOrNull());
1566+
}
15521567
}
15531568
if (!Scope)
15541569
Scope = getOrCreateContext(Context);

test/ClangImporter/objc_ir.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ func testBlocksWithGenerics(hba: HasBlockArray) -> Any {
360360
// CHECK: attributes [[NOUNWIND]] = { nounwind }
361361

362362
// CHECK: ![[SWIFT_NAME_ALIAS_VAR]] = !DILocalVariable(name: "obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_NAME_ALIAS_TYPE:[0-9]+]])
363-
// CHECK: ![[SWIFT_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo14SwiftNameAliasaD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})
363+
// CHECK: ![[SWIFT_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo14SwiftNameAliasaD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, baseType: !{{[0-9]+}})
364364

365365
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
366-
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo21SwiftGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})
366+
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo21SwiftGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, baseType: !{{[0-9]+}})
367367

368368
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "constr_generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
369-
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo27SwiftConstrGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})
369+
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$sSo27SwiftConstrGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, baseType: !{{[0-9]+}})

test/DebugInfo/C-typedef-Darwin.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-frontend -emit-ir %s -g -I %S/Inputs | %FileCheck %s
2+
// REQUIRES: OS=macosx
3+
4+
import Darwin
5+
6+
func use<T>(_ t: T) {}
7+
8+
let blah = size_t(1024)
9+
use(blah)
10+
// CHECK: !DIDerivedType(tag: DW_TAG_typedef,
11+
// CHECK-SAME: scope: ![[DARWIN_MODULE:[0-9]+]],
12+
// CHECK: ![[DARWIN_MODULE]] = !DIModule({{.*}}, name: "stddef"

test/DebugInfo/C-typedef.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -emit-ir %s -g -I %S/Inputs | %FileCheck %s
2+
import ClangModule
3+
4+
func use<T>(_ t: T) {}
5+
6+
let foo = s_Foo()
7+
// CHECK: !DIDerivedType(tag: DW_TAG_typedef,
8+
// CHECK-SAME: scope: ![[CLANG_MODULE:[0-9]+]],
9+
// CHECK: ![[CLANG_MODULE]] = !DIModule(scope: null, name: "ClangModule"
10+
use(foo)

test/DebugInfo/Inputs/ClangModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
struct Foo {};
2+
3+
typedef struct Foo s_Foo;

0 commit comments

Comments
 (0)