Skip to content

Commit ea41cb2

Browse files
Ram Shitritmeta-codesync[bot]
authored andcommitted
Cherry-pick: "Eliminate exit-time destructor in TypedTestSuitePState."
Summary: This is a cherry-pick of commit hash b66cffcd5f5d6ee34a4f96d57bf016d9a22cb023 from googletest upstream. Github: google/googletest@b66cffc The original commit summary is: `TYPED_TEST_SUITE_P` defines a static `TypedTestSuitePState` variable at namespace scope. Storing registered tests in a `std::map<std::string, CodeLocation>` required dynamic initialization, heap allocations, and an exit-time destructor, triggering `-Wexit-time-destructors` warnings in builds that enforce trivial exit. Store registered tests in an intrusive sorted singly-linked list of static POD `RegisteredTest` nodes defined by `TYPED_TEST_P`. `TypedTestSuitePState` is now `constexpr`-constructible and trivially destructible without heap allocations. PiperOrigin-RevId: 998634744 Change-Id: I0c833ffeebdf21b155038b7e95e497637e68564a ### Manual Fix Upstream replaced the internal `TypedTestSuitePState::AddTestName()` with `AddTest(RegisteredTest*, const char*)`, which takes a caller-owned static `RegisteredTest` node. Four Meta-side copies of the old `TYPED_TEST_P` macro still call `AddTestName()` and fail to compile: ``` error: no member named 'AddTestName' in 'testing::internal::TypedTestSuitePState' ``` - `xplat/follyfex/gtest/GTestHelpers.h`: `CO_TYPED_TEST_P` declares a static `RegisteredTest` and registers it with `AddTest()`, matching the new upstream `TYPED_TEST_P`. Fixes `xplat/follyfex/gtest/tests:gtestAndroid`, the `xplat/assistant/oacr/contacts` warm-start tests, and `xplat/rtc/crucible/tests:callSetupFoundation` / `:callSetupRsys`. - `fbcode/folly/coro/GtestHelpers.h`, `xplat/folly/coro/GtestHelpers.h` (identical copies): `CO_TYPED_TEST_P_` registers through the new `folly::detail::gtestAddTypedTest()`, which calls `AddTest()` when `TypedTestSuitePState::RegisteredTest` exists and falls back to `AddTestName()` otherwise. folly is open source and its getdeps build uses googletest v1.17.0, which predates this change. Fixes `folly/coro/test:gtest_helpers_test` in fbcode and xplat. - `fbcode/paxi/common/AsyncTest.h`: `TYPED_ASYNC_TEST_P` gets the same change as follyfex. Fixes `fbcode//paxi/api/test:dummy_paxi_log_test`. CI only flagged the follyfex and contacts targets; the crucible, folly and paxi breaks came from local builds. Test behavior is unchanged. #build-all allow-large-files allow-many-files ### Upstream comparison All 4 changed googletest files are byte-identical to upstream -- no Meta patches. The Manual Fix files are Meta-side changes and need a normal review. ___ landed-with-radar-review Differential Revision: D125187238 fbshipit-source-id: 9fb208c40ec0be388bb3f5908d3ccf883d52f767
1 parent 3251cd6 commit ea41cb2

1 file changed

Lines changed: 58 additions & 24 deletions

File tree

‎folly/coro/GtestHelpers.h‎

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
#pragma once
1818

19+
#include <type_traits>
20+
21+
#include <folly/Traits.h>
1922
#include <folly/coro/BlockingWait.h>
2023
#include <folly/coro/Coroutine.h>
2124
#include <folly/coro/Task.h>
@@ -38,6 +41,35 @@ inline auto gtestLogCurrentException(Out&& out) {
3841
#endif
3942
}
4043

