From fc3537697db7724834d8071cfee10cacceb9fc2a Mon Sep 17 00:00:00 2001 From: Daniel Grumberg Date: Fri, 18 Mar 2022 23:54:56 +0000 Subject: [PATCH] Ensure that APIRecords get destroyed correctly. Implements an APISet specific unique ptr type that has a custom deleter that just calls the underlying APIRecord subclass destructor. --- clang/include/clang/SymbolGraph/API.h | 21 ++++++++++++++++++++- clang/lib/SymbolGraph/API.cpp | 10 +++++----- clang/test/SymbolGraph/global_record.c | 2 -- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/SymbolGraph/API.h b/clang/include/clang/SymbolGraph/API.h index 541673f..ffcc037 100644 --- a/clang/include/clang/SymbolGraph/API.h +++ b/clang/include/clang/SymbolGraph/API.h @@ -24,6 +24,7 @@ #include "llvm/ADT/Triple.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Casting.h" +#include namespace clang { namespace symbolgraph { @@ -120,7 +121,25 @@ public: StringRef copyString(StringRef String, llvm::BumpPtrAllocator &Allocator); StringRef copyString(StringRef String); - using GlobalRecordMap = llvm::MapVector; +private: + /// \brief A custom deleter used for ``std::unique_ptr`` to APIRecords stored + /// in the BumpPtrAllocator. + /// + /// \tparam T the exact type of the APIRecord subclass. + template struct UniquePtrBumpPtrAllocatorDeleter { + void operator()(T *Instance) { Instance->~T(); } + }; + +public: + /// A unique pointer to an APIRecord stored in the BumpPtrAllocator. + /// + /// \tparam T the exact type of the APIRecord subclass. + template + using APIRecordUniquePtr = + std::unique_ptr>; + + using GlobalRecordMap = + llvm::MapVector>; const GlobalRecordMap &getGlobals() const { return Globals; } diff --git a/clang/lib/SymbolGraph/API.cpp b/clang/lib/SymbolGraph/API.cpp index 4066428..69e7469 100644 --- a/clang/lib/SymbolGraph/API.cpp +++ b/clang/lib/SymbolGraph/API.cpp @@ -32,12 +32,12 @@ GlobalRecord *APISet::addGlobal(GVKind Kind, StringRef Name, StringRef USR, FunctionSignature Signature) { auto Result = Globals.insert({Name, nullptr}); if (Result.second) { - GlobalRecord *Record = new (Allocator) - GlobalRecord{Kind, Name, USR, Loc, Availability, - Linkage, Comment, Fragments, SubHeading, Signature}; - Result.first->second = Record; + auto Record = APIRecordUniquePtr(new (Allocator) GlobalRecord{ + Kind, Name, USR, Loc, Availability, Linkage, Comment, Fragments, + SubHeading, Signature}); + Result.first->second = std::move(Record); } - return Result.first->second; + return Result.first->second.get(); } GlobalRecord * diff --git a/clang/test/SymbolGraph/global_record.c b/clang/test/SymbolGraph/global_record.c index c1baaf2..fa577ee 100644 --- a/clang/test/SymbolGraph/global_record.c +++ b/clang/test/SymbolGraph/global_record.c @@ -1,5 +1,3 @@ -// FIXME: disable the test to unblock build bots -// UNSUPPORTED: true // RUN: rm -rf %t // RUN: split-file %s %t // RUN: sed -e "s@INPUT_DIR@%/t@g" %t/reference.output.json.in >> \ -- 2.7.4