//===- llvm/unittest/Support/AlignOfTest.cpp - Alignment utility 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/AlignOf.h" #include "llvm/Support/Compiler.h" #include "gtest/gtest.h" using namespace llvm; namespace { // Define some fixed alignment types to use in these tests. #if __cplusplus == 201103L || __has_feature(cxx_alignas) typedef char alignas(1) A1; typedef char alignas(2) A2; typedef char alignas(4) A4; typedef char alignas(8) A8; #elif defined(__clang__) || defined(__GNUC__) typedef char A1 __attribute__((aligned(1))); typedef char A2 __attribute__((aligned(2))); typedef char A4 __attribute__((aligned(4))); typedef char A8 __attribute__((aligned(8))); #elif defined(_MSC_VER) typedef __declspec(align(1)) char A1; typedef __declspec(align(2)) char A2; typedef __declspec(align(4)) char A4; typedef __declspec(align(8)) char A8; #else # error No supported align as directive. #endif // Wrap the forced aligned types in structs to hack around compiler bugs. struct SA1 { A1 a; }; struct SA2 { A2 a; }; struct SA4 { A4 a; }; struct SA8 { A8 a; }; struct S1 {}; struct S2 { char a; }; struct S3 { int x; }; struct S4 { double y; }; struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; }; struct S6 { double f(); }; struct D1 : S1 {}; struct D2 : S6 { float g(); }; struct D3 : S2 {}; struct D4 : S2 { int x; }; struct D5 : S3 { char c; }; struct D6 : S2, S3 {}; struct D7 : S1, S3 {}; struct D8 : S1, D4, D5 { double x[2]; }; struct D9 : S1, D1 { S1 s1; }; struct V1 { virtual ~V1(); }; struct V2 { int x; virtual ~V2(); }; struct V3 : V1 { virtual ~V3(); }; struct V4 : virtual V2 { int y; virtual ~V4(); }; struct V5 : V4, V3 { double z; virtual ~V5(); }; struct V6 : S1 { virtual ~V6(); }; struct V7 : virtual V2, virtual V6 { virtual ~V7(); }; struct V8 : V5, virtual V6, V7 { double zz; virtual ~V8(); }; // Ensure alignment is a compile-time constant. char LLVM_ATTRIBUTE_UNUSED test_arr1 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0]; char LLVM_ATTRIBUTE_UNUSED test_arr2 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0]; char LLVM_ATTRIBUTE_UNUSED test_arr3 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0]; char LLVM_ATTRIBUTE_UNUSED test_arr4 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0]; char LLVM_ATTRIBUTE_UNUSED test_arr5 [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0] [AlignOf::Alignment > 0]; TEST(AlignOfTest, BasicAlignmentInvariants) { // For a very strange reason, many compilers do not support this. Both Clang // and GCC fail to align these properly. EXPECT_EQ(1u, alignOf()); #if 0 EXPECT_EQ(2u, alignOf()); EXPECT_EQ(4u, alignOf()); EXPECT_EQ(8u, alignOf()); #endif // But once wrapped in structs, the alignment is correctly managed. EXPECT_LE(1u, alignOf()); EXPECT_LE(2u, alignOf()); EXPECT_LE(4u, alignOf()); EXPECT_LE(8u, alignOf()); EXPECT_EQ(1u, alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_EQ(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); EXPECT_LE(alignOf(), alignOf()); } TEST(AlignOfTest, BasicAlignedArray) { // Note: this code exclusively uses the struct-wrapped arbitrarily aligned // types because of the bugs mentioned above where GCC and Clang both // disregard the arbitrary alignment specifier until the type is used to // declare a member of a struct. EXPECT_LE(1u, alignOf::union_type>()); EXPECT_LE(2u, alignOf::union_type>()); EXPECT_LE(4u, alignOf::union_type>()); EXPECT_LE(8u, alignOf::union_type>()); EXPECT_LE(1u, sizeof(AlignedCharArray::union_type)); EXPECT_LE(2u, sizeof(AlignedCharArray::union_type)); EXPECT_LE(4u, sizeof(AlignedCharArray::union_type)); EXPECT_LE(8u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(1u, (alignOf::union_type>())); EXPECT_EQ(2u, (alignOf::union_type>())); EXPECT_EQ(4u, (alignOf::union_type>())); EXPECT_EQ(8u, (alignOf::union_type>())); EXPECT_EQ(1u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(2u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(4u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(8u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(1u, (alignOf::union_type>())); EXPECT_EQ(2u, (alignOf::union_type>())); EXPECT_EQ(4u, (alignOf::union_type>())); EXPECT_EQ(8u, (alignOf::union_type>())); EXPECT_EQ(1u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(2u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(4u, sizeof(AlignedCharArray::union_type)); EXPECT_EQ(16u, sizeof(AlignedCharArray::union_type)); // For other tests we simply assert that the alignment of the union mathes // that of the fundamental type and hope that we have any weird type // productions that would trigger bugs. EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); EXPECT_EQ(alignOf(), alignOf::union_type>()); // Some versions of MSVC get this wrong somewhat disturbingly. The failure // appears to be benign: alignOf() produces a preposterous value: 12 #ifndef _MSC_VER EXPECT_EQ(alignOf(), alignOf::union_type>()); #endif EXPECT_EQ(sizeof(char), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(short), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(int), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(long), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(long long), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(float), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(double), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(long double), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(double (*)(double)), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(double (S6::*)()), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArray::union_type)); EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArray::union_type)); // Some versions of MSVC also get this wrong. The failure again appears to be // benign: sizeof(V8) is only 52 bytes, but our array reserves 56. #ifndef _MSC_VER EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArray::union_type)); #endif } }