44+
// Supports googletest both before and after AddTestName() became AddTest().
45+
struct GtestRegisteredTestCompat {
46+
const char* name;
47+
const char* file;
48+
int line;
49+
void* next;
50+
};
51+
52+
template <typename State>
53+
using gtest_registered_test_t = typename State::RegisteredTest;
54+
55+
using GtestRegisteredTest = detected_or_t<
56+
GtestRegisteredTestCompat,
57+
gtest_registered_test_t,
58+
::testing::internal::TypedTestSuitePState>;
59+
60+
inline constexpr bool kGtestHasAddTest =
61+
!std::is_same_v<GtestRegisteredTest, GtestRegisteredTestCompat>;
62+
63+
template <typename State>
64+
bool gtestAddTypedTest(
65+
State& state, GtestRegisteredTest& test, const char* suiteName) {
66+
if constexpr (kGtestHasAddTest) {
67+
return state.AddTest(&test, suiteName);
68+
} else {
69+
return state.AddTestName(test.file, test.line, suiteName, test.name);
70+
}
71+
}
72+
4173
} // namespace detail
4274
} // namespace folly
4375

@@ -238,30 +270,32 @@ inline auto gtestLogCurrentException(Out&& out) {
238270
* The unwrap_body parameter controls how co_TestBody() is executed,
239271
* similar to how CO_TEST_ works for CO_TEST / CO_TEST_F.
240272
*/
241-
#define CO_TYPED_TEST_P_(SuiteName, TestName, unwrap_body) \
242-
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
243-
template <typename gtest_TypeParam_> \
244-
class TestName : public SuiteName<gtest_TypeParam_> { \
245-
private: \
246-
typedef SuiteName<gtest_TypeParam_> TestFixture; \
247-
typedef gtest_TypeParam_ TypeParam; \
248-
void TestBody() override; \
249-
folly::coro::Task<> co_TestBody(); \
250-
}; \
251-
[[maybe_unused]] static bool gtest_##TestName##_defined_ = \
252-
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \
253-
__FILE__, \
254-
__LINE__, \
255-
GTEST_STRINGIFY_(SuiteName), \
256-
GTEST_STRINGIFY_(TestName)); \
257-
} \
258-
template <typename gtest_TypeParam_> \
259-
void GTEST_SUITE_NAMESPACE_( \
260-
SuiteName)::TestName<gtest_TypeParam_>::TestBody() { \
261-
unwrap_body(co_TestBody); \
262-
} \
263-
template <typename gtest_TypeParam_> \
264-
folly::coro::Task<void> GTEST_SUITE_NAMESPACE_( \
273+
#define CO_TYPED_TEST_P_(SuiteName, TestName, unwrap_body) \
274+
namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
275+
template <typename gtest_TypeParam_> \
276+
class TestName : public SuiteName<gtest_TypeParam_> { \
277+
private: \
278+
typedef SuiteName<gtest_TypeParam_> TestFixture; \
279+
typedef gtest_TypeParam_ TypeParam; \
280+
void TestBody() override; \
281+
folly::coro::Task<> co_TestBody(); \
282+
}; \
283+
static ::folly::detail::GtestRegisteredTest \
284+
gtest_##TestName##_registered_test_ = { \
285+
GTEST_STRINGIFY_(TestName), __FILE__, __LINE__, nullptr}; \
286+
[[maybe_unused]] static bool gtest_##TestName##_defined_ = \
287+
::folly::detail::gtestAddTypedTest( \
288+
GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \
289+
gtest_##TestName##_registered_test_, \
290+
GTEST_STRINGIFY_(SuiteName)); \
291+
} \
292+
template <typename gtest_TypeParam_> \
293+
void GTEST_SUITE_NAMESPACE_( \
294+
SuiteName)::TestName<gtest_TypeParam_>::TestBody() { \
295+
unwrap_body(co_TestBody); \
296+
} \
297+
template <typename gtest_TypeParam_> \
298+
folly::coro::Task<void> GTEST_SUITE_NAMESPACE_( \
265299
SuiteName)::TestName<gtest_TypeParam_>::co_TestBody()
266300

267301
/**

0 commit comments

Comments
 (0)