Avoid a static member declared "weak after use"

Using OpenCL C ++ Wrapper 1.2.7 in GCC 5.1.0 gives error

/usr/include/CL/cl.hpp:2442:29: error: cl::Context::default_initialized_ declared weak after being used
     static std::atomic<int> default_initialized_;

      

this is a list of relevant location codes:

class Context 
    : public detail::Wrapper<cl_context>
{
private:

#ifdef CL_HPP_CPP11_ATOMICS_SUPPORTED 
    static std::atomic<int> default_initialized_; // <- line 2442
#else // !CL_HPP_CPP11_ATOMICS_SUPPORTED
    static volatile int default_initialized_;
#endif // !CL_HPP_CPP11_ATOMICS_SUPPORTED

      

I was able to find this error message in some of the fixes for GCC and its source code and did not explain what it means and how to avoid it.

I think this refers to weak characters , but the error seems to be for functions that don't make sense in this context (based on the old error message declared inline after calling ").

For me it boils down to the following questions:

Who is the culprit? Me, OpenCL wrapper or GCC?

and

How can I avoid this error?

MWE

#include <CL/cl.hpp>    
int main() {}

      

While creating this example, I noticed that the error only occurs when using the undefined ( -fsanitize=undefined

) behavior sanitizer .

The problem occurs with another member when using pre C ++ 11, but the error message remains the same.

Additional context

I went through the code cl.hpp

and found the likely source of the error.

Both cl::Context

and cl::CommandQueue

start with

private:
#ifdef CL_HPP_CPP11_ATOMICS_SUPPORTED
    static std::atomic<int> default_initialized_;
#else // !CL_HPP_CPP11_ATOMICS_SUPPORTED
    static volatile int default_initialized_;
#endif // !CL_HPP_CPP11_ATOMICS_SUPPORTED
    static CommandQueue default_;
    static volatile cl_int default_error_;

      

and then have, for example,

#ifdef _WIN32
#ifdef CL_HPP_CPP11_ATOMICS_SUPPORTED
__declspec(selectany) std::atomic<int> Context::default_initialized_;
#else // !CL_HPP_CPP11_ATOMICS_SUPPORTED
__declspec(selectany) volatile int Context::default_initialized_ = __DEFAULT_NOT_INITIALIZED;
#endif // !CL_HPP_CPP11_ATOMICS_SUPPORTED
__declspec(selectany) Context Context::default_;
__declspec(selectany) volatile cl_int Context::default_error_ = CL_SUCCESS;
#else // !_WIN32
#ifdef CL_HPP_CPP11_ATOMICS_SUPPORTED
__attribute__((weak)) std::atomic<int> Context::default_initialized_;
#else // !CL_HPP_CPP11_ATOMICS_SUPPORTED
__attribute__((weak)) volatile int Context::default_initialized_ = __DEFAULT_NOT_INITIALIZED;
#endif // !CL_HPP_CPP11_ATOMICS_SUPPORTED
__attribute__((weak)) Context Context::default_;
__attribute__((weak)) volatile cl_int Context::default_error_ = CL_SUCCESS;
#endif // !_WIN32

      

Obviously, a weak attribute is added to the definition after a declaration that does not have this attribute.

So my question is this: is this a flaw in the library code, or is the UB sanitizer too sensitive?

+3


source to share





All Articles