Skip to content

Commit 1f927d9

Browse files
committed
Overhaul the handling of return locations for auto-generated code.
- change SILGenFunction to use Cleanup and Implicit return locations for auto-generated cleanups/returns where sensible. - Fix a bug in where ConstructorDecl that would return the wrong source range. - Move the expected locations of some errors to the end of the function where they should belong. Fixes <rdar://problem/15609768> Line tables for classes that don't have init but just initialize ivars are odd Swift SVN r11086
1 parent 0fb0dff commit 1f927d9

File tree

7 files changed

+191
-77
lines changed

7 files changed

+191
-77
lines changed

include/swift/SIL/SILInstruction.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class SILInstruction : public ValueBase,public llvm::ilist_node<SILInstruction>{
104104

105105
SILLocation getLoc() const { return Loc; }
106106
SILDebugScope *getDebugScope() const { return DebugScope; }
107-
void setDebugScope(SILDebugScope *DS) { DebugScope = DS; }
107+
SILInstruction *setDebugScope(SILDebugScope *DS) { DebugScope = DS; return this; }
108108

109109
/// removeFromParent - This method unlinks 'self' from the containing basic
110110
/// block, but does not delete it.

include/swift/SIL/SILLocation.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ class SILLocation {
131131
return cast<T>(Node.get<typename base_type<T>::type*>());
132132
}
133133

134-
/// \brief Check if the corresponding source code location definitely points
135-
/// to the start of the AST node.
136-
bool alwaysPointsToStart() const { return KindData & (1 << PointsToStartBit);}
137-
138-
/// \brief Check if the corresponding source code location definitely points
139-
/// to the end of the AST node.
140-
bool alwaysPointsToEnd() const { return KindData & (1 << PointsToEndBit); }
141-
142134
// SILLocation constructors.
143135
SILLocation(LocationKind K) : KindData(K) {}
144136
SILLocation(Stmt *S, LocationKind K) : ASTNode(S), KindData(K) {}
@@ -208,6 +200,14 @@ class SILLocation {
208200

209201
bool hasASTLocation() const { return !ASTNode.isNull(); }
210202

203+
/// \brief Check if the corresponding source code location definitely points
204+
/// to the start of the AST node.
205+
bool alwaysPointsToStart() const { return KindData & (1 << PointsToStartBit);}
206+
207+
/// \brief Check if the corresponding source code location definitely points
208+
/// to the end of the AST node.
209+
bool alwaysPointsToEnd() const { return KindData & (1 << PointsToEndBit); }
210+
211211
LocationKind getKind() const { return (LocationKind)(KindData & BaseMask); }
212212

213213
template <typename T>
@@ -340,21 +340,20 @@ class ImplicitReturnLocation : public SILLocation {
340340
public:
341341

342342
ImplicitReturnLocation(AbstractClosureExpr *E)
343-
: SILLocation(E, ImplicitReturnKind) { pointToEnd(); }
343+
: SILLocation(E, ImplicitReturnKind) { }
344344

345345
ImplicitReturnLocation(AbstractFunctionDecl *AFD)
346-
: SILLocation(AFD, ImplicitReturnKind) { pointToEnd(); }
346+
: SILLocation(AFD, ImplicitReturnKind) { }
347347

348348
/// \brief Construct from a RegularLocation; preserve all special bits.
349349
///
350350
/// Note, this can construct an implicit return for an arbitrary expression
351351
/// (specifically, in case of auto-generated bodies).
352352
static SILLocation getImplicitReturnLoc(SILLocation L) {
353353
assert(L.isASTNode<Expr>() ||
354-
L.isASTNode<AbstractFunctionDecl>() ||
354+
L.isASTNode<ValueDecl>() ||
355355
(L.isNull() && L.isInTopLevel()));
356356
L.setKind(ImplicitReturnKind);
357-
L.pointToEnd();
358357
return L;
359358
}
360359

@@ -446,7 +445,7 @@ class MandatoryInlinedLocation : public SILLocation {
446445
/// \brief Used on the instruction performing auto-generated cleanup such as
447446
/// deallocs, destructor calls.
448447
///
449-
/// The cleanups are performed after completing the evaluztion of the AST Node
448+
/// The cleanups are performed after completing the evaluation of the AST Node
450449
/// wrapped inside the SILLocation. This location wraps the statement
451450
/// representing the enclosing scope, for example, FuncDecl, ParenExpr. The
452451
/// scope's end location points to the SourceLoc that shows when the operation

lib/AST/Decl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,9 @@ SourceRange ConstructorDecl::getSourceRange() const {
15251525
const DeclContext *DC = getDeclContext();
15261526
switch (DC->getContextKind()) {
15271527
case DeclContextKind::ExtensionDecl:
1528-
return cast<ExtensionDecl>(DC)->getLoc();
1528+
return cast<ExtensionDecl>(DC)->getSourceRange();
15291529
case DeclContextKind::NominalTypeDecl:
1530-
return cast<NominalTypeDecl>(DC)->getLoc();
1530+
return cast<NominalTypeDecl>(DC)->getSourceRange();
15311531
default:
15321532
if (isInvalid())
15331533
return getConstructorLoc();

lib/IRGen/IRGenDebugInfo.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ static FullLocation getLocation(SourceManager &SM, Optional<SILLocation> OptLoc)
189189
return {};
190190

191191
SILLocation Loc = OptLoc.getValue();
192+
bool UseEnd = Loc.alwaysPointsToEnd();
192193

193194
if (Loc.isNull()) return {};
194195
if (Expr *E = Loc.getAsASTNode<Expr>()) {
@@ -198,29 +199,29 @@ static FullLocation getLocation(SourceManager &SM, Optional<SILLocation> OptLoc)
198199
// on how we decide to resolve rdar://problem/14627460, we may
199200
// want to use the regular getLoc instead and rather use the
200201
// column info.
201-
auto ELoc = getLoc(SM, E);
202+
auto ELoc = getLoc(SM, E, UseEnd);
202203
if (isa<AutoClosureExpr>(E))
203204
return {{}, ELoc};
204205
return {ELoc, ELoc};
205206
}
206207
if (Pattern* P = Loc.getAsASTNode<Pattern>()) {
207-
auto PLoc = getLoc(SM, P);
208+
auto PLoc = getLoc(SM, P, UseEnd);
208209
return {PLoc, PLoc};
209210
}
210211
if (Stmt* S = Loc.getAsASTNode<Stmt>()) {
211-
auto SLoc = getLoc(SM, S);
212+
auto SLoc = getLoc(SM, S, UseEnd);
212213
return {SLoc, SLoc};
213214
}
214215

215216
if (Decl* D = Loc.getAsASTNode<Decl>()) {
216-
auto DLoc = getLoc(SM, D);
217+
auto DLoc = getLoc(SM, D, UseEnd);
217218
auto LinetableLoc = DLoc;
218219
if (Loc.isInPrologue())
219220
LinetableLoc = {};
220221

221-
// FIXME: It may or may not be better to fix this in SILLocation.
222-
// If this is an implicit return, we want the end of the
223-
// AbstractFunctionDecl's body.
222+
// For return and cleanup code SILLocation points to the start,
223+
// because this is where we want diagnostics to appear. For the
224+
// line table, we want them to point to the end of the Decl.
224225
else if (Loc.getKind() == SILLocation::ImplicitReturnKind ||
225226
Loc.getKind() == SILLocation::CleanupKind)
226227
LinetableLoc = getLoc(SM, D, true);

0 commit comments

Comments
 (0)