cmocka: use ISO C99 PRIu64 over LargestIntegralTypePrintfUnsignedFormat
authorEmil Velikov <emil.l.velikov@gmail.com>
Fri, 25 Jul 2014 13:31:23 +0000 (14:31 +0100)
committerAndreas Schneider <asn@cryptomilk.org>
Mon, 28 Jul 2014 07:19:03 +0000 (09:19 +0200)
With commit commit 8642ef21b07(cmocka: introduce
LargestIntegralTypePrintfUnsignedFormat modifier) we introduces this
superfluous macro in order to work around the differences when printing
64bit unsigned integer values.

Rather than reinventing the wheel use the C99's PRIu64 and fall back
locally to their platform/compiler specific counterparts if the
inttypes.h header does not exist.

Thanks to scythe from the #cmocka channel for pointing out.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
include/cmocka.h
src/cmocka.c

index 5814805..a60b2a5 100644 (file)
@@ -85,15 +85,6 @@ int __stdcall IsDebuggerPresent();
 #endif /* _WIN32 */
 #endif /* LargestIntegralTypePrintfFormat */
 
-/* Printf format used to display LargestIntegralType as unsigned. */
-#ifndef LargestIntegralTypePrintfUnsignedFormat
-#ifdef _WIN32
-#define LargestIntegralTypePrintfUnsignedFormat "%I64u"
-#else
-#define LargestIntegralTypePrintfUnsignedFormat "%llu"
-#endif /* _WIN32 */
-#endif /* LargestIntegralTypePrintfFormat */
-
 /* Perform an unsigned cast to LargestIntegralType. */
 #define cast_to_largest_integral_type(value) \
     ((LargestIntegralType)((size_t)(value)))
index ef6ee8a..69f792d 100644 (file)
 #include <malloc.h>
 #endif
 
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#endif
+
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stddef.h>
@@ -43,12 +47,20 @@ WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID);
 #define PRIdS "Id"
 #endif
 
+#ifndef PRIu64
+#define PRIu64 "I64u"
+#endif
+
 #else /* _WIN32 */
 
 #ifndef PRIdS
 #define PRIdS "zd"
 #endif
 
+#ifndef PRIu64
+#define PRIu64 "llu"
+#endif
+
 #include <signal.h>
 #endif /* _WIN32 */
 
@@ -819,10 +831,10 @@ static int value_in_set_display_error(
         if (succeeded) {
             return 1;
         }
-        print_error(LargestIntegralTypePrintfUnsignedFormat
-                    "is %sin the set (", value, invert ? "" : "not ");
+        print_error("%" PRIu64 " is %sin the set (", value,
+                    invert ? "" : "not ");
         for (i = 0; i < size_of_set; i++) {
-            print_error(LargestIntegralTypePrintfUnsignedFormat ", ", set[i]);
+            print_error("%" PRIu64 ", ", set[i]);
         }
         print_error(")\n");
     }
@@ -841,10 +853,7 @@ static int integer_in_range_display_error(
     if (value >= range_min && value <= range_max) {
         return 1;
     }
-    print_error(LargestIntegralTypePrintfUnsignedFormat
-                " is not within the range "
-                LargestIntegralTypePrintfUnsignedFormat "-"
-                LargestIntegralTypePrintfUnsignedFormat "\n",
+    print_error("%" PRIu64 " is not within the range %" PRIu64 "-%" PRIu64 "\n",
                 value, range_min, range_max);
     return 0;
 }
@@ -861,10 +870,7 @@ static int integer_not_in_range_display_error(
     if (value < range_min || value > range_max) {
         return 1;
     }
-    print_error(LargestIntegralTypePrintfUnsignedFormat
-                " is within the range "
-                LargestIntegralTypePrintfUnsignedFormat "-"
-                LargestIntegralTypePrintfUnsignedFormat "\n",
+    print_error("%" PRIu64 " is within the range %" PRIu64 "-%" PRIu64 "\n",
                 value, range_min, range_max);
     return 0;
 }
@@ -1341,8 +1347,7 @@ void _assert_return_code(const LargestIntegralType result,
 
     if (result > valmax - 1) {
         if (error > 0) {
-            print_error("%s < 0, errno("
-                        LargestIntegralTypePrintfUnsignedFormat "): %s\n",
+            print_error("%s < 0, errno(%" PRIu64 "): %s\n",
                         expression, error, strerror(error));
         } else {
             print_error("%s < 0\n", expression);