[clang] 8f66f13 - Fix memory leak in [Clang] Implement __builtin_source_location.

James Y Knight via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 29 14:33:32 PDT 2022


Author: James Y Knight
Date: 2022-03-29T17:32:59-04:00
New Revision: 8f66f1371981bda1af1ca43d505e1bc5836b3e36

URL: https://github.com/llvm/llvm-project/commit/8f66f1371981bda1af1ca43d505e1bc5836b3e36
DIFF: https://github.com/llvm/llvm-project/commit/8f66f1371981bda1af1ca43d505e1bc5836b3e36.diff

LOG: Fix memory leak in [Clang] Implement __builtin_source_location.

Fixes: d61487490022

Added: 
    

Modified: 
    clang/include/clang/AST/DeclCXX.h
    clang/lib/AST/DeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index f5bd856fdb186..04a9daa14e05e 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 b3f3efee9931e..cb6a355f7294a 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 {


        


More information about the cfe-commits mailing list