Skip to content

Commit 2389ed0

Browse files
eecksteinxedin
authored andcommitted
Swift SIL: add some APIs to Location
* `var description` * `func ==` * `func hasSameSourceLocation(as:)` (cherry picked from commit d96ef3b)
1 parent e239441 commit 2389ed0

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

SwiftCompilerSources/Sources/SIL/Location.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@
1212

1313
import SILBridging
1414

15-
public struct Location {
15+
public struct Location: Equatable, CustomStringConvertible {
1616
let bridged: swift.SILDebugLocation
17-
17+
18+
public var description: String {
19+
let stdString = SILLocation_debugDescription(bridged)
20+
return String(_cxxString: stdString)
21+
}
22+
1823
/// Keeps the debug scope but marks it as auto-generated.
1924
public var autoGenerated: Location {
2025
Location(bridged: SILLocation_getAutogeneratedLocation(bridged))
2126
}
27+
28+
public static func ==(lhs: Location, rhs: Location) -> Bool {
29+
SILLocation_equal(lhs.bridged, rhs.bridged)
30+
}
31+
32+
public func hasSameSourceLocation(as other: Location) -> Bool {
33+
SILLocation_hasSameSourceLocation(bridged, other.bridged)
34+
}
2235
}

include/swift/SIL/SILBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ llvm::StringRef SILType_getNominalFieldName(BridgedType type, SwiftInt index);
331331
SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
332332
llvm::StringRef caseName);
333333

334+
std::string SILLocation_debugDescription(swift::SILDebugLocation loc);
334335
swift::SILDebugLocation
335336
SILLocation_getAutogeneratedLocation(swift::SILDebugLocation loc);
337+
bool SILLocation_equal(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
338+
bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
336339

337340
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
338341
BridgedArgumentConvention SILArgument_getConvention(BridgedArgument argument);

include/swift/SIL/SILLocation.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class SILLocation {
427427
/// Pretty-print the value.
428428
void dump() const;
429429
void print(raw_ostream &OS, const SourceManager &SM) const;
430+
void print(raw_ostream &OS) const;
430431

431432
inline bool operator==(const SILLocation& R) const {
432433
return kindAndFlags.packedKindAndFlags == R.kindAndFlags.packedKindAndFlags
@@ -435,6 +436,15 @@ class SILLocation {
435436

436437
inline bool operator!=(const SILLocation &R) const { return !(*this == R); }
437438

439+
bool hasSameSourceLocation(const SILLocation &rhs) {
440+
if (*this == rhs)
441+
return true;
442+
if (isASTNode() && rhs.isASTNode()) {
443+
return getSourceLoc(getPrimaryASTNode()) == rhs.getSourceLoc(rhs.getPrimaryASTNode());
444+
}
445+
return false;
446+
}
447+
438448
friend llvm::hash_code hash_value(const SILLocation &);
439449
};
440450

lib/SIL/IR/SILLocation.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ SILLocation::FilenameAndLocation *SILLocation::getCompilerGeneratedLoc() {
151151
return &compilerGenerated;
152152
}
153153

154-
static void dumpSourceLoc(SourceLoc loc) {
154+
static void printSourceLoc(SourceLoc loc, raw_ostream &OS) {
155155
if (!loc.isValid()) {
156-
llvm::dbgs() << "<invalid loc>";
156+
OS << "<invalid loc>";
157157
return;
158158
}
159159
const char *srcPtr = (const char *)loc.getOpaquePointerValue();
160160
unsigned len = strnlen(srcPtr, 20);
161161
if (len < 20) {
162-
llvm::dbgs() << '"' << StringRef(srcPtr, len) << '"';
162+
OS << '"' << StringRef(srcPtr, len) << '"';
163163
} else {
164-
llvm::dbgs() << '"' << StringRef(srcPtr, 20) << "[...]\"";
164+
OS << '"' << StringRef(srcPtr, 20) << "[...]\"";
165165
}
166166
}
167167

@@ -182,7 +182,7 @@ void SILLocation::dump() const {
182182
if (isFilenameAndLocation()) {
183183
getFilenameAndLocation()->dump();
184184
} else {
185-
dumpSourceLoc(getSourceLoc());
185+
printSourceLoc(getSourceLoc(), llvm::dbgs());
186186
}
187187

188188
if (isAutoGenerated()) llvm::dbgs() << ":auto";
@@ -191,7 +191,7 @@ void SILLocation::dump() const {
191191
if (isSILFile()) llvm::dbgs() << ":sil";
192192
if (hasASTNodeForDebugging()) {
193193
llvm::dbgs() << ":debug[";
194-
dumpSourceLoc(getSourceLocForDebugging());
194+
printSourceLoc(getSourceLocForDebugging(), llvm::dbgs());
195195
llvm::dbgs() << "]\n";
196196
}
197197
}
@@ -206,6 +206,18 @@ void SILLocation::print(raw_ostream &OS, const SourceManager &SM) const {
206206
}
207207
}
208208

209+
void SILLocation::print(raw_ostream &OS) const {
210+
if (isNull()) {
211+
OS << "<no loc>";
212+
} else if (isFilenameAndLocation()) {
213+
getFilenameAndLocation()->print(OS);
214+
} else if (DeclContext *dc = getAsDeclContext()){
215+
getSourceLoc().print(OS, dc->getASTContext().SourceMgr);
216+
} else {
217+
printSourceLoc(getSourceLoc(), OS);
218+
}
219+
}
220+
209221
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module) :
210222
SILLocation(new (Module) ExtendedASTNodeLoc(S, P), RegularKind) {}
211223

lib/SIL/Utils/SILBridging.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,39 @@ SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
607607
// SILLocation
608608
//===----------------------------------------------------------------------===//
609609

610+
std::string SILLocation_debugDescription(swift::SILDebugLocation dloc) {
611+
std::string str;
612+
llvm::raw_string_ostream os(str);
613+
SILLocation loc = dloc.getLocation();
614+
loc.print(os);
615+
#ifndef NDEBUG
616+
if (const SILDebugScope *scope = dloc.getScope()) {
617+
if (DeclContext *dc = loc.getAsDeclContext()) {
618+
os << ", scope=";
619+
scope->print(dc->getASTContext().SourceMgr, os, /*indent*/ 2);
620+
} else {
621+
os << ", scope=?";
622+
}
623+
}
624+
#endif
625+
return str;
626+
}
627+
610628
SILDebugLocation SILLocation_getAutogeneratedLocation(SILDebugLocation loc) {
611629
SILDebugLocation autoGenLoc(RegularLocation::getAutoGeneratedLocation(),
612630
loc.getScope());
613631
return autoGenLoc;
614632
}
615633

634+
bool SILLocation_equal(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs) {
635+
return lhs.getLocation() == rhs.getLocation() && lhs.getScope() == rhs.getScope();
636+
}
637+
638+
bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs) {
639+
return lhs.getLocation().hasSameSourceLocation(rhs.getLocation()) &&
640+
lhs.getScope() == rhs.getScope();
641+
}
642+
616643
//===----------------------------------------------------------------------===//
617644
// SILGlobalVariable
618645
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)