Skip to content

Commit 5d91635

Browse files
committed
[SourceKit] Fix crash in syntax model
The end location of an attribute used to point to the next token after the attribute's content, which is the closing parenthesis in valid Swift code. But when the parenthesis is missing, it points to the next token, which is most likely no longer part of the attribute. Fix by parsting the closing parenthesis (conditionally) first and using the location of last token parsed for the attribute (`PreviuosLoc`) as the attribute range's end location. Resolves rdar://64304839
1 parent e1e0f54 commit 5d91635

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,13 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
23032303
if (Status.isErrorOrHasCompletion())
23042304
return false;
23052305

2306-
AttrRange = SourceRange(Loc, Tok.getLoc());
2306+
if (!consumeIf(tok::r_paren)) {
2307+
diagnose(Tok.getLoc(), diag::attr_expected_rparen, AttrName,
2308+
DeclAttribute::isDeclModifier(DK));
2309+
return false;
2310+
}
2311+
2312+
AttrRange = SourceRange(Loc, PreviousLoc);
23072313
// For each platform version spec in the spec list, create an
23082314
// implicit AvailableAttr for the platform with the introduced
23092315
// version from the spec. For example, if we have
@@ -2367,12 +2373,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
23672373
/*Implicit=*/false));
23682374
}
23692375

2370-
if (!consumeIf(tok::r_paren)) {
2371-
diagnose(Tok.getLoc(), diag::attr_expected_rparen, AttrName,
2372-
DeclAttribute::isDeclModifier(DK));
2373-
return false;
2374-
}
2375-
23762376
break;
23772377
}
23782378

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %sourcekitd-test -req=open %s -- %s
2+
// Test that this doesn't crash sourcekitd
3+
4+
@available(macOS 10.12
5+
@available(macOS 10.13
6+
func foo() {} {code}

0 commit comments

Comments
 (0)