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