• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 namespace llvm {
18 
19 // set up two example classes
20 // with conversion facility
21 //
22 struct bar {
barllvm::bar23   bar() {}
24   struct foo *baz();
25   struct foo *caz();
26   struct foo *daz();
27   struct foo *naz();
28 private:
29   bar(const bar &);
30 };
31 struct foo {
32   void ext() const;
33   /*  static bool classof(const bar *X) {
34     cerr << "Classof: " << X << "\n";
35     return true;
36     }*/
37 };
38 
39 template <> struct isa_impl<foo, bar> {
doitllvm::isa_impl40   static inline bool doit(const bar &Val) {
41     dbgs() << "Classof: " << &Val << "\n";
42     return true;
43   }
44 };
45 
baz()46 foo *bar::baz() {
47     return cast<foo>(this);
48 }
49 
caz()50 foo *bar::caz() {
51     return cast_or_null<foo>(this);
52 }
53 
daz()54 foo *bar::daz() {
55     return dyn_cast<foo>(this);
56 }
57 
naz()58 foo *bar::naz() {
59     return dyn_cast_or_null<foo>(this);
60 }
61 
62 
63 bar *fub();
64 } // End llvm namespace
65 
66 using namespace llvm;
67 
68 namespace {
69 
70 const foo *null_foo = NULL;
71 
72 extern bar &B1;
73 extern const bar *B2;
74 // test various configurations of const
75 const bar &B3 = B1;
76 const bar *const B4 = B2;
77 
TEST(CastingTest,isa)78 TEST(CastingTest, isa) {
79   EXPECT_TRUE(isa<foo>(B1));
80   EXPECT_TRUE(isa<foo>(B2));
81   EXPECT_TRUE(isa<foo>(B3));
82   EXPECT_TRUE(isa<foo>(B4));
83 }
84 
TEST(CastingTest,cast)85 TEST(CastingTest, cast) {
86   foo &F1 = cast<foo>(B1);
87   EXPECT_NE(&F1, null_foo);
88   const foo *F3 = cast<foo>(B2);
89   EXPECT_NE(F3, null_foo);
90   const foo *F4 = cast<foo>(B2);
91   EXPECT_NE(F4, null_foo);
92   const foo &F5 = cast<foo>(B3);
93   EXPECT_NE(&F5, null_foo);
94   const foo *F6 = cast<foo>(B4);
95   EXPECT_NE(F6, null_foo);
96   foo *F7 = cast<foo>(fub());
97   EXPECT_EQ(F7, null_foo);
98   foo *F8 = B1.baz();
99   EXPECT_NE(F8, null_foo);
100 }
101 
TEST(CastingTest,cast_or_null)102 TEST(CastingTest, cast_or_null) {
103   const foo *F11 = cast_or_null<foo>(B2);
104   EXPECT_NE(F11, null_foo);
105   const foo *F12 = cast_or_null<foo>(B2);
106   EXPECT_NE(F12, null_foo);
107   const foo *F13 = cast_or_null<foo>(B4);
108   EXPECT_NE(F13, null_foo);
109   const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
110   EXPECT_EQ(F14, null_foo);
111   foo *F15 = B1.caz();
112   EXPECT_NE(F15, null_foo);
113 }
114 
TEST(CastingTest,dyn_cast)115 TEST(CastingTest, dyn_cast) {
116   const foo *F1 = dyn_cast<foo>(B2);
117   EXPECT_NE(F1, null_foo);
118   const foo *F2 = dyn_cast<foo>(B2);
119   EXPECT_NE(F2, null_foo);
120   const foo *F3 = dyn_cast<foo>(B4);
121   EXPECT_NE(F3, null_foo);
122   // foo *F4 = dyn_cast<foo>(fub()); // not permittible
123   // EXPECT_EQ(F4, null_foo);
124   foo *F5 = B1.daz();
125   EXPECT_NE(F5, null_foo);
126 }
127 
TEST(CastingTest,dyn_cast_or_null)128 TEST(CastingTest, dyn_cast_or_null) {
129   const foo *F1 = dyn_cast_or_null<foo>(B2);
130   EXPECT_NE(F1, null_foo);
131   const foo *F2 = dyn_cast_or_null<foo>(B2);
132   EXPECT_NE(F2, null_foo);
133   const foo *F3 = dyn_cast_or_null<foo>(B4);
134   EXPECT_NE(F3, null_foo);
135   foo *F4 = dyn_cast_or_null<foo>(fub());
136   EXPECT_EQ(F4, null_foo);
137   foo *F5 = B1.naz();
138   EXPECT_NE(F5, null_foo);
139 }
140 
141 // These lines are errors...
142 //foo *F20 = cast<foo>(B2);  // Yields const foo*
143 //foo &F21 = cast<foo>(B3);  // Yields const foo&
144 //foo *F22 = cast<foo>(B4);  // Yields const foo*
145 //foo &F23 = cast_or_null<foo>(B1);
146 //const foo &F24 = cast_or_null<foo>(B3);
147 
148 
149 bar B;
150 bar &B1 = B;
151 const bar *B2 = &B;
152 }  // anonymous namespace
153 
fub()154 bar *llvm::fub() { return 0; }
155