Skip to content

Commit a5d7a09

Browse files
committed
[sil] Add the ability to create an ExtendedASTNodeLoc that is only used for diagnostics and not for debugging.
We already have entry points for creating an ExtendedASTNodeLoc with an empty location for diagnostics and a location for debugging. This does the inverse so one can emit diagnostics that point at a terminator sourceloc without needing to add a ReturnInst to the instruction which is illegal and would cause the SILVerifier to assert.
1 parent 151d275 commit a5d7a09

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ class SILLocation {
267267

268268
SILLocation(ExtendedASTNodeLoc *ext, LocationKind K)
269269
: storage(ext), kindAndFlags(K, ExtendedASTNodeKind) {
270-
assert(ext && !ext->forDebugging.isNull());
271270
}
272271

273272
SILLocation(SourceLoc L, LocationKind K)
@@ -470,7 +469,8 @@ class RegularLocation : public SILLocation {
470469
RegularLocation(Decl *D) : SILLocation(ASTNodeTy(D), RegularKind) {}
471470
RegularLocation(Pattern *P) : SILLocation(ASTNodeTy(P), RegularKind) {}
472471
RegularLocation(Stmt *S, Pattern *P, SILModule &Module);
473-
RegularLocation(SILLocation ForDebuggingOnly, SILModule &Module);
472+
RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly, SILModule &Module,
473+
bool isForDebugOnly = true);
474474
RegularLocation(SourceLoc L) : SILLocation(L, RegularKind) {}
475475
RegularLocation(FilenameAndLocation *filePos)
476476
: SILLocation(filePos, RegularKind) {}
@@ -510,6 +510,15 @@ class RegularLocation : public SILLocation {
510510
return getAutoGeneratedLocation(L);
511511
}
512512

513+
/// Returns a location that uses L for diagnostics but is otherwise an auto
514+
/// generated location.
515+
static RegularLocation getDiagnosticsOnlyLocation(SILLocation L,
516+
SILModule &M) {
517+
if (L.isASTNode())
518+
return RegularLocation(L, M, false /*is for debug only*/);
519+
return getAutoGeneratedLocation(L);
520+
}
521+
513522
static bool isKind(const SILLocation& L) {
514523
return L.getKind() == RegularKind;
515524
}
@@ -518,6 +527,8 @@ class RegularLocation : public SILLocation {
518527
RegularLocation() : SILLocation(RegularKind) {}
519528
static SILLocation::ExtendedASTNodeLoc *
520529
getDebugOnlyExtendedASTNodeLoc(SILLocation L, SILModule &Module);
530+
static SILLocation::ExtendedASTNodeLoc *
531+
getDiagnosticOnlyExtendedASTNodeLoc(SILLocation L, SILModule &Module);
521532
};
522533

523534
/// Used to represent a return instruction in user code.

lib/SIL/IR/SILLocation.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,25 @@ RegularLocation::getDebugOnlyExtendedASTNodeLoc(SILLocation L,
235235
return new (Module) ExtendedASTNodeLoc((Decl *)nullptr, P);
236236
}
237237

238-
RegularLocation::RegularLocation(SILLocation ForDebuggingOnly,
239-
SILModule &Module)
240-
: SILLocation(getDebugOnlyExtendedASTNodeLoc(ForDebuggingOnly, Module),
238+
SILLocation::ExtendedASTNodeLoc *
239+
RegularLocation::getDiagnosticOnlyExtendedASTNodeLoc(SILLocation L,
240+
SILModule &Module) {
241+
if (auto D = L.getAsASTNode<Decl>())
242+
return new (Module) ExtendedASTNodeLoc(D, (Decl *)nullptr);
243+
if (auto E = L.getAsASTNode<Expr>())
244+
return new (Module) ExtendedASTNodeLoc(E, (Decl *)nullptr);
245+
if (auto S = L.getAsASTNode<Stmt>())
246+
return new (Module) ExtendedASTNodeLoc(S, (Decl *)nullptr);
247+
auto P = L.getAsASTNode<Pattern>();
248+
return new (Module) ExtendedASTNodeLoc(P, (Decl *)nullptr);
249+
}
250+
251+
RegularLocation::RegularLocation(SILLocation ForDebuggingOrDiagnosticsOnly,
252+
SILModule &Module, bool isForDebugOnly)
253+
: SILLocation(isForDebugOnly ? getDebugOnlyExtendedASTNodeLoc(
254+
ForDebuggingOrDiagnosticsOnly, Module)
255+
: getDiagnosticOnlyExtendedASTNodeLoc(
256+
ForDebuggingOrDiagnosticsOnly, Module),
241257
RegularKind) {
242258
markAutoGenerated();
243259
}

0 commit comments

Comments
 (0)