Home | History | Annotate | Download | only in Support
      1 //=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility 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 "llvm/Support/AlignOf.h"
     11 #include "llvm/Support/Compiler.h"
     12 #include "gtest/gtest.h"
     13 
     14 using namespace llvm;
     15 
     16 namespace {
     17 // Disable warnings about questionable type definitions.
     18 // We're testing that even questionable types work with the alignment utilities.
     19 #ifdef _MSC_VER
     20 #pragma warning(disable:4584)
     21 #endif
     22 
     23 // Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
     24 // due to ambiguity warning.
     25 //
     26 // Pragma based warning suppression was introduced in GGC 4.2.  Additionally
     27 // this warning is "enabled by default".  The warning still appears if -Wall is
     28 // suppressed.  Apparently GCC suppresses it when -w is specifed, which is odd.
     29 // At any rate, clang on the other hand gripes about -Wunknown-pragma, so
     30 // leaving it out of this.
     31 #if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402 && !defined(__clang__)
     32 #pragma GCC diagnostic warning "-w"
     33 #endif
     34 
     35 // Define some fixed alignment types to use in these tests.
     36 #if __has_feature(cxx_alignas)
     37 struct alignas(1) A1 { };
     38 struct alignas(2) A2 { };
     39 struct alignas(4) A4 { };
     40 struct alignas(8) A8 { };
     41 #elif defined(__GNUC__)
     42 struct A1 { } __attribute__((aligned(1)));
     43 struct A2 { } __attribute__((aligned(2)));
     44 struct A4 { } __attribute__((aligned(4)));
     45 struct A8 { } __attribute__((aligned(8)));
     46 #elif defined(_MSC_VER)
     47 __declspec(align(1)) struct A1 { };
     48 __declspec(align(2)) struct A2 { };
     49 __declspec(align(4)) struct A4 { };
     50 __declspec(align(8)) struct A8 { };
     51 #else
     52 # error No supported align as directive.
     53 #endif
     54 
     55 struct S1 {};
     56 struct S2 { char a; };
     57 struct S3 { int x; };
     58 struct S4 { double y; };
     59 struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
     60 struct S6 { double f(); };
     61 struct D1 : S1 {};
     62 struct D2 : S6 { float g(); };
     63 struct D3 : S2 {};
     64 struct D4 : S2 { int x; };
     65 struct D5 : S3 { char c; };
     66 struct D6 : S2, S3 {};
     67 struct D7 : S1, S3 {};
     68 struct D8 : S1, D4, D5 { double x[2]; };
     69 struct D9 : S1, D1 { S1 s1; };
     70 struct V1 { virtual ~V1(); };
     71 struct V2 { int x; virtual ~V2(); };
     72 struct V3 : V1 { virtual ~V3(); };
     73 struct V4 : virtual V2 { int y; virtual ~V4(); };
     74 struct V5 : V4, V3 { double z; virtual ~V5(); };
     75 struct V6 : S1 { virtual ~V6(); };
     76 struct V7 : virtual V2, virtual V6 { virtual ~V7(); };
     77 struct V8 : V5, virtual V6, V7 { double zz; virtual ~V8(); };
     78 
     79 double S6::f() { return 0.0; }
     80 float D2::g() { return 0.0f; }
     81 V1::~V1() {}
     82 V2::~V2() {}
     83 V3::~V3() {}
     84 V4::~V4() {}
     85 V5::~V5() {}
     86 V6::~V6() {}
     87 V7::~V7() {}
     88 V8::~V8() {}
     89 
     90 // Ensure alignment is a compile-time constant.
     91 char LLVM_ATTRIBUTE_UNUSED test_arr1
     92   [AlignOf<char>::Alignment > 0]
     93   [AlignOf<short>::Alignment > 0]
     94   [AlignOf<int>::Alignment > 0]
     95   [AlignOf<long>::Alignment > 0]
     96   [AlignOf<long long>::Alignment > 0]
     97   [AlignOf<float>::Alignment > 0]
     98   [AlignOf<double>::Alignment > 0]
     99   [AlignOf<long double>::Alignment > 0]
    100   [AlignOf<void *>::Alignment > 0]
    101   [AlignOf<int *>::Alignment > 0]
    102   [AlignOf<double (*)(double)>::Alignment > 0]
    103   [AlignOf<double (S6::*)()>::Alignment > 0];
    104 char LLVM_ATTRIBUTE_UNUSED test_arr2
    105   [AlignOf<A1>::Alignment > 0]
    106   [AlignOf<A2>::Alignment > 0]
    107   [AlignOf<A4>::Alignment > 0]
    108   [AlignOf<A8>::Alignment > 0];
    109 char LLVM_ATTRIBUTE_UNUSED test_arr3
    110   [AlignOf<S1>::Alignment > 0]
    111   [AlignOf<S2>::Alignment > 0]
    112   [AlignOf<S3>::Alignment > 0]
    113   [AlignOf<S4>::Alignment > 0]
    114   [AlignOf<S5>::Alignment > 0]
    115   [AlignOf<S6>::Alignment > 0];
    116 char LLVM_ATTRIBUTE_UNUSED test_arr4
    117   [AlignOf<D1>::Alignment > 0]
    118   [AlignOf<D2>::Alignment > 0]
    119   [AlignOf<D3>::Alignment > 0]
    120   [AlignOf<D4>::Alignment > 0]
    121   [AlignOf<D5>::Alignment > 0]
    122   [AlignOf<D6>::Alignment > 0]
    123   [AlignOf<D7>::Alignment > 0]
    124   [AlignOf<D8>::Alignment > 0]
    125   [AlignOf<D9>::Alignment > 0];
    126 char LLVM_ATTRIBUTE_UNUSED test_arr5
    127   [AlignOf<V1>::Alignment > 0]
    128   [AlignOf<V2>::Alignment > 0]
    129   [AlignOf<V3>::Alignment > 0]
    130   [AlignOf<V4>::Alignment > 0]
    131   [AlignOf<V5>::Alignment > 0]
    132   [AlignOf<V6>::Alignment > 0]
    133   [AlignOf<V7>::Alignment > 0]
    134   [AlignOf<V8>::Alignment > 0];
    135 
    136 TEST(AlignOfTest, BasicAlignmentInvariants) {
    137   EXPECT_LE(1u, alignOf<A1>());
    138   EXPECT_LE(2u, alignOf<A2>());
    139   EXPECT_LE(4u, alignOf<A4>());
    140   EXPECT_LE(8u, alignOf<A8>());
    141 
    142   EXPECT_EQ(1u, alignOf<char>());
    143   EXPECT_LE(alignOf<char>(),   alignOf<short>());
    144   EXPECT_LE(alignOf<short>(),  alignOf<int>());
    145   EXPECT_LE(alignOf<int>(),    alignOf<long>());
    146   EXPECT_LE(alignOf<long>(),   alignOf<long long>());
    147   EXPECT_LE(alignOf<char>(),   alignOf<float>());
    148   EXPECT_LE(alignOf<float>(),  alignOf<double>());
    149   EXPECT_LE(alignOf<char>(),   alignOf<long double>());
    150   EXPECT_LE(alignOf<char>(),   alignOf<void *>());
    151   EXPECT_EQ(alignOf<void *>(), alignOf<int *>());
    152   EXPECT_LE(alignOf<char>(),   alignOf<S1>());
    153   EXPECT_LE(alignOf<S1>(),     alignOf<S2>());
    154   EXPECT_LE(alignOf<S1>(),     alignOf<S3>());
    155   EXPECT_LE(alignOf<S1>(),     alignOf<S4>());
    156   EXPECT_LE(alignOf<S1>(),     alignOf<S5>());
    157   EXPECT_LE(alignOf<S1>(),     alignOf<S6>());
    158   EXPECT_LE(alignOf<S1>(),     alignOf<D1>());
    159   EXPECT_LE(alignOf<S1>(),     alignOf<D2>());
    160   EXPECT_LE(alignOf<S1>(),     alignOf<D3>());
    161   EXPECT_LE(alignOf<S1>(),     alignOf<D4>());
    162   EXPECT_LE(alignOf<S1>(),     alignOf<D5>());
    163   EXPECT_LE(alignOf<S1>(),     alignOf<D6>());
    164   EXPECT_LE(alignOf<S1>(),     alignOf<D7>());
    165   EXPECT_LE(alignOf<S1>(),     alignOf<D8>());
    166   EXPECT_LE(alignOf<S1>(),     alignOf<D9>());
    167   EXPECT_LE(alignOf<S1>(),     alignOf<V1>());
    168   EXPECT_LE(alignOf<V1>(),     alignOf<V2>());
    169   EXPECT_LE(alignOf<V1>(),     alignOf<V3>());
    170   EXPECT_LE(alignOf<V1>(),     alignOf<V4>());
    171   EXPECT_LE(alignOf<V1>(),     alignOf<V5>());
    172   EXPECT_LE(alignOf<V1>(),     alignOf<V6>());
    173   EXPECT_LE(alignOf<V1>(),     alignOf<V7>());
    174   EXPECT_LE(alignOf<V1>(),     alignOf<V8>());
    175 }
    176 
    177 TEST(AlignOfTest, BasicAlignedArray) {
    178   EXPECT_LE(1u, alignOf<AlignedCharArrayUnion<A1> >());
    179   EXPECT_LE(2u, alignOf<AlignedCharArrayUnion<A2> >());
    180   EXPECT_LE(4u, alignOf<AlignedCharArrayUnion<A4> >());
    181   EXPECT_LE(8u, alignOf<AlignedCharArrayUnion<A8> >());
    182 
    183   EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
    184   EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
    185   EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
    186   EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
    187 
    188   EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1> >()));
    189   EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1, A2> >()));
    190   EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1, A2, A4> >()));
    191   EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1, A2, A4, A8> >()));
    192 
    193   EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
    194   EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
    195   EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
    196   EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
    197 
    198   EXPECT_EQ(1u, (alignOf<AlignedCharArrayUnion<A1[1]> >()));
    199   EXPECT_EQ(2u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1]> >()));
    200   EXPECT_EQ(4u, (alignOf<AlignedCharArrayUnion<A1[42], A2[55],
    201                                                A4[13]> >()));
    202   EXPECT_EQ(8u, (alignOf<AlignedCharArrayUnion<A1[2], A2[1],
    203                                                A4, A8> >()));
    204 
    205   EXPECT_EQ(1u,  sizeof(AlignedCharArrayUnion<A1[1]>));
    206   EXPECT_EQ(2u,  sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
    207   EXPECT_EQ(4u,  sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
    208   EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
    209                                               A4[3], A8>));
    210 
    211   // For other tests we simply assert that the alignment of the union mathes
    212   // that of the fundamental type and hope that we have any weird type
    213   // productions that would trigger bugs.
    214   EXPECT_EQ(alignOf<char>(), alignOf<AlignedCharArrayUnion<char> >());
    215   EXPECT_EQ(alignOf<short>(), alignOf<AlignedCharArrayUnion<short> >());
    216   EXPECT_EQ(alignOf<int>(), alignOf<AlignedCharArrayUnion<int> >());
    217   EXPECT_EQ(alignOf<long>(), alignOf<AlignedCharArrayUnion<long> >());
    218   EXPECT_EQ(alignOf<long long>(),
    219             alignOf<AlignedCharArrayUnion<long long> >());
    220   EXPECT_EQ(alignOf<float>(), alignOf<AlignedCharArrayUnion<float> >());
    221   EXPECT_EQ(alignOf<double>(), alignOf<AlignedCharArrayUnion<double> >());
    222   EXPECT_EQ(alignOf<long double>(),
    223             alignOf<AlignedCharArrayUnion<long double> >());
    224   EXPECT_EQ(alignOf<void *>(), alignOf<AlignedCharArrayUnion<void *> >());
    225   EXPECT_EQ(alignOf<int *>(), alignOf<AlignedCharArrayUnion<int *> >());
    226   EXPECT_EQ(alignOf<double (*)(double)>(),
    227             alignOf<AlignedCharArrayUnion<double (*)(double)> >());
    228   EXPECT_EQ(alignOf<double (S6::*)()>(),
    229             alignOf<AlignedCharArrayUnion<double (S6::*)()> >());
    230   EXPECT_EQ(alignOf<S1>(), alignOf<AlignedCharArrayUnion<S1> >());
    231   EXPECT_EQ(alignOf<S2>(), alignOf<AlignedCharArrayUnion<S2> >());
    232   EXPECT_EQ(alignOf<S3>(), alignOf<AlignedCharArrayUnion<S3> >());
    233   EXPECT_EQ(alignOf<S4>(), alignOf<AlignedCharArrayUnion<S4> >());
    234   EXPECT_EQ(alignOf<S5>(), alignOf<AlignedCharArrayUnion<S5> >());
    235   EXPECT_EQ(alignOf<S6>(), alignOf<AlignedCharArrayUnion<S6> >());
    236   EXPECT_EQ(alignOf<D1>(), alignOf<AlignedCharArrayUnion<D1> >());
    237   EXPECT_EQ(alignOf<D2>(), alignOf<AlignedCharArrayUnion<D2> >());
    238   EXPECT_EQ(alignOf<D3>(), alignOf<AlignedCharArrayUnion<D3> >());
    239   EXPECT_EQ(alignOf<D4>(), alignOf<AlignedCharArrayUnion<D4> >());
    240   EXPECT_EQ(alignOf<D5>(), alignOf<AlignedCharArrayUnion<D5> >());
    241   EXPECT_EQ(alignOf<D6>(), alignOf<AlignedCharArrayUnion<D6> >());
    242   EXPECT_EQ(alignOf<D7>(), alignOf<AlignedCharArrayUnion<D7> >());
    243   EXPECT_EQ(alignOf<D8>(), alignOf<AlignedCharArrayUnion<D8> >());
    244   EXPECT_EQ(alignOf<D9>(), alignOf<AlignedCharArrayUnion<D9> >());
    245   EXPECT_EQ(alignOf<V1>(), alignOf<AlignedCharArrayUnion<V1> >());
    246   EXPECT_EQ(alignOf<V2>(), alignOf<AlignedCharArrayUnion<V2> >());
    247   EXPECT_EQ(alignOf<V3>(), alignOf<AlignedCharArrayUnion<V3> >());
    248   EXPECT_EQ(alignOf<V4>(), alignOf<AlignedCharArrayUnion<V4> >());
    249   EXPECT_EQ(alignOf<V5>(), alignOf<AlignedCharArrayUnion<V5> >());
    250   EXPECT_EQ(alignOf<V6>(), alignOf<AlignedCharArrayUnion<V6> >());
    251   EXPECT_EQ(alignOf<V7>(), alignOf<AlignedCharArrayUnion<V7> >());
    252 
    253   // Some versions of MSVC get this wrong somewhat disturbingly. The failure
    254   // appears to be benign: alignOf<V8>() produces a preposterous value: 12
    255 #ifndef _MSC_VER
    256   EXPECT_EQ(alignOf<V8>(), alignOf<AlignedCharArrayUnion<V8> >());
    257 #endif
    258 
    259   EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
    260   EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
    261   EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
    262   EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
    263   EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
    264   EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
    265   EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
    266   EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
    267   EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
    268   EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
    269   EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
    270   EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
    271   EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
    272   EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
    273   EXPECT_EQ(sizeof(long long),
    274             sizeof(AlignedCharArrayUnion<long long>));
    275   EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
    276   EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
    277   EXPECT_EQ(sizeof(long double),
    278             sizeof(AlignedCharArrayUnion<long double>));
    279   EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
    280   EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
    281   EXPECT_EQ(sizeof(double (*)(double)),
    282             sizeof(AlignedCharArrayUnion<double (*)(double)>));
    283   EXPECT_EQ(sizeof(double (S6::*)()),
    284             sizeof(AlignedCharArrayUnion<double (S6::*)()>));
    285   EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
    286   EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
    287   EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
    288   EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
    289   EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
    290   EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
    291   EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
    292   EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
    293   EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
    294   EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
    295   EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
    296   EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
    297   EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
    298   EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
    299   EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
    300   EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
    301   EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
    302   EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
    303   EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
    304   EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
    305   EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
    306   EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
    307   EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
    308   EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
    309   EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
    310   EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
    311   EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
    312   EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
    313   EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
    314   EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
    315   EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
    316   EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
    317 
    318   // Some versions of MSVC also get this wrong. The failure again appears to be
    319   // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
    320 #ifndef _MSC_VER
    321   EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
    322 #endif
    323 
    324   EXPECT_EQ(1u, (alignOf<AlignedCharArray<1, 1> >()));
    325   EXPECT_EQ(2u, (alignOf<AlignedCharArray<2, 1> >()));
    326   EXPECT_EQ(4u, (alignOf<AlignedCharArray<4, 1> >()));
    327   EXPECT_EQ(8u, (alignOf<AlignedCharArray<8, 1> >()));
    328   EXPECT_EQ(16u, (alignOf<AlignedCharArray<16, 1> >()));
    329 
    330   EXPECT_EQ(1u, sizeof(AlignedCharArray<1, 1>));
    331   EXPECT_EQ(7u, sizeof(AlignedCharArray<1, 7>));
    332   EXPECT_EQ(2u, sizeof(AlignedCharArray<2, 2>));
    333   EXPECT_EQ(16u, sizeof(AlignedCharArray<2, 16>));
    334 }
    335 }
    336