Skip to content

Commit d96ef3b

Browse files
committed
Swift SIL: add some APIs to Location
* `var description` * `func ==` * `func hasSameSourceLocation(as:)`
1 parent 8d90daf commit d96ef3b

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
@@ -337,8 +337,11 @@ llvm::StringRef SILType_getNominalFieldName(BridgedType type, SwiftInt index);
337337
SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
338338
llvm::StringRef caseName);
339339

340+
std::string SILLocation_debugDescription(swift::SILDebugLocation loc);
340341
swift::SILDebugLocation
341342
SILLocation_getAutogeneratedLocation(swift::SILDebugLocation loc);
343+
bool SILLocation_equal(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
344+
bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
342345

343346
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
344347
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
@@ -623,12 +623,39 @@ SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
623623
// SILLocation
624624
//===----------------------------------------------------------------------===//
625625

626+
std::string SILLocation_debugDescription(swift::SILDebugLocation dloc) {
627+
std::string str;
628+
llvm::raw_string_ostream os(str);
629+
SILLocation loc = dloc.getLocation();
630+
loc.print(os);
631+
#ifndef NDEBUG
632+
if (const SILDebugScope *scope = dloc.getScope()) {
633+
if (DeclContext *dc = loc.getAsDeclContext()) {
634+
os << ", scope=";
635+
scope->print(dc->getASTContext().SourceMgr, os, /*indent*/ 2);
636+
} else {
637+
os << ", scope=?";
638+
}
639+
}
640+
#endif
641+
return str;
642+
}
643+
626644
SILDebugLocation SILLocation_getAutogeneratedLocation(SILDebugLocation loc) {
627645
SILDebugLocation autoGenLoc(RegularLocation::getAutoGeneratedLocation(),
628646
loc.getScope());
629647
return autoGenLoc;
630648
}
631649

650+
bool SILLocation_equal(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs) {
651+
return lhs.getLocation() == rhs.getLocation() && lhs.getScope() == rhs.getScope();
652+
}
653+
654+
bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs) {
655+
return lhs.getLocation().hasSameSourceLocation(rhs.getLocation()) &&
656+
lhs.getScope() == rhs.getScope();
657+
}
658+
632659
//===----------------------------------------------------------------------===//
633660
// SILGlobalVariable
634661
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)