[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/5] cmocka: Make casting macros public


Comparsion function which is passed to expect_check() macro
should have signature:

int check(LargestIntegralType, LargestIntegralType)

when we would like to compare two pointers we have to cast
from argument type to void * (or other ptr type). This
result in ugly complains from compiler:

warning: cast to pointer from integer of different
                            size [-Wint-to-pointer-cast]

cmocka has some very usefull macros for casting from
LargestIntegralType to void *. This commits makes
those macros available for all users of cmocka framework

Signed-off-by: Krzysztof Opasiak <k.opasiak@xxxxxxxxxxx>
---
 include/CMakeLists.txt                        |   15 ++++++-
 include/{cmocka_extra.h => cmocka_extra.h.in} |   53 +++++++++++++++++++++----
 src/CMakeLists.txt                            |    1 +
 src/cmocka.c                                  |   33 ---------------
 4 files changed, 60 insertions(+), 42 deletions(-)
 rename include/{cmocka_extra.h => cmocka_extra.h.in} (75%)

diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index b4f9a5c..8e62120 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -1,9 +1,22 @@
 project(cmocka-headers C)
 
+set(CMOCKA_WORDS_BIGENDIAN
+  "*/ ${WORDS_BIGENDIAN} /*"
+)
+
+set(CMOCKA_WORDS_SIZEOF_VOID_P
+  "*/ ${WORDS_SIZEOF_VOID_P} /*"
+)
+
+#cmake.h configuration
+configure_file(cmocka_extra.h.in
+  ${CMAKE_CURRENT_BINARY_DIR}/cmocka_extra.h
+)
+
 set(cmocka_HDRS
   cmocka.h
   cmocka_pbc.h
-  cmocka_extra.h
+  ${CMAKE_CURRENT_BINARY_DIR}/cmocka_extra.h
 )
 
 install(
diff --git a/include/cmocka_extra.h b/include/cmocka_extra.h.in
similarity index 75%
rename from include/cmocka_extra.h
rename to include/cmocka_extra.h.in
index 420472e..1e1a94c 100644
--- a/include/cmocka_extra.h
+++ b/include/cmocka_extra.h.in
@@ -97,14 +97,6 @@ int __stdcall IsDebuggerPresent();
 # define _UINTPTR_T_DEFINED
 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
 
-/* Perform an unsigned cast to uintptr_t. */
-#define cast_to_pointer_integral_type(value) \
-    ((uintptr_t)(value))
-
-/* Perform a cast of a pointer to uintmax_t */
-#define cast_ptr_to_largest_integral_type(value) \
-cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
-
 /* GCC have printf type attribute check.  */
 #ifdef __GNUC__
 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
@@ -121,5 +113,50 @@ cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
 #define CMOCKA_DEPRECATED
 #endif
 
+#define CMOCKA_WORDS_BIGENDIAN /* @CMOCKA_WORDS_BIGENDIAN@ */
+#define CMOCKA_WORDS_SIZEOF_VOID_P /* @CMOCKA_WORDS_SIZEOF_VOID_P@ */
+
+/* Used to cast LargetIntegralType to void* and vice versa. */
+typedef union ValuePointer {
+    LargestIntegralType value;
+    struct {
+#if (CMOCKA_WORDS_BIGENDIAN == 1) && (CMOCKA_WORDS_SIZEOF_VOID_P == 4)
+        unsigned int padding;
+#endif
+        void *pointer;
+    } x;
+} ValuePointer;
+
+/*
+ * Declare and initialize the pointer member of ValuePointer variable name
+ * with ptr.
+ */
+#define declare_initialize_value_pointer_pointer(name, ptr) \
+	ValuePointer name = {				    \
+		.value = 0,				    \
+		.x.pointer = (void*)(ptr),		    \
+	}
+
+/*
+ * Declare and initialize the value member of ValuePointer variable name
+ * with val.
+ */
+#define declare_initialize_value_pointer_value(name, val) \
+	ValuePointer name = {				  \
+		.value = val,				  \
+	}
+
+/* Cast a LargestIntegralType to pointer_type via a ValuePointer. */
+#define cast_largest_integral_type_to_pointer( \
+    pointer_type, largest_integral_type) \
+    ((pointer_type)((ValuePointer*)&(largest_integral_type))->x.pointer)
+
+/* Perform an unsigned cast to uintptr_t. */
+#define cast_to_pointer_integral_type(value) \
+    ((uintptr_t)(value))
+
+/* Perform a cast of a pointer to uintmax_t */
+#define cast_ptr_to_largest_integral_type(value) \
+cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
 
 #endif /* CMOCKA_EXTRA_H_ */
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4ab084e..93e783f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -4,6 +4,7 @@ set(CMOCKA_PLATFORM_INCLUDE CACHE PATH "Path to include directory for cmocka_pla
 
 set(CMOCKA_PUBLIC_INCLUDE_DIRS
     ${CMAKE_SOURCE_DIR}/include
+    ${CMAKE_BINARY_DIR}/include
     ${CMOCKA_PLATFORM_INCLUDE}
     CACHE INTERNAL "cmocka public include directories"
 )
diff --git a/src/cmocka.c b/src/cmocka.c
index fd7b8b3..78b9e4f 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -143,39 +143,6 @@ WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID);
 #define CMOCKA_CLOCK_GETTIME(clock_id, ts)
 #endif
 
-/*
- * Declare and initialize the pointer member of ValuePointer variable name
- * with ptr.
- */
-#define declare_initialize_value_pointer_pointer(name, ptr) \
-    ValuePointer name ; \
-    name.value = 0; \
-    name.x.pointer = (void*)(ptr)
-
-/*
- * Declare and initialize the value member of ValuePointer variable name
- * with val.
- */
-#define declare_initialize_value_pointer_value(name, val) \
-    ValuePointer name ; \
-    name.value = val
-
-/* Cast a LargestIntegralType to pointer_type via a ValuePointer. */
-#define cast_largest_integral_type_to_pointer( \
-    pointer_type, largest_integral_type) \
-    ((pointer_type)((ValuePointer*)&(largest_integral_type))->x.pointer)
-
-/* Used to cast LargetIntegralType to void* and vice versa. */
-typedef union ValuePointer {
-    LargestIntegralType value;
-    struct {
-#if defined(WORDS_BIGENDIAN) && (WORDS_SIZEOF_VOID_P == 4)
-        unsigned int padding;
-#endif
-        void *pointer;
-    } x;
-} ValuePointer;
-
 /* Doubly linked list node. */
 typedef struct ListNode {
     const void *value;
-- 
1.7.9.5



References:
[PATCH 0/5] cmocka: Make casting macros publicKrzysztof Opasiak <k.opasiak@xxxxxxxxxxx>