[vlc-commits] [Git][videolan/vlc][master] 2 commits: plugins: add type strictness to plugin descriptor macros

Rémi Denis-Courmont (@Courmisch) gitlab at videolan.org
Thu Feb 3 18:39:18 UTC 2022



Rémi Denis-Courmont pushed to branch master at VideoLAN / VLC


Commits:
e8f6a595 by Lyndon Brown at 2022-02-03T16:14:17+00:00
plugins: add type strictness to plugin descriptor macros

I.e. check the type of the values provided, rather than just blindly
casting, and thus allow mistakes to get caught with compilation errors.

I once lost some hours tracking down the cause of a crash that this type
strictness would have prevented.

Note, now that `change_string_list()` and `change_integer_list()` use type
strictness for the value and label arrays to help make sure they're the
right types, we can more safely make use of `ARRAY_SIZE()` to more cleanly
get the length.

`set_callback[s]()` is excluded from this for obvious reasons.

I've taken the opportunity to ditch remaining use of Hungarian notation
and to fix a param name that should be singular rather than plural
(psz_caps on `add_module[_list]()`.

Re-implemented using compound literals upon excellent suggestion by
Alexandre Janniaux.

- - - - -
17604330 by Lyndon Brown at 2022-02-03T16:14:17+00:00
plugins: use helper to workaround missing c++ language feature support

The previous commit made use of compound literals. MSVC was identified
as not yet having support for compound literals in C++ though, which is
considered a blocking problem to their use. This work around was
suggested by Alexandre Janniaux.

- - - - -


1 changed file:

- include/vlc_plugin.h


Changes:

=====================================
include/vlc_plugin.h
=====================================
@@ -283,6 +283,13 @@ EXTERN_SYMBOL DLL_SYMBOL
 int CDECL_SYMBOL VLC_SYMBOL(vlc_entry_cfg_str_enum)(const char *name,
     char ***values, char ***descs);
 
+/* Workaround for lack of C++ compound literals support in some compilers */
+#ifdef __cplusplus
+# define VLC_CHECKED_TYPE(type, value) [](type v){ return v; }(value)
+#else
+# define VLC_CHECKED_TYPE(type, value) (type){ value }
+#endif
+
 /*
  * InitModule: this function is called once and only once, when the module
  * is looked at for the first time. We get the useful data from it, for
@@ -322,20 +329,20 @@ VLC_METADATA_EXPORTS
 }
 
 #define set_shortname( shortname ) \
-    if (vlc_module_set (VLC_MODULE_SHORTNAME, (const char *)(shortname))) \
+    if (vlc_module_set (VLC_MODULE_SHORTNAME, VLC_CHECKED_TYPE(const char *, shortname))) \
         goto error;
 
 #define set_description( desc ) \
-    if (vlc_module_set (VLC_MODULE_DESCRIPTION, (const char *)(desc))) \
+    if (vlc_module_set (VLC_MODULE_DESCRIPTION, VLC_CHECKED_TYPE(const char *, desc))) \
         goto error;
 
 #define set_help( help ) \
-    if (vlc_module_set (VLC_MODULE_HELP, (const char *)(help))) \
+    if (vlc_module_set (VLC_MODULE_HELP, VLC_CHECKED_TYPE(const char *, help))) \
         goto error;
 
 #define set_capability( cap, score ) \
-    if (vlc_module_set (VLC_MODULE_CAPABILITY, (const char *)(cap)) \
-     || vlc_module_set (VLC_MODULE_SCORE, (int)(score))) \
+    if (vlc_module_set (VLC_MODULE_CAPABILITY, VLC_CHECKED_TYPE(const char *, cap)) \
+     || vlc_module_set (VLC_MODULE_SCORE, VLC_CHECKED_TYPE(int, score))) \
         goto error;
 
 #define set_callback(activate) \
@@ -353,7 +360,7 @@ VLC_METADATA_EXPORTS
         goto error;
 
 #define set_text_domain( dom ) \
-    if (vlc_plugin_set (VLC_MODULE_TEXTDOMAIN, (dom))) \
+    if (vlc_plugin_set (VLC_MODULE_TEXTDOMAIN, VLC_CHECKED_TYPE(const char *, dom))) \
         goto error;
 
 /*****************************************************************************
@@ -374,25 +381,25 @@ VLC_METADATA_EXPORTS
 
 #define add_typedesc_inner( type, text, longtext ) \
     add_type_inner( type ) \
-    vlc_config_set (VLC_CONFIG_DESC, \
-                    (const char *)(text), (const char *)(longtext));
+    vlc_config_set (VLC_CONFIG_DESC, VLC_CHECKED_TYPE(const char *, text), \
+                                     VLC_CHECKED_TYPE(const char *, longtext));
 
 #define add_typename_inner(type, name, text, longtext) \
     add_typedesc_inner(type, text, longtext) \
-    vlc_config_set (VLC_CONFIG_NAME, (const char *)(name));
+    vlc_config_set (VLC_CONFIG_NAME, VLC_CHECKED_TYPE(const char *, name));
 
 #define add_string_inner(type, name, text, longtext, v) \
     add_typename_inner(type, name, text, longtext) \
-    vlc_config_set (VLC_CONFIG_VALUE, (const char *)(v));
+    vlc_config_set (VLC_CONFIG_VALUE, VLC_CHECKED_TYPE(const char *, v));
 
 #define add_int_inner(type, name, text, longtext, v) \
     add_typename_inner(type, name, text, longtext) \
-    vlc_config_set (VLC_CONFIG_VALUE, (int64_t)(v));
+    vlc_config_set (VLC_CONFIG_VALUE, VLC_CHECKED_TYPE(int64_t, v));
 
 
-#define set_subcategory( i_id ) \
+#define set_subcategory( id ) \
     add_type_inner( CONFIG_SUBCATEGORY ) \
-    vlc_config_set (VLC_CONFIG_VALUE, (int64_t)(i_id));
+    vlc_config_set (VLC_CONFIG_VALUE, VLC_CHECKED_TYPE(int64_t, id));
 
 #define set_section( text, longtext ) \
     add_typedesc_inner( CONFIG_SECTION, text, longtext )
@@ -420,23 +427,23 @@ VLC_METADATA_EXPORTS
 #define add_font(name, value, text, longtext) \
     add_string_inner(CONFIG_ITEM_FONT, name, text, longtext, value)
 
-#define add_module(name, psz_caps, value, text, longtext) \
+#define add_module(name, cap, value, text, longtext) \
     add_string_inner(CONFIG_ITEM_MODULE, name, text, longtext, value) \
-    vlc_config_set (VLC_CONFIG_CAPABILITY, (const char *)(psz_caps));
+    vlc_config_set (VLC_CONFIG_CAPABILITY, VLC_CHECKED_TYPE(const char *, cap));
 
-#define add_module_list(name, psz_caps, value, text, longtext) \
+#define add_module_list(name, cap, value, text, longtext) \
     add_string_inner(CONFIG_ITEM_MODULE_LIST, name, text, longtext, value) \
-    vlc_config_set (VLC_CONFIG_CAPABILITY, (const char *)(psz_caps));
+    vlc_config_set (VLC_CONFIG_CAPABILITY, VLC_CHECKED_TYPE(const char *, cap));
 
 #ifndef __PLUGIN__
-#define add_module_cat(name, i_subcategory, value, text, longtext) \
+#define add_module_cat(name, subcategory, value, text, longtext) \
     add_string_inner(CONFIG_ITEM_MODULE_CAT, name, text, longtext, value) \
-    change_integer_range (i_subcategory /* gruik */, 0);
+    change_integer_range (subcategory /* gruik */, 0);
 
-#define add_module_list_cat(name, i_subcategory, value, text, longtext) \
+#define add_module_list_cat(name, subcategory, value, text, longtext) \
     add_string_inner(CONFIG_ITEM_MODULE_LIST_CAT, name, text, longtext, \
                      value) \
-    change_integer_range (i_subcategory /* gruik */, 0);
+    change_integer_range (subcategory /* gruik */, 0);
 #endif
 
 #define add_integer( name, value, text, longtext ) \
@@ -451,17 +458,17 @@ VLC_METADATA_EXPORTS
                      KEY_UNSET) \
     add_string_inner(CONFIG_ITEM_KEY, name, text, longtext, value)
 
-#define add_integer_with_range( name, value, i_min, i_max, text, longtext ) \
+#define add_integer_with_range( name, value, min, max, text, longtext ) \
     add_integer( name, value, text, longtext ) \
-    change_integer_range( i_min, i_max )
+    change_integer_range( min, max )
 
 #define add_float( name, v, text, longtext ) \
     add_typename_inner(CONFIG_ITEM_FLOAT, name, text, longtext) \
-    vlc_config_set (VLC_CONFIG_VALUE, (double)(v));
+    vlc_config_set (VLC_CONFIG_VALUE, VLC_CHECKED_TYPE(double, v));
 
