Skip to content

Commit 1ca90ef

Browse files
committed
[SyntaxParse] Fix memory leaks
There were a few places discarding recorded syntax: - '#<code-complete>' at top-level (this should be parsed as UnknownDecl). - 'typealias' decl with inheritance clause in protocol decl.
1 parent 6fcbb40 commit 1ca90ef

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,7 +4074,7 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
40744074
ParserPosition startPosition = getParserPosition();
40754075
llvm::Optional<SyntaxParsingContext> TmpCtxt;
40764076
TmpCtxt.emplace(SyntaxContext);
4077-
TmpCtxt->setTransparent();
4077+
TmpCtxt->setBackTracking();
40784078

40794079
SourceLoc TypeAliasLoc = consumeToken(tok::kw_typealias);
40804080
SourceLoc EqualLoc;
@@ -4111,13 +4111,13 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
41114111
}
41124112

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

41234123
auto *TAD = new (Context) TypeAliasDecl(TypeAliasLoc, EqualLoc, Id, IdLoc,

lib/Parse/ParseStmt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ParserStatus Parser::parseExprOrStmt(ASTNode &Result) {
122122

123123
if (Tok.is(tok::pound) && Tok.isAtStartOfLine() &&
124124
peekToken().is(tok::code_complete)) {
125+
SyntaxParsingContext CCCtxt(SyntaxContext, SyntaxContextKind::Decl);
125126
consumeToken();
126127
if (CodeCompletion)
127128
CodeCompletion->completeAfterPoundDirective();

lib/Parse/SyntaxParsingContext.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,12 @@ ParsedRawSyntaxNode SyntaxParsingContext::finalizeSourceFile() {
257257
ParsedRawSyntaxNode EOFToken = Parts.back();
258258
Parts = Parts.drop_back();
259259

260-
for (auto RawNode : Parts) {
261-
if (RawNode.getKind() != SyntaxKind::CodeBlockItem) {
262-
// FIXME: Skip toplevel garbage nodes for now. we shouldn't emit them in
263-
// the first place.
264-
if (RawNode.isRecorded())
265-
getSyntaxCreator().finalizeNode(RawNode.getOpaqueNode());
266-
continue;
267-
}
268-
269-
AllTopLevel.push_back(RawNode);
270-
}
260+
assert(llvm::all_of(Parts, [](const ParsedRawSyntaxNode& node) {
261+
return node.getKind() == SyntaxKind::CodeBlockItem;
262+
}) && "all top level element must be 'CodeBlockItem'");
271263

272264
auto itemList = Recorder.recordRawSyntax(SyntaxKind::CodeBlockItemList,
273-
AllTopLevel);
265+
Parts);
274266
return Recorder.recordRawSyntax(SyntaxKind::SourceFile,
275267
{ itemList, EOFToken });
276268
}

test/Syntax/serialize_tupletype.swift.result

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
null,
1818
null,
1919
{
20-
"id": 1,
20+
"id": 33,
2121
"tokenKind": {
2222
"kind": "kw_typealias"
2323
},
@@ -48,7 +48,7 @@
4848
"presence": "Present"
4949
},
5050
{
51-
"id": 2,
51+
"id": 34,
5252
"tokenKind": {
5353
"kind": "identifier",
5454
"text": "x"
@@ -64,11 +64,11 @@
6464
},
6565
null,
6666
{
67-
"id": 34,
67+
"id": 32,
6868
"kind": "TypeInitializerClause",
6969
"layout": [
7070
{
71-
"id": 3,
71+
"id": 1,
7272
"tokenKind": {
7373
"kind": "equal"
7474
},
@@ -82,11 +82,11 @@
8282
"presence": "Present"
8383
},
8484
{
85-
"id": 33,
85+
"id": 31,
8686
"kind": "TupleType",
8787
"layout": [
8888
{
89-
"id": 20,
89+
"id": 18,
9090
"tokenKind": {
9191
"kind": "l_paren"
9292
},
@@ -95,16 +95,16 @@
9595
"presence": "Present"
9696
},
9797
{
98-
"id": 31,
98+
"id": 29,
9999
"kind": "TupleTypeElementList",
100100
"layout": [
101101
{
102-
"id": 26,
102+
"id": 24,
103103
"kind": "TupleTypeElement",
104104
"layout": [
105105
null,
106106
{
107-
"id": 21,
107+
"id": 19,
108108
"tokenKind": {
109109
"kind": "identifier",
110110
"text": "b"
@@ -115,7 +115,7 @@
115115
},
116116
null,
117117
{
118-
"id": 22,
118+
"id": 20,
119119
"tokenKind": {
120120
"kind": "colon"
121121
},
@@ -129,11 +129,11 @@
129129
"presence": "Present"
130130
},
131131
{
132-
"id": 24,
132+
"id": 22,
133133
"kind": "SimpleTypeIdentifier",
134134
"layout": [
135135
{
136-
"id": 23,
136+
"id": 21,
137137
"tokenKind": {
138138
"kind": "identifier",
139139
"text": "Int"
@@ -149,7 +149,7 @@
149149
null,
150150
null,
151151
{
152-
"id": 25,
152+
"id": 23,
153153
"tokenKind": {
154154
"kind": "comma"
155155
},
@@ -166,12 +166,12 @@
166166
"presence": "Present"
167167
},
168168
{
169-
"id": 30,
169+
"id": 28,
170170
"kind": "TupleTypeElement",
171171
"layout": [
172172
null,
173173
{
174-
"id": 27,
174+
"id": 25,
175175
"tokenKind": {
176176
"kind": "kw__"
177177
},
@@ -181,7 +181,7 @@
181181
},
182182
null,
183183
{
184-
"id": 22,
184+
"id": 20,
185185
"tokenKind": {
186186
"kind": "colon"
187187
},
@@ -195,11 +195,11 @@
195195
"presence": "Present"
196196
},
197197
{
198-
"id": 29,
198+
"id": 27,
199199
"kind": "SimpleTypeIdentifier",
200200
"layout": [
201201
{
202-
"id": 28,
202+
"id": 26,
203203
"tokenKind": {
204204
"kind": "identifier",
205205
"text": "String"
@@ -222,7 +222,7 @@
222222
"presence": "Present"
223223
},
224224
{
225-
"id": 32,
225+
"id": 30,
226226
"tokenKind": {
227227
"kind": "r_paren"
228228
},

0 commit comments

Comments
 (0)