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.
This commit is contained in:
Daniel Grumberg
2022-03-18 23:54:56 +00:00
parent 8db4dc8686
commit fc3537697d
3 changed files with 25 additions and 8 deletions

View File

@@ -24,6 +24,7 @@
#include "llvm/ADT/Triple.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Casting.h"
#include <memory>
namespace clang {
namespace symbolgraph {
@@ -120,7 +121,25 @@ public:
StringRef copyString(StringRef String, llvm::BumpPtrAllocator &Allocator);
StringRef copyString(StringRef String);
using GlobalRecordMap = llvm::MapVector<StringRef, GlobalRecord *>;
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 <typename T> 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 <typename T>
using APIRecordUniquePtr =
std::unique_ptr<T, UniquePtrBumpPtrAllocatorDeleter<T>>;
using GlobalRecordMap =
llvm::MapVector<StringRef, APIRecordUniquePtr<GlobalRecord>>;
const GlobalRecordMap &getGlobals() const { return Globals; }

View File

@@ -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<GlobalRecord>(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 *

View File

@@ -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 >> \