Skip to content

Commit 6d11b17

Browse files
authored
[Support] Change test to use TrailingObjects API per documentation (llvm#139319)
- Use private inheritance for TrailingObjects as recommended in TrailingObjects.h - No need to define `numTrailingObjects` for the last trailing type. - Fix comment typos.
1 parent e74877b commit 6d11b17

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

llvm/unittests/Support/TrailingObjectsTest.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ namespace {
1717
// This class, beyond being used by the test case, a nice
1818
// demonstration of the intended usage of TrailingObjects, with a
1919
// single trailing array.
20-
class Class1 final : protected TrailingObjects<Class1, short> {
20+
class Class1 final : private TrailingObjects<Class1, short> {
2121
friend TrailingObjects;
2222

2323
unsigned NumShorts;
2424

2525
protected:
26-
size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
27-
2826
Class1(ArrayRef<int> ShortArray) : NumShorts(ShortArray.size()) {
2927
// This tests the non-templated getTrailingObjects() that returns a pointer
3028
// when using a single trailing type.
@@ -52,18 +50,15 @@ class Class1 final : protected TrailingObjects<Class1, short> {
5250
using TrailingObjects::getTrailingObjects;
5351
};
5452

55-
// Here, there are two singular optional object types appended. Note
53+
// Here, there are two singular optional object types appended. Note
5654
// that the alignment of Class2 is automatically increased to account
5755
// for the alignment requirements of the trailing objects.
58-
class Class2 final : protected TrailingObjects<Class2, double, short> {
56+
class Class2 final : private TrailingObjects<Class2, double, short> {
5957
friend TrailingObjects;
6058

6159
bool HasShort, HasDouble;
6260

6361
protected:
64-
size_t numTrailingObjects(OverloadToken<short>) const {
65-
return HasShort ? 1 : 0;
66-
}
6762
size_t numTrailingObjects(OverloadToken<double>) const {
6863
return HasDouble ? 1 : 0;
6964
}
@@ -179,14 +174,23 @@ TEST(TrailingObjects, TwoArg) {
179174
}
180175

181176
// This test class is not trying to be a usage demo, just asserting
182-
// that three args does actually work too (it's the same code as
177+
// that three args does actually work too (it's the same code that
183178
// handles the second arg, so it's basically covered by the above, but
184179
// just in case..)
185-
class Class3 final : public TrailingObjects<Class3, double, short, bool> {
180+
class Class3 final : private TrailingObjects<Class3, double, short, bool> {
186181
friend TrailingObjects;
187182

188183
size_t numTrailingObjects(OverloadToken<double>) const { return 1; }
189184
size_t numTrailingObjects(OverloadToken<short>) const { return 1; }
185+
186+
public:
187+
// Pull some protected members in as public, for testability.
188+
template <typename... Ty>
189+
using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;
190+
191+
using TrailingObjects::additionalSizeToAlloc;
192+
using TrailingObjects::getTrailingObjects;
193+
using TrailingObjects::totalSizeToAlloc;
190194
};
191195

192196
TEST(TrailingObjects, ThreeArg) {
@@ -216,9 +220,18 @@ TEST(TrailingObjects, ThreeArg) {
216220
1));
217221
}
218222

219-
class Class4 final : public TrailingObjects<Class4, char, long> {
223+
class Class4 final : private TrailingObjects<Class4, char, long> {
220224
friend TrailingObjects;
221225
size_t numTrailingObjects(OverloadToken<char>) const { return 1; }
226+
227+
public:
228+
// Pull some protected members in as public, for testability.
229+
template <typename... Ty>
230+
using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;
231+
232+
using TrailingObjects::additionalSizeToAlloc;
233+
using TrailingObjects::getTrailingObjects;
234+
using TrailingObjects::totalSizeToAlloc;
222235
};
223236

224237
TEST(TrailingObjects, Realignment) {
@@ -255,11 +268,6 @@ class Class5Tmpl : private llvm::TrailingObjects<Derived, float, int> {
255268
typename TrailingObjects::template OverloadToken<float>) const {
256269
return 1;
257270
}
258-
259-
size_t numTrailingObjects(
260-
typename TrailingObjects::template OverloadToken<int>) const {
261-
return 2;
262-
}
263271
};
264272

265273
class Class5 : public Class5Tmpl<Class5> {};

0 commit comments

Comments
 (0)