Преглед на файлове

Add GCC specific format attribute to the function prototype of shellmatta_printf to mark it as a printf-like function.

This change will allow GCC versions >3.3 to detect the shellmatta_printf() as printf-like and automatically check the format specifiers.
In case of a missing or wrong format specifier a warning/error will bne raised during compile time. E.g.:

example/main.c:52:54: error: format ‘%d’ expects a matching ‘int’ argument [-Werror=format=]
   52 |     shellmatta_printf(handle, "removing command: %s %d\r\n", doSomeCmd.cmd);
      |                                                     ~^
      |                                                      |
      |                                                      int
Mario Hüttel преди 1 година
родител
ревизия
d313514fe8
променени са 1 файла, в които са добавени 17 реда и са изтрити 1 реда
  1. 17 1
      api/shellmatta.h

+ 17 - 1
api/shellmatta.h

@@ -25,6 +25,21 @@
 
 /* global defines */
 
+
+/*
+ * Define the printf format specifier for all GCC versions > 3.3
+ * This will let the compiler know that shelmatta_printf() is a function taking printf-like format specifiers.
+ */
+#ifndef SHELLMATTA_ATTR_FORMAT
+#   if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)))
+#       define SHELLMATTA_ATTR_FORMAT(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
+#   else
+#       define SHELLMATTA_ATTR_FORMAT(fmt, args)
+#   endif
+#else
+#   define SHELLMATTA_ATTR_FORMAT(fmt, args)
+#endif
+
 /**
  * @brief definition of a shellmatta handle
  */
@@ -207,7 +222,8 @@ shellmatta_retCode_t shellmatta_opt_long(   shellmatta_handle_t         handle,
 #ifndef SHELLMATTA_STRIP_PRINTF
 shellmatta_retCode_t shellmatta_printf(     shellmatta_handle_t handle,
                                             const char          *fmt,
-                                            ...);
+                                            ...)
+                                            SHELLMATTA_ATTR_FORMAT(2, 3);
 #endif
 
 #endif