[Bf-blender-cvs] [7f32cf4] master: Prevent macros hiding casts from `const` pointers

Campbell Barton noreply at git.blender.org
Fri Aug 1 14:04:59 CEST 2014


Commit: 7f32cf46054ceb924afcfef4ebf7c26822aa524d
Author: Campbell Barton
Date:   Fri Aug 1 21:59:42 2014 +1000
Branches: master
https://developer.blender.org/rB7f32cf46054ceb924afcfef4ebf7c26822aa524d

Prevent macros hiding casts from `const` pointers

===================================================================

M	source/blender/blenkernel/BKE_idprop.h
M	source/blender/blenkernel/intern/idprop.c
M	source/blender/blenlib/BLI_utildefines.h
M	source/blender/bmesh/bmesh_class.h
M	source/blender/editors/include/BIF_gl.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h
index 3cf944f..0f3f4ef 100644
--- a/source/blender/blenkernel/BKE_idprop.h
+++ b/source/blender/blenkernel/BKE_idprop.h
@@ -121,11 +121,27 @@ void IDP_ClearProperty(IDProperty *prop);
 void IDP_UnlinkProperty(struct IDProperty *prop);
 
 #define IDP_Int(prop)                     ((prop)->data.val)
-#define IDP_Float(prop)        (*(float *)&(prop)->data.val)
-#define IDP_Double(prop)      (*(double *)&(prop)->data.val)
-#define IDP_String(prop)         ((char *) (prop)->data.pointer)
 #define IDP_Array(prop)                   ((prop)->data.pointer)
-#define IDP_IDPArray(prop) ((IDProperty *) (prop)->data.pointer)
+/* C11 const correctness for casts */
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#  define IDP_Float(prop)  _Generic(prop, \
+	IDProperty *:             (*(float *)&(prop)->data.val), \
+	const IDProperty *: (*(const float *)&(prop)->data.val))
+#  define IDP_Double(prop)  _Generic(prop, \
+	IDProperty *:             (*(double *)&(prop)->data.val), \
+	const IDProperty *: (*(const double *)&(prop)->data.val))
+#  define IDP_String(prop)  _Generic(prop, \
+	IDProperty *:             ((char *) (prop)->data.pointer), \
+	const IDProperty *: ((const char *) (prop)->data.pointer))
+#  define IDP_IDPArray(prop)  _Generic(prop, \
+	IDProperty *:             ((IDProperty *) (prop)->data.pointer), \
+	const IDProperty *: ((const IDProperty *) (prop)->data.pointer))
+#else
+#  define IDP_Float(prop)        (*(float *)&(prop)->data.val)
+#  define IDP_Double(prop)      (*(double *)&(prop)->data.val)
+#  define IDP_String(prop)         ((char *) (prop)->data.pointer)
+#  define IDP_IDPArray(prop) ((IDProperty *) (prop)->data.pointer)
+#endif
 
 #ifdef DEBUG
 /* for printout only */
diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c
index 3188676..95bfd72 100644
--- a/source/blender/blenkernel/intern/idprop.c
+++ b/source/blender/blenkernel/intern/idprop.c
@@ -64,7 +64,7 @@ static char idp_size_table[] = {
 /** \name IDP Array API
  * \{ */
 
-#define GETPROP(prop, i) (((IDProperty *)(prop)->data.pointer) + (i))
+#define GETPROP(prop, i) &(IDP_IDPArray(prop)[i])
 
 /* --------- property array type -------------*/
 
diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h
index 564b31f..5937880 100644
--- a/source/blender/blenlib/BLI_utildefines.h
+++ b/source/blender/blenlib/BLI_utildefines.h
@@ -174,10 +174,16 @@
 	(void)__tmp;                                         \
 }))
 
+#define CHECK_TYPE_NONCONST(var)  {           \
+	void *non_const = ((__typeof(var))NULL);  \
+	(void)non_const;                          \
+} (void)0
+
 #else
 #  define CHECK_TYPE(var, type)
 #  define CHECK_TYPE_PAIR(var_a, var_b)
 #  define CHECK_TYPE_PAIR_INLINE(var_a, var_b) (void)0
+#  define CHECK_TYPE_NONCONST(var) (void)0
 #endif
 
 /* can be used in simple macros */
diff --git a/source/blender/bmesh/bmesh_class.h b/source/blender/bmesh/bmesh_class.h
index 0174539..39359b9 100644
--- a/source/blender/bmesh/bmesh_class.h
+++ b/source/blender/bmesh/bmesh_class.h
@@ -285,8 +285,8 @@ extern void bpy_bm_generic_invalidate(struct BPy_BMGeneric *self);
 typedef bool (*BMElemFilterFunc)(BMElem *, void *user_data);
 
 /* defines */
-#define BM_ELEM_CD_SET_INT(ele, offset, f) \
-	{ assert(offset != -1); *((int *)((char *)(ele)->head.data + (offset))) = (f); } (void)0
+#define BM_ELEM_CD_SET_INT(ele, offset, f) { CHECK_TYPE_NONCONST(ele); \
+	assert(offset != -1); *((int *)((char *)(ele)->head.data + (offset))) = (f); } (void)0
 
 #define BM_ELEM_CD_GET_INT(ele, offset) \
 	(assert(offset != -1), *((int *)((char *)(ele)->head.data + (offset))))
@@ -294,8 +294,8 @@ typedef bool (*BMElemFilterFunc)(BMElem *, void *user_data);
 #define BM_ELEM_CD_GET_VOID_P(ele, offset) \
 	(assert(offset != -1), (void *)((char *)(ele)->head.data + (offset)))
 
-#define BM_ELEM_CD_SET_FLOAT(ele, offset, f) \
-	{ assert(offset != -1); *((float *)((char *)(ele)->head.data + (offset))) = (f); } (void)0
+#define BM_ELEM_CD_SET_FLOAT(ele, offset, f) { CHECK_TYPE_NONCONST(ele); \
+	assert(offset != -1); *((float *)((char *)(ele)->head.data + (offset))) = (f); } (void)0
 
 #define BM_ELEM_CD_GET_FLOAT(ele, offset) \
 	(assert(offset != -1), *((float *)((char *)(ele)->head.data + (offset))))
diff --git a/source/blender/editors/include/BIF_gl.h b/source/blender/editors/include/BIF_gl.h
index 477a7c0..4258bbc 100644
--- a/source/blender/editors/include/BIF_gl.h
+++ b/source/blender/editors/include/BIF_gl.h
@@ -58,8 +58,24 @@
  * */
 void cpack(unsigned int x);
 
-#define glMultMatrixf(x)  glMultMatrixf( (float *)(x))
-#define glLoadMatrixf(x)  glLoadMatrixf( (float *)(x))
+
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
+#  define glMultMatrixf(x)  \
+	glMultMatrixf(_Generic(x, \
+	        float *:      (float *)(x), \
+	        float (*)[4]: (float *)(x), \
+	        const float *:      (float *)(x), \
+	        const float (*)[4]: (float *)(x)) \
+)
+#  define glLoadMatrixf(x)  \
+	glLoadMatrixf(_Generic(x, \
+	        float *:      (float *)(x), \
+	        float (*)[4]: (float *)(x)) \
+)
+#else
+#  define glMultMatrixf(x)  glMultMatrixf((float *)(x))
+#  define glLoadMatrixf(x)  glLoadMatrixf((float *)(x))
+#endif
 
 #define GLA_PIXEL_OFS 0.375f




More information about the Bf-blender-cvs mailing list