Skip to content

[Support] Change test to use TrailingObjects API per documentation #139319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented May 9, 2025

  • Use private inheritance for TrailingObjects as recommended in TrailingObjects.h
  • No need to define numTrailingObjects for the last trailing type.
  • Fix comment typos.

@jurahul jurahul changed the title [Support] Change test use TrailingObjects API per documentation [Support] Change test to use TrailingObjects API per documentation May 9, 2025
- Use private inheritance for TrailingObjects as recommended in
  TrailingObjects.h
- No need to define `numTrailingObjects` for the last trailing type.
- Fix comment typos.
@jurahul jurahul force-pushed the trailing_objects_test branch from bfa2574 to 9df983d Compare May 10, 2025 13:16
@jurahul jurahul marked this pull request as ready for review May 10, 2025 15:40
@llvmbot
Copy link
Member

llvmbot commented May 10, 2025

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

Changes
  • Use private inheritance for TrailingObjects as recommended in TrailingObjects.h
  • No need to define numTrailingObjects for the last trailing type.
  • Fix comment typos.

Full diff: https://github.com/llvm/llvm-project/pull/139319.diff

1 Files Affected:

  • (modified) llvm/unittests/Support/TrailingObjectsTest.cpp (+24-16)
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> {};

@jurahul jurahul requested review from dwblaikie and joker-eph May 10, 2025 15:41
@jurahul jurahul merged commit 6d11b17 into llvm:main May 12, 2025
14 checks passed
@jurahul jurahul deleted the trailing_objects_test branch May 12, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants