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, TargetExtType) { 39 LLVMContext Context; 40 Type *A = TargetExtType::get(Context, "typea"); 41 Type *Aparam = TargetExtType::get(Context, "typea", {}, {0, 1}); 42 Type *Aparam2 = TargetExtType::get(Context, "typea", {}, {0, 1}); 43 44 // Opaque types with same parameters are identical... 45 EXPECT_EQ(Aparam, Aparam2); 46 // ... but just having the same name is not enough. 47 EXPECT_NE(A, Aparam); 48 49 // ensure struct types in targest extension types 50 // only show the struct name, not the struct body 51 Type *Int32Type = Type::getInt32Ty(Context); 52 Type *FloatType = Type::getFloatTy(Context); 53 std::array<Type *, 2> Elements = {Int32Type, FloatType}; 54 55 StructType *Struct = llvm::StructType::create(Context, Elements, "MyStruct", 56 /*isPacked=*/false); 57 SmallVector<char, 50> TETV; 58 llvm::raw_svector_ostream TETStream(TETV); 59 Type *TargetExtensionType = 60 TargetExtType::get(Context, "structTET", {Struct}, {0, 1}); 61 TargetExtensionType->print(TETStream); 62 63 EXPECT_STREQ(TETStream.str().str().data(), 64 "target(\"structTET\", %MyStruct, 0, 1)"); 65 66 // ensure that literal structs in the target extension type print the struct 67 // body 68 Struct = StructType::get(Context, Struct->elements(), /*isPacked=*/false); 69 70 TargetExtensionType = 71 TargetExtType::get(Context, "structTET", {Struct}, {0, 1}); 72 TETV.clear(); 73 TargetExtensionType->print(TETStream); 74 75 EXPECT_STREQ(TETStream.str().str().data(), 76 "target(\"structTET\", { i32, float }, 0, 1)"); 77 } 78 79 TEST(TypedPointerType, PrintTest) { 80 std::string Buffer; 81 LLVMContext Context; 82 raw_string_ostream OS(Buffer); 83 84 Type *I8Ptr = TypedPointerType::get(Type::getInt8Ty(Context), 0); 85 I8Ptr->print(OS); 86 EXPECT_EQ(StringRef(Buffer), ("typedptr(i8, 0)")); 87 } 88 89 } // end anonymous namespace 90