summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustin Lebar <jlebar@google.com>2016-12-29 19:59:26 +0000
committerJustin Lebar <jlebar@google.com>2016-12-29 19:59:26 +0000
commit175ab74dc52f52e9d14d31d4fbf8a1e2d0995017 (patch)
tree18bff7f7252b972aa52e7e79c3fa6746eb3f69eb
parent291264b612415dc3462ace9aab8c713cc81de7c8 (diff)
downloadbcm5719-llvm-175ab74dc52f52e9d14d31d4fbf8a1e2d0995017.tar.gz
bcm5719-llvm-175ab74dc52f52e9d14d31d4fbf8a1e2d0995017.zip
[ADT] Delete RefCountedBaseVPTR.
Summary: This class is unnecessary. Its comment indicated that it was a compile error to allocate an instance of a class that inherits from RefCountedBaseVPTR on the stack. This may have been true at one point, but it's not today. Moreover you really do not want to allocate *any* refcounted object on the stack, vptrs or not, so if we did have a way to prevent these objects from being stack-allocated, we'd want to apply it to regular RefCountedBase too, obviating the need for a separate RefCountedBaseVPTR class. It seems that the main way RefCountedBaseVPTR provides safety is by making its subclass's destructor virtual. This may have been helpful at one point, but these days clang will emit an error if you define a class with virtual functions that inherits from RefCountedBase but doesn't have a virtual destructor. Reviewers: compnerd, dblaikie Subscribers: cfe-commits, klimek, llvm-commits, mgorny Differential Revision: https://reviews.llvm.org/D28162 llvm-svn: 290717
-rw-r--r--clang/include/clang/ASTMatchers/Dynamic/VariantValue.h4
-rw-r--r--clang/include/clang/Basic/LLVM.h2
-rw-r--r--clang/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h4
-rw-r--r--llvm/include/llvm/ADT/IntrusiveRefCntPtr.h43
-rw-r--r--llvm/lib/Support/CMakeLists.txt1
-rw-r--r--llvm/lib/Support/IntrusiveRefCntPtr.cpp14
-rw-r--r--llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp37
7 files changed, 28 insertions, 77 deletions
diff --git a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
index 5296eddc61c..9f694d0ce43 100644
--- a/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
+++ b/clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
@@ -119,9 +119,9 @@ class VariantMatcher {
/// \brief Payload interface to be specialized by each matcher type.
///
/// It follows a similar interface as VariantMatcher itself.
- class Payload : public RefCountedBaseVPTR {
+ class Payload : public RefCountedBase<Payload> {
public:
- ~Payload() override;
+ virtual ~Payload();
virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const = 0;
virtual std::string getTypeAsString() const = 0;
virtual llvm::Optional<DynTypedMatcher>
diff --git a/clang/include/clang/Basic/LLVM.h b/clang/include/clang/Basic/LLVM.h
index def72a49a16..f32ab5e11bd 100644
--- a/clang/include/clang/Basic/LLVM.h
+++ b/clang/include/clang/Basic/LLVM.h
@@ -43,7 +43,6 @@ namespace llvm {
template <typename T> class IntrusiveRefCntPtr;
template <typename T> struct IntrusiveRefCntPtrInfo;
template <class Derived> class RefCountedBase;
- class RefCountedBaseVPTR;
class raw_ostream;
class raw_pwrite_stream;
@@ -76,7 +75,6 @@ namespace clang {
using llvm::IntrusiveRefCntPtr;
using llvm::IntrusiveRefCntPtrInfo;
using llvm::RefCountedBase;
- using llvm::RefCountedBaseVPTR;
using llvm::raw_ostream;
using llvm::raw_pwrite_stream;
diff --git a/clang/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h b/clang/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
index 1cc3cae9d99..efe809fb198 100644
--- a/clang/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
+++ b/clang/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h
@@ -334,7 +334,7 @@ public:
// Path "pieces" for path-sensitive diagnostics.
//===----------------------------------------------------------------------===//
-class PathDiagnosticPiece : public RefCountedBaseVPTR {
+class PathDiagnosticPiece : public RefCountedBase<PathDiagnosticPiece> {
public:
enum Kind { ControlFlow, Event, Macro, Call, Note };
enum DisplayHint { Above, Below };
@@ -366,7 +366,7 @@ protected:
PathDiagnosticPiece(Kind k, DisplayHint hint = Below);
public:
- ~PathDiagnosticPiece() override;
+ virtual ~PathDiagnosticPiece();
StringRef getString() const { return str; }
diff --git a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
index b0c2ce76a33..287b40f09df 100644
--- a/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
+++ b/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h
@@ -9,9 +9,9 @@
//
// This file defines IntrusiveRefCntPtr, a template class that
// implements a "smart" pointer for objects that maintain their own
-// internal reference count, and RefCountedBase/RefCountedBaseVPTR, two
-// generic base classes for objects that wish to have their lifetimes
-// managed using reference counting.
+// internal reference count, and RefCountedBase, a generic base class
+// for objects that wish to have their lifetimes managed using reference
+// counting.
//
// IntrusiveRefCntPtr is similar to Boost's intrusive_ptr with added
// LLVM-style casting.
@@ -52,36 +52,6 @@ namespace llvm {
}
};
-//===----------------------------------------------------------------------===//
-/// RefCountedBaseVPTR - A class that has the same function as
-/// RefCountedBase, but with a virtual destructor. Should be used
-/// instead of RefCountedBase for classes that already have virtual
-/// methods to enforce dynamic allocation via 'new'. Classes that
-/// inherit from RefCountedBaseVPTR can't be allocated on stack -
-/// attempting to do this will produce a compile error.
-//===----------------------------------------------------------------------===//
- class RefCountedBaseVPTR {
- mutable unsigned ref_cnt = 0;
-
- virtual void anchor();
-
- protected:
- RefCountedBaseVPTR() = default;
- RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
-
- virtual ~RefCountedBaseVPTR() = default;
-
- void Retain() const { ++ref_cnt; }
- void Release() const {
- assert (ref_cnt > 0 && "Reference count is already zero.");
- if (--ref_cnt == 0) delete this;
- }
-
- template <typename T>
- friend struct IntrusiveRefCntPtrInfo;
- };
-
-
template <typename T> struct IntrusiveRefCntPtrInfo {
static void retain(T *obj) { obj->Retain(); }
static void release(T *obj) { obj->Release(); }
@@ -124,10 +94,9 @@ public:
/// wrapping NULL pointers.
///
/// Reference counting is implemented via calls to
-/// Obj->Retain()/Obj->Release(). Release() is required to destroy
-/// the object when the reference count reaches zero. Inheriting from
-/// RefCountedBase/RefCountedBaseVPTR takes care of this
-/// automatically.
+/// Obj->Retain()/Obj->Release(). Release() is required to destroy the
+/// object when the reference count reaches zero. Inheriting from
+/// RefCountedBase takes care of this automatically.
//===----------------------------------------------------------------------===//
template <typename T>
class IntrusiveRefCntPtr {
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 03addcbcd16..ca344b1dc05 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -61,7 +61,6 @@ add_llvm_library(LLVMSupport
Hashing.cpp
IntEqClasses.cpp
IntervalMap.cpp
- IntrusiveRefCntPtr.cpp
JamCRC.cpp
LEB128.cpp
LineIterator.cpp
diff --git a/llvm/lib/Support/IntrusiveRefCntPtr.cpp b/llvm/lib/Support/IntrusiveRefCntPtr.cpp
deleted file mode 100644
index a8b45593ae7..00000000000
--- a/llvm/lib/Support/IntrusiveRefCntPtr.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-//== IntrusiveRefCntPtr.cpp - Smart Refcounting Pointer ----------*- C++ -*-==//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/ADT/IntrusiveRefCntPtr.h"
-
-using namespace llvm;
-
-void RefCountedBaseVPTR::anchor() { }
diff --git a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
index c67ec130912..143a8cc4910 100644
--- a/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
+++ b/llvm/unittests/ADT/IntrusiveRefCntPtrTest.cpp
@@ -10,30 +10,29 @@
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "gtest/gtest.h"
-namespace {
-struct VirtualRefCounted : public llvm::RefCountedBaseVPTR {
- virtual void f() {}
-};
-}
-
namespace llvm {
-// Run this test with valgrind to detect memory leaks.
-TEST(IntrusiveRefCntPtr, RefCountedBaseVPTRCopyDoesNotLeak) {
- VirtualRefCounted *V1 = new VirtualRefCounted;
- IntrusiveRefCntPtr<VirtualRefCounted> R1 = V1;
- VirtualRefCounted *V2 = new VirtualRefCounted(*V1);
- IntrusiveRefCntPtr<VirtualRefCounted> R2 = V2;
-}
+namespace {
+struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {
+ SimpleRefCounted() { ++NumInstances; }
+ SimpleRefCounted(const SimpleRefCounted &) { ++NumInstances; }
+ ~SimpleRefCounted() { --NumInstances; }
-struct SimpleRefCounted : public RefCountedBase<SimpleRefCounted> {};
+ static int NumInstances;
+};
+int SimpleRefCounted::NumInstances = 0;
+} // anonymous namespace
-// Run this test with valgrind to detect memory leaks.
TEST(IntrusiveRefCntPtr, RefCountedBaseCopyDoesNotLeak) {
- SimpleRefCounted *S1 = new SimpleRefCounted;
- IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
- SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
- IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
+ EXPECT_EQ(0, SimpleRefCounted::NumInstances);
+ {
+ SimpleRefCounted *S1 = new SimpleRefCounted;
+ IntrusiveRefCntPtr<SimpleRefCounted> R1 = S1;
+ SimpleRefCounted *S2 = new SimpleRefCounted(*S1);
+ IntrusiveRefCntPtr<SimpleRefCounted> R2 = S2;
+ EXPECT_EQ(2, SimpleRefCounted::NumInstances);
+ }
+ EXPECT_EQ(0, SimpleRefCounted::NumInstances);
}
struct InterceptRefCounted : public RefCountedBase<InterceptRefCounted> {
OpenPOWER on IntegriCloud