245 lines · cpp
1//=== - llvm/unittest/Support/AlignOfTest.cpp - Alignment utility 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifdef _MSC_VER10// Disable warnings about alignment-based structure padding.11// This must be above the includes to suppress warnings in included templates.12#pragma warning(disable:4324)13#endif14 15#include "llvm/Support/AlignOf.h"16#include "llvm/Support/Compiler.h"17#include "gtest/gtest.h"18 19using namespace llvm;20 21namespace {22// Disable warnings about questionable type definitions.23// We're testing that even questionable types work with the alignment utilities.24#ifdef _MSC_VER25#pragma warning(disable:4584)26#endif27 28// Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'29// due to ambiguity warning.30#ifdef __clang__31#pragma clang diagnostic ignored "-Wunknown-pragmas"32#pragma clang diagnostic ignored "-Winaccessible-base"33#elif defined(__GNUC__)34// Pragma based warning suppression was introduced in GGC 4.2. Additionally35// this warning is "enabled by default". The warning still appears if -Wall is36// suppressed. Apparently GCC suppresses it when -w is specifed, which is odd.37#pragma GCC diagnostic warning "-w"38#endif39 40// Define some fixed alignment types to use in these tests.41struct alignas(1) A1 {};42struct alignas(2) A2 {};43struct alignas(4) A4 {};44struct alignas(8) A8 {};45 46struct S1 {};47struct S2 { char a; };48struct S3 { int x; };49struct S4 { double y; };50struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };51struct S6 { double f(); };52struct D1 : S1 {};53struct D2 : S6 { float g(); };54struct D3 : S2 {};55struct D4 : S2 { int x; };56struct D5 : S3 { char c; };57struct D6 : S2, S3 {};58struct D7 : S1, S3 {};59struct D8 : S1, D4, D5 { double x[2]; };60struct D9 : S1, D1 { S1 s1; };61struct V1 { virtual ~V1(); };62struct V2 { int x; virtual ~V2(); };63struct V3 : V1 {64 ~V3() override;65};66struct V4 : virtual V2 { int y;67 ~V4() override;68};69struct V5 : V4, V3 { double z;70 ~V5() override;71};72struct V6 : S1 { virtual ~V6(); };73struct V7 : virtual V2, virtual V6 {74 ~V7() override;75};76struct V8 : V5, virtual V6, V7 { double zz;77 ~V8() override;78};79 80double S6::f() { return 0.0; }81float D2::g() { return 0.0f; }82V1::~V1() = default;83V2::~V2() = default;84V3::~V3() = default;85V4::~V4() = default;86V5::~V5() = default;87V6::~V6() = default;88V7::~V7() = default;89V8::~V8() = default;90 91template <typename M> struct T { M m; };92 93TEST(AlignOfTest, BasicAlignedArray) {94 EXPECT_LE(1u, alignof(AlignedCharArrayUnion<A1>));95 EXPECT_LE(2u, alignof(AlignedCharArrayUnion<A2>));96 EXPECT_LE(4u, alignof(AlignedCharArrayUnion<A4>));97 EXPECT_LE(8u, alignof(AlignedCharArrayUnion<A8>));98 99 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));100 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));101 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));102 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));103 104 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1>)));105 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1, A2>)));106 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1, A2, A4>)));107 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1, A2, A4, A8>)));108 109 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));110 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));111 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));112 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));113 114 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1[1]>)));115 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1[2], A2[1]>)));116 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1[42], A2[55], A4[13]>)));117 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1[2], A2[1], A4, A8>)));118 119 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1[1]>));120 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));121 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));122 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],123 A4[3], A8>));124 125 // For other tests we simply assert that the alignment of the union mathes126 // that of the fundamental type and hope that we have any weird type127 // productions that would trigger bugs.128 EXPECT_EQ(alignof(T<char>), alignof(AlignedCharArrayUnion<char>));129 EXPECT_EQ(alignof(T<short>), alignof(AlignedCharArrayUnion<short>));130 EXPECT_EQ(alignof(T<int>), alignof(AlignedCharArrayUnion<int>));131 EXPECT_EQ(alignof(T<long>), alignof(AlignedCharArrayUnion<long>));132 EXPECT_EQ(alignof(T<long long>), alignof(AlignedCharArrayUnion<long long>));133 EXPECT_EQ(alignof(T<float>), alignof(AlignedCharArrayUnion<float>));134#ifdef _AIX135 EXPECT_LE(alignof(T<double>), alignof(AlignedCharArrayUnion<double>));136 EXPECT_LE(alignof(T<long double>),137 alignof(AlignedCharArrayUnion<long double>));138 EXPECT_LE(alignof(S4), alignof(AlignedCharArrayUnion<S4>));139#else140 EXPECT_EQ(alignof(T<double>), alignof(AlignedCharArrayUnion<double>));141 EXPECT_EQ(alignof(T<long double>),142 alignof(AlignedCharArrayUnion<long double>));143 EXPECT_EQ(alignof(S4), alignof(AlignedCharArrayUnion<S4>));144#endif145 EXPECT_EQ(alignof(T<void *>), alignof(AlignedCharArrayUnion<void *>));146 EXPECT_EQ(alignof(T<int *>), alignof(AlignedCharArrayUnion<int *>));147 EXPECT_EQ(alignof(T<double (*)(double)>),148 alignof(AlignedCharArrayUnion<double (*)(double)>));149 EXPECT_EQ(alignof(T<double (S6::*)()>),150 alignof(AlignedCharArrayUnion<double (S6::*)()>));151 EXPECT_EQ(alignof(S1), alignof(AlignedCharArrayUnion<S1>));152 EXPECT_EQ(alignof(S2), alignof(AlignedCharArrayUnion<S2>));153 EXPECT_EQ(alignof(S3), alignof(AlignedCharArrayUnion<S3>));154 EXPECT_EQ(alignof(S5), alignof(AlignedCharArrayUnion<S5>));155 EXPECT_EQ(alignof(S6), alignof(AlignedCharArrayUnion<S6>));156 EXPECT_EQ(alignof(D1), alignof(AlignedCharArrayUnion<D1>));157 EXPECT_EQ(alignof(D2), alignof(AlignedCharArrayUnion<D2>));158 EXPECT_EQ(alignof(D3), alignof(AlignedCharArrayUnion<D3>));159 EXPECT_EQ(alignof(D4), alignof(AlignedCharArrayUnion<D4>));160 EXPECT_EQ(alignof(D5), alignof(AlignedCharArrayUnion<D5>));161 EXPECT_EQ(alignof(D6), alignof(AlignedCharArrayUnion<D6>));162 EXPECT_EQ(alignof(D7), alignof(AlignedCharArrayUnion<D7>));163 EXPECT_EQ(alignof(D8), alignof(AlignedCharArrayUnion<D8>));164 EXPECT_EQ(alignof(D9), alignof(AlignedCharArrayUnion<D9>));165 EXPECT_EQ(alignof(V1), alignof(AlignedCharArrayUnion<V1>));166 EXPECT_EQ(alignof(V2), alignof(AlignedCharArrayUnion<V2>));167 EXPECT_EQ(alignof(V3), alignof(AlignedCharArrayUnion<V3>));168 EXPECT_EQ(alignof(V4), alignof(AlignedCharArrayUnion<V4>));169 EXPECT_EQ(alignof(V5), alignof(AlignedCharArrayUnion<V5>));170 EXPECT_EQ(alignof(V6), alignof(AlignedCharArrayUnion<V6>));171 EXPECT_EQ(alignof(V7), alignof(AlignedCharArrayUnion<V7>));172 173 // Some versions of MSVC get this wrong somewhat disturbingly. The failure174 // appears to be benign: alignof(V8) produces a preposterous value: 12175#ifndef _MSC_VER176 EXPECT_EQ(alignof(V8), alignof(AlignedCharArrayUnion<V8>));177#endif178 179 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));180 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));181 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));182 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));183 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));184 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));185 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));186 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));187 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));188 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));189 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));190 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));191 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));192 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));193 EXPECT_EQ(sizeof(long long),194 sizeof(AlignedCharArrayUnion<long long>));195 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));196 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));197 EXPECT_EQ(sizeof(long double),198 sizeof(AlignedCharArrayUnion<long double>));199 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));200 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));201 EXPECT_EQ(sizeof(double (*)(double)),202 sizeof(AlignedCharArrayUnion<double (*)(double)>));203 EXPECT_EQ(sizeof(double (S6::*)()),204 sizeof(AlignedCharArrayUnion<double (S6::*)()>));205 EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));206 EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));207 EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));208 EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));209 EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));210 EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));211 EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));212 EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));213 EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));214 EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));215 EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));216 EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));217 EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));218 EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));219 EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));220 EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));221 EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));222 EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));223 EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));224 EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));225 EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));226 EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));227 EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));228 EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));229 EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));230 EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));231 EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));232 EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));233 EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));234 EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));235 EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));236 EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));237 238 // Some versions of MSVC also get this wrong. The failure again appears to be239 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.240#ifndef _MSC_VER241 EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));242#endif243}244} // end anonymous namespace245