1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Casting.h" 11 #include "llvm/Support/Debug.h" 12 #include "llvm/Support/raw_ostream.h" 13 #include "gtest/gtest.h" 14 #include <cstdlib> 15 16 namespace llvm { 17 18 // set up two example classes 19 // with conversion facility 20 // 21 struct bar { 22 bar() {} 23 struct foo *baz(); 24 struct foo *caz(); 25 struct foo *daz(); 26 struct foo *naz(); 27 private: 28 bar(const bar &); 29 }; 30 struct foo { 31 void ext() const; 32 /* static bool classof(const bar *X) { 33 cerr << "Classof: " << X << "\n"; 34 return true; 35 }*/ 36 }; 37 38 template <> struct isa_impl<foo, bar> { 39 static inline bool doit(const bar &Val) { 40 dbgs() << "Classof: " << &Val << "\n"; 41 return true; 42 } 43 }; 44 45 foo *bar::baz() { 46 return cast<foo>(this); 47 } 48 49 foo *bar::caz() { 50 return cast_or_null<foo>(this); 51 } 52 53 foo *bar::daz() { 54 return dyn_cast<foo>(this); 55 } 56 57 foo *bar::naz() { 58 return dyn_cast_or_null<foo>(this); 59 } 60 61 62 bar *fub(); 63 } // End llvm namespace 64 65 using namespace llvm; 66 67 namespace { 68 69 const foo *null_foo = NULL; 70 71 bar B; 72 extern bar &B1; 73 bar &B1 = B; 74 extern const bar *B2; 75 // test various configurations of const 76 const bar &B3 = B1; 77 const bar *const B4 = B2; 78 79 TEST(CastingTest, isa) { 80 EXPECT_TRUE(isa<foo>(B1)); 81 EXPECT_TRUE(isa<foo>(B2)); 82 EXPECT_TRUE(isa<foo>(B3)); 83 EXPECT_TRUE(isa<foo>(B4)); 84 } 85 86 TEST(CastingTest, cast) { 87 foo &F1 = cast<foo>(B1); 88 EXPECT_NE(&F1, null_foo); 89 const foo *F3 = cast<foo>(B2); 90 EXPECT_NE(F3, null_foo); 91 const foo *F4 = cast<foo>(B2); 92 EXPECT_NE(F4, null_foo); 93 const foo &F5 = cast<foo>(B3); 94 EXPECT_NE(&F5, null_foo); 95 const foo *F6 = cast<foo>(B4); 96 EXPECT_NE(F6, null_foo); 97 // Can't pass null pointer to cast<>. 98 // foo *F7 = cast<foo>(fub()); 99 // EXPECT_EQ(F7, null_foo); 100 foo *F8 = B1.baz(); 101 EXPECT_NE(F8, null_foo); 102 } 103 104 TEST(CastingTest, cast_or_null) { 105 const foo *F11 = cast_or_null<foo>(B2); 106 EXPECT_NE(F11, null_foo); 107 const foo *F12 = cast_or_null<foo>(B2); 108 EXPECT_NE(F12, null_foo); 109 const foo *F13 = cast_or_null<foo>(B4); 110 EXPECT_NE(F13, null_foo); 111 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. 112 EXPECT_EQ(F14, null_foo); 113 foo *F15 = B1.caz(); 114 EXPECT_NE(F15, null_foo); 115 } 116 117 TEST(CastingTest, dyn_cast) { 118 const foo *F1 = dyn_cast<foo>(B2); 119 EXPECT_NE(F1, null_foo); 120 const foo *F2 = dyn_cast<foo>(B2); 121 EXPECT_NE(F2, null_foo); 122 const foo *F3 = dyn_cast<foo>(B4); 123 EXPECT_NE(F3, null_foo); 124 // Can't pass null pointer to dyn_cast<>. 125 // foo *F4 = dyn_cast<foo>(fub()); 126 // EXPECT_EQ(F4, null_foo); 127 foo *F5 = B1.daz(); 128 EXPECT_NE(F5, null_foo); 129 } 130 131 TEST(CastingTest, dyn_cast_or_null) { 132 const foo *F1 = dyn_cast_or_null<foo>(B2); 133 EXPECT_NE(F1, null_foo); 134 const foo *F2 = dyn_cast_or_null<foo>(B2); 135 EXPECT_NE(F2, null_foo); 136 const foo *F3 = dyn_cast_or_null<foo>(B4); 137 EXPECT_NE(F3, null_foo); 138 foo *F4 = dyn_cast_or_null<foo>(fub()); 139 EXPECT_EQ(F4, null_foo); 140 foo *F5 = B1.naz(); 141 EXPECT_NE(F5, null_foo); 142 } 143 144 // These lines are errors... 145 //foo *F20 = cast<foo>(B2); // Yields const foo* 146 //foo &F21 = cast<foo>(B3); // Yields const foo& 147 //foo *F22 = cast<foo>(B4); // Yields const foo* 148 //foo &F23 = cast_or_null<foo>(B1); 149 //const foo &F24 = cast_or_null<foo>(B3); 150 151 const bar *B2 = &B; 152 } // anonymous namespace 153 154 bar *llvm::fub() { return 0; } 155 156 namespace { 157 namespace inferred_upcasting { 158 // This test case verifies correct behavior of inferred upcasts when the 159 // types are statically known to be OK to upcast. This is the case when, 160 // for example, Derived inherits from Base, and we do `isa<Base>(Derived)`. 161 162 // Note: This test will actually fail to compile without inferred 163 // upcasting. 164 165 class Base { 166 public: 167 // No classof. We are testing that the upcast is inferred. 168 Base() {} 169 }; 170 171 class Derived : public Base { 172 public: 173 Derived() {} 174 }; 175 176 // Even with no explicit classof() in Base, we should still be able to cast 177 // Derived to its base class. 178 TEST(CastingTest, UpcastIsInferred) { 179 Derived D; 180 EXPECT_TRUE(isa<Base>(D)); 181 Base *BP = dyn_cast<Base>(&D); 182 EXPECT_TRUE(BP != NULL); 183 } 184 185 186 // This test verifies that the inferred upcast takes precedence over an 187 // explicitly written one. This is important because it verifies that the 188 // dynamic check gets optimized away. 189 class UseInferredUpcast { 190 public: 191 int Dummy; 192 static bool classof(const UseInferredUpcast *) { 193 return false; 194 } 195 }; 196 197 TEST(CastingTest, InferredUpcastTakesPrecedence) { 198 UseInferredUpcast UIU; 199 // Since the explicit classof() returns false, this will fail if the 200 // explicit one is used. 201 EXPECT_TRUE(isa<UseInferredUpcast>(&UIU)); 202 } 203 204 } // end namespace inferred_upcasting 205 } // end anonymous namespace 206