Skip to content

Commit 5dd6e6f

Browse files
committed
Move the functionality to extract the debug location from a SILLocation
into SILLocation, since this will become useful for serialization. NFC <rdar://problem/22706994>
1 parent 0188d27 commit 5dd6e6f

File tree

4 files changed

+80
-71
lines changed

4 files changed

+80
-71
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ class SILLocation {
332332
return { getStartSourceLoc(), getEndSourceLoc() };
333333
}
334334

335+
typedef struct {
336+
unsigned Line = 0, Col = 0;
337+
const char *Filename = nullptr;
338+
} DebugLoc;
339+
340+
/// Extract the line, column, and filename.
341+
static DebugLoc decode(SourceLoc Loc, const SourceManager &SM);
342+
343+
/// Return the decoded debug location.
344+
DebugLoc decodeDebugLoc(const SourceManager &SM) const {
345+
return decode(getDebugSourceLoc(), SM);
346+
}
347+
335348
/// Pretty-print the value.
336349
void dump(const SourceManager &SM) const;
337350
void print(raw_ostream &OS, const SourceManager &SM) const;

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,21 @@ static const char *getFilenameFromDC(const DeclContext *DC) {
194194
return nullptr;
195195
}
196196

197-
198-
Location getDeserializedLoc(Pattern*) { return {}; }
199-
Location getDeserializedLoc(Expr*) { return {}; }
200-
Location getDeserializedLoc(Stmt*) { return {}; }
201-
Location getDeserializedLoc(Decl* D) {
202-
Location L = {};
197+
SILLocation::DebugLoc getDeserializedLoc(Pattern *) { return {}; }
198+
SILLocation::DebugLoc getDeserializedLoc(Expr *) { return {}; }
199+
SILLocation::DebugLoc getDeserializedLoc(Stmt *) { return {}; }
200+
SILLocation::DebugLoc getDeserializedLoc(Decl *D) {
201+
SILLocation::DebugLoc L;
203202
const DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
204-
if (const char *Filename = getFilenameFromDC(DC)) {
203+
if (const char *Filename = getFilenameFromDC(DC))
205204
L.Filename = Filename;
206-
L.Line = 0;
207-
}
208-
return L;
209-
}
210-
211-
Location getLoc(SourceManager &SM, SourceLoc Loc) {
212-
Location L = {};
213-
if (Loc.isValid()) {
214-
L.Filename = SM.getBufferIdentifierForLoc(Loc);
215-
std::tie(L.Line, L.Col) = SM.getLineAndColumn(Loc);
216-
}
217205
return L;
218206
}
219207

220208
/// Use the SM to figure out the actual line/column of a SourceLoc.
221209
template <typename WithLoc>
222-
Location getLoc(SourceManager &SM, WithLoc *S, bool End = false) {
223-
Location L = {};
210+
SILLocation::DebugLoc getDebugLoc(SourceManager &SM, WithLoc *S, bool End = false) {
211+
SILLocation::DebugLoc L;
224212
if (S == nullptr)
225213
return L;
226214

@@ -231,37 +219,38 @@ Location getLoc(SourceManager &SM, WithLoc *S, bool End = false) {
231219
// the module.
232220
return getDeserializedLoc(S);
233221

234-
return getLoc(SM, Loc);
222+
return SILLocation::decode(Loc, SM);
235223
}
236224

237-
/// \brief Return the start of the location's source range.
238-
static Location getStartLocation(SourceManager &SM,
239-
Optional<SILLocation> OptLoc) {
225+
/// Return the start of the location's source range.
226+
static SILLocation::DebugLoc getStartLocation(Optional<SILLocation> OptLoc,
227+
SourceManager &SM) {
240228
if (!OptLoc) return {};
241-
return getLoc(SM, OptLoc->getStartSourceLoc());
229+
return SILLocation::decode(OptLoc->getStartSourceLoc(), SM);
242230
}
243231

244-
/// \brief Return the debug location from a SILLocation.
245-
static Location getDebugLocation(SourceManager &SM,
246-
Optional<SILLocation> OptLoc) {
232+
/// Return the debug location from a SILLocation.
233+
static SILLocation::DebugLoc getDebugLocation(Optional<SILLocation> OptLoc,
234+
SourceManager &SM) {
247235
if (!OptLoc || OptLoc->isInPrologue())
248236
return {};
249-
return getLoc(SM, OptLoc->getDebugSourceLoc());
237+
return OptLoc->decodeDebugLoc(SM);
250238
}
251239

252240

253-
/// \brief Extract the start location from a SILLocation.
241+
/// Extract the start location from a SILLocation.
254242
///
255243
/// This returns a FullLocation, which contains the location that
256244
/// should be used for the linetable and the "true" AST location (used
257245
/// for, e.g., variable declarations).
258-
static FullLocation getLocation(SourceManager &SM,
259-
Optional<SILLocation> OptLoc) {
260-
if (!OptLoc) return {};
246+
static FullLocation getFullLocation(Optional<SILLocation> OptLoc,
247+
SourceManager &SM) {
248+
if (!OptLoc)
249+
return {};
261250

262251
SILLocation Loc = OptLoc.getValue();
263-
return { getLoc(SM, Loc.getDebugSourceLoc()),
264-
getLoc(SM, Loc.getSourceLoc())};
252+
return { Loc.decodeDebugLoc(SM),
253+
SILLocation::decode(Loc.getSourceLoc(), SM) };
265254
}
266255

267256
/// Determine whether this debug scope belongs to an explicit closure.
@@ -295,7 +284,7 @@ llvm::MDNode *IRGenDebugInfo::createInlinedAt(const SILDebugScope *DS) {
295284
// SIL scope to a location means skipping the inlined-at scope.
296285
auto *Parent = CS->Parent.get<const SILDebugScope *>();
297286
auto *ParentScope = getOrCreateScope(Parent);
298-
auto L = getLoc(SM, CS->Loc.getDebugSourceLoc());
287+
auto L = CS->Loc.decodeDebugLoc(SM);
299288
InlinedAt = llvm::DebugLoc::get(L.Line, L.Col, ParentScope, InlinedAt);
300289
}
301290
}
@@ -337,7 +326,7 @@ void IRGenDebugInfo::setCurrentLoc(IRBuilder &Builder, const SILDebugScope *DS,
337326
if (!Scope)
338327
return;
339328

340-
Location L = getDebugLocation(SM, Loc);
329+
auto L = getDebugLocation(Loc, SM);
341330
auto *File = getOrCreateFile(L.Filename);
342331
if (File->getFilename() != Scope->getFilename()) {
343332
// We changed files in the middle of a scope. This happens, for
@@ -425,7 +414,7 @@ llvm::DIScope *IRGenDebugInfo::getOrCreateScope(const SILDebugScope *DS) {
425414
return Parent;
426415

427416
assert(DS->Parent && "lexical block must have a parent subprogram");
428-
Location L = getStartLocation(SM, DS->Loc);
417+
auto L = getStartLocation(DS->Loc, SM);
429418
llvm::DIFile *File = getOrCreateFile(L.Filename);
430419
auto *DScope = DBuilder.createLexicalBlock(Parent, File, L.Line, L.Col);
431420

@@ -572,7 +561,7 @@ llvm::DIScope *IRGenDebugInfo::getOrCreateContext(DeclContext *DC) {
572561

573562
// Create a Forward-declared type.
574563
auto *TyDecl = cast<NominalTypeDecl>(DC);
575-
auto Loc = getLoc(SM, TyDecl);
564+
auto Loc = getDebugLoc(SM, TyDecl);
576565
auto File = getOrCreateFile(Loc.Filename);
577566
auto Line = Loc.Line;
578567
auto FwdDecl = DBuilder.createForwardDecl(
@@ -663,14 +652,14 @@ llvm::DISubprogram *IRGenDebugInfo::emitFunction(
663652
Name = getName(DS->Loc);
664653
}
665654

666-
Location L = {};
655+
SILLocation::DebugLoc L;
667656
unsigned ScopeLine = 0; /// The source line used for the function prologue.
668657
// Bare functions and thunks should not have any line numbers. This
669658
// is especially important for shared functions like reabstraction
670-
// thunk helpers, where getLocation() returns an arbitrary location
659+
// thunk helpers, where getFullLocation() returns an arbitrary location
671660
// of whichever use was emitted first.
672661
if (DS && (!SILFn || (!SILFn->isBare() && !SILFn->isThunk()))) {
673-
auto FL = getLocation(SM, DS->Loc);
662+
auto FL = getFullLocation(DS->Loc, SM);
674663
L = FL.Loc;
675664
ScopeLine = FL.LocForLinetable.Line;
676665
}
@@ -770,7 +759,7 @@ void IRGenDebugInfo::emitImport(ImportDecl *D) {
770759
return;
771760
}
772761
auto DIMod = getOrCreateModule({D->getModulePath(), M});
773-
Location L = getLoc(SM, D);
762+
auto L = getDebugLoc(SM, D);
774763
DBuilder.createImportedModule(getOrCreateFile(L.Filename), DIMod, L.Line);
775764
}
776765

@@ -979,7 +968,7 @@ void IRGenDebugInfo::emitVariableDeclaration(
979968

980969
auto *Scope = dyn_cast<llvm::DILocalScope>(getOrCreateScope(DS));
981970
assert(Scope && "variable has no local scope");
982-
Location Loc = getLoc(SM, DbgTy.getDecl());
971+
auto Loc = getDebugLoc(SM, DbgTy.getDecl());
983972

984973
// FIXME: this should be the scope of the type's declaration.
985974
// If this is an argument, attach it to the current function scope.
@@ -1113,7 +1102,7 @@ void IRGenDebugInfo::emitGlobalVariableDeclaration(llvm::GlobalValue *Var,
11131102
// would confuse both the user and LLDB.
11141103
return;
11151104

1116-
Location L = getStartLocation(SM, Loc);
1105+
auto L = getStartLocation(Loc, SM);
11171106
auto File = getOrCreateFile(L.Filename);
11181107

11191108
// Emit it as global variable of the current module.
@@ -1455,7 +1444,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
14551444
case TypeKind::Struct: {
14561445
auto *StructTy = BaseTy->castTo<StructType>();
14571446
auto *Decl = StructTy->getDecl();
1458-
Location L = getLoc(SM, Decl);
1447+
auto L = getDebugLoc(SM, Decl);
14591448
return createStructType(DbgTy, Decl, StructTy, Scope,
14601449
getOrCreateFile(L.Filename), L.Line, SizeInBits,
14611450
AlignInBits, Flags,
@@ -1469,7 +1458,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
14691458
// used to differentiate them from C++ and ObjC classes.
14701459
auto *ClassTy = BaseTy->castTo<ClassType>();
14711460
auto *Decl = ClassTy->getDecl();
1472-
Location L = getLoc(SM, Decl);
1461+
auto L = getDebugLoc(SM, Decl);
14731462
if (auto *ClangDecl = Decl->getClangDecl()) {
14741463
auto ClangSrcLoc = ClangDecl->getLocStart();
14751464
clang::SourceManager &ClangSM =
@@ -1506,7 +1495,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15061495
auto *ProtocolTy = BaseTy->castTo<ProtocolType>();
15071496
auto *Decl = ProtocolTy->getDecl();
15081497
// FIXME: (LLVM branch) This should probably be a DW_TAG_interface_type.
1509-
Location L = getLoc(SM, Decl);
1498+
auto L = getDebugLoc(SM, Decl);
15101499
auto File = getOrCreateFile(L.Filename);
15111500
return createPointerSizedStruct(Scope,
15121501
Decl ? Decl->getNameStr() : MangledName,
@@ -1515,7 +1504,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15151504

15161505
case TypeKind::ProtocolComposition: {
15171506
auto *Decl = DbgTy.getDecl();
1518-
Location L = getLoc(SM, Decl);
1507+
auto L = getDebugLoc(SM, Decl);
15191508
auto File = getOrCreateFile(L.Filename);
15201509

15211510
// FIXME: emit types
@@ -1528,7 +1517,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15281517
case TypeKind::UnboundGeneric: {
15291518
auto *UnboundTy = BaseTy->castTo<UnboundGenericType>();
15301519
auto *Decl = UnboundTy->getDecl();
1531-
Location L = getLoc(SM, Decl);
1520+
auto L = getDebugLoc(SM, Decl);
15321521
return createPointerSizedStruct(Scope,
15331522
Decl ? Decl->getNameStr() : MangledName,
15341523
File, L.Line, Flags, MangledName);
@@ -1537,7 +1526,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15371526
case TypeKind::BoundGenericStruct: {
15381527
auto *StructTy = BaseTy->castTo<BoundGenericStructType>();
15391528
auto *Decl = StructTy->getDecl();
1540-
Location L = getLoc(SM, Decl);
1529+
auto L = getDebugLoc(SM, Decl);
15411530
return createPointerSizedStruct(Scope,
15421531
Decl ? Decl->getNameStr() : MangledName,
15431532
File, L.Line, Flags, MangledName);
@@ -1546,7 +1535,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15461535
case TypeKind::BoundGenericClass: {
15471536
auto *ClassTy = BaseTy->castTo<BoundGenericClassType>();
15481537
auto *Decl = ClassTy->getDecl();
1549-
Location L = getLoc(SM, Decl);
1538+
auto L = getDebugLoc(SM, Decl);
15501539
// TODO: We may want to peek at Decl->isObjC() and set this
15511540
// attribute accordingly.
15521541
return createPointerSizedStruct(Scope,
@@ -1593,7 +1582,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
15931582

15941583
case TypeKind::Archetype: {
15951584
auto *Archetype = BaseTy->castTo<ArchetypeType>();
1596-
Location L = getLoc(SM, Archetype->getAssocType());
1585+
auto L = getDebugLoc(SM, Archetype->getAssocType());
15971586
auto Superclass = Archetype->getSuperclass();
15981587
auto DerivedFrom = Superclass.isNull()
15991588
? nullptr
@@ -1628,7 +1617,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
16281617
case TypeKind::Metatype: {
16291618
// Metatypes are (mostly) singleton type descriptors, often without storage.
16301619
Flags |= llvm::DINode::FlagArtificial;
1631-
Location L = getLoc(SM, DbgTy.getDecl());
1620+
auto L = getDebugLoc(SM, DbgTy.getDecl());
16321621
auto File = getOrCreateFile(L.Filename);
16331622
return DBuilder.createStructType(
16341623
Scope, MangledName, File, L.Line, SizeInBits, AlignInBits, Flags,
@@ -1680,15 +1669,15 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
16801669
case TypeKind::Enum: {
16811670
auto *EnumTy = BaseTy->castTo<EnumType>();
16821671
auto *Decl = EnumTy->getDecl();
1683-
Location L = getLoc(SM, Decl);
1672+
auto L = getDebugLoc(SM, Decl);
16841673
return createEnumType(DbgTy, Decl, MangledName, Scope,
16851674
getOrCreateFile(L.Filename), L.Line, Flags);
16861675
}
16871676

16881677
case TypeKind::BoundGenericEnum: {
16891678
auto *EnumTy = BaseTy->castTo<BoundGenericEnumType>();
16901679
auto *Decl = EnumTy->getDecl();
1691-
Location L = getLoc(SM, Decl);
1680+
auto L = getDebugLoc(SM, Decl);
16921681
return createEnumType(DbgTy, Decl, MangledName, Scope,
16931682
getOrCreateFile(L.Filename), L.Line, Flags);
16941683
}
@@ -1711,7 +1700,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
17111700
case TypeKind::WeakStorage: {
17121701
auto *ReferenceTy = cast<ReferenceStorageType>(BaseTy);
17131702
auto CanTy = ReferenceTy->getReferentType();
1714-
Location L = getLoc(SM, DbgTy.getDecl());
1703+
auto L = getDebugLoc(SM, DbgTy.getDecl());
17151704
auto File = getOrCreateFile(L.Filename);
17161705
return DBuilder.createTypedef(getOrCreateDesugaredType(CanTy, DbgTy),
17171706
MangledName, File, L.Line, File);
@@ -1723,7 +1712,7 @@ llvm::DIType *IRGenDebugInfo::createType(DebugTypeInfo DbgTy,
17231712

17241713
auto *NameAliasTy = cast<NameAliasType>(BaseTy);
17251714
auto *Decl = NameAliasTy->getDecl();
1726-
Location L = getLoc(SM, Decl);
1715+
auto L = getDebugLoc(SM, Decl);
17271716
auto AliasedTy = Decl->getUnderlyingType();
17281717
auto File = getOrCreateFile(L.Filename);
17291718
// For NameAlias types, the DeclContext for the aliasED type is

lib/IRGen/IRGenDebugInfo.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ namespace irgen {
5757

5858
class IRGenFunction;
5959

60-
typedef struct {
61-
unsigned Line, Col;
62-
const char *Filename;
63-
} Location;
64-
65-
typedef struct { Location LocForLinetable, Loc; } FullLocation;
60+
typedef struct { SILLocation::DebugLoc LocForLinetable, Loc; } FullLocation;
6661

6762
typedef llvm::DenseMap<const llvm::MDString *, llvm::TrackingMDNodeRef>
6863
TrackingDIRefMap;
@@ -91,25 +86,26 @@ class IRGenDebugInfo {
9186
llvm::SmallPtrSet<const llvm::DIType *, 16> IndirectEnumCases;
9287

9388
llvm::BumpPtrAllocator DebugInfoNames;
94-
StringRef CWDName; /// The current working directory.
89+
StringRef CWDName; /// The current working directory.
9590
llvm::DICompileUnit *TheCU = nullptr; /// The current compilation unit.
9691
llvm::DIFile *MainFile = nullptr; /// The main file.
9792
llvm::DIModule *MainModule = nullptr; /// The current module.
98-
llvm::MDNode *EntryPointFn; /// Scope of SWIFT_ENTRY_POINT_FUNCTION.
99-
TypeAliasDecl *MetadataTypeDecl; /// The type decl for swift.type.
93+
llvm::MDNode *EntryPointFn; /// Scope of SWIFT_ENTRY_POINT_FUNCTION.
94+
TypeAliasDecl *MetadataTypeDecl; /// The type decl for swift.type.
10095
llvm::DIType *InternalType; /// Catch-all type for opaque internal types.
10196

102-
Location LastDebugLoc; /// The last location that was emitted.
103-
const SILDebugScope *LastScope; /// The scope of that last location.
104-
bool IsLibrary; /// Whether this is a library or a top level module.
97+
SILLocation::DebugLoc LastDebugLoc; /// The last location that was emitted.
98+
const SILDebugScope *LastScope; /// The scope of that last location.
99+
bool IsLibrary; /// Whether this is a library or a top level module.
105100
#ifndef NDEBUG
106101
/// The basic block where the location was last changed.
107102
llvm::BasicBlock *LastBasicBlock;
108103
bool lineNumberIsSane(IRBuilder &Builder, unsigned Line);
109104
#endif
110105

111106
/// Used by pushLoc.
112-
SmallVector<std::pair<Location, const SILDebugScope *>, 8> LocationStack;
107+
SmallVector<std::pair<SILLocation::DebugLoc, const SILDebugScope *>, 8>
108+
LocationStack;
113109

114110
public:
115111
IRGenDebugInfo(const IRGenOptions &Opts, ClangImporter &CI, IRGenModule &IGM,

lib/SIL/SILLocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "swift/AST/Expr.h"
1616
#include "swift/AST/Pattern.h"
1717
#include "swift/AST/Stmt.h"
18+
#include "swift/Basic/SourceManager.h"
1819
#include "llvm/Support/raw_ostream.h"
1920

2021
using namespace swift;
@@ -119,6 +120,16 @@ SourceLoc SILLocation::getEndSourceLoc(ASTNodeTy N) const {
119120
llvm_unreachable("impossible SILLocation");
120121
}
121122

123+
SILLocation::DebugLoc SILLocation::decode(SourceLoc Loc,
124+
const SourceManager &SM) {
125+
DebugLoc DL;
126+
if (Loc.isValid()) {
127+
DL.Filename = SM.getBufferIdentifierForLoc(Loc);
128+
std::tie(DL.Line, DL.Col) = SM.getLineAndColumn(Loc);
129+
}
130+
return DL;
131+
}
132+
122133
void SILLocation::dump(const SourceManager &SM) const {
123134
if (auto D = ASTNode.dyn_cast<Decl *>())
124135
llvm::errs() << Decl::getKindName(D->getKind()) << "Decl @ ";

0 commit comments

Comments
 (0)