[clang] fc35376 - Ensure that APIRecords get destroyed correctly.
Daniel Grumberg via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 18 17:49:54 PDT 2022
Author: Daniel Grumberg
Date: 2022-03-19T00:49:37Z
New Revision: fc3537697db7724834d8071cfee10cacceb9fc2a
URL: https://github.com/llvm/llvm-project/commit/fc3537697db7724834d8071cfee10cacceb9fc2a
DIFF: https://github.com/llvm/llvm-project/commit/fc3537697db7724834d8071cfee10cacceb9fc2a.diff
LOG: 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.
Added:
Modified:
clang/include/clang/SymbolGraph/API.h
clang/lib/SymbolGraph/API.cpp
clang/test/SymbolGraph/global_record.c
Removed:
################################################################################
diff --git a/clang/include/clang/SymbolGraph/API.h b/clang/include/clang/SymbolGraph/API.h
index 541673f36b223..ffcc03758f4d2 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 <memory>
namespace clang {
namespace symbolgraph {
@@ -120,7 +121,25 @@ class APISet {
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; }
diff --git a/clang/lib/SymbolGraph/API.cpp b/clang/lib/SymbolGraph/API.cpp
index 4066428c97fc9..69e7469425c98 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<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 *
diff --git a/clang/test/SymbolGraph/global_record.c b/clang/test/SymbolGraph/global_record.c
index c1baaf260fd10..fa577ee29b17d 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 at INPUT_DIR@%/t at g" %t/reference.output.json.in >> \
More information about the cfe-commits
mailing list