| 1 |
//===- llvm/unittest/IR/TypesTest.cpp - Type unit tests -------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/IR/DerivedTypes.h" |
| 10 |
#include "llvm/IR/LLVMContext.h" |
| 11 |
#include "llvm/IR/TypedPointerType.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
using namespace llvm; |
| 14 |
|
| 15 |
namespace { |
| 16 |
|
| 17 |
TEST(TypesTest, StructType) { |
| 18 |
LLVMContext C; |
| 19 |
|
| 20 |
// PR13522 |
| 21 |
StructType *Struct = StructType::create(C, "FooBar"); |
| 22 |
EXPECT_EQ("FooBar", Struct->getName()); |
| 23 |
Struct->setName(Struct->getName().substr(0, 3)); |
| 24 |
EXPECT_EQ("Foo", Struct->getName()); |
| 25 |
Struct->setName(""); |
| 26 |
EXPECT_TRUE(Struct->getName().empty()); |
| 27 |
EXPECT_FALSE(Struct->hasName()); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(TypesTest, LayoutIdenticalEmptyStructs) { |
| 31 |
LLVMContext C; |
| 32 |
|
| 33 |
StructType *Foo = StructType::create(C, "Foo"); |
| 34 |
StructType *Bar = StructType::create(C, "Bar"); |
| 35 |
EXPECT_TRUE(Foo->isLayoutIdentical(Bar)); |
| 36 |
} |
| 37 |
|
| 38 |
TEST(TypesTest, CopyPointerType) { |
| 39 |
LLVMContext COpaquePointers; |
| 40 |
COpaquePointers.setOpaquePointers(true); |
| 41 |
|
| 42 |
PointerType *P1 = PointerType::get(COpaquePointers, 1); |
| 43 |
EXPECT_TRUE(P1->isOpaque()); |
| 44 |
PointerType *P1C = PointerType::getWithSamePointeeType(P1, 1); |
| 45 |
EXPECT_EQ(P1, P1C); |
| 46 |
EXPECT_TRUE(P1C->isOpaque()); |
| 47 |
PointerType *P1C0 = PointerType::getWithSamePointeeType(P1, 0); |
| 48 |
EXPECT_NE(P1, P1C0); |
| 49 |
EXPECT_TRUE(P1C0->isOpaque()); |
| 50 |
|
| 51 |
LLVMContext CTypedPointers; |
| 52 |
CTypedPointers.setOpaquePointers(false); |
| 53 |
Type *Int8 = Type::getInt8Ty(CTypedPointers); |
| 54 |
PointerType *P2 = PointerType::get(Int8, 1); |
| 55 |
EXPECT_FALSE(P2->isOpaque()); |
| 56 |
PointerType *P2C = PointerType::getWithSamePointeeType(P2, 1); |
| 57 |
EXPECT_EQ(P2, P2C); |
| 58 |
EXPECT_FALSE(P2C->isOpaque()); |
| 59 |
PointerType *P2C0 = PointerType::getWithSamePointeeType(P2, 0); |
| 60 |
EXPECT_NE(P2, P2C0); |
| 61 |
EXPECT_FALSE(P2C0->isOpaque()); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(TypesTest, TargetExtType) { |
| 65 |
LLVMContext Context; |
| 66 |
Type *A = TargetExtType::get(Context, "typea"); |
| 67 |
Type *Aparam = TargetExtType::get(Context, "typea", {}, {0, 1}); |
| 68 |
Type *Aparam2 = TargetExtType::get(Context, "typea", {}, {0, 1}); |
| 69 |
// Opaque types with same parameters are identical... |
| 70 |
EXPECT_EQ(Aparam, Aparam2); |
| 71 |
// ... but just having the same name is not enough. |
| 72 |
EXPECT_NE(A, Aparam); |
| 73 |
} |
| 74 |
|
| 75 |
TEST(TypedPointerType, PrintTest) { |
| 76 |
std::string Buffer; |
| 77 |
LLVMContext Context; |
| 78 |
raw_string_ostream OS(Buffer); |
| 79 |
|
| 80 |
Type *I8Ptr = TypedPointerType::get(Type::getInt8Ty(Context), 0); |
| 81 |
I8Ptr->print(OS); |
| 82 |
EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)")); |
| 83 |
} |
| 84 |
|
| 85 |
} // end anonymous namespace |