googletest
googletest copied to clipboard
[Bug]: NVCC, lambdas and private `TestBody`
Describe the issue
When using nvcc (for compiling on NVidia GPUs), it is not possible to create lambdas withing the test body because ::testing::Test::TestBody is private (e.g. when using the macro GTEST_TEST). It is a restriction from CUDA, see https://docs.nvidia.com/cuda/cuda-c-programming-guide/#extended-lambda-restrictions.
This means code bases like https://github.com/trilinos/Trilinos have less chance to use Google Test for their unit testing, since their goal is to work with a code on any CPU or GPU.
The fix could be to promote ::testing::Test::TestBody as a public method. It could even be an opt-in choice (i.e. keep it private by default, and allow it to be public based on some CMake option.
Steps to reproduce the problem
Here is a minimal reproducer:
class MyClass
{
protected:
void TestBody()
{
auto my_lambda = [=] __host__ __device__ (void) -> int { return 5.0; };
}
};
that would fail compiling with
test.cpp(...): error: The enclosing parent function ("TestBody") for an extended __host__ __device__ lambda cannot have private or protected access within its class
What version of GoogleTest are you using?
I'm using the latest release 1.12.1.
What operating system and version are you using?
I'm on Ubuntu 22.04.
What compiler and version are you using?
I'm using nvcc from CUDA 12.0.0 in conjunction with gcc 11.3.0.
What build system are you using?
cmake version 3.25.1
Proposed patch
From 84c85d697bc6309f448df7af691ff6e71027fc59 Mon Sep 17 00:00:00 2001
From: "romin.tomasetti" <[email protected]>
Date: Wed, 28 Dec 2022 16:48:53 +0100
Subject: [PATCH] fix: allow for TestBody to be public, see #4104
---
googletest/include/gtest/internal/gtest-internal.h | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h
index e9c2441a..b8920176 100644
--- a/googletest/include/gtest/internal/gtest-internal.h
+++ b/googletest/include/gtest/internal/gtest-internal.h
@@ -94,6 +94,10 @@
#define GTEST_STRINGIFY_HELPER_(name, ...) #name
#define GTEST_STRINGIFY_(...) GTEST_STRINGIFY_HELPER_(__VA_ARGS__, )
+#ifndef GTEST_TESTBODY_VISIBILITY
+ #define GTEST_TESTBODY_VISIBILITY private
+#endif
+
namespace proto2 {
class MessageLite;
}
@@ -1549,8 +1553,9 @@ class NeverThrown {
GTEST_TEST_CLASS_NAME_(test_suite_name, \
test_name) &&) noexcept = delete; /* NOLINT */ \
\
- private: \
+ GTEST_TESTBODY_VISIBILITY: \
void TestBody() override; \
+ private: \
static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \
}; \
\
--
2.34.1