boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
96 stars 116 forks source link

Incompatible pointer types warning when building with Visual Studio Clang #246

Open stertingen opened 1 year ago

stertingen commented 1 year ago

This warning occurs when building with Clang from Visual Studio (using CMake). This affects

Using clang from MinGW or MSVC without Clang does not emit these warnings.

Build log: https://pastebin.com/zQes4tph

stertingen commented 1 year ago

I boiled it down to the following lines:


/* First, define CAS_LOCK and CLEAR_LOCK on ints */
/* Note CAS_LOCK defined to return 0 on success */

#if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
/* snip */
#elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
/* snip */
#else /* Win32 MSC */
#define CAS_LOCK(sl)     interlockedexchange(sl, (LONG)1)
#define CLEAR_LOCK(sl)   interlockedexchange (sl, (LONG)0)

#endif /* ... gcc spins locks ... */

/* snip */

#define MLOCK_T               int
#define TRY_LOCK(sl)          !CAS_LOCK(sl)
#define RELEASE_LOCK(sl)      CLEAR_LOCK(sl)
#define ACQUIRE_LOCK(sl)      (CAS_LOCK(sl)? spin_acquire_lock(sl) : 0)
#define INITIAL_LOCK(sl)      (*sl = 0)
#define DESTROY_LOCK(sl)      (0)
static MLOCK_T malloc_global_mutex = 0;

interlockedexchange is defined for long volatile * while MLOCK_T is defined as int.

Since clang tries to be kind of compatible to MSVC here, the GNUC checks earlier on fail, so it uses the Win32 implementation with interlockedexchange. MSVC did not seem to mind or the warning has been silenced.

Clang from MinGW selects another implementation, which also avoids the warning.

To resolve this issue, I see two approaches:

  1. define MLOCK_T to volatile long if using the interlockedexchange-based implementation
  2. cast sl to long volatile* when using it: #define CAS_LOCK(sl) interlockedexchange((LONG volatile*)sl, (LONG)1)

See also: https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-interlockedexchange

EDIT: See also GitHub Action https://github.com/boostorg/container/actions/runs/4951665385/jobs/8856936413#step:4:21