Fix: remove type constness in URCU_FORCE_CAST's C++ version
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Aug 2022 17:11:21 +0000 (13:11 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 17 Aug 2022 17:42:21 +0000 (13:42 -0400)
The test added by the following patch wouldn't compile, when built
without _LGPL_SOURCE:

      CXX      test_build_dynlink_cxx-test_build_cxx.o
    In file included from ../../include/urcu/arch.h:25,
                     from /home/simark/src/urcu/tests/unit/test_build.c:28,
                     from /home/simark/src/urcu/tests/unit/test_build_cxx.cpp:3:
    /home/simark/src/urcu/tests/unit/test_build.c: In function ‘void test_build_rcu_dereference()’:
    /home/simark/src/urcu/include/urcu/compiler.h:85:42: error: type qualifiers ignored on cast result type [-Werror=ignored-qualifiers]
       85 | #define URCU_FORCE_CAST(type, arg)      (reinterpret_cast<type>(arg))
          |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/simark/src/urcu/include/urcu/pointer.h:71:49: note: in expansion of macro ‘URCU_FORCE_CAST’
       71 |                 __typeof__(p) _________p1 =     URCU_FORCE_CAST(__typeof__(p), \
          |                                                 ^~~~~~~~~~~~~~~
    /home/simark/src/urcu/tests/unit/test_build.c:133:9: note: in expansion of macro ‘rcu_dereference’
      133 |         rcu_dereference(opaque_const);
          |         ^~~~~~~~~~~~~~~

The compiler complains that we do a cast to a const type, equivalent to:

  reinterpret_cast<const int>(arg)

... and that the const is meaningless in this context.

Use std::remove_cv to strip away any const or volatile qualifiers from
the type (using a volatile type would result in the same warning).

Change-Id: I94e79fcccfc2108021752f65977e1548084c646a
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/urcu/compiler.h

index 34eb564bb77795e9e77d21983240c3327b3401d1..2f32b38d6fb1850291ebd500f0a2fdca53f5c428 100644 (file)
 
 #include <stddef.h>    /* for offsetof */
 
+#if defined __cplusplus
+# include <type_traits>        /* for std::remove_cv */
+#endif
+
 #define caa_likely(x)  __builtin_expect(!!(x), 1)
 #define caa_unlikely(x)        __builtin_expect(!!(x), 0)
 
@@ -82,7 +86,7 @@
 #define __rcu
 
 #ifdef __cplusplus
-#define URCU_FORCE_CAST(type, arg)     (reinterpret_cast<type>(arg))
+#define URCU_FORCE_CAST(_type, arg)    (reinterpret_cast<std::remove_cv<_type>::type>(arg))
 #else
 #define URCU_FORCE_CAST(type, arg)     ((type) (arg))
 #endif
This page took 0.026311 seconds and 4 git commands to generate.