From 8f66f1371981bda1af1ca43d505e1bc5836b3e36 Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Tue, 29 Mar 2022 17:32:52 -0400 Subject: [PATCH] Fix memory leak in [Clang] Implement __builtin_source_location. Fixes: d61487490022 --- clang/include/clang/AST/DeclCXX.h | 3 ++- clang/lib/AST/DeclCXX.cpp | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index f5bd856fdb18..04a9daa14e05 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -4221,7 +4221,8 @@ class UnnamedGlobalConstantDecl : public ValueDecl, void anchor() override; - UnnamedGlobalConstantDecl(DeclContext *DC, QualType T, const APValue &Val); + UnnamedGlobalConstantDecl(const ASTContext &C, DeclContext *DC, QualType T, + const APValue &Val); static UnnamedGlobalConstantDecl *Create(const ASTContext &C, QualType T, const APValue &APVal); diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index b3f3efee9931..cb6a355f7294 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -3365,23 +3365,30 @@ APValue &MSGuidDecl::getAsAPValue() const { void UnnamedGlobalConstantDecl::anchor() {} -UnnamedGlobalConstantDecl::UnnamedGlobalConstantDecl(DeclContext *DC, +UnnamedGlobalConstantDecl::UnnamedGlobalConstantDecl(const ASTContext &C, + DeclContext *DC, QualType Ty, - const APValue &Value) + const APValue &Val) : ValueDecl(Decl::UnnamedGlobalConstant, DC, SourceLocation(), DeclarationName(), Ty), - Value(Value) {} + Value(Val) { + // Cleanup the embedded APValue if required (note that our destructor is never + // run) + if (Value.needsCleanup()) + C.addDestruction(&Value); +} UnnamedGlobalConstantDecl * UnnamedGlobalConstantDecl::Create(const ASTContext &C, QualType T, const APValue &Value) { DeclContext *DC = C.getTranslationUnitDecl(); - return new (C, DC) UnnamedGlobalConstantDecl(DC, T, Value); + return new (C, DC) UnnamedGlobalConstantDecl(C, DC, T, Value); } UnnamedGlobalConstantDecl * UnnamedGlobalConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { - return new (C, ID) UnnamedGlobalConstantDecl(nullptr, QualType(), APValue()); + return new (C, ID) + UnnamedGlobalConstantDecl(C, nullptr, QualType(), APValue()); } void UnnamedGlobalConstantDecl::printName(llvm::raw_ostream &OS) const {