Swift SIL: add some APIs to Location

* `var description`
* `func ==`
* `func hasSameSourceLocation(as:)`
This commit is contained in:
Erik Eckstein
2023-01-16 15:40:51 +01:00
parent 8d90dafb22
commit d96ef3bedd
5 changed files with 73 additions and 8 deletions

View File

@@ -151,17 +151,17 @@ SILLocation::FilenameAndLocation *SILLocation::getCompilerGeneratedLoc() {
return &compilerGenerated;
}
static void dumpSourceLoc(SourceLoc loc) {
static void printSourceLoc(SourceLoc loc, raw_ostream &OS) {
if (!loc.isValid()) {
llvm::dbgs() << "<invalid loc>";
OS << "<invalid loc>";
return;
}
const char *srcPtr = (const char *)loc.getOpaquePointerValue();
unsigned len = strnlen(srcPtr, 20);
if (len < 20) {
llvm::dbgs() << '"' << StringRef(srcPtr, len) << '"';
OS << '"' << StringRef(srcPtr, len) << '"';
} else {
llvm::dbgs() << '"' << StringRef(srcPtr, 20) << "[...]\"";
OS << '"' << StringRef(srcPtr, 20) << "[...]\"";
}
}
@@ -182,7 +182,7 @@ void SILLocation::dump() const {
if (isFilenameAndLocation()) {
getFilenameAndLocation()->dump();
} else {
dumpSourceLoc(getSourceLoc());
printSourceLoc(getSourceLoc(), llvm::dbgs());
}
if (isAutoGenerated()) llvm::dbgs() << ":auto";
@@ -191,7 +191,7 @@ void SILLocation::dump() const {
if (isSILFile()) llvm::dbgs() << ":sil";
if (hasASTNodeForDebugging()) {
llvm::dbgs() << ":debug[";
dumpSourceLoc(getSourceLocForDebugging());
printSourceLoc(getSourceLocForDebugging(), llvm::dbgs());
llvm::dbgs() << "]\n";
}
}
@@ -206,6 +206,18 @@ void SILLocation::print(raw_ostream &OS, const SourceManager &SM) const {
}
}
void SILLocation::print(raw_ostream &OS) const {
if (isNull()) {
OS << "<no loc>";
} else if (isFilenameAndLocation()) {
getFilenameAndLocation()->print(OS);
} else if (DeclContext *dc = getAsDeclContext()){
getSourceLoc().print(OS, dc->getASTContext().SourceMgr);
} else {
printSourceLoc(getSourceLoc(), OS);
}
}
RegularLocation::RegularLocation(Stmt *S, Pattern *P, SILModule &Module) :
SILLocation(new (Module) ExtendedASTNodeLoc(S, P), RegularKind) {}