[ADT] Teach PointerUnion to support assignment directly from nullptr to
authorChandler Carruth <chandlerc@gmail.com>
Tue, 29 Apr 2014 00:14:27 +0000 (00:14 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 29 Apr 2014 00:14:27 +0000 (00:14 +0000)
clear it out.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207471 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/PointerUnion.h
unittests/ADT/PointerUnionTest.cpp

index dafd0e05109399fd3d23aa4fd3b275325145807b..a6dddd277621806a12cac918a0d86ce3f0abcf84 100644 (file)
@@ -154,6 +154,12 @@ namespace llvm {
          "Can't get the address because PointerLikeTypeTraits changes the ptr");
       return (PT1 *)Val.getAddrOfPointer();
     }
+
+    /// \brief Assignment from nullptr which just clears the union.
+    const PointerUnion &operator=(std::nullptr_t) {
+      Val.initWithPointer(nullptr);
+      return *this;
+    }
     
     /// Assignment operators - Allow assigning into this union from either
     /// pointer type, setting the discriminator to remember what it came from.
@@ -298,6 +304,12 @@ namespace llvm {
       if (is<T>()) return get<T>();
       return T();
     }
+
+    /// \brief Assignment from nullptr which just clears the union.
+    const PointerUnion3 &operator=(std::nullptr_t) {
+      Val = nullptr;
+      return *this;
+    }
     
     /// Assignment operators - Allow assigning into this union from either
     /// pointer type, setting the discriminator to remember what it came from.
@@ -407,6 +419,12 @@ namespace llvm {
       if (is<T>()) return get<T>();
       return T();
     }
+
+    /// \brief Assignment from nullptr which just clears the union.
+    const PointerUnion4 &operator=(std::nullptr_t) {
+      Val = nullptr;
+      return *this;
+    }
     
     /// Assignment operators - Allow assigning into this union from either
     /// pointer type, setting the discriminator to remember what it came from.
index 23a833310d27495473545a934d13db9d7029d22f..3bfb79cee4910b5455de64f1214eba746651e22f 100644 (file)
@@ -46,6 +46,12 @@ TEST_F(PointerUnionTest, Null) {
   EXPECT_TRUE((bool)a);
   EXPECT_TRUE((bool)b);
   EXPECT_FALSE(n);
+
+  EXPECT_NE(n, b);
+  EXPECT_EQ(b, c);
+  b = nullptr;
+  EXPECT_EQ(n, b);
+  EXPECT_NE(b, c);
 }
 
 TEST_F(PointerUnionTest, Is) {