Skip to content

[libSyntax] Resolve a memory leak leaking the syntax tree #18698

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 2 commits into from
Aug 14, 2018
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
7 changes: 7 additions & 0 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,13 @@ struct ParserUnit::Implementation {
Opts.CollectParsedToken,
Opts.BuildSyntaxTree)) {
}

~Implementation() {
// We need to delete the parser before the context so that it can finalize
// its SourceFileSyntax while it is still alive
TheParser.reset();
delete &Ctx;
}
};

ParserUnit::ParserUnit(SourceManager &SM, unsigned BufferID)
Expand Down
8 changes: 4 additions & 4 deletions lib/Syntax/SyntaxArena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ class RawSyntaxCacheNode : public llvm::FoldingSetNode {
friend llvm::FoldingSetTrait<RawSyntaxCacheNode>;

/// Associated RawSyntax.
RawSyntax *Obj;
RC<RawSyntax> Obj;
/// FoldingSet node identifier of the associated RawSyntax.
llvm::FoldingSetNodeIDRef IDRef;

public:
RawSyntaxCacheNode(RawSyntax *Obj, const llvm::FoldingSetNodeIDRef IDRef)
RawSyntaxCacheNode(RC<RawSyntax> Obj, const llvm::FoldingSetNodeIDRef IDRef)
: Obj(Obj), IDRef(IDRef) {}

/// Retrieve assciated RawSyntax.
RawSyntax *get() { return Obj; }
RC<RawSyntax> get() { return Obj; }

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