[llvm] 6d11b17 - [Support] Change test to use TrailingObjects API per documentation (#139319)
via llvm-commits
llvm-commits at lists.llvm.org
Mon May 12 10:28:13 PDT 2025
Author: Rahul Joshi
Date: 2025-05-12T10:28:09-07:00
New Revision: 6d11b17162a921cd3a66c1d59c6b4b94c9466aed
URL: https://github.com/llvm/llvm-project/commit/6d11b17162a921cd3a66c1d59c6b4b94c9466aed
DIFF: https://github.com/llvm/llvm-project/commit/6d11b17162a921cd3a66c1d59c6b4b94c9466aed.diff
LOG: [Support] Change test to use TrailingObjects API per documentation (#139319)
- Use private inheritance for TrailingObjects as recommended in
TrailingObjects.h
- No need to define `numTrailingObjects` for the last trailing type.
- Fix comment typos.
Added:
Modified:
llvm/unittests/Support/TrailingObjectsTest.cpp
Removed:
################################################################################
diff --git a/llvm/unittests/Support/TrailingObjectsTest.cpp b/llvm/unittests/Support/TrailingObjectsTest.cpp
index 6f9d7bda7fe5a..2590f375b6598 100644
--- a/llvm/unittests/Support/TrailingObjectsTest.cpp
+++ b/llvm/unittests/Support/TrailingObjectsTest.cpp
@@ -17,14 +17,12 @@ namespace {
// This class, beyond being used by the test case, a nice
// demonstration of the intended usage of TrailingObjects, with a
// single trailing array.
-class Class1 final : protected TrailingObjects<Class1, short> {
+class Class1 final : private TrailingObjects<Class1, short> {
friend TrailingObjects;
unsigned NumShorts;
protected:
- size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
-
Class1(ArrayRef<int> ShortArray) : NumShorts(ShortArray.size()) {
// This tests the non-templated getTrailingObjects() that returns a pointer
// when using a single trailing type.
@@ -52,18 +50,15 @@ class Class1 final : protected TrailingObjects<Class1, short> {
using TrailingObjects::getTrailingObjects;
};
-// Here, there are two singular optional object types appended. Note
+// Here, there are two singular optional object types appended. Note
// that the alignment of Class2 is automatically increased to account
// for the alignment requirements of the trailing objects.
-class Class2 final : protected TrailingObjects<Class2, double, short> {
+class Class2 final : private TrailingObjects<Class2, double, short> {
friend TrailingObjects;
bool HasShort, HasDouble;
protected:
- size_t numTrailingObjects(OverloadToken<short>) const {
- return HasShort ? 1 : 0;
- }
size_t numTrailingObjects(OverloadToken<double>) const {
return HasDouble ? 1 : 0;
}
@@ -179,14 +174,23 @@ TEST(TrailingObjects, TwoArg) {
}
// This test class is not trying to be a usage demo, just asserting
-// that three args does actually work too (it's the same code as
+// that three args does actually work too (it's the same code that
// handles the second arg, so it's basically covered by the above, but
// just in case..)
-class Class3 final : public TrailingObjects<Class3, double, short, bool> {
+class Class3 final : private TrailingObjects<Class3, double, short, bool> {
friend TrailingObjects;
size_t numTrailingObjects(OverloadToken<double>) const { return 1; }
size_t numTrailingObjects(OverloadToken<short>) const { return 1; }
+
+public:
+ // Pull some protected members in as public, for testability.
+ template <typename... Ty>
+ using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;
+
+ using TrailingObjects::additionalSizeToAlloc;
+ using TrailingObjects::getTrailingObjects;
+ using TrailingObjects::totalSizeToAlloc;
};
TEST(TrailingObjects, ThreeArg) {
@@ -216,9 +220,18 @@ TEST(TrailingObjects, ThreeArg) {
1));
}
-class Class4 final : public TrailingObjects<Class4, char, long> {
+class Class4 final : private TrailingObjects<Class4, char, long> {
friend TrailingObjects;
size_t numTrailingObjects(OverloadToken<char>) const { return 1; }
+
+public:
+ // Pull some protected members in as public, for testability.
+ template <typename... Ty>
+ using FixedSizeStorage = TrailingObjects::FixedSizeStorage<Ty...>;
+
+ using TrailingObjects::additionalSizeToAlloc;
+ using TrailingObjects::getTrailingObjects;
+ using TrailingObjects::totalSizeToAlloc;
};
TEST(TrailingObjects, Realignment) {
@@ -255,11 +268,6 @@ class Class5Tmpl : private llvm::TrailingObjects<Derived, float, int> {
typename TrailingObjects::template OverloadToken<float>) const {
return 1;
}
-
- size_t numTrailingObjects(
- typename TrailingObjects::template OverloadToken<int>) const {
- return 2;
- }
};
class Class5 : public Class5Tmpl<Class5> {};
More information about the llvm-commits
mailing list