1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
//===---------- llvm/unittest/Support/Casting.cpp - Casting tests ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <cstdlib>
namespace llvm {
// set up two example classes
// with conversion facility
//
struct bar {
bar() {}
struct foo *baz();
struct foo *caz();
struct foo *daz();
struct foo *naz();
private:
bar(const bar &);
};
struct foo {
void ext() const;
/* static bool classof(const bar *X) {
cerr << "Classof: " << X << "\n";
return true;
}*/
};
template <> struct isa_impl<foo, bar> {
static inline bool doit(const bar &Val) {
dbgs() << "Classof: " << &Val << "\n";
return true;
}
};
foo *bar::baz() {
return cast<foo>(this);
}
foo *bar::caz() {
return cast_or_null<foo>(this);
}
foo *bar::daz() {
return dyn_cast<foo>(this);
}
foo *bar::naz() {
return dyn_cast_or_null<foo>(this);
}
bar *fub();
} // End llvm namespace
using namespace llvm;
namespace {
const foo *null_foo = NULL;
bar B;
extern bar &B1;
bar &B1 = B;
extern const bar *B2;
// test various configurations of const
const bar &B3 = B1;
const bar *const B4 = B2;
TEST(CastingTest, isa) {
EXPECT_TRUE(isa<foo>(B1));
EXPECT_TRUE(isa<foo>(B2));
EXPECT_TRUE(isa<foo>(B3));
EXPECT_TRUE(isa<foo>(B4));
}
TEST(CastingTest, cast) {
foo &F1 = cast<foo>(B1);
EXPECT_NE(&F1, null_foo);
const foo *F3 = cast<foo>(B2);
EXPECT_NE(F3, null_foo);
const foo *F4 = cast<foo>(B2);
EXPECT_NE(F4, null_foo);
const foo &F5 = cast<foo>(B3);
EXPECT_NE(&F5, null_foo);
const foo *F6 = cast<foo>(B4);
EXPECT_NE(F6, null_foo);
foo *F7 = cast<foo>(fub());
EXPECT_EQ(F7, null_foo);
foo *F8 = B1.baz();
EXPECT_NE(F8, null_foo);
}
TEST(CastingTest, cast_or_null) {
const foo *F11 = cast_or_null<foo>(B2);
EXPECT_NE(F11, null_foo);
const foo *F12 = cast_or_null<foo>(B2);
EXPECT_NE(F12, null_foo);
const foo *F13 = cast_or_null<foo>(B4);
EXPECT_NE(F13, null_foo);
const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
EXPECT_EQ(F14, null_foo);
foo *F15 = B1.caz();
EXPECT_NE(F15, null_foo);
}
TEST(CastingTest, dyn_cast) {
const foo *F1 = dyn_cast<foo>(B2);
EXPECT_NE(F1, null_foo);
const foo *F2 = dyn_cast<foo>(B2);
EXPECT_NE(F2, null_foo);
const foo *F3 = dyn_cast<foo>(B4);
EXPECT_NE(F3, null_foo);
// foo *F4 = dyn_cast<foo>(fub()); // not permittible
// EXPECT_EQ(F4, null_foo);
foo *F5 = B1.daz();
EXPECT_NE(F5, null_foo);
}
TEST(CastingTest, dyn_cast_or_null) {
const foo *F1 = dyn_cast_or_null<foo>(B2);
EXPECT_NE(F1, null_foo);
const foo *F2 = dyn_cast_or_null<foo>(B2);
EXPECT_NE(F2, null_foo);
const foo *F3 = dyn_cast_or_null<foo>(B4);
EXPECT_NE(F3, null_foo);
foo *F4 = dyn_cast_or_null<foo>(fub());
EXPECT_EQ(F4, null_foo);
foo *F5 = B1.naz();
EXPECT_NE(F5, null_foo);
}
// These lines are errors...
//foo *F20 = cast<foo>(B2); // Yields const foo*
//foo &F21 = cast<foo>(B3); // Yields const foo&
//foo *F22 = cast<foo>(B4); // Yields const foo*
//foo &F23 = cast_or_null<foo>(B1);
//const foo &F24 = cast_or_null<foo>(B3);
const bar *B2 = &B;
} // anonymous namespace
bar *llvm::fub() { return 0; }
|