-#define add_float_with_range( name, value, f_min, f_max, text, longtext ) \
+#define add_float_with_range( name, value, min, max, text, longtext ) \
     add_float( name, value, text, longtext ) \
-    change_float_range( f_min, f_max )
+    change_float_range( min, max )
 
 #define add_bool( name, v, text, longtext ) \
     add_typename_inner(CONFIG_ITEM_BOOL, name, text, longtext) \
@@ -470,7 +477,7 @@ VLC_METADATA_EXPORTS
 /* For removed option */
 #define add_obsolete_inner( name, type ) \
     add_type_inner( type ) \
-    vlc_config_set (VLC_CONFIG_NAME, (const char *)(name)); \
+    vlc_config_set (VLC_CONFIG_NAME, VLC_CHECKED_TYPE(const char *, name)); \
     vlc_config_set (VLC_CONFIG_REMOVED);
 
 #define add_obsolete_bool( name ) \
@@ -488,25 +495,27 @@ VLC_METADATA_EXPORTS
 /* Modifier macros for the config options (used for fine tuning) */
 
 #define change_short( ch ) \
-    vlc_config_set (VLC_CONFIG_SHORTCUT, (int)(ch));
+    vlc_config_set (VLC_CONFIG_SHORTCUT, VLC_CHECKED_TYPE(char, ch));
 
 #define change_string_list( list, list_text ) \
     vlc_config_set (VLC_CONFIG_LIST, \
-                    (size_t)(sizeof (list) / sizeof (char *)), \
-                    (const char *const *)(list), \
-                    (const char *const *)(list_text));
+                    ARRAY_SIZE(list), \
+                    VLC_CHECKED_TYPE(const char *const *, list), \
+                    VLC_CHECKED_TYPE(const char *const *, list_text));
 
 #define change_integer_list( list, list_text ) \
     vlc_config_set (VLC_CONFIG_LIST, \
-                    (size_t)(sizeof (list) / sizeof (int)), \
-                    (const int *)(list), \
-                    (const char *const *)(list_text));
+                    ARRAY_SIZE(list), \
+                    VLC_CHECKED_TYPE(const int *, list), \
+                    VLC_CHECKED_TYPE(const char *const *, list_text));
 
 #define change_integer_range( minv, maxv ) \
-    vlc_config_set (VLC_CONFIG_RANGE, (int64_t)(minv), (int64_t)(maxv));
+    vlc_config_set (VLC_CONFIG_RANGE, VLC_CHECKED_TYPE(int64_t, minv), \
+                                      VLC_CHECKED_TYPE(int64_t, maxv));
 
 #define change_float_range( minv, maxv ) \
-    vlc_config_set (VLC_CONFIG_RANGE, (double)(minv), (double)(maxv));
+    vlc_config_set (VLC_CONFIG_RANGE, VLC_CHECKED_TYPE(double, minv), \
+                                      VLC_CHECKED_TYPE(double, maxv));
 
 /* For options that are saved but hidden from the preferences panel */
 #define change_private() \



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/aa5a6aad699ab83d3eb00903a7ff46d3ca3b7822...17604330bf6de22851806f3b36d6455cef0f57cb

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/aa5a6aad699ab83d3eb00903a7ff46d3ca3b7822...17604330bf6de22851806f3b36d6455cef0f57cb
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list