<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Tue, Sep 5, 2017 at 2:47 PM Richard Smith via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rsmith<br>
Date: Tue Sep 5 14:46:22 2017<br>
New Revision: 312580<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=312580&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=312580&view=rev</a><br>
Log:<br>
Fix memory leak after r312467. The ModuleMap is the owner of the global module object until it's reparented under a real module.<br>
<br>
Modified:<br>
cfe/trunk/include/clang/Lex/ModuleMap.h<br>
cfe/trunk/lib/Lex/ModuleMap.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Lex/ModuleMap.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleMap.h?rev=312580&r1=312579&r2=312580&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/ModuleMap.h?rev=312580&r1=312579&r2=312580&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Lex/ModuleMap.h (original)<br>
+++ cfe/trunk/include/clang/Lex/ModuleMap.h Tue Sep 5 14:46:22 2017<br>
@@ -82,22 +82,26 @@ class ModuleMap {<br>
<br>
/// \brief The directory used for Clang-supplied, builtin include headers,<br>
/// such as "stdint.h".<br>
- const DirectoryEntry *BuiltinIncludeDir;<br>
+ const DirectoryEntry *BuiltinIncludeDir = nullptr;<br>
<br>
/// \brief Language options used to parse the module map itself.<br>
///<br>
/// These are always simple C language options.<br>
LangOptions MMapLangOpts;<br>
<br>
- // The module that the main source file is associated with (the module<br>
- // named LangOpts::CurrentModule, if we've loaded it).<br>
- Module *SourceModule;<br>
+ /// The module that the main source file is associated with (the module<br>
+ /// named LangOpts::CurrentModule, if we've loaded it).<br>
+ Module *SourceModule = nullptr;<br>
+<br>
+ /// The global module for the current TU, if we still own it. (Ownership is<br>
+ /// transferred if/when we create an enclosing module.<br>
+ std::unique_ptr<Module> PendingGlobalModule;<br>
<br>
/// \brief The top-level modules that are known.<br>
llvm::StringMap<Module *> Modules;<br>
<br>
/// \brief The number of modules we have created in total.<br>
- unsigned NumCreatedModules;<br>
+ unsigned NumCreatedModules = 0;<br>
<br>
public:<br>
/// \brief Flags describing the role of a module header.<br>
<br>
Modified: cfe/trunk/lib/Lex/ModuleMap.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ModuleMap.cpp?rev=312580&r1=312579&r2=312580&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/ModuleMap.cpp?rev=312580&r1=312579&r2=312580&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Lex/ModuleMap.cpp (original)<br>
+++ cfe/trunk/lib/Lex/ModuleMap.cpp Tue Sep 5 14:46:22 2017<br>
@@ -256,8 +256,7 @@ ModuleMap::ModuleMap(SourceManager &Sour<br>
const LangOptions &LangOpts, const TargetInfo *Target,<br>
HeaderSearch &HeaderInfo)<br>
: SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),<br>
- HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),<br>
- SourceModule(nullptr), NumCreatedModules(0) {<br>
+ HeaderInfo(HeaderInfo) {<br>
MMapLangOpts.LineComment = true;<br>
}<br>
<br>
@@ -747,10 +746,12 @@ std::pair<Module *, bool> ModuleMap::fin<br>
}<br>
<br>
Module *ModuleMap::createGlobalModuleForInterfaceUnit(SourceLocation Loc) {<br>
- auto *Result = new Module("<global>", Loc, nullptr, /*IsFramework*/ false,<br>
- /*IsExplicit*/ true, NumCreatedModules++);<br>
- Result->Kind = Module::GlobalModuleFragment;<br>
- return Result;<br>
+ assert(!PendingGlobalModule && "created multiple global modules");<br>
+ PendingGlobalModule.reset(<br>
+ new Module("<global>", Loc, nullptr, /*IsFramework*/ false,<br>
+ /*IsExplicit*/ true, NumCreatedModules++));<br>
+ PendingGlobalModule->Kind = Module::GlobalModuleFragment;<br>
+ return PendingGlobalModule.get();<br>
}<br>
<br>
Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,<br>
@@ -766,7 +767,10 @@ Module *ModuleMap::createModuleForInterf<br>
Modules[Name] = SourceModule = Result;<br>
<br>
// Reparent the current global module fragment as a submodule of this module.<br>
+ assert(GlobalModule == PendingGlobalModule.get() &&<br>
+ "unexpected global module");<br>
GlobalModule->setParent(Result);<br>
+ PendingGlobalModule.release(); // now owned by parent<br></blockquote><div><br>Would it be reasonable to reverse the 'setParent' API (into an addSubmodule call on the parent - that takes the child/submodule by std::unique_ptr value) so the hand-off/ownership is more clear/less error prone?<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
// Mark the main source file as being within the newly-created module so that<br>
// declarations and macros are properly visibility-restricted to it.<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div></div>