Skip to content
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

[Bug]: NVCC, lambdas and private TestBody #4104

Closed
romintomasetti opened this issue Dec 28, 2022 · 1 comment
Closed

[Bug]: NVCC, lambdas and private TestBody #4104

romintomasetti opened this issue Dec 28, 2022 · 1 comment
Assignees

Comments

@romintomasetti
Copy link

romintomasetti commented Dec 28, 2022

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" <romin.tomasetti@gmail.com>
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
@asoffer
Copy link
Contributor

asoffer commented Jan 9, 2023

This seems to be a bug in nvcc which is currently an unsupported compiler. We'd strongly prefer not to contort valid C++ code around this sort of bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants