Skip to content

Commit 093f917

Browse files
authored
Merge pull request #18698 from ahoppen/leak-fix
[libSyntax] Resolve a memory leak leaking the syntax tree
2 parents bc46175 + 05ae109 commit 093f917

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/Parse/Parser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,13 @@ struct ParserUnit::Implementation {
10161016
Opts.CollectParsedToken,
10171017
Opts.BuildSyntaxTree)) {
10181018
}
1019+
1020+
~Implementation() {
1021+
// We need to delete the parser before the context so that it can finalize
1022+
// its SourceFileSyntax while it is still alive
1023+
TheParser.reset();
1024+
delete &Ctx;
1025+
}
10191026
};
10201027

10211028
ParserUnit::ParserUnit(SourceManager &SM, unsigned BufferID)

lib/Syntax/SyntaxArena.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ class RawSyntaxCacheNode : public llvm::FoldingSetNode {
7575
friend llvm::FoldingSetTrait<RawSyntaxCacheNode>;
7676

7777
/// Associated RawSyntax.
78-
RawSyntax *Obj;
78+
RC<RawSyntax> Obj;
7979
/// FoldingSet node identifier of the associated RawSyntax.
8080
llvm::FoldingSetNodeIDRef IDRef;
8181

8282
public:
83-
RawSyntaxCacheNode(RawSyntax *Obj, const llvm::FoldingSetNodeIDRef IDRef)
83+
RawSyntaxCacheNode(RC<RawSyntax> Obj, const llvm::FoldingSetNodeIDRef IDRef)
8484
: Obj(Obj), IDRef(IDRef) {}
8585

8686
/// Retrieve assciated RawSyntax.
87-
RawSyntax *get() { return Obj; }
87+
RC<RawSyntax> get() { return Obj; }
8888

8989
// Only allow allocation of Node using the allocator in SyntaxArena.
9090
void *operator new(size_t Bytes, SyntaxArena &Arena,
@@ -156,7 +156,7 @@ RC<RawSyntax> RawSyntax::getToken(SyntaxArena &Arena, tok TokKind,
156156
auto Raw = RawSyntax::make(TokKind, Text, LeadingTrivia, TrailingTrivia,
157157
SourcePresence::Present, &Arena);
158158
auto IDRef = ID.Intern(Arena.getAllocator());
159-
auto CacheNode = new (Arena) RawSyntaxCacheNode(Raw.get(), IDRef);
159+
auto CacheNode = new (Arena) RawSyntaxCacheNode(Raw, IDRef);
160160
CachedTokens.InsertNode(CacheNode, insertPos);
161161
return Raw;
162162
}

0 commit comments

Comments
 (0)