Skip to content

Commit 101d594

Browse files
committed
treewide: avoid address-of-compound-literal idiom in headers
In C99 the construct (T){init-list} is called a compound literal, and is an lvalue. In C++ it is simply a cast expression to non-rvalue type, which is a prvalue. In both languages the expression is a temporary, but in C99 taking its address is well-defined while in C++ it is an error diagnosed as "taking address of temporary". Headers that may be used in C++ application code must avoid invalid expressions. Replace all uses of &(T){init-list} in headers with the functionally equivalent but C++-legal (T[]){{init-list}}. Signed-off-by: Peter A. Bigot <[email protected]>
1 parent 6ade720 commit 101d594

File tree

6 files changed

+32
-32
lines changed

6 files changed

+32
-32
lines changed

include/bluetooth/addr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ typedef struct {
3232
bt_addr_t a;
3333
} bt_addr_le_t;
3434

35-
#define BT_ADDR_ANY (&(bt_addr_t) { { 0, 0, 0, 0, 0, 0 } })
36-
#define BT_ADDR_NONE (&(bt_addr_t) { \
37-
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } })
38-
#define BT_ADDR_LE_ANY (&(bt_addr_le_t) { 0, { { 0, 0, 0, 0, 0, 0 } } })
39-
#define BT_ADDR_LE_NONE (&(bt_addr_le_t) { 0, \
40-
{ { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } })
35+
#define BT_ADDR_ANY ((bt_addr_t[]) { { { 0, 0, 0, 0, 0, 0 } } })
36+
#define BT_ADDR_NONE ((bt_addr_t[]) { { \
37+
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } })
38+
#define BT_ADDR_LE_ANY ((bt_addr_le_t[]) { { 0, { { 0, 0, 0, 0, 0, 0 } } } })
39+
#define BT_ADDR_LE_NONE ((bt_addr_le_t[]) { { 0, \
40+
{ { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } } })
4141

4242
static inline int bt_addr_cmp(const bt_addr_t *a, const bt_addr_t *b)
4343
{

include/bluetooth/bluetooth.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ struct bt_le_adv_param {
321321
* @param _int_max Maximum advertising interval
322322
*/
323323
#define BT_LE_ADV_PARAM(_options, _int_min, _int_max) \
324-
(&(struct bt_le_adv_param) { \
324+
((struct bt_le_adv_param[]) { { \
325325
.options = (_options), \
326326
.interval_min = (_int_min), \
327327
.interval_max = (_int_max), \
328-
})
328+
} })
329329

330330
#define BT_LE_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, \
331331
BT_GAP_ADV_FAST_INT_MIN_2, \
@@ -452,12 +452,12 @@ struct bt_le_scan_param {
452452
* @param _window Scan Window (N * 0.625 ms)
453453
*/
454454
#define BT_LE_SCAN_PARAM(_type, _filter, _interval, _window) \
455-
(&(struct bt_le_scan_param) { \
455+
((struct bt_le_scan_param[]) { { \
456456
.type = (_type), \
457457
.filter_dup = (_filter), \
458458
.interval = (_interval), \
459459
.window = (_window), \
460-
})
460+
} })
461461

462462
/** Helper macro to enable active scanning to discover new devices. */
463463
#define BT_LE_SCAN_ACTIVE BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \

include/bluetooth/conn.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ struct bt_le_conn_param {
4646
* @param to Supervision Timeout (N * 10 ms)
4747
*/
4848
#define BT_LE_CONN_PARAM(int_min, int_max, lat, to) \
49-
(&(struct bt_le_conn_param) { \
49+
((struct bt_le_conn_param[]) { { \
5050
.interval_min = (int_min), \
5151
.interval_max = (int_max), \
5252
.latency = (lat), \
5353
.timeout = (to), \
54-
})
54+
} })
5555

5656
/** Default LE connection parameters:
5757
* Connection Interval: 30-50 ms
@@ -859,9 +859,9 @@ struct bt_br_conn_param {
859859
* @param role_switch True if role switch is allowed
860860
*/
861861
#define BT_BR_CONN_PARAM(role_switch) \
862-
(&(struct bt_br_conn_param) { \
862+
((struct bt_br_conn_param[]) { { \
863863
.allow_role_switch = (role_switch), \
864-
})
864+
} })
865865

866866
/** Default BR/EDR connection parameters:
867867
* Role switch allowed

include/bluetooth/gatt.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
546546
#define BT_GATT_CHARACTERISTIC(_uuid, _props, _perm, _read, _write, _value) \
547547
BT_GATT_ATTRIBUTE(BT_UUID_GATT_CHRC, BT_GATT_PERM_READ, \
548548
bt_gatt_attr_read_chrc, NULL, \
549-
(&(struct bt_gatt_chrc) { .uuid = _uuid, \
550-
.value_handle = 0U, \
551-
.properties = _props, })), \
549+
((struct bt_gatt_chrc[]) { { .uuid = _uuid, \
550+
.value_handle = 0U, \
551+
.properties = _props, } })), \
552552
BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _value)
553553

554554
#if IS_ENABLED(CONFIG_BT_SETTINGS_CCC_LAZY_LOADING)
@@ -660,8 +660,8 @@ ssize_t bt_gatt_attr_write_ccc(struct bt_conn *conn,
660660
* @param _perm CCC access permissions.
661661
*/
662662
#define BT_GATT_CCC(_changed, _perm) \
663-
BT_GATT_CCC_MANAGED((&(struct _bt_gatt_ccc) \
664-
BT_GATT_CCC_INITIALIZER(_changed, NULL, NULL)), _perm)
663+
BT_GATT_CCC_MANAGED(((struct _bt_gatt_ccc[]) \
664+
{BT_GATT_CCC_INITIALIZER(_changed, NULL, NULL)}), _perm)
665665

666666
/** @brief Read Characteristic Extended Properties Attribute helper
667667
*

include/bluetooth/uuid.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ struct bt_uuid_128 {
6969
}
7070

7171
#define BT_UUID_DECLARE_16(value) \
72-
((struct bt_uuid *) (&(struct bt_uuid_16) BT_UUID_INIT_16(value)))
72+
((struct bt_uuid *) ((struct bt_uuid_16[]) {BT_UUID_INIT_16(value)}))
7373
#define BT_UUID_DECLARE_32(value) \
74-
((struct bt_uuid *) (&(struct bt_uuid_32) BT_UUID_INIT_32(value)))
74+
((struct bt_uuid *) ((struct bt_uuid_32[]) {BT_UUID_INIT_32(value)}))
7575
#define BT_UUID_DECLARE_128(value...) \
76-
((struct bt_uuid *) (&(struct bt_uuid_128) BT_UUID_INIT_128(value)))
76+
((struct bt_uuid *) ((struct bt_uuid_128[]) {BT_UUID_INIT_128(value)}))
7777

7878
#define BT_UUID_16(__u) CONTAINER_OF(__u, struct bt_uuid_16, uuid)
7979
#define BT_UUID_32(__u) CONTAINER_OF(__u, struct bt_uuid_32, uuid)

include/data/json.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
213213
.offset = offsetof(struct_, field_name_), \
214214
{ \
215215
.array = { \
216-
.element_descr = &(struct json_obj_descr) { \
216+
.element_descr = (struct json_obj_descr[]) { { \
217217
.align_shift = \
218218
Z_ALIGN_SHIFT(struct_), \
219219
.type = elem_type_, \
220220
.offset = \
221221
offsetof(struct_, \
222222
len_field_), \
223-
}, \
223+
} }, \
224224
.n_elements = (max_len_), \
225225
}, \
226226
}, \
@@ -275,7 +275,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
275275
.offset = offsetof(struct_, field_name_), \
276276
{ \
277277
.array = { \
278-
.element_descr = &(struct json_obj_descr) { \
278+
.element_descr = (struct json_obj_descr[]) { { \
279279
.align_shift = \
280280
Z_ALIGN_SHIFT(struct_), \
281281
.type = JSON_TOK_OBJECT_START, \
@@ -289,7 +289,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
289289
elem_descr_len_, \
290290
}, \
291291
}, \
292-
}, \
292+
} }, \
293293
.n_elements = (max_len_), \
294294
}, \
295295
}, \
@@ -353,7 +353,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
353353
.offset = offsetof(struct_, field_name_), \
354354
{ \
355355
.array = { \
356-
.element_descr = &(struct json_obj_descr) { \
356+
.element_descr = (struct json_obj_descr[]) { { \
357357
.align_shift = \
358358
Z_ALIGN_SHIFT(struct_), \
359359
.type = JSON_TOK_LIST_START, \
@@ -367,7 +367,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
367367
elem_descr_len_, \
368368
}, \
369369
}, \
370-
}, \
370+
} }, \
371371
.n_elements = (max_len_), \
372372
}, \
373373
}, \
@@ -464,13 +464,13 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
464464
.offset = offsetof(struct_, struct_field_name_), \
465465
{ \
466466
.array = { \
467-
.element_descr = &(struct json_obj_descr) { \
467+
.element_descr = (struct json_obj_descr[]) { { \
468468
.align_shift = \
469469
Z_ALIGN_SHIFT(struct_), \
470470
.type = elem_type_, \
471471
.offset = offsetof(struct_, \
472472
len_field_), \
473-
}, \
473+
} }, \
474474
.n_elements = (max_len_), \
475475
}, \
476476
}, \
@@ -534,7 +534,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
534534
.offset = offsetof(struct_, struct_field_name_), \
535535
{ \
536536
.array = { \
537-
.element_descr = &(struct json_obj_descr) { \
537+
.element_descr = (struct json_obj_descr[]) { { \
538538
.align_shift = \
539539
Z_ALIGN_SHIFT(struct_), \
540540
.type = JSON_TOK_OBJECT_START, \
@@ -548,7 +548,7 @@ typedef int (*json_append_bytes_t)(const char *bytes, size_t len,
548548
elem_descr_len_, \
549549
}, \
550550
}, \
551-
}, \
551+
} }, \
552552
.n_elements = (max_len_), \
553553
}, \
554554
}, \

0 commit comments

Comments
 (0)