[PATCH] D30608: Make SmallPtrSet count and find able to take const PtrType's

Daniel Berlin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 4 08:46:27 PST 2017


dberlin updated this revision to Diff 90581.
dberlin added a comment.

- Add PointerLikeTypeTraits for const things, as long as there is one for the non-const version.

Clang and other users have a number of types they use as pointers and
put in SmallPtrSet, and this avoids having to define both const and
non-const versions of PointerLikeTraits for about 5 types.

(Note: You can't easily merge the const T and const T * versions of
these that i can see).


https://reviews.llvm.org/D30608

Files:
  include/llvm/ADT/SmallPtrSet.h
  include/llvm/Support/PointerLikeTypeTraits.h


Index: include/llvm/Support/PointerLikeTypeTraits.h
===================================================================
--- include/llvm/Support/PointerLikeTypeTraits.h
+++ include/llvm/Support/PointerLikeTypeTraits.h
@@ -60,6 +60,20 @@
   enum { NumLowBitsAvailable = 2 };
 };
 
+// Provide PointerLikeTypeTraits for const things.
+template <typename T> class PointerLikeTypeTraits<const T> {
+  typedef PointerLikeTypeTraits<T> NonConst;
+
+public:
+  static inline const void *getAsVoidPointer(const T P) {
+    return NonConst::getAsVoidPointer(P);
+  }
+  static inline const T getFromVoidPointer(const void *P) {
+    return NonConst::getFromVoidPointer(const_cast<void *>(P));
+  }
+  enum { NumLowBitsAvailable = NonConst::NumLowBitsAvailable };
+};
+
 // Provide PointerLikeTypeTraits for const pointers.
 template <typename T> class PointerLikeTypeTraits<const T *> {
   typedef PointerLikeTypeTraits<T *> NonConst;
Index: include/llvm/ADT/SmallPtrSet.h
===================================================================
--- include/llvm/ADT/SmallPtrSet.h
+++ include/llvm/ADT/SmallPtrSet.h
@@ -18,6 +18,7 @@
 #include "llvm/Config/abi-breaking.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
+#include "llvm/Support/type_traits.h"
 #include <cassert>
 #include <cstddef>
 #include <cstring>
@@ -343,7 +344,9 @@
 /// to avoid encoding a particular small size in the interface boundary.
 template <typename PtrType>
 class SmallPtrSetImpl : public SmallPtrSetImplBase {
+  using ConstPtrType = typename add_const_past_pointer<PtrType>::type;
   typedef PointerLikeTypeTraits<PtrType> PtrTraits;
+  typedef PointerLikeTypeTraits<ConstPtrType> ConstPtrTraits;
 
 protected:
   // Constructors that forward to the base.
@@ -375,13 +378,12 @@
   bool erase(PtrType Ptr) {
     return erase_imp(PtrTraits::getAsVoidPointer(Ptr));
   }
-
   /// count - Return 1 if the specified pointer is in the set, 0 otherwise.
-  size_type count(PtrType Ptr) const {
+  size_type count(ConstPtrType Ptr) const {
     return find(Ptr) != endPtr() ? 1 : 0;
   }
-  iterator find(PtrType Ptr) const {
-    auto *P = find_imp(PtrTraits::getAsVoidPointer(Ptr));
+  iterator find(ConstPtrType Ptr) const {
+    auto *P = find_imp(ConstPtrTraits::getAsVoidPointer(Ptr));
     return iterator(P, EndPointer());
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30608.90581.patch
Type: text/x-patch
Size: 2342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170304/c3275569/attachment.bin>


More information about the llvm-commits mailing list