Skip to content

Commit 9a37780

Browse files
committed
unix/coveragecpp: Verify struct-initializing macros' C++-compatibility.
Add code using all relevant macros to make sure they initialize structs correctly. Signed-off-by: stijn <[email protected]>
1 parent 02eea0d commit 9a37780

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

ports/unix/coveragecpp.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,61 @@ extern "C" {
1717
#include <py/runtime.h>
1818
}
1919

20+
// Invoke all (except one, see below) public API macros which initialize structs to make sure
21+
// they are C++-compatible, meaning they explicitly initialize all struct members.
22+
mp_obj_t f0();
23+
MP_DEFINE_CONST_FUN_OBJ_0(f0_obj, f0);
24+
mp_obj_t f1(mp_obj_t);
25+
MP_DEFINE_CONST_FUN_OBJ_1(f1_obj, f1);
26+
mp_obj_t f2(mp_obj_t, mp_obj_t);
27+
MP_DEFINE_CONST_FUN_OBJ_2(f2_obj, f2);
28+
mp_obj_t f3(mp_obj_t, mp_obj_t, mp_obj_t);
29+
MP_DEFINE_CONST_FUN_OBJ_3(f3_obj, f3);
30+
mp_obj_t fvar(size_t, const mp_obj_t *);
31+
MP_DEFINE_CONST_FUN_OBJ_VAR(fvar_obj, 1, fvar);
32+
mp_obj_t fvarbetween(size_t, const mp_obj_t *);
33+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fvarbetween_obj, 1, 2, fvarbetween);
34+
mp_obj_t fkw(size_t, const mp_obj_t *, mp_map_t *);
35+
MP_DEFINE_CONST_FUN_OBJ_KW(fkw_obj, 1, fkw);
36+
37+
static const mp_rom_map_elem_t table[] = {
38+
{ MP_ROM_QSTR(MP_QSTR_f0), MP_ROM_PTR(&f0_obj) },
39+
};
40+
MP_DEFINE_CONST_MAP(map, table);
41+
MP_DEFINE_CONST_DICT(dict, table);
42+
43+
static const qstr attrtuple_fields[] = {
44+
MP_QSTR_f0,
45+
};
46+
MP_DEFINE_ATTRTUPLE(attrtuple, attrtuple_fields, 1, MP_ROM_PTR(&f0_obj));
47+
48+
void nlr_cb(void *);
49+
void nlr_cb(void *){
50+
}
51+
52+
// The MP_DEFINE_CONST_OBJ_TYPE macro is not C++-compatible because each of the
53+
// MP_DEFINE_CONST_OBJ_TYPE_NARGS_X macros only initializes some of _mp_obj_type_t's
54+
// .slot_index_xxx members but that cannot be fixed to be done in a deterministic way.
55+
56+
2057
#if defined(MICROPY_UNIX_COVERAGE)
2158

2259
// Just to test building of C++ code.
2360
static mp_obj_t extra_cpp_coverage_impl() {
61+
MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, nlr_cb, (void *) nlr_cb);
62+
63+
// To avoid 'error: unused variable [-Werror,-Wunused-const-variable]'.
64+
(void) ctx;
65+
(void) f0_obj;
66+
(void) f1_obj;
67+
(void) f2_obj;
68+
(void) f3_obj;
69+
(void) fvar_obj;
70+
(void) fvarbetween_obj;
71+
(void) fkw_obj;
72+
(void) map;
73+
(void) dict;
74+
(void) attrtuple;
2475
return mp_const_none;
2576
}
2677

0 commit comments

Comments
 (0)