diff options
author | Ted Kremenek <kremenek@apple.com> | 2008-04-23 16:25:39 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2008-04-23 16:25:39 +0000 |
commit | c1e9dea3d974dcfcf2baa15810aa3511db688bda (patch) | |
tree | 007ab9e08637c6ef1bae731499751957903bfa31 /include/clang | |
parent | 5014ab113eb211b8320ae30b173d7020352663c6 (diff) |
TranslationUnit now owns IdentifierTable, TargetInfo, and Selectors objects
when it is constructed via deserialization. This is done by recording a flag
indicating that this is the case, and it deletes these objects by getting
the references stored in the ASTContext object. This fixes some memory
leaks that occurs when we deserialize translation units from bitcode files.
The rationale between having TranslationUnit sometimes own these objects and
sometimes not is that a TranslationUnit object can be constructed from
state generated by the parser (Preprocessor; semantic analyzer, etc.), and thus
in these cases won't own the IdentifierTable or Selectors, etc. During
deserialization, there is no Preprocessor, so somebody needs to own these
objects in order for them to be properly reclaimed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@50149 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang')
-rw-r--r-- | include/clang/AST/TranslationUnit.h | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/include/clang/AST/TranslationUnit.h b/include/clang/AST/TranslationUnit.h index 0c7d0348da..6aa3a40b87 100644 --- a/include/clang/AST/TranslationUnit.h +++ b/include/clang/AST/TranslationUnit.h @@ -34,19 +34,17 @@ class TranslationUnit { LangOptions LangOpts; ASTContext* Context; std::vector<Decl*> TopLevelDecls; + bool OwnsMetaData; // The default ctor is only invoked during deserialization. - explicit TranslationUnit() : Context(NULL) {} + explicit TranslationUnit() : Context(NULL), OwnsMetaData(true) {} public: - explicit TranslationUnit(const LangOptions& lopt) - : LangOpts(lopt), Context(NULL) {} + explicit TranslationUnit(ASTContext& Ctx, const LangOptions& lopt) + : LangOpts(lopt), Context(&Ctx), OwnsMetaData(false) {} ~TranslationUnit(); - void setContext(ASTContext* context) { Context = context; } - ASTContext* getContext() const { return Context; } - const LangOptions& getLangOpts() const { return LangOpts; } const std::string& getSourceFile() const; @@ -58,9 +56,12 @@ public: // Accessors const LangOptions& getLangOptions() const { return LangOpts; } - ASTContext* getASTContext() { return Context; } + + ASTContext& getContext() { return *Context; } + const ASTContext& getContext() const { return *Context; } /// AddTopLevelDecl - Add a top-level declaration to the translation unit. + /// Ownership of the Decl is transfered to the TranslationUnit object. void AddTopLevelDecl(Decl* d) { TopLevelDecls.push_back(d); } @@ -77,6 +78,9 @@ public: /// EmitASTBitcodeFile - Emit a translation unit to a bitcode file. bool EmitASTBitcodeFile(const TranslationUnit& TU, const llvm::sys::Path& Filename); + +bool EmitASTBitcodeFile(const TranslationUnit* TU, + const llvm::sys::Path& Filename); /// ReadASTBitcodeFile - Reconsitute a translation unit from a bitcode file. TranslationUnit* ReadASTBitcodeFile(const llvm::sys::Path& Filename, |