brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.7 KiB · 9184a4d Raw
275 lines · cpp
1//=== - llvm/unittest/Support/TrailingObjectsTest.cpp ---------------------===//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#include "llvm/Support/TrailingObjects.h"10#include "llvm/ADT/ArrayRef.h"11#include "llvm/ADT/STLExtras.h"12#include "gtest/gtest.h"13 14using namespace llvm;15 16namespace {17// This class, beyond being used by the test case, a nice18// demonstration of the intended usage of TrailingObjects, with a19// single trailing array.20class Class1 final : private TrailingObjects<Class1, short> {21  friend TrailingObjects;22 23  unsigned NumShorts;24 25protected:26  Class1(ArrayRef<int> ShortArray) : NumShorts(ShortArray.size()) {27    // This tests the non-templated getTrailingObjects() that returns a pointer28    // when using a single trailing type.29    llvm::copy(ShortArray, getTrailingObjects());30  }31 32public:33  static Class1 *create(ArrayRef<int> ShortArray) {34    void *Mem = ::operator new(totalSizeToAlloc<short>(ShortArray.size()));35    return new (Mem) Class1(ShortArray);36  }37  void operator delete(void *Ptr) { ::operator delete(Ptr); }38 39  // This indexes into the ArrayRef<> returned by `getTrailingObjects`.40  short get(unsigned Num) const { return getTrailingObjects(NumShorts)[Num]; }41 42  unsigned numShorts() const { return NumShorts; }43 44  // Pull some protected members in as public, for testability.45  template <typename... Ty>46  using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;47 48  using TrailingObjects::additionalSizeToAlloc;49  using TrailingObjects::getTrailingObjects;50  using TrailingObjects::getTrailingObjectsNonStrict;51  using TrailingObjects::totalSizeToAlloc;52};53 54// Here, there are two singular optional object types appended. Note55// that the alignment of Class2 is automatically increased to account56// for the alignment requirements of the trailing objects.57class Class2 final : private TrailingObjects<Class2, double, short> {58  friend TrailingObjects;59 60  bool HasShort, HasDouble;61 62protected:63  size_t numTrailingObjects(OverloadToken<double>) const {64    return HasDouble ? 1 : 0;65  }66 67  Class2(bool HasShort, bool HasDouble)68      : HasShort(HasShort), HasDouble(HasDouble) {}69 70public:71  static Class2 *create(short S = 0, double D = 0.0) {72    bool HasShort = S != 0;73    bool HasDouble = D != 0.0;74 75    void *Mem =76        ::operator new(totalSizeToAlloc<double, short>(HasDouble, HasShort));77    Class2 *C = new (Mem) Class2(HasShort, HasDouble);78    if (HasShort)79      *C->getTrailingObjects<short>() = S;80    if (HasDouble)81      *C->getTrailingObjects<double>() = D;82    return C;83  }84  void operator delete(void *Ptr) { ::operator delete(Ptr); }85 86  short getShort() const {87    if (!HasShort)88      return 0;89    return *getTrailingObjects<short>();90  }91 92  double getDouble() const {93    if (!HasDouble)94      return 0.0;95    return *getTrailingObjects<double>();96  }97 98  // Pull some protected members in as public, for testability.99  template <typename... Ty>100  using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;101 102  using TrailingObjects::totalSizeToAlloc;103  using TrailingObjects::additionalSizeToAlloc;104  using TrailingObjects::getTrailingObjects;105};106 107TEST(TrailingObjects, OneArg) {108  int arr[] = {1, 2, 3};109  Class1 *C = Class1::create(arr);110  EXPECT_EQ(sizeof(Class1), sizeof(unsigned));111  EXPECT_EQ(Class1::additionalSizeToAlloc<short>(1), sizeof(short));112  EXPECT_EQ(Class1::additionalSizeToAlloc<short>(3), sizeof(short) * 3);113 114  EXPECT_EQ(alignof(Class1),115            alignof(Class1::FixedSizeStorage<short>::with_counts<1>::type));116  EXPECT_EQ(sizeof(Class1::FixedSizeStorage<short>::with_counts<1>::type),117            llvm::alignTo(Class1::totalSizeToAlloc<short>(1), alignof(Class1)));118  EXPECT_EQ(Class1::totalSizeToAlloc<short>(1), sizeof(Class1) + sizeof(short));119 120  EXPECT_EQ(alignof(Class1),121            alignof(Class1::FixedSizeStorage<short>::with_counts<3>::type));122  EXPECT_EQ(sizeof(Class1::FixedSizeStorage<short>::with_counts<3>::type),123            llvm::alignTo(Class1::totalSizeToAlloc<short>(3), alignof(Class1)));124  EXPECT_EQ(Class1::totalSizeToAlloc<short>(3),125            sizeof(Class1) + sizeof(short) * 3);126 127  EXPECT_EQ(C->getTrailingObjects(), reinterpret_cast<short *>(C + 1));128  EXPECT_EQ(C->get(0), 1);129  EXPECT_EQ(C->get(2), 3);130 131  EXPECT_EQ(C->getTrailingObjects(), C->getTrailingObjectsNonStrict<short>());132 133  delete C;134}135 136TEST(TrailingObjects, TwoArg) {137  Class2 *C1 = Class2::create(4);138  Class2 *C2 = Class2::create(0, 4.2);139 140  EXPECT_EQ(sizeof(Class2), llvm::alignTo(sizeof(bool) * 2, alignof(double)));141  EXPECT_EQ(alignof(Class2), alignof(double));142 143  EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(1, 0)),144            sizeof(double));145  EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(0, 1)),146            sizeof(short));147  EXPECT_EQ((Class2::additionalSizeToAlloc<double, short>(3, 1)),148            sizeof(double) * 3 + sizeof(short));149 150  EXPECT_EQ(151      alignof(Class2),152      (alignof(153          Class2::FixedSizeStorage<double, short>::with_counts<1, 1>::type)));154  EXPECT_EQ(155      sizeof(Class2::FixedSizeStorage<double, short>::with_counts<1, 1>::type),156      llvm::alignTo(Class2::totalSizeToAlloc<double, short>(1, 1),157                    alignof(Class2)));158  EXPECT_EQ((Class2::totalSizeToAlloc<double, short>(1, 1)),159            sizeof(Class2) + sizeof(double) + sizeof(short));160 161  EXPECT_EQ(C1->getDouble(), 0);162  EXPECT_EQ(C1->getShort(), 4);163  EXPECT_EQ(C1->getTrailingObjects<double>(),164            reinterpret_cast<double *>(C1 + 1));165  EXPECT_EQ(C1->getTrailingObjects<short>(), reinterpret_cast<short *>(C1 + 1));166 167  EXPECT_EQ(C2->getDouble(), 4.2);168  EXPECT_EQ(C2->getShort(), 0);169  EXPECT_EQ(C2->getTrailingObjects<double>(),170            reinterpret_cast<double *>(C2 + 1));171  EXPECT_EQ(C2->getTrailingObjects<short>(),172            reinterpret_cast<short *>(reinterpret_cast<double *>(C2 + 1) + 1));173  delete C1;174  delete C2;175}176 177// This test class is not trying to be a usage demo, just asserting178// that three args does actually work too (it's the same code that179// handles the second arg, so it's basically covered by the above, but180// just in case..)181class Class3 final : private TrailingObjects<Class3, double, short, bool> {182  friend TrailingObjects;183 184  size_t numTrailingObjects(OverloadToken<double>) const { return 1; }185  size_t numTrailingObjects(OverloadToken<short>) const { return 1; }186 187public:188  // Pull some protected members in as public, for testability.189  template <typename... Ty>190  using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;191 192  using TrailingObjects::additionalSizeToAlloc;193  using TrailingObjects::getTrailingObjects;194  using TrailingObjects::totalSizeToAlloc;195};196 197TEST(TrailingObjects, ThreeArg) {198  EXPECT_EQ((Class3::additionalSizeToAlloc<double, short, bool>(1, 1, 3)),199            sizeof(double) + sizeof(short) + 3 * sizeof(bool));200  EXPECT_EQ(sizeof(Class3), llvm::alignTo(1, alignof(double)));201 202  EXPECT_EQ(203      alignof(Class3),204      (alignof(Class3::FixedSizeStorage<double, short,205                                        bool>::with_counts<1, 1, 3>::type)));206  EXPECT_EQ(207      sizeof(Class3::FixedSizeStorage<double, short,208                                      bool>::with_counts<1, 1, 3>::type),209      llvm::alignTo(Class3::totalSizeToAlloc<double, short, bool>(1, 1, 3),210                    alignof(Class3)));211 212  std::unique_ptr<char[]> P(new char[1000]);213  Class3 *C = reinterpret_cast<Class3 *>(P.get());214  EXPECT_EQ(C->getTrailingObjects<double>(), reinterpret_cast<double *>(C + 1));215  EXPECT_EQ(C->getTrailingObjects<short>(),216            reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1));217  EXPECT_EQ(218      C->getTrailingObjects<bool>(),219      reinterpret_cast<bool *>(220          reinterpret_cast<short *>(reinterpret_cast<double *>(C + 1) + 1) +221          1));222}223 224class Class4 final : private TrailingObjects<Class4, char, long> {225  friend TrailingObjects;226  size_t numTrailingObjects(OverloadToken<char>) const { return 1; }227 228public:229  // Pull some protected members in as public, for testability.230  template <typename... Ty>231  using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;232 233  using TrailingObjects::additionalSizeToAlloc;234  using TrailingObjects::getTrailingObjects;235  using TrailingObjects::totalSizeToAlloc;236};237 238TEST(TrailingObjects, Realignment) {239  EXPECT_EQ((Class4::additionalSizeToAlloc<char, long>(1, 1)),240            llvm::alignTo(sizeof(long) + 1, alignof(long)));241  EXPECT_EQ(sizeof(Class4), llvm::alignTo(1, alignof(long)));242 243  EXPECT_EQ(244      alignof(Class4),245      (alignof(Class4::FixedSizeStorage<char, long>::with_counts<1, 1>::type)));246  EXPECT_EQ(247      sizeof(Class4::FixedSizeStorage<char, long>::with_counts<1, 1>::type),248      llvm::alignTo(Class4::totalSizeToAlloc<char, long>(1, 1),249                    alignof(Class4)));250 251  std::unique_ptr<char[]> P(new char[1000]);252  Class4 *C = reinterpret_cast<Class4 *>(P.get());253  EXPECT_EQ(C->getTrailingObjects<char>(), reinterpret_cast<char *>(C + 1));254  EXPECT_EQ(C->getTrailingObjects<long>(),255            reinterpret_cast<long *>(llvm::alignAddr(256                reinterpret_cast<char *>(C + 1) + 1, Align::Of<long>())));257}258}259 260// Test the use of TrailingObjects with a template class. This261// previously failed to compile due to a bug in MSVC's member access262// control/lookup handling for OverloadToken.263template <typename Derived>264class Class5Tmpl : private llvm::TrailingObjects<Derived, float, int> {265  using TrailingObjects = typename llvm::TrailingObjects<Derived, float>;266  friend TrailingObjects;267 268  size_t numTrailingObjects(269      typename TrailingObjects::template OverloadToken<float>) const {270    return 1;271  }272};273 274class Class5 : public Class5Tmpl<Class5> {};275