Skip to content

[SyntaxParse] Fix memory leaks #26937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4074,7 +4074,7 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
ParserPosition startPosition = getParserPosition();
llvm::Optional<SyntaxParsingContext> TmpCtxt;
TmpCtxt.emplace(SyntaxContext);
TmpCtxt->setTransparent();
TmpCtxt->setBackTracking();

SourceLoc TypeAliasLoc = consumeToken(tok::kw_typealias);
SourceLoc EqualLoc;
Expand Down Expand Up @@ -4111,13 +4111,13 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
}

if (Flags.contains(PD_InProtocol) && !genericParams && !Tok.is(tok::equal)) {
TmpCtxt->setBackTracking();
TmpCtxt.reset();
// If we're in a protocol and don't see an '=' this looks like leftover Swift 2
// code intending to be an associatedtype.
backtrackToPosition(startPosition);
return parseDeclAssociatedType(Flags, Attributes);
}
TmpCtxt->setTransparent();
TmpCtxt.reset();

auto *TAD = new (Context) TypeAliasDecl(TypeAliasLoc, EqualLoc, Id, IdLoc,
Expand Down
1 change: 1 addition & 0 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ ParserStatus Parser::parseExprOrStmt(ASTNode &Result) {

if (Tok.is(tok::pound) && Tok.isAtStartOfLine() &&
peekToken().is(tok::code_complete)) {
SyntaxParsingContext CCCtxt(SyntaxContext, SyntaxContextKind::Decl);
consumeToken();
if (CodeCompletion)
CodeCompletion->completeAfterPoundDirective();
Expand Down
16 changes: 4 additions & 12 deletions lib/Parse/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,12 @@ ParsedRawSyntaxNode SyntaxParsingContext::finalizeSourceFile() {
ParsedRawSyntaxNode EOFToken = Parts.back();
Parts = Parts.drop_back();

for (auto RawNode : Parts) {
if (RawNode.getKind() != SyntaxKind::CodeBlockItem) {
// FIXME: Skip toplevel garbage nodes for now. we shouldn't emit them in
// the first place.
if (RawNode.isRecorded())
getSyntaxCreator().finalizeNode(RawNode.getOpaqueNode());
continue;
}

AllTopLevel.push_back(RawNode);
}
assert(llvm::all_of(Parts, [](const ParsedRawSyntaxNode& node) {
return node.getKind() == SyntaxKind::CodeBlockItem;
}) && "all top level element must be 'CodeBlockItem'");

auto itemList = Recorder.recordRawSyntax(SyntaxKind::CodeBlockItemList,
AllTopLevel);
Parts);
return Recorder.recordRawSyntax(SyntaxKind::SourceFile,
{ itemList, EOFToken });
}
Expand Down
38 changes: 19 additions & 19 deletions test/Syntax/serialize_tupletype.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
null,
null,
{
"id": 1,
"id": 33,
"tokenKind": {
"kind": "kw_typealias"
},
Expand Down Expand Up @@ -48,7 +48,7 @@
"presence": "Present"
},
{
"id": 2,
"id": 34,
"tokenKind": {
"kind": "identifier",
"text": "x"
Expand All @@ -64,11 +64,11 @@
},
null,
{
"id": 34,
"id": 32,
"kind": "TypeInitializerClause",
"layout": [
{
"id": 3,
"id": 1,
"tokenKind": {
"kind": "equal"
},
Expand All @@ -82,11 +82,11 @@
"presence": "Present"
},
{
"id": 33,
"id": 31,
"kind": "TupleType",
"layout": [
{
"id": 20,
"id": 18,
"tokenKind": {
"kind": "l_paren"
},
Expand All @@ -95,16 +95,16 @@
"presence": "Present"
},
{
"id": 31,
"id": 29,
"kind": "TupleTypeElementList",
"layout": [
{
"id": 26,
"id": 24,
"kind": "TupleTypeElement",
"layout": [
null,
{
"id": 21,
"id": 19,
"tokenKind": {
"kind": "identifier",
"text": "b"
Expand All @@ -115,7 +115,7 @@
},
null,
{
"id": 22,
"id": 20,
"tokenKind": {
"kind": "colon"
},
Expand All @@ -129,11 +129,11 @@
"presence": "Present"
},
{
"id": 24,
"id": 22,
"kind": "SimpleTypeIdentifier",
"layout": [
{
"id": 23,
"id": 21,
"tokenKind": {
"kind": "identifier",
"text": "Int"
Expand All @@ -149,7 +149,7 @@
null,
null,
{
"id": 25,
"id": 23,
"tokenKind": {
"kind": "comma"
},
Expand All @@ -166,12 +166,12 @@
"presence": "Present"
},
{
"id": 30,
"id": 28,
"kind": "TupleTypeElement",
"layout": [
null,
{
"id": 27,
"id": 25,
"tokenKind": {
"kind": "kw__"
},
Expand All @@ -181,7 +181,7 @@
},
null,
{
"id": 22,
"id": 20,
"tokenKind": {
"kind": "colon"
},
Expand All @@ -195,11 +195,11 @@
"presence": "Present"
},
{
"id": 29,
"id": 27,
"kind": "SimpleTypeIdentifier",
"layout": [
{
"id": 28,
"id": 26,
"tokenKind": {
"kind": "identifier",
"text": "String"
Expand All @@ -222,7 +222,7 @@
"presence": "Present"
},
{
"id": 32,
"id": 30,
"tokenKind": {
"kind": "r_paren"
},
Expand Down