Fix: headers: make static inline upcasts compatible with C++
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 27 Apr 2019 21:04:45 +0000 (17:04 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 2 May 2019 04:12:56 +0000 (00:12 -0400)
Issue
=====
There are many static inline functions which upcast a pointer from its
original type to another one known to be a parent. The functions upcast
with `(void *)` or `(const void *)`. While this works in C, it does not
in C++, which is more strict:

    In file included from install/include/babeltrace/babeltrace.h:75,
                     from test.cpp:1:

    install/include/babeltrace/trace-ir/field-class-const.h: In function
    ‘const bt_field_class_enumeration_mapping*
    bt_field_class_unsigned_enumeration_mapping_as_mapping_const(const
    bt_field_class_unsigned_enumeration_mapping*)’:

    install/include/babeltrace/trace-ir/field-class-const.h:100:9:
    error: invalid conversion from ‘const void*’ to ‘const
    bt_field_class_enumeration_mapping*’ [-fpermissive]

      return (const void *) mapping;

Solution
========
Use the new __BT_UPCAST() and __BT_UPCAST_CONST() macros to upcast
within headers. Those macros are defined in `babeltrace/types.h`. We use
static_cast<>() in C++ with the specific destination type, going through
`void *` or `const void *` to keep the same address.

Known drawbacks
===============
None.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Iaac8e55635eb2f358a546c32bd886a484f5ebfeb
Reviewed-on: https://review.gerrithub.io/c/eepp/babeltrace/+/452302
Reviewed-by: Simon Marchi <simon.marchi@polymtl.ca>
25 files changed:
include/babeltrace/graph/component-class-filter-const.h
include/babeltrace/graph/component-class-filter.h
include/babeltrace/graph/component-class-sink-const.h
include/babeltrace/graph/component-class-sink.h
include/babeltrace/graph/component-class-source-const.h
include/babeltrace/graph/component-class-source.h
include/babeltrace/graph/component-filter-const.h
include/babeltrace/graph/component-sink-const.h
include/babeltrace/graph/component-source-const.h
include/babeltrace/graph/port-input-const.h
include/babeltrace/graph/port-output-const.h
include/babeltrace/graph/port-output-message-iterator.h
include/babeltrace/graph/self-component-class-filter.h
include/babeltrace/graph/self-component-class-sink.h
include/babeltrace/graph/self-component-class-source.h
include/babeltrace/graph/self-component-filter.h
include/babeltrace/graph/self-component-port-input-message-iterator.h
include/babeltrace/graph/self-component-port-input.h
include/babeltrace/graph/self-component-port-output.h
include/babeltrace/graph/self-component-port.h
include/babeltrace/graph/self-component-sink.h
include/babeltrace/graph/self-component-source.h
include/babeltrace/graph/self-component.h
include/babeltrace/trace-ir/field-class-const.h
include/babeltrace/types.h

index 668d326b678effe2bc7ec9de3ada5fe7ebc4950c..7b11819d3ea31c43b1637925e391564890582185 100644 (file)
  * SOFTWARE.
  */
 
-/* For bt_component_class, bt_component_class_filter */
+/*
+ * For bt_component_class, bt_component_class_filter,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +39,7 @@ const bt_component_class *
 bt_component_class_filter_as_component_class_const(
                const bt_component_class_filter *comp_cls_filter)
 {
-       return (const void *) comp_cls_filter;
+       return __BT_UPCAST_CONST(bt_component_class, comp_cls_filter);
 }
 
 extern void bt_component_class_filter_get_ref(
index cf001bf8cd9d49d809814c1727fc54e4df2d9bad..2e8f472e6632c84c34763f9b851a64f0847a8a0e 100644 (file)
@@ -43,7 +43,7 @@
  * bt_port_output, bt_query_executor, bt_self_component_class_filter,
  * bt_self_component_filter, bt_self_component_port_input,
  * bt_self_component_port_output, bt_value, bt_message_array_const,
- * bt_bool, bt_self_message_iterator
+ * bt_bool, bt_self_message_iterator, __BT_UPCAST
  */
 #include <babeltrace/types.h>
 
@@ -128,7 +128,7 @@ static inline
 bt_component_class *bt_component_class_filter_as_component_class(
                bt_component_class_filter *comp_cls_filter)
 {
-       return (void *) comp_cls_filter;
+       return __BT_UPCAST(bt_component_class, comp_cls_filter);
 }
 
 extern
index ecd0059c31ac01b2f782532091d7b49e90977f79..fd49c6df80eff4f9e714bdb45d815252f93bc297 100644 (file)
@@ -24,7 +24,7 @@
  * SOFTWARE.
  */
 
-/* For bt_component_class, bt_component_class_sink */
+/* For bt_component_class, bt_component_class_sink, __BT_UPCAST_CONST */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +36,7 @@ const bt_component_class *
 bt_component_class_sink_as_component_class_const(
                const bt_component_class_sink *comp_cls_sink)
 {
-       return (const void *) comp_cls_sink;
+       return __BT_UPCAST_CONST(bt_component_class, comp_cls_sink);
 }
 
 extern void bt_component_class_sink_get_ref(
index 601974b9774751edc06f3fe0f33e490a2de2937f..a34dcfd6ea4379b0b67f1cfac8fb80fd09daa5e0 100644 (file)
@@ -38,7 +38,8 @@
 /*
  * For bt_component_class, bt_component_class_sink, bt_port_output,
  * bt_query_executor, bt_self_component_class_sink,
- * bt_self_component_sink, bt_self_component_port_input, bt_value
+ * bt_self_component_sink, bt_self_component_port_input, bt_value,
+ * __BT_UPCAST
  */
 #include <babeltrace/types.h>
 
@@ -83,7 +84,7 @@ static inline
 bt_component_class *bt_component_class_sink_as_component_class(
                bt_component_class_sink *comp_cls_sink)
 {
-       return (void *) comp_cls_sink;
+       return __BT_UPCAST(bt_component_class, comp_cls_sink);
 }
 
 extern
index 60c2665ff68932305c613306ada03bfff87bdcdf..f3c87ff6a60235c1d266b6583fcdc9a6e2bdb065 100644 (file)
  * SOFTWARE.
  */
 
-/* For bt_component_class, bt_component_class_source */
+/*
+ * For bt_component_class, bt_component_class_source,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +39,7 @@ const bt_component_class *
 bt_component_class_source_as_component_class_const(
                const bt_component_class_source *comp_cls_source)
 {
-       return (const void *) comp_cls_source;
+       return __BT_UPCAST_CONST(bt_component_class, comp_cls_source);
 }
 
 extern void bt_component_class_source_get_ref(
index 8697faaa9dfcd985e9af454e983786369e6639cf..4a7088151fb4dc3d3aeb92f08fb0f7f367210eeb 100644 (file)
@@ -42,7 +42,8 @@
  * For bt_component_class, bt_component_class_source, bt_port_input,
  * bt_query_executor, bt_self_component_class_source,
  * bt_self_component_source, bt_self_component_port_output, bt_value,
- * bt_message_array_const, bt_bool, bt_self_message_iterator
+ * bt_message_array_const, bt_bool, bt_self_message_iterator,
+ * __BT_UPCAST
  */
 #include <babeltrace/types.h>
 
@@ -114,7 +115,7 @@ static inline
 bt_component_class *bt_component_class_source_as_component_class(
                bt_component_class_source *comp_cls_source)
 {
-       return (void *) comp_cls_source;
+       return __BT_UPCAST(bt_component_class, comp_cls_source);
 }
 
 extern
index b5d6947dd25877c21d1687a9a8b4727f3625482a..546761e375d5e0af1cb872afe21682714fda1b6c 100644 (file)
 
 #include <stdint.h>
 
-/* For bt_component, bt_component_filter, bt_port_input, bt_port_output */
+/*
+ * For bt_component, bt_component_filter, bt_port_input, bt_port_output,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -37,7 +40,7 @@ static inline
 const bt_component *bt_component_filter_as_component_const(
                const bt_component_filter *component)
 {
-       return (const void *) component;
+       return __BT_UPCAST_CONST(bt_component, component);
 }
 
 extern uint64_t bt_component_filter_get_input_port_count(
index 0fece9ce253124e0c3c4edaf477ff560d8a91d39..d0bd773d3ff0ddfde0628cb74e3fe0c008a01efb 100644 (file)
 
 #include <stdint.h>
 
-/* For bt_component, bt_component_filter, bt_port_input */
+/*
+ * For bt_component, bt_component_filter, bt_port_input,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -37,7 +40,7 @@ static inline
 const bt_component *bt_component_sink_as_component_const(
                const bt_component_sink *component)
 {
-       return (const void *) component;
+       return __BT_UPCAST_CONST(bt_component, component);
 }
 
 extern uint64_t bt_component_sink_get_input_port_count(
index f0d9ee51b7cf9620022f8fff3fb0514ca1f0b592..628f22dbf77b98a47d70ca2815e1a31c7ecd6f39 100644 (file)
 
 #include <stdint.h>
 
-/* For bt_component, bt_component_filter, bt_port_output */
+/*
+ * For bt_component, bt_component_filter, bt_port_output,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -37,7 +40,7 @@ static inline
 const bt_component *bt_component_source_as_component_const(
                const bt_component_source *component)
 {
-       return (void *) component;
+       return __BT_UPCAST_CONST(bt_component, component);
 }
 
 extern uint64_t bt_component_source_get_output_port_count(
index c98972e803238565a7e39944c0ae7e2e7f0d1e59..5301769f08f5476d6b76a61b5ac4e1671905b4b5 100644 (file)
@@ -24,7 +24,7 @@
  * SOFTWARE.
  */
 
-/* For bt_port, bt_port_input */
+/* For bt_port, bt_port_input, __BT_UPCAST_CONST */
 #include <babeltrace/types.h>
 
 #include <stdint.h>
@@ -36,7 +36,7 @@ extern "C" {
 static inline
 const bt_port *bt_port_input_as_port_const(const bt_port_input *port_input)
 {
-       return (const void *) port_input;
+       return __BT_UPCAST_CONST(bt_port, port_input);
 }
 
 extern void bt_port_input_get_ref(const bt_port_input *port_input);
index 19a5db51c1d20cc18d3a81acb63b15387fa19865..0afa640fe84db23c504951d92734cdcbcfdd6daf 100644 (file)
@@ -24,7 +24,7 @@
  * SOFTWARE.
  */
 
-/* For bt_port, bt_port_output */
+/* For bt_port, bt_port_output, __BT_UPCAST_CONST */
 #include <babeltrace/types.h>
 
 #include <stdint.h>
@@ -36,7 +36,7 @@ extern "C" {
 static inline
 const bt_port *bt_port_output_as_port_const(const bt_port_output *port_output)
 {
-       return (const void *) port_output;
+       return __BT_UPCAST_CONST(bt_port, port_output);
 }
 
 extern void bt_port_output_get_ref(const bt_port_output *port_output);
index fa1ab2742b97750c0dd6ffbaf3efc434aeb230fb..f75437812d7051aa876af7a890977c4f958357ee 100644 (file)
@@ -31,7 +31,7 @@
 /*
  * For bt_port, bt_message, bt_message_iterator,
  * bt_port_output_message_iterator, bt_graph, bt_port_output,
- * bt_message_array_const, bt_bool
+ * bt_message_array_const, bt_bool, __BT_UPCAST
  */
 #include <babeltrace/types.h>
 
@@ -44,7 +44,7 @@ bt_message_iterator *
 bt_port_output_message_iterator_as_message_iterator(
                bt_port_output_message_iterator *iterator)
 {
-       return (void *) iterator;
+       return __BT_UPCAST(bt_message_iterator, iterator);
 }
 
 extern bt_port_output_message_iterator *
index 2d209ac241c21f44ebe4848b4d8554fb6c7091ce..1ff7b391cda247bfc032629b55ff7b74a8a637e6 100644 (file)
  * SOFTWARE.
  */
 
-/* For bt_component_class_filter, bt_self_component_class_filter */
+/*
+ * For bt_component_class_filter, bt_self_component_class_filter,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +39,8 @@ const bt_component_class_filter *
 bt_self_component_class_filter_as_component_class_filter(
                bt_self_component_class_filter *self_comp_cls_filter)
 {
-       return (const void *) self_comp_cls_filter;
+       return __BT_UPCAST_CONST(bt_component_class_filter,
+               self_comp_cls_filter);
 }
 
 #ifdef __cplusplus
index 8f40385e4d76378a3e54e74f3d2f65717a784b98..2661e83026f1a77666154a0d34b382110f06a35e 100644 (file)
  * SOFTWARE.
  */
 
-/* For bt_component_class_sink, bt_self_component_class_sink */
+/*
+ * For bt_component_class_sink, bt_self_component_class_sink,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +39,7 @@ const bt_component_class_sink *
 bt_self_component_class_sink_as_component_class_sink(
                bt_self_component_class_sink *self_comp_cls_sink)
 {
-       return (const void *) self_comp_cls_sink;
+       return __BT_UPCAST_CONST(bt_component_class_sink, self_comp_cls_sink);
 }
 
 #ifdef __cplusplus
index 29bb1aa105fef0a2705b311055433396763ffda3..f78b4f78701369ebe178b36c52a80f3e47a2c037 100644 (file)
  * SOFTWARE.
  */
 
-/* For bt_component_class_source, bt_self_component_class_source */
+/*
+ * For bt_component_class_source, bt_self_component_class_source,
+ * __BT_UPCAST_CONST
+ */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -36,7 +39,8 @@ const bt_component_class_source *
 bt_self_component_class_source_as_component_class_source(
                bt_self_component_class_source *self_comp_cls_source)
 {
-       return (const void *) self_comp_cls_source;
+       return __BT_UPCAST_CONST(bt_component_class_source,
+               self_comp_cls_source);
 }
 
 #ifdef __cplusplus
index 8d12e106e4ec36cc3c9856e5c912e4899b3520d1..3fec35cf8424e6c89d088d8ba389b1e748d70ec0 100644 (file)
@@ -31,7 +31,8 @@
 
 /*
  * For bt_component_filter, bt_self_component, bt_self_component_filter,
- * bt_self_component_port_input, bt_self_component_port_output
+ * bt_self_component_port_input, bt_self_component_port_output,
+ * __BT_UPCAST, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -43,7 +44,7 @@ static inline
 bt_self_component *bt_self_component_filter_as_self_component(
                bt_self_component_filter *self_comp_filter)
 {
-       return (void *) self_comp_filter;
+       return __BT_UPCAST(bt_self_component, self_comp_filter);
 }
 
 static inline
@@ -51,7 +52,7 @@ const bt_component_filter *
 bt_self_component_filter_as_component_filter(
                bt_self_component_filter *self_comp_filter)
 {
-       return (const void *) self_comp_filter;
+       return __BT_UPCAST_CONST(bt_component_filter, self_comp_filter);
 }
 
 extern bt_self_component_port_output *
index 61fd2b21acafb50e63ba928b289afc3a51ff5373..1efefe13565d757b9fe575f91bc70438b927c563 100644 (file)
@@ -31,7 +31,8 @@
 /*
  * For bt_component, bt_message_iterator,
  * bt_self_component_port_input_message_iterator,
- * bt_self_component_port_input, bt_message_array_const, bt_bool
+ * bt_self_component_port_input, bt_message_array_const, bt_bool,
+ * __BT_UPCAST
  */
 #include <babeltrace/types.h>
 
@@ -44,7 +45,7 @@ bt_message_iterator *
 bt_self_component_port_input_message_iterator_as_message_iterator(
                bt_self_component_port_input_message_iterator *iterator)
 {
-       return (void *) iterator;
+       return __BT_UPCAST(bt_message_iterator, iterator);
 }
 
 extern bt_self_component_port_input_message_iterator *
index ba3991b28ab6151297a44234fa40d485de0cd512..150499a2e14a3959efe59fdd0cc2040edbdcb5a0 100644 (file)
@@ -25,7 +25,7 @@
 
 /*
  * For bt_port_input, bt_self_component_port,
- * bt_self_component_port_input
+ * bt_self_component_port_input, __BT_UPCAST, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -38,14 +38,14 @@ bt_self_component_port *
 bt_self_component_port_input_as_self_component_port(
                bt_self_component_port_input *self_component_port)
 {
-       return (void *) self_component_port;
+       return __BT_UPCAST(bt_self_component_port, self_component_port);
 }
 
 static inline
 const bt_port_input *bt_self_component_port_input_as_port_input(
                const bt_self_component_port_input *self_component_port)
 {
-       return (const void *) self_component_port;
+       return __BT_UPCAST_CONST(bt_port_input, self_component_port);
 }
 
 #ifdef __cplusplus
index 293f705bf4a2a1707824128a955532a5a7a0fe4f..fee38aa84cc58211fadb4fed4da15f98c61302a6 100644 (file)
@@ -25,7 +25,7 @@
 
 /*
  * For bt_port_output, bt_self_component_port,
- * bt_self_component_port_output
+ * bt_self_component_port_output, __BT_UPCAST, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -38,14 +38,14 @@ bt_self_component_port *
 bt_self_component_port_output_as_self_component_port(
                bt_self_component_port_output *self_component_port)
 {
-       return (void *) self_component_port;
+       return __BT_UPCAST(bt_self_component_port, self_component_port);
 }
 
 static inline
 const bt_port_output *bt_self_component_port_output_as_port_output(
                bt_self_component_port_output *self_component_port)
 {
-       return (const void *) self_component_port;
+       return __BT_UPCAST_CONST(bt_port_output, self_component_port);
 }
 
 #ifdef __cplusplus
index ad5daeb8cf52917a4486b92555e8b94b2622ba06..c524cac30eaa5f1a6f10086c99e51385f11d79c8 100644 (file)
@@ -24,7 +24,8 @@
  */
 
 /*
- * For bt_port, bt_self_component_port, bt_self_component, bt_connection
+ * For bt_port, bt_self_component_port, bt_self_component,
+ * bt_connection, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -40,7 +41,7 @@ static inline
 const bt_port *bt_self_component_port_as_port(
                bt_self_component_port *self_port)
 {
-       return (const void *) self_port;
+       return __BT_UPCAST_CONST(bt_port, self_port);
 }
 
 extern bt_self_component *bt_self_component_port_borrow_component(
index 9f2d6bd6d7504d9b11b68339fb2788407ad5da8b..04b8d206a19d639582f0f918175415eaf7ba9735 100644 (file)
@@ -30,7 +30,7 @@
 
 /*
  * For bt_component_sink, bt_self_component, bt_self_component_sink,
- * bt_self_component_port_input
+ * bt_self_component_port_input, __BT_UPCAST, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -42,7 +42,7 @@ static inline
 bt_self_component *bt_self_component_sink_as_self_component(
                bt_self_component_sink *self_comp_sink)
 {
-       return (void *) self_comp_sink;
+       return __BT_UPCAST(bt_self_component, self_comp_sink);
 }
 
 static inline
@@ -50,7 +50,7 @@ const bt_component_sink *
 bt_self_component_sink_as_component_sink(
                bt_self_component_sink *self_comp_sink)
 {
-       return (const void *) self_comp_sink;
+       return __BT_UPCAST_CONST(bt_component_sink, self_comp_sink);
 }
 
 extern bt_self_component_port_input *
index 3b96af1e2592b085aaf51b42f5bbbd8db642a808..5fdee3f33e416bc884277f64ed6d1144c61a389f 100644 (file)
@@ -30,7 +30,7 @@
 
 /*
  * For bt_component_source, bt_self_component, bt_self_component_source,
- * bt_self_component_port_output
+ * bt_self_component_port_output, __BT_UPCAST, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -42,7 +42,7 @@ static inline
 bt_self_component *bt_self_component_source_as_self_component(
                bt_self_component_source *self_comp_source)
 {
-       return (void *) self_comp_source;
+       return __BT_UPCAST(bt_self_component, self_comp_source);
 }
 
 static inline
@@ -50,7 +50,7 @@ const bt_component_source *
 bt_self_component_source_as_component_source(
                bt_self_component_source *self_comp_source)
 {
-       return (const void *) self_comp_source;
+       return __BT_UPCAST_CONST(bt_component_source, self_comp_source);
 }
 
 extern bt_self_component_port_output *
index 65ff2e67de9853d0f3ec567e9ff41c2c310fa64b..0be09442831b26e7d4e099a0bb357e3fb72e9476 100644 (file)
@@ -23,7 +23,7 @@
  * SOFTWARE.
  */
 
-/* For bt_component, bt_self_component */
+/* For bt_component, bt_self_component, __BT_UPCAST */
 #include <babeltrace/types.h>
 
 #ifdef __cplusplus
@@ -43,7 +43,7 @@ static inline
 const bt_component *bt_self_component_as_component(
                bt_self_component *self_component)
 {
-       return (const void *) self_component;
+       return __BT_UPCAST(bt_component, self_component);
 }
 
 extern void *bt_self_component_get_data(
index eb9cb1508a2f1fb5767081f4df73b2a98f3f3a72..9f2f960b40ec9ff4b4b4612ef9eb8c9c70f2caa8 100644 (file)
@@ -32,7 +32,7 @@
  * bt_field_class_enumeration_mapping,
  * bt_field_class_unsigned_enumeration_mapping,
  * bt_field_class_signed_enumeration_mapping,
- * bt_field_class_enumeration_mapping_label_array
+ * bt_field_class_enumeration_mapping_label_array, __BT_UPCAST_CONST
  */
 #include <babeltrace/types.h>
 
@@ -97,7 +97,7 @@ const bt_field_class_enumeration_mapping *
 bt_field_class_unsigned_enumeration_mapping_as_mapping_const(
                const bt_field_class_unsigned_enumeration_mapping *mapping)
 {
-       return (const void *) mapping;
+       return __BT_UPCAST_CONST(bt_field_class_enumeration_mapping, mapping);
 }
 
 static inline
@@ -105,7 +105,7 @@ const bt_field_class_enumeration_mapping *
 bt_field_class_signed_enumeration_mapping_as_mapping_const(
                const bt_field_class_signed_enumeration_mapping *mapping)
 {
-       return (const void *) mapping;
+       return __BT_UPCAST_CONST(bt_field_class_enumeration_mapping, mapping);
 }
 
 extern const char *bt_field_class_enumeration_mapping_get_label(
index 9f71793a1b4b095f73f60bf2132e6a2083daec90..12b35f602445698b99e544c7919dbe3b3e8fda9f 100644 (file)
 
 #include <stdint.h>
 
+/* Internal use */
+#ifdef __cplusplus
+# define __BT_UPCAST(_type, _p)                static_cast<_type *>(static_cast<void *>(_p))
+# define __BT_UPCAST_CONST(_type, _p)  static_cast<const _type *>(static_cast<const void *>(_p))
+#else
+# define __BT_UPCAST(_type, _p)                ((_type *) (_p))
+# define __BT_UPCAST_CONST(_type, _p)  ((const _type *) (_p))
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
This page took 0.037611 seconds and 5 git commands to generate.