9dea6ff2a904d675324321566d1dacc8b7d99a67
[oota-llvm.git] / unittests / VMCore / DerivedTypesTest.cpp
1 //===- llvm/unittest/VMCore/DerivedTypesTest.cpp - Types unit 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 "gtest/gtest.h"
11 #include "../lib/VMCore/LLVMContextImpl.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/LLVMContext.h"
14 #include "llvm/Constants.h"
15 #include "llvm/Support/ValueHandle.h"
16 using namespace llvm;
17
18 namespace {
19
20 static void PR7658() {
21   LLVMContext ctx;
22   
23   WeakVH NullPtr;
24   PATypeHolder h1;
25   {
26     OpaqueType *o1 = OpaqueType::get(ctx);
27     PointerType *p1 = PointerType::get(o1, 0);
28     
29     std::vector<const Type *> t1;
30     t1.push_back(IntegerType::get(ctx, 32));
31     t1.push_back(p1);
32     NullPtr = ConstantPointerNull::get(p1);
33     OpaqueType *o2 = OpaqueType::get (ctx);
34     PointerType *p2 = PointerType::get (o2, 0);
35     t1.push_back(p2);
36     
37     
38     StructType *s1 = StructType::get(ctx, t1);
39     h1 = s1;
40     o1->refineAbstractTypeTo(s1);
41     o2->refineAbstractTypeTo(h1.get());  // h1 = { i32, \2*, \2* }
42   }
43   
44   
45   OpaqueType *o3 = OpaqueType::get(ctx);
46   PointerType *p3 = PointerType::get(o3, 0);  // p3 = opaque*
47   
48   std::vector<const Type *> t2;
49   t2.push_back(IntegerType::get(ctx, 32));
50   t2.push_back(p3);
51   
52   std::vector<Constant *> v2;
53   v2.push_back(ConstantInt::get(IntegerType::get(ctx, 32), 14));
54   v2.push_back(ConstantPointerNull::get(p3));
55   
56   OpaqueType *o4 = OpaqueType::get(ctx);
57   {
58     PointerType *p4 = PointerType::get(o4, 0);
59     t2.push_back(p4);
60     v2.push_back(ConstantPointerNull::get(p4));
61   }
62   
63   WeakVH CS = ConstantStruct::get(ctx, v2, false); // { i32 14, opaque* null, opaque* null}
64   
65   StructType *s2 = StructType::get(ctx, t2);
66   PATypeHolder h2(s2);
67   o3->refineAbstractTypeTo(s2);
68   o4->refineAbstractTypeTo(h2.get());
69 }
70   
71
72 TEST(OpaqueTypeTest, RegisterWithContext) {
73   LLVMContext C;
74   LLVMContextImpl *pImpl = C.pImpl;
75
76   // 1 refers to the AlwaysOpaqueTy allocated in the Context's constructor and
77   // destroyed in the destructor.
78   EXPECT_EQ(1u, pImpl->OpaqueTypes.size());
79   {
80     PATypeHolder Type = OpaqueType::get(C);
81     EXPECT_EQ(2u, pImpl->OpaqueTypes.size());
82   }
83   EXPECT_EQ(1u, pImpl->OpaqueTypes.size());
84   
85   PR7658();
86 }
87
88 }  // namespace