Skip to content

Commit 7cb1302

Browse files
committed
[Scope map/parser/AST] Miscellaneous cleanups to avoid producing invalid source ranges.
The scope map relies fairly deeply on having reasonable source ranges for AST nodes. Fix the construction and query of source ranges in a few places throughout the parser and AST to provide stronger invariants.
1 parent 86ef6d6 commit 7cb1302

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

lib/AST/ASTScope.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static bool hasAccessors(AbstractStorageDecl *asd) {
9090
case AbstractStorageDecl::ComputedWithMutableAddress:
9191
case AbstractStorageDecl::InheritedWithObservers:
9292
case AbstractStorageDecl::StoredWithObservers:
93-
return true;
93+
return asd->getBracesRange().isValid();
9494

9595
case AbstractStorageDecl::Stored:
9696
case AbstractStorageDecl::StoredWithTrivialAccessors:
@@ -1754,7 +1754,8 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
17541754
break;
17551755

17561756
case ASTScopeKind::LocalDeclaration:
1757-
result.push_back(cast<ValueDecl>(localDeclaration));
1757+
if (auto value = dyn_cast<ValueDecl>(localDeclaration))
1758+
result.push_back(value);
17581759
break;
17591760

17601761
case ASTScopeKind::ConditionalClause:
@@ -1776,8 +1777,10 @@ SmallVector<ValueDecl *, 4> ASTScope::getLocalBindings() const {
17761777
break;
17771778

17781779
case ASTScopeKind::ForStmtInitializer:
1779-
for (auto decl : forStmt->getInitializerVarDecls())
1780-
result.push_back(cast<ValueDecl>(decl));
1780+
for (auto decl : forStmt->getInitializerVarDecls()) {
1781+
if (auto value = dyn_cast<ValueDecl>(decl))
1782+
result.push_back(value);
1783+
}
17811784
break;
17821785

17831786
case ASTScopeKind::PatternInitializer:

lib/AST/Decl.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,38 +3832,41 @@ ParamDecl *ParamDecl::createSelf(SourceLoc loc, DeclContext *DC,
38323832

38333833
/// Return the full source range of this parameter.
38343834
SourceRange ParamDecl::getSourceRange() const {
3835-
SourceRange range;
3836-
38373835
SourceLoc APINameLoc = getArgumentNameLoc();
38383836
SourceLoc nameLoc = getNameLoc();
3839-
3840-
if (APINameLoc.isValid() && nameLoc.isInvalid())
3841-
range = APINameLoc;
3842-
else if (APINameLoc.isInvalid() && nameLoc.isValid())
3843-
range = nameLoc;
3844-
else
3845-
range = SourceRange(APINameLoc, nameLoc);
3846-
3847-
if (range.isInvalid()) return range;
3848-
3837+
3838+
SourceLoc startLoc;
3839+
if (APINameLoc.isValid())
3840+
startLoc = APINameLoc;
3841+
else if (nameLoc.isValid())
3842+
startLoc = nameLoc;
3843+
else {
3844+
startLoc = getTypeLoc().getSourceRange().Start;
3845+
}
3846+
if (startLoc.isInvalid())
3847+
return SourceRange();
3848+
38493849
// It would be nice to extend the front of the range to show where inout is,
38503850
// but we don't have that location info. Extend the back of the range to the
38513851
// location of the default argument, or the typeloc if they are valid.
38523852
if (auto expr = getDefaultValue()) {
38533853
auto endLoc = expr->getEndLoc();
38543854
if (endLoc.isValid())
3855-
return SourceRange(range.Start, endLoc);
3855+
return SourceRange(startLoc, endLoc);
38563856
}
38573857

38583858
// If the typeloc has a valid location, use it to end the range.
38593859
if (auto typeRepr = getTypeLoc().getTypeRepr()) {
38603860
auto endLoc = typeRepr->getEndLoc();
38613861
if (endLoc.isValid() && !isTypeLocImplicit())
3862-
return SourceRange(range.Start, endLoc);
3862+
return SourceRange(startLoc, endLoc);
38633863
}
3864-
3865-
// Otherwise, just return the info we have about the parameter.
3866-
return range;
3864+
3865+
// The name has a location we can use.
3866+
if (nameLoc.isValid())
3867+
return SourceRange(startLoc, nameLoc);
3868+
3869+
return startLoc;
38673870
}
38683871

38693872
Type ParamDecl::getVarargBaseTy(Type VarArgT) {

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,15 +3421,6 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags, ParameterList *Indices,
34213421
// an implicit fallthrough off the end.
34223422
if (Tok.is(tok::r_brace)) {
34233423
diagnose(Tok, diag::computed_property_no_accessors);
3424-
auto *TheDecl = createAccessorFunc(VarLBLoc, nullptr, TypeLoc(), nullptr,
3425-
SourceLoc(), Flags,
3426-
AccessorKind::IsGetter,
3427-
AddressorKind::NotAddressor, this,
3428-
SourceLoc());
3429-
3430-
TheDecl->setBody(BraceStmt::create(Context, VarLBLoc, {}, Tok.getLoc()));
3431-
TheDecl->setInvalid();
3432-
Decls.push_back(TheDecl);
34333424
return true;
34343425
}
34353426

lib/Parse/ParseStmt.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,11 +2017,10 @@ ParserResult<CatchStmt> Parser::parseStmtCatch() {
20172017
return makeParserCodeCompletionResult<CatchStmt>();
20182018
}
20192019

2020-
SourceLoc startOfBody = Tok.getLoc();
20212020
auto bodyResult = parseBraceItemList(diag::expected_lbrace_after_catch);
20222021
status |= bodyResult;
20232022
if (bodyResult.isNull()) {
2024-
bodyResult = makeParserErrorResult(BraceStmt::create(Context, startOfBody,
2023+
bodyResult = makeParserErrorResult(BraceStmt::create(Context, PreviousLoc,
20252024
{}, PreviousLoc,
20262025
/*implicit=*/ true));
20272026
}
@@ -2330,7 +2329,7 @@ ParserResult<Stmt> Parser::parseStmtForCStyle(SourceLoc ForLoc,
23302329
Status |= Body;
23312330
if (Body.isNull())
23322331
Body = makeParserResult(
2333-
Body, BraceStmt::create(Context, ForLoc, {}, PreviousLoc, true));
2332+
Body, BraceStmt::create(Context, PreviousLoc, {}, PreviousLoc, true));
23342333

23352334
return makeParserResult(
23362335
Status,

0 commit comments

Comments
 (0)