From 096fd7b5a7b19912bfedeaef870a0c655f16e325 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 25 Apr 2009 22:14:04 +0000 Subject: Add a new TypeBuilder helper class, which eases making LLVM IR types. Patch by Jeffrey Yasskin! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70084 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/TypeBuilderTest.cpp | 233 ++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 unittests/Support/TypeBuilderTest.cpp (limited to 'unittests/Support') diff --git a/unittests/Support/TypeBuilderTest.cpp b/unittests/Support/TypeBuilderTest.cpp new file mode 100644 index 0000000..425ee6b --- /dev/null +++ b/unittests/Support/TypeBuilderTest.cpp @@ -0,0 +1,233 @@ +//===- llvm/unittest/Support/TypeBuilderTest.cpp - TypeBuilder 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/TypeBuilder.h" + +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +TEST(TypeBuilderTest, Void) { + EXPECT_EQ(Type::VoidTy, (TypeBuilder::get())); + EXPECT_EQ(Type::VoidTy, (TypeBuilder::get())); + // Special case for C compatibility: + EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty), + (TypeBuilder::get())); +} + +TEST(TypeBuilderTest, HostIntegers) { + EXPECT_EQ(Type::Int8Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int8Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int16Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int16Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int32Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int32Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int64Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::Int64Ty, (TypeBuilder::get())); + + EXPECT_EQ(IntegerType::get(sizeof(size_t) * CHAR_BIT), + (TypeBuilder::get())); + EXPECT_EQ(IntegerType::get(sizeof(ptrdiff_t) * CHAR_BIT), + (TypeBuilder::get())); +} + +TEST(TypeBuilderTest, CrossCompilableIntegers) { + EXPECT_EQ(IntegerType::get(1), (TypeBuilder, true>::get())); + EXPECT_EQ(IntegerType::get(1), (TypeBuilder, false>::get())); + EXPECT_EQ(IntegerType::get(72), (TypeBuilder, true>::get())); + EXPECT_EQ(IntegerType::get(72), (TypeBuilder, false>::get())); +} + +TEST(TypeBuilderTest, Float) { + EXPECT_EQ(Type::FloatTy, (TypeBuilder::get())); + EXPECT_EQ(Type::DoubleTy, (TypeBuilder::get())); + // long double isn't supported yet. + EXPECT_EQ(Type::FloatTy, (TypeBuilder::get())); + EXPECT_EQ(Type::FloatTy, (TypeBuilder::get())); + EXPECT_EQ(Type::DoubleTy, (TypeBuilder::get())); + EXPECT_EQ(Type::DoubleTy, (TypeBuilder::get())); + EXPECT_EQ(Type::X86_FP80Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::X86_FP80Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::FP128Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::FP128Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::PPC_FP128Ty, (TypeBuilder::get())); + EXPECT_EQ(Type::PPC_FP128Ty, (TypeBuilder::get())); +} + +TEST(TypeBuilderTest, Derived) { + EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)), + (TypeBuilder::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7), + (TypeBuilder::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0), + (TypeBuilder::get())); + + EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)), + (TypeBuilder**, false>::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7), + (TypeBuilder[7], false>::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0), + (TypeBuilder[], false>::get())); + + EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)), + (TypeBuilder**, true>::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7), + (TypeBuilder[7], true>::get())); + EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0), + (TypeBuilder[], true>::get())); + + + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder::get())); + + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, false>::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, false>::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, false>::get())); + + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, true>::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, true>::get())); + EXPECT_EQ(Type::Int8Ty, + (TypeBuilder, true>::get())); + + EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty), + (TypeBuilder::get())); +} + +TEST(TypeBuilderTest, Functions) { + std::vector params; + EXPECT_EQ(FunctionType::get(Type::VoidTy, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); + params.push_back(TypeBuilder::get()); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); + params.push_back(TypeBuilder::get()); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); + params.push_back(TypeBuilder::get()); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); + params.push_back(TypeBuilder::get()); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); + params.push_back(TypeBuilder::get()); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false), + (TypeBuilder::get())); + EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true), + (TypeBuilder::get())); +} + +class MyType { + int a; + int *b; + void *array[1]; +}; + +class MyPortableType { + int32_t a; + int32_t *b; + void *array[1]; +}; + +} // anonymous namespace + +namespace llvm { +template class TypeBuilder { +public: + static const StructType *get() { + // Using the static result variable ensures that the type is + // only looked up once. + static const StructType *const result = StructType::get( + TypeBuilder::get(), + TypeBuilder::get(), + TypeBuilder::get(), + NULL); + return result; + } + + // You may find this a convenient place to put some constants + // to help with getelementptr. They don't have any effect on + // the operation of TypeBuilder. + enum Fields { + FIELD_A, + FIELD_B, + FIELD_ARRAY + }; +}; + +template class TypeBuilder { +public: + static const StructType *get() { + // Using the static result variable ensures that the type is + // only looked up once. + static const StructType *const result = StructType::get( + TypeBuilder, cross>::get(), + TypeBuilder*, cross>::get(), + TypeBuilder*[], cross>::get(), + NULL); + return result; + } + + // You may find this a convenient place to put some constants + // to help with getelementptr. They don't have any effect on + // the operation of TypeBuilder. + enum Fields { + FIELD_A, + FIELD_B, + FIELD_ARRAY + }; +}; +} // namespace llvm +namespace { + +TEST(TypeBuilderTest, Extensions) { + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder::get(), + TypeBuilder::get(), + TypeBuilder::get(), + NULL)), + (TypeBuilder::get())); + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder, false>::get(), + TypeBuilder*, false>::get(), + TypeBuilder*[], false>::get(), + NULL)), + (TypeBuilder::get())); + EXPECT_EQ(PointerType::getUnqual(StructType::get( + TypeBuilder, false>::get(), + TypeBuilder*, false>::get(), + TypeBuilder*[], false>::get(), + NULL)), + (TypeBuilder::get())); +} + +} // anonymous namespace -- cgit v1.1