1 //===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/Casting.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include "gtest/gtest.h"
18 // set up two example classes
19 // with conversion facility
32 /* static bool classof(const bar *X) {
33 cerr << "Classof: " << X << "\n";
38 template <> struct isa_impl<foo, bar> {
39 static inline bool doit(const bar &Val) {
40 dbgs() << "Classof: " << &Val << "\n";
46 return cast<foo>(this);
50 return cast_or_null<foo>(this);
54 return dyn_cast<foo>(this);
58 return dyn_cast_or_null<foo>(this);
63 } // End llvm namespace
69 const foo *null_foo = NULL;
75 // test various configurations of const
77 const bar *const B4 = B2;
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));
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);
101 EXPECT_NE(F8, null_foo);
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);
114 EXPECT_NE(F15, null_foo);
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);
128 EXPECT_NE(F5, null_foo);
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);
141 EXPECT_NE(F5, null_foo);
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);
152 } // anonymous namespace
154 bar *llvm::fub() { return 0; }
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)`.
162 // Note: This test will actually fail to compile without inferred
167 // No classof. We are testing that the upcast is inferred.
171 class Derived : public Base {
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) {
180 EXPECT_TRUE(isa<Base>(D));
181 Base *BP = dyn_cast<Base>(&D);
182 EXPECT_TRUE(BP != NULL);
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 {
192 static bool classof(const UseInferredUpcast *) {
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));
204 } // end namespace inferred_upcasting
205 } // end anonymous namespace