Skip to content

Commit fc35376

Browse files
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.
1 parent 8db4dc8 commit fc35376

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

clang/include/clang/SymbolGraph/API.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/Triple.h"
2525
#include "llvm/Support/Allocator.h"
2626
#include "llvm/Support/Casting.h"
27+
#include <memory>
2728

2829
namespace clang {
2930
namespace symbolgraph {
@@ -120,7 +121,25 @@ class APISet {
120121
StringRef copyString(StringRef String, llvm::BumpPtrAllocator &Allocator);
121122
StringRef copyString(StringRef String);
122123

123-
using GlobalRecordMap = llvm::MapVector<StringRef, GlobalRecord *>;
124+
private:
125+
/// \brief A custom deleter used for ``std::unique_ptr`` to APIRecords stored
126+
/// in the BumpPtrAllocator.
127+
///
128+
/// \tparam T the exact type of the APIRecord subclass.
129+
template <typename T> struct UniquePtrBumpPtrAllocatorDeleter {
130+
void operator()(T *Instance) { Instance->~T(); }
131+
};
132+
133+
public:
134+
/// A unique pointer to an APIRecord stored in the BumpPtrAllocator.
135+
///
136+
/// \tparam T the exact type of the APIRecord subclass.
137+
template <typename T>
138+
using APIRecordUniquePtr =
139+
std::unique_ptr<T, UniquePtrBumpPtrAllocatorDeleter<T>>;
140+
141+
using GlobalRecordMap =
142+
llvm::MapVector<StringRef, APIRecordUniquePtr<GlobalRecord>>;
124143

125144
const GlobalRecordMap &getGlobals() const { return Globals; }
126145

clang/lib/SymbolGraph/API.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ GlobalRecord *APISet::addGlobal(GVKind Kind, StringRef Name, StringRef USR,
3232
FunctionSignature Signature) {
3333
auto Result = Globals.insert({Name, nullptr});
3434
if (Result.second) {
35-
GlobalRecord *Record = new (Allocator)
36-
GlobalRecord{Kind, Name, USR, Loc, Availability,
37-
Linkage, Comment, Fragments, SubHeading, Signature};
38-
Result.first->second = Record;
35+
auto Record = APIRecordUniquePtr<GlobalRecord>(new (Allocator) GlobalRecord{
36+
Kind, Name, USR, Loc, Availability, Linkage, Comment, Fragments,
37+
SubHeading, Signature});
38+
Result.first->second = std::move(Record);
3939
}
40-
return Result.first->second;
40+
return Result.first->second.get();
4141
}
4242

4343
GlobalRecord *

clang/test/SymbolGraph/global_record.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// FIXME: disable the test to unblock build bots
2-
// UNSUPPORTED: true
31
// RUN: rm -rf %t
42
// RUN: split-file %s %t
53
// RUN: sed -e "s@INPUT_DIR@%/t@g" %t/reference.output.json.in >> \

0 commit comments

Comments
 (0)