Skip to contentSkip to conversation

Fix MISRA C 2012 Rule 20.7 violations

PATCH Superseded Xenia Ragiadakou 6 participants 37 messages Aug 19, 2022 – Sep 29, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fix MISRA C 2012 Rule 20.7 violations

Xenia Ragiadakou (7):
xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations xen/elf: Fix MISRA C 2012 Rule 20.7 violations xen/vgic: Fix MISRA C 2012 Rule 20.7 violation xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

 xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
 xen/arch/arm/include/asm/new_vgic.h   |  2 +-
 xen/arch/arm/include/asm/processor.h  |  4 ++--
 xen/include/xen/device_tree.h         |  6 +++---
 xen/include/xen/elfstructs.h          |  4 ++--
 xen/lib/rbtree.c                      |  2 +-
 xen/xsm/flask/ss/sidtab.c             |  8 ++++----
 7 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/7 xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations

In macros GITS_TYPER_DEVICE_ID_BITS(), GITS_TYPER_EVENT_ID_BITS() and GITS_BASER_ENTRY_SIZE(), add parentheses around the macro parameter to prevent against unintended expansions.
Realign subsequent lines, if any.

---
 xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/gic_v3_its.h +5 -5
index 94e5cb99c5..168617097f 100644
@@ -46,13 +46,13 @@
 #define GITS_TYPER_PTA                  BIT(19, UL)
 #define GITS_TYPER_DEVIDS_SHIFT         13
 #define GITS_TYPER_DEVIDS_MASK          (0x1fUL << GITS_TYPER_DEVIDS_SHIFT)
-#define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
-                                               GITS_TYPER_DEVIDS_SHIFT) + 1)
+#define GITS_TYPER_DEVICE_ID_BITS(r)    ((((r) & GITS_TYPER_DEVIDS_MASK) >> \
+                                                 GITS_TYPER_DEVIDS_SHIFT) + 1)
 
 #define GITS_TYPER_IDBITS_SHIFT         8
 #define GITS_TYPER_IDBITS_MASK          (0x1fUL << GITS_TYPER_IDBITS_SHIFT)
-#define GITS_TYPER_EVENT_ID_BITS(r)     (((r & GITS_TYPER_IDBITS_MASK) >> \
-                                               GITS_TYPER_IDBITS_SHIFT) + 1)
+#define GITS_TYPER_EVENT_ID_BITS(r)     ((((r) & GITS_TYPER_IDBITS_MASK) >> \
+                                                 GITS_TYPER_IDBITS_SHIFT) + 1)
 
 #define GITS_TYPER_ITT_SIZE_SHIFT       4
 #define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
@@ -75,7 +75,7 @@
 #define GITS_BASER_TYPE_RESERVED7       7UL
 #define GITS_BASER_ENTRY_SIZE_SHIFT     48
 #define GITS_BASER_ENTRY_SIZE(reg)                                       \
-                        (((reg >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1)
+                        ((((reg) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1)
 #define GITS_BASER_SHAREABILITY_SHIFT   10
 #define GITS_BASER_PAGE_SIZE_SHIFT      8
 #define GITS_BASER_SIZE_MASK            0xff
Signature · 1 line
2.34.1
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations

Quoted 6 lines · Xenia Ragiadakou

In macros GITS_TYPER_DEVICE_ID_BITS(), GITS_TYPER_EVENT_ID_BITS() and GITS_BASER_ENTRY_SIZE(), add parentheses around the macro parameter to prevent against unintended expansions.
Realign subsequent lines, if any.

Quoted 38 lines
---
 xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/arch/arm/include/asm/gic_v3_its.h +5 -5
index 94e5cb99c5..168617097f 100644
@@ -46,13 +46,13 @@
 #define GITS_TYPER_PTA                  BIT(19, UL)
 #define GITS_TYPER_DEVIDS_SHIFT         13
 #define GITS_TYPER_DEVIDS_MASK          (0x1fUL << GITS_TYPER_DEVIDS_SHIFT)
-#define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
-                                               GITS_TYPER_DEVIDS_SHIFT) + 1)
+#define GITS_TYPER_DEVICE_ID_BITS(r)    ((((r) & GITS_TYPER_DEVIDS_MASK) >> \
+                                                 GITS_TYPER_DEVIDS_SHIFT) + 1)
 
 #define GITS_TYPER_IDBITS_SHIFT         8
 #define GITS_TYPER_IDBITS_MASK          (0x1fUL << GITS_TYPER_IDBITS_SHIFT)
-#define GITS_TYPER_EVENT_ID_BITS(r)     (((r & GITS_TYPER_IDBITS_MASK) >> \
-                                               GITS_TYPER_IDBITS_SHIFT) + 1)
+#define GITS_TYPER_EVENT_ID_BITS(r)     ((((r) & GITS_TYPER_IDBITS_MASK) >> \
+                                                 GITS_TYPER_IDBITS_SHIFT) + 1)
 
 #define GITS_TYPER_ITT_SIZE_SHIFT       4
 #define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
@@ -75,7 +75,7 @@
 #define GITS_BASER_TYPE_RESERVED7       7UL
 #define GITS_BASER_ENTRY_SIZE_SHIFT     48
 #define GITS_BASER_ENTRY_SIZE(reg)                                       \
-                        (((reg >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1)
+                        ((((reg) >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1)
 #define GITS_BASER_SHAREABILITY_SHIFT   10
 #define GITS_BASER_PAGE_SIZE_SHIFT      8
 #define GITS_BASER_SIZE_MASK            0xff
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/7 xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou

In macros SIDTAB_HASH(), INIT_SIDTAB_LOCK(), SIDTAB_LOCK() and SIDTAB_UNLOCK(), add parentheses around the macro parameter to prevent against unintended expansions.

---
 xen/xsm/flask/ss/sidtab.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/xsm/flask/ss/sidtab.c +4 -4
index 74babfac9c..69fc3389b3 100644
@@ -14,11 +14,11 @@
 #include "security.h"
 #include "sidtab.h"
 
-#define SIDTAB_HASH(sid) (sid & SIDTAB_HASH_MASK)
+#define SIDTAB_HASH(sid) ((sid) & SIDTAB_HASH_MASK)
 
-#define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
-#define SIDTAB_LOCK(s) spin_lock(&s->lock)
-#define SIDTAB_UNLOCK(s) spin_unlock(&s->lock)
+#define INIT_SIDTAB_LOCK(s) spin_lock_init(&(s)->lock)
+#define SIDTAB_LOCK(s) spin_lock(&(s)->lock)
+#define SIDTAB_UNLOCK(s) spin_unlock(&(s)->lock)
 
 int sidtab_init(struct sidtab *s)
 {
Signature · 1 line
2.34.1
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations

Quoted 5 lines · Xenia Ragiadakou

In macros SIDTAB_HASH(), INIT_SIDTAB_LOCK(), SIDTAB_LOCK() and SIDTAB_UNLOCK(), add parentheses around the macro parameter to prevent against unintended expansions.

Quoted 28 lines
---
 xen/xsm/flask/ss/sidtab.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/xsm/flask/ss/sidtab.c +4 -4
index 74babfac9c..69fc3389b3 100644
@@ -14,11 +14,11 @@
 #include "security.h"
 #include "sidtab.h"
 
-#define SIDTAB_HASH(sid) (sid & SIDTAB_HASH_MASK)
+#define SIDTAB_HASH(sid) ((sid) & SIDTAB_HASH_MASK)
 
-#define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
-#define SIDTAB_LOCK(s) spin_lock(&s->lock)
-#define SIDTAB_UNLOCK(s) spin_unlock(&s->lock)
+#define INIT_SIDTAB_LOCK(s) spin_lock_init(&(s)->lock)
+#define SIDTAB_LOCK(s) spin_lock(&(s)->lock)
+#define SIDTAB_UNLOCK(s) spin_unlock(&(s)->lock)
 
 int sidtab_init(struct sidtab *s)
 {
Signature · 1 line
2.34.1
Acked-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou
Quoted 19 lines · Xenia Ragiadakou

In macros SIDTAB_HASH(), INIT_SIDTAB_LOCK(), SIDTAB_LOCK() and SIDTAB_UNLOCK(), add parentheses around the macro parameter to prevent against unintended expansions.

---
 xen/xsm/flask/ss/sidtab.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/xsm/flask/ss/sidtab.c +1 -1
index 74babfac9c..69fc3389b3 100644
@@ -14,11 +14,11 @@
 #include "security.h"
 #include "sidtab.h"
 
-#define SIDTAB_HASH(sid) (sid & SIDTAB_HASH_MASK)
+#define SIDTAB_HASH(sid) ((sid) & SIDTAB_HASH_MASK)

xen/xsm/flask/ss/sidtab.c line 24

Quoted 10 lines
xen/xsm/flask/ss/sidtab.c +3 -3
 
-#define INIT_SIDTAB_LOCK(s) spin_lock_init(&s->lock)
-#define SIDTAB_LOCK(s) spin_lock(&s->lock)
-#define SIDTAB_UNLOCK(s) spin_unlock(&s->lock)
+#define INIT_SIDTAB_LOCK(s) spin_lock_init(&(s)->lock)
+#define SIDTAB_LOCK(s) spin_lock(&(s)->lock)
+#define SIDTAB_UNLOCK(s) spin_unlock(&(s)->lock)
 
 int sidtab_init(struct sidtab *s)
 {
#6 lore raw

PATCH 3/7 xen/elf: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou

In macros ELF32_ST_TYPE() and ELF64_ST_TYPE(), add parentheses around the macro parameter to prevent against unintended expansions.

---
 xen/include/xen/elfstructs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/elfstructs.h +2 -2
index 616ebf9269..0a7c558a80 100644
@@ -305,11 +305,11 @@ typedef struct {
 
 /* Extract symbol info - st_info */
 #define ELF32_ST_BIND(x)	((x) >> 4)
-#define ELF32_ST_TYPE(x)	(((unsigned int) x) & 0xf)
+#define ELF32_ST_TYPE(x)	(((unsigned int) (x)) & 0xf)
 #define ELF32_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
 
 #define ELF64_ST_BIND(x)	((x) >> 4)
-#define ELF64_ST_TYPE(x)	(((unsigned int) x) & 0xf)
+#define ELF64_ST_TYPE(x)	(((unsigned int) (x)) & 0xf)
 #define ELF64_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
 
 /* Symbol Binding - ELF32_ST_BIND - st_info */
Signature · 1 line
2.34.1
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] xen/elf: Fix MISRA C 2012 Rule 20.7 violations

Quoted 4 lines · Xenia Ragiadakou

In macros ELF32_ST_TYPE() and ELF64_ST_TYPE(), add parentheses around the macro parameter to prevent against unintended expansions.

Quoted 25 lines
---
 xen/include/xen/elfstructs.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/elfstructs.h +2 -2
index 616ebf9269..0a7c558a80 100644
@@ -305,11 +305,11 @@ typedef struct {
 
 /* Extract symbol info - st_info */
 #define ELF32_ST_BIND(x)	((x) >> 4)
-#define ELF32_ST_TYPE(x)	(((unsigned int) x) & 0xf)
+#define ELF32_ST_TYPE(x)	(((unsigned int) (x)) & 0xf)
 #define ELF32_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
 
 #define ELF64_ST_BIND(x)	((x) >> 4)
-#define ELF64_ST_TYPE(x)	(((unsigned int) x) & 0xf)
+#define ELF64_ST_TYPE(x)	(((unsigned int) (x)) & 0xf)
 #define ELF64_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
 
 /* Symbol Binding - ELF32_ST_BIND - st_info */
Signature · 1 line
2.34.1
#8 lore raw

PATCH 4/7 xen/vgic: Fix MISRA C 2012 Rule 20.7 violation

In reply to Xenia Ragiadakou

In macro VGIC_V3_LR_INDEX(), add parentheses around the macro parameter to prevent against unintended expansions.

---
 xen/arch/arm/include/asm/new_vgic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/include/asm/new_vgic.h +1 -1
index ab57fcd91d..b7fa9ab11a 100644
@@ -43,7 +43,7 @@ enum vgic_type {
 
 #define VGIC_V2_MAX_LRS         (1 << 6)
 #define VGIC_V3_MAX_LRS         16
-#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - lr)
+#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - (lr))
 
 #define VGIC_CONFIG_EDGE        false
 #define VGIC_CONFIG_LEVEL       true
Signature · 1 line
2.34.1
Reviewed-by on v1 4/7
#9 lore raw

Re: [PATCH 4/7] xen/vgic: Fix MISRA C 2012 Rule 20.7 violation

Quoted 4 lines · Xenia Ragiadakou

In macro VGIC_V3_LR_INDEX(), add parentheses around the macro parameter to prevent against unintended expansions.

Quoted 20 lines
---
 xen/arch/arm/include/asm/new_vgic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/include/asm/new_vgic.h +1 -1
index ab57fcd91d..b7fa9ab11a 100644
@@ -43,7 +43,7 @@ enum vgic_type {
 
 #define VGIC_V2_MAX_LRS         (1 << 6)
 #define VGIC_V3_MAX_LRS         16
-#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - lr)
+#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - (lr))
 
 #define VGIC_CONFIG_EDGE        false
 #define VGIC_CONFIG_LEVEL       true
Signature · 1 line
2.34.1
#10 lore raw

PATCH 5/7 xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation

In reply to Xenia Ragiadakou

In macro __rb_parent(), add parentheses around the macro parameter to prevent against unintended expansions.

---
 xen/lib/rbtree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/lib/rbtree.c +1 -1
index 85a4f20313..eb418baabb 100644
@@ -46,7 +46,7 @@
 #define		RB_RED		0
 #define		RB_BLACK	1
 
-#define __rb_parent(pc)    ((struct rb_node *)(pc & ~3))
+#define __rb_parent(pc)    ((struct rb_node *)((pc) & ~3))
 
 #define __rb_color(pc)     ((pc) & 1)
 #define __rb_is_black(pc)  __rb_color(pc)
Signature · 1 line
2.34.1
Reviewed-by on v1 5/7
#11 lore raw

Re: [PATCH 5/7] xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation

Quoted 4 lines · Xenia Ragiadakou

In macro __rb_parent(), add parentheses around the macro parameter to prevent against unintended expansions.

Quoted 20 lines
---
 xen/lib/rbtree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/lib/rbtree.c +1 -1
index 85a4f20313..eb418baabb 100644
@@ -46,7 +46,7 @@
 #define		RB_RED		0
 #define		RB_BLACK	1
 
-#define __rb_parent(pc)    ((struct rb_node *)(pc & ~3))
+#define __rb_parent(pc)    ((struct rb_node *)((pc) & ~3))
 
 #define __rb_color(pc)     ((pc) & 1)
 #define __rb_is_black(pc)  __rb_color(pc)
Signature · 1 line
2.34.1
#12 lore raw

PATCH 6/7 xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou

In macros MPIDR_LEVEL_SHIFT() and MPIDR_AFFINITY_LEVEL(), add parentheses around the macro parameters 'level' and 'mpidr', respectively, to prevent against unintended expansions.

---
 xen/arch/arm/include/asm/processor.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/arm/include/asm/processor.h +2 -2
index 55f56b33bc..1dd81d7d52 100644
@@ -127,10 +127,10 @@
 #define MPIDR_LEVEL_MASK        ((1 << MPIDR_LEVEL_BITS) - 1)
 
 #define MPIDR_LEVEL_SHIFT(level) \
-         (((1 << level) >> 1) << MPIDR_LEVEL_BITS_SHIFT)
+         (((1 << (level)) >> 1) << MPIDR_LEVEL_BITS_SHIFT)
 
 #define MPIDR_AFFINITY_LEVEL(mpidr, level) \
-         ((mpidr >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK)
+         (((mpidr) >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK)
 
 #define AFFINITY_MASK(level)    ~((_AC(0x1,UL) << MPIDR_LEVEL_SHIFT(level)) - 1)
 
Signature · 1 line
2.34.1
Reviewed-by on v1 6/7
#13 lore raw

Re: [PATCH 6/7] xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations

Quoted 5 lines · Xenia Ragiadakou

In macros MPIDR_LEVEL_SHIFT() and MPIDR_AFFINITY_LEVEL(), add parentheses around the macro parameters 'level' and 'mpidr', respectively, to prevent against unintended expansions.

Quoted 24 lines
---
 xen/arch/arm/include/asm/processor.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/arm/include/asm/processor.h +2 -2
index 55f56b33bc..1dd81d7d52 100644
@@ -127,10 +127,10 @@
 #define MPIDR_LEVEL_MASK        ((1 << MPIDR_LEVEL_BITS) - 1)
 
 #define MPIDR_LEVEL_SHIFT(level) \
-         (((1 << level) >> 1) << MPIDR_LEVEL_BITS_SHIFT)
+         (((1 << (level)) >> 1) << MPIDR_LEVEL_BITS_SHIFT)
 
 #define MPIDR_AFFINITY_LEVEL(mpidr, level) \
-         ((mpidr >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK)
+         (((mpidr) >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK)
 
 #define AFFINITY_MASK(level)    ~((_AC(0x1,UL) << MPIDR_LEVEL_SHIFT(level)) - 1)
 
Signature · 1 line
2.34.1
#14 lore raw

PATCH 7/7 xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

---
 xen/include/xen/device_tree.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/include/xen/device_tree.h +3 -3
index 430a1ef445..6e4391c126 100644
@@ -222,13 +222,13 @@ dt_find_interrupt_controller(const struct dt_device_match *matches);
 #define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
 
 #define dt_for_each_property_node(dn, pp)                   \
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
 
 #define dt_for_each_device_node(dt, dn)                     \
-    for ( dn = dt; dn != NULL; dn = dn->allnext )
+    for ( dn = dt; dn != NULL; dn = (dn)->allnext )
 
 #define dt_for_each_child_node(dt, dn)                      \
-    for ( dn = dt->child; dn != NULL; dn = dn->sibling )
+    for ( dn = (dt)->child; dn != NULL; dn = (dn)->sibling )
 
 /* Helper to read a big number; size is in cells (not bytes) */
 static inline u64 dt_read_number(const __be32 *cell, int size)
Signature · 1 line
2.34.1
Reviewed-by on v1 7/7
#15 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 5 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Quoted 28 lines
---
 xen/include/xen/device_tree.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/include/xen/device_tree.h +3 -3
index 430a1ef445..6e4391c126 100644
@@ -222,13 +222,13 @@ dt_find_interrupt_controller(const struct dt_device_match *matches);
 #define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
 
 #define dt_for_each_property_node(dn, pp)                   \
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
 
 #define dt_for_each_device_node(dt, dn)                     \
-    for ( dn = dt; dn != NULL; dn = dn->allnext )
+    for ( dn = dt; dn != NULL; dn = (dn)->allnext )
 
 #define dt_for_each_child_node(dt, dn)                      \
-    for ( dn = dt->child; dn != NULL; dn = dn->sibling )
+    for ( dn = (dt)->child; dn != NULL; dn = (dn)->sibling )
 
 /* Helper to read a big number; size is in cells (not bytes) */
 static inline u64 dt_read_number(const __be32 *cell, int size)
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

In reply to Xenia Ragiadakou
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Quoted 19 lines
xen/include/xen/device_tree.h +3 -3
@@ -222,13 +222,13 @@ dt_find_interrupt_controller(const struct dt_device_match *matches);
 #define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
 
 #define dt_for_each_property_node(dn, pp)                   \
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
 
 #define dt_for_each_device_node(dt, dn)                     \
-    for ( dn = dt; dn != NULL; dn = dn->allnext )
+    for ( dn = dt; dn != NULL; dn = (dn)->allnext )
 
 #define dt_for_each_child_node(dt, dn)                      \
-    for ( dn = dt->child; dn != NULL; dn = dn->sibling )
+    for ( dn = (dt)->child; dn != NULL; dn = (dn)->sibling )
 
 /* Helper to read a big number; size is in cells (not bytes) */
 static inline u64 dt_read_number(const __be32 *cell, int size)
#17 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Hi Jan,

Quoted 8 lines · Jan Beulich
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Yes, I agree with you but some older patches that I sent that were adding parentheses around the lhs of the assignments were not accepted and I thought that the rhs of the assignments as well these comparisons fall to the same category.

Personally, I would expect to see parentheses, also, around the macro parameters that are used as the lhs or the rhs of assignments, the operands of comparison or the arguments of a function.
Not only because they can prevent against unintentional bugs but because the parentheses help me to identify more easily the macro parameters when reading a macro definition. I totally understand that for other people parentheses may reduce readability.

Quoted 21 lines
Quoted 20 lines
xen/include/xen/device_tree.h +3 -3
@@ -222,13 +222,13 @@ dt_find_interrupt_controller(const struct dt_device_match *matches);
  #define DT_ROOT_NODE_SIZE_CELLS_DEFAULT 1
 
  #define dt_for_each_property_node(dn, pp)                   \
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
 
  #define dt_for_each_device_node(dt, dn)                     \
-    for ( dn = dt; dn != NULL; dn = dn->allnext )
+    for ( dn = dt; dn != NULL; dn = (dn)->allnext )
 
  #define dt_for_each_child_node(dt, dn)                      \
-    for ( dn = dt->child; dn != NULL; dn = dn->sibling )
+    for ( dn = (dt)->child; dn != NULL; dn = (dn)->sibling )
 
  /* Helper to read a big number; size is in cells (not bytes) */
  static inline u64 dt_read_number(const __be32 *cell, int size)
Signature · 1 line
Xenia
#18 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 15 lines · Xenia Ragiadakou
Quoted 8 lines · Jan Beulich
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Yes, I agree with you but some older patches that I sent that were adding parentheses around the lhs of the assignments were not accepted and I thought that the rhs of the assignments as well these comparisons fall to the same category.

Quoted 7 lines

Personally, I would expect to see parentheses, also, around the macro parameters that are used as the lhs or the rhs of assignments, the operands of comparison or the arguments of a function.
Not only because they can prevent against unintentional bugs but because the parentheses help me to identify more easily the macro parameters when reading a macro definition. I totally understand that for other people parentheses may reduce readability.

Afair Julien's comments were very specific to the lhs of assignments. So at the very least everything else ought to be parenthesized imo.

Jan+

#19 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 24 lines · Jan Beulich
Quoted 15 lines · Xenia Ragiadakou
Quoted 8 lines · Jan Beulich
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Yes, I agree with you but some older patches that I sent that were adding parentheses around the lhs of the assignments were not accepted and I thought that the rhs of the assignments as well these comparisons fall to the same category.

Quoted 7 lines

Personally, I would expect to see parentheses, also, around the macro parameters that are used as the lhs or the rhs of assignments, the operands of comparison or the arguments of a function.
Not only because they can prevent against unintentional bugs but because the parentheses help me to identify more easily the macro parameters when reading a macro definition. I totally understand that for other people parentheses may reduce readability.

Quoted 3 lines

Afair Julien's comments were very specific to the lhs of assignments. So at the very least everything else ought to be parenthesized imo.

So, IIUC, the only deviations from the MISRA C 2012 Rule 20.7 will be for macro parameters used as the lhs of assignments and function arguments?

This feels a bit of a hack to parenthesize the macro parameters that are used as the rhs of an assignment but not those used as the lhs.
From previous discussions on the topic, I understood that the parentheses are considered needed only when they eliminate operator precedence problems, while for the wrong parameter format bugs we can rely on the reviewers.

I think we need to decide if the rule will be applied as is and if not which will be the deviations along with their justification and add it to the document.

Signature · 1 line
Xenia
#20 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 25 lines · Xenia Ragiadakou
Quoted 24 lines · Jan Beulich
Quoted 22 lines · Xenia Ragiadakou
Quoted 8 lines · Jan Beulich
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Yes, I agree with you but some older patches that I sent that were adding parentheses around the lhs of the assignments were not accepted and I thought that the rhs of the assignments as well these comparisons fall to the same category.

Personally, I would expect to see parentheses, also, around the macro parameters that are used as the lhs or the rhs of assignments, the operands of comparison or the arguments of a function.
Not only because they can prevent against unintentional bugs but because the parentheses help me to identify more easily the macro parameters when reading a macro definition. I totally understand that for other people parentheses may reduce readability.

Quoted 6 lines
Quoted 3 lines

Afair Julien's comments were very specific to the lhs of assignments. So at the very least everything else ought to be parenthesized imo.

So, IIUC, the only deviations from the MISRA C 2012 Rule 20.7 will be for macro parameters used as the lhs of assignments and function arguments?

Afaic I don't consider that discussion settled.

Quoted 2 lines

This feels a bit of a hack to parenthesize the macro parameters that are used as the rhs of an assignment but not those used as the lhs.

lhs and rhs of assignments are quite different, and hence making such a distinction wouldn't look to be a "hack" to me. In fact I've always considered this part of the language somewhat strange: To me parenthesizing e.g. an identifier already makes it (visually) an rvalue. Leaving aside odd (and easy to spot as odd) uses at the call sizes, I don't think I can come up with a case where parentheses are really needed. Anything needing parenthesizing actually yields an rvalue afaics, thus causing a diagnostic anyway.

Quoted 8 lines

From previous discussions on the topic, I understood that the
parentheses are considered needed only when they eliminate operator precedence problems, while for the wrong parameter format bugs we can rely on the reviewers.

I think we need to decide if the rule will be applied as is and if not which will be the deviations along with their justification and add it to the document.

Yes. But this shouldn't hinder adjustments for all other, non- controversial cases.

Jan

#21 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

CC MISRA C working group

Short summary: we are discussing whether the following is sufficient to address MISRA C Rule 20.7, and also in general for safety:

#define dt_for_each_property_node(dn, pp) \
- for ( pp = dn->properties; pp != NULL; pp = pp->next )
+ for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )

as you can see "dn" has been parenthesizing because is used as a rhs, while "pp" has *not* because it is used as lhs.

More below.

Quoted 38 lines · Jan Beulich
Quoted 25 lines · Xenia Ragiadakou
Quoted 24 lines · Jan Beulich
Quoted 22 lines · Xenia Ragiadakou
Quoted 8 lines · Jan Beulich
Quoted 3 lines · Xenia Ragiadakou

In macros dt_for_each_property_node(), dt_for_each_device_node() and dt_for_each_child_node(), add parentheses around the macro parameters that have the arrow operator applied, to prevent against unintended expansions.

Why is this relevant only when -> is used? For comparisons and the rhs of assignments it's as relevant, ad even for the lhs of assignments I doubt it can be generally omitted.

Yes, I agree with you but some older patches that I sent that were adding parentheses around the lhs of the assignments were not accepted and I thought that the rhs of the assignments as well these comparisons fall to the same category.

Personally, I would expect to see parentheses, also, around the macro parameters that are used as the lhs or the rhs of assignments, the operands of comparison or the arguments of a function.
Not only because they can prevent against unintentional bugs but because the parentheses help me to identify more easily the macro parameters when reading a macro definition. I totally understand that for other people parentheses may reduce readability.

Quoted 6 lines
Quoted 3 lines

Afair Julien's comments were very specific to the lhs of assignments. So at the very least everything else ought to be parenthesized imo.

So, IIUC, the only deviations from the MISRA C 2012 Rule 20.7 will be for macro parameters used as the lhs of assignments and function arguments?

Afaic I don't consider that discussion settled.

Quoted 2 lines

This feels a bit of a hack to parenthesize the macro parameters that are used as the rhs of an assignment but not those used as the lhs.

Quoted 8 lines

lhs and rhs of assignments are quite different, and hence making such a distinction wouldn't look to be a "hack" to me. In fact I've always considered this part of the language somewhat strange: To me parenthesizing e.g. an identifier already makes it (visually) an rvalue. Leaving aside odd (and easy to spot as odd) uses at the call sizes, I don't think I can come up with a case where parentheses are really needed. Anything needing parenthesizing actually yields an rvalue afaics, thus causing a diagnostic anyway.

Although I can see where you are coming from, parenthesizing an identifier doesn't actually make it an rvalue. Also it is a lot simpler to understand, review, and apply a policy that says:

"all macro parameters are parenthesized"

compared to a policy that says:

"most macro paremeters are parenthesized, let's go into the details of which ones are parenthesized and which ones are not, including examples and corner cases"

For simplicity, I would go with the simplest version, the MISRA version.

I am assuming that the MISRA Rule 20.7 requires that "pp" is also parenthesized. Roberto, is that correct?

Quoted 11 lines
Quoted 8 lines

From previous discussions on the topic, I understood that the
parentheses are considered needed only when they eliminate operator precedence problems, while for the wrong parameter format bugs we can rely on the reviewers.

I think we need to decide if the rule will be applied as is and if not which will be the deviations along with their justification and add it to the document.

Yes. But this shouldn't hinder adjustments for all other, non- controversial cases.

It looks like we need a discussion and see where the majority of the team is on this issue. I prefer the original MISRA version for simplicity, but I also think it is OK if we make a small customization to it. In that case, we would add the extra explanation and details to docs/misra/rules.rst.

However, as we make the decision we also need to take into account that if we don't go with the vanilla MISRA rule, there is a price to pay: all the MISRA scanners, including cppcheck, Eclair, Coverity and others would probably still flag these issues as violations polluting the results and making the scanners less useful. We might have to mark each "deviation" with a one-line in-code comment on top, or we would have to disable automatic scanning for Rule 20.7 altogether. Either option is not great.

This is actually the main reason why I prefer the vanillay MISRA
version: even if the resulting style might not be as good the the custom version, we don't need to worry about reviewing this rule because we can easily get automatic scans for it.

But first, let's confirm whether this change:

#define dt_for_each_property_node(dn, pp) \
- for ( pp = dn->properties; pp != NULL; pp = pp->next )
+ for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )

is sufficient to make the violation go away in Eclair or cppcheck. I am assuming it is not sufficient, but let's confirm.

#22 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 10 lines · Stefano Stabellini

But first, let's confirm whether this change:

#define dt_for_each_property_node(dn, pp) \

(quoted diff) +1 -1
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )

is sufficient to make the violation go away in Eclair or cppcheck. I am assuming it is not sufficient, but let's confirm.

Well, even if for the lhs of assignments there was an exception, this still wouldn't be sufficient. The minimum needed is

#define dt_for_each_property_node(dn, pp)                   \
    for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )

Jan

#23 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 9 lines · Jan Beulich
Quoted 8 lines · Stefano Stabellini

But first, let's confirm whether this change:

  #define dt_for_each_property_node(dn, pp)                   \
(quoted diff) +1 -1
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
Quoted 9 lines
Quoted 2 lines

is sufficient to make the violation go away in Eclair or cppcheck. I am assuming it is not sufficient, but let's confirm.

Well, even if for the lhs of assignments there was an exception, this still wouldn't be sufficient. The minimum needed is

#define dt_for_each_property_node(dn, pp)                   \
     for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )

If pp is assumed to be a valid lvalue, then why it is needed to add parentheses here (pp) != NULL ?

For the violations to go away, parentheses should be placed around all macro parameters that represent expressions, that is

#define dt_for_each_property_node(dn, pp)                   \
      for ( (pp) = (dn)->properties; (pp) != NULL; (pp) = (pp)->next )
Signature · 1 line
Xenia
#24 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 16 lines · Xenia Ragiadakou
Quoted 15 lines · Jan Beulich
Quoted 10 lines · Stefano Stabellini

But first, let's confirm whether this change:

  #define dt_for_each_property_node(dn, pp)                   \
(quoted diff) +1 -1
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )

is sufficient to make the violation go away in Eclair or cppcheck. I am assuming it is not sufficient, but let's confirm.

Well, even if for the lhs of assignments there was an exception, this still wouldn't be sufficient. The minimum needed is

Quoted 6 lines
Quoted 3 lines
#define dt_for_each_property_node(dn, pp)                   \
     for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )

If pp is assumed to be a valid lvalue, then why it is needed to add parentheses here (pp) != NULL ?

Because in one expression is doesn't matter that in another expression the same identifier is used as the lhs of an assignment. Whether parentheses are needed should solely depend on the operators in use, not any further context.

Jan

#25 lore raw

Re: [PATCH 7/7] xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Xenia Ragiadakou
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Stefano Stabellini

But first, let's confirm whether this change:

  #define dt_for_each_property_node(dn, pp)                   \
(quoted diff) +1 -1
-    for ( pp = dn->properties; pp != NULL; pp = pp->next )
+    for ( pp = (dn)->properties; pp != NULL; pp = (pp)->next )
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

is sufficient to make the violation go away in Eclair or cppcheck. I am assuming it is not sufficient, but let's confirm.

Well, even if for the lhs of assignments there was an exception, this still wouldn't be sufficient. The minimum needed is

#define dt_for_each_property_node(dn, pp)                   \
     for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )

Thank you for noticing this

Quoted 7 lines
Quoted 2 lines

If pp is assumed to be a valid lvalue, then why it is needed to add parentheses here (pp) != NULL ?

Because in one expression is doesn't matter that in another expression the same identifier is used as the lhs of an assignment. Whether parentheses are needed should solely depend on the operators in use, not any further context.

This is the problem with going with a more sophisticated version of the
rule: it is not immediately obvious any longer. I have to read this explanation three times to appreciate what it means, I don't think a new contributor would really have any chances of getting this right, especially as cppcheck/Eclair wouldn't be able to help them either.

#26 lore raw
In reply to Xenia Ragiadakou

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3 and 5 in the next couple of days.

Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

Quoted 21 lines · Xenia Ragiadakou

Xenia Ragiadakou (7):
xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations xen/elf: Fix MISRA C 2012 Rule 20.7 violations xen/vgic: Fix MISRA C 2012 Rule 20.7 violation xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

 xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
 xen/arch/arm/include/asm/new_vgic.h   |  2 +-
 xen/arch/arm/include/asm/processor.h  |  4 ++--
 xen/include/xen/device_tree.h         |  6 +++---
 xen/include/xen/elfstructs.h          |  4 ++--
 xen/lib/rbtree.c                      |  2 +-
 xen/xsm/flask/ss/sidtab.c             |  8 ++++----
 7 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
#27 lore raw
Quoted 6 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3 and 5 in the next couple of days.

Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule. There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses. But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator). While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 23 lines · Xenia Ragiadakou
Quoted 21 lines

Xenia Ragiadakou (7):
xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations xen/elf: Fix MISRA C 2012 Rule 20.7 violations xen/vgic: Fix MISRA C 2012 Rule 20.7 violation xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

  xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
  xen/arch/arm/include/asm/new_vgic.h   |  2 +-
  xen/arch/arm/include/asm/processor.h  |  4 ++--
  xen/include/xen/device_tree.h         |  6 +++---
  xen/include/xen/elfstructs.h          |  4 ++--
  xen/lib/rbtree.c                      |  2 +-
  xen/xsm/flask/ss/sidtab.c             |  8 ++++----
  7 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
Signature · 1 line
Xenia
#28 lore raw

Hi Xenia,

Quoted 17 lines · Xenia Ragiadakou
Quoted 4 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3 and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule. There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses. But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Quoted 6 lines

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator). While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Regards
Bertrand

Quoted 26 lines
Quoted 23 lines · Xenia Ragiadakou
Quoted 21 lines

Xenia Ragiadakou (7):
xen/arm: gic_v3_its: Fix MISRA C 2012 Rule 20.7 violations xsm/flask: sidtab: Fix MISRA C 2012 Rule 20.7 violations xen/elf: Fix MISRA C 2012 Rule 20.7 violations xen/vgic: Fix MISRA C 2012 Rule 20.7 violation xen/rbtree: Fix MISRA C 2012 Rule 20.7 violation xen/arm: processor: Fix MISRA C 2012 Rule 20.7 violations xen/device_tree: Fix MISRA C 2012 Rule 20.7 violations

 xen/arch/arm/include/asm/gic_v3_its.h | 10 +++++-----
 xen/arch/arm/include/asm/new_vgic.h   |  2 +-
 xen/arch/arm/include/asm/processor.h  |  4 ++--
 xen/include/xen/device_tree.h         |  6 +++---
 xen/include/xen/elfstructs.h          |  4 ++--
 xen/lib/rbtree.c                      |  2 +-
 xen/xsm/flask/ss/sidtab.c             |  8 ++++----
 7 files changed, 18 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
Signature · 1 line
Xenia
#29 lore raw
Quoted 22 lines · Bertrand Marquis

Hi Xenia,

Quoted 20 lines · Xenia Ragiadakou
Quoted 4 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3 and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule. There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses. But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator). While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

Quoted 8 lines
Quoted 3 lines

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

#30 lore raw
Quoted 28 lines · Stefano Stabellini
Quoted 27 lines · Bertrand Marquis

Hi Xenia,

Quoted 23 lines · Xenia Ragiadakou
Quoted 4 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3 and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule. There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses. But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator). While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 6 lines
Quoted 3 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function. Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

Signature · 1 line
Xenia
#31 lore raw

+Roberto

I think we need Roberto's advice on Rule 20.7. (Full thread below.)

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments
- macro parameters used as macro arguments
- macro parameter used as array index
- macro parameter used as lhs in assignment

Some of these cases are interesting because they should function correctly even without parenthesis, hence the discussion. In particular parenthesis don't seem necessary at least for the function argument case.

Regardless of the MISRA C interpretation, Xenia noticed that Eclair reports violations on these cases (cppcheck does not, I don't know other checkers).

Quoted 75 lines · Xenia Ragiadakou
Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Bertrand Marquis

Hi Xenia,

Quoted 51 lines · Xenia Ragiadakou
Quoted 5 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3
and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule.
There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses.
But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator).
While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 8 lines
Quoted 5 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function.
Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

#32 lore raw
Quoted 3 lines · Stefano Stabellini

+Roberto

I think we need Roberto's advice on Rule 20.7. (Full thread below.)

Hi there, sorry for the delay: I missed this message. Please see below, where I took the freedom of rearranging the cases.

Quoted 6 lines

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments
[...]
- macro parameter used as lhs in assignment

You can obtain different semantics depending on whether parentheses are or are not used (in the macro call and/or macro expansion depending on the case):

#include <stdio.h>

void g(int v) {
printf("%d\n", v);
}

#define m1(x, y, ...) g(y)

void f1(int x, int y, ...) {
g(y);
}

#define p 0, 1

void test1() {
m1(p, 2);
f1(p, 2);
}

#define m4(x) x = 4

void f4(int &x) {
x = 4;
}

void test4() {
int y;
int z;
z = 3;
m4(y = z);
printf("%d\n", z);
z = 3;
f4(y = z);
printf("%d\n", z);
}

int main() {
test1();
test4();
}

Quoted 1 line

- macro parameters used as macro arguments

Please note that Rule 20.7 depends on the final expansion:
so whether parentheses are or are not used in a certain macro body is irrelevant, the point being that, at the end of all expansions, expressions resulting from the expansion of macro parameters are enclosed in parentheses.

Quoted 1 line

- macro parameter used as array index

This is safe today, but my understanding is that in C++23 the [] operator will accept more than one expression. A similar change might (who knows?) be considered for C26 or even offered before (intentionally or by mistake) by some C compiler.

Quoted 4 lines

Some of these cases are interesting because they should function correctly even without parenthesis, hence the discussion. In particular parenthesis don't seem necessary at least for the function argument case.

This is not the right spirit for MISRA compliance: why would you want splitting hairs when inserting a pair of parentheses is so easy? C and C++ are very complex languages, and the MISRA coding standards are the result of a (very difficult!) compromise between simplicity and effectiveness: rules that are exactly targeted to all and only all the problematic instances would be very difficult to express and to remember. So, yes: in many cases you might spend time to demonstrate that a particular (real) MISRA violation does not imply the existence of a real issue, but this time is not well spent. Critical code must be boring and obviously right, in the sense that whomever is reading the code should not be distracted by thoughts like "there are no parentheses here: am I sure nothing bad can happen?"

Quoted 3 lines

Regardless of the MISRA C interpretation, Xenia noticed that Eclair reports violations on these cases (cppcheck does not, I don't know other checkers).

I am not aware of any false positives (or flse negatives) for the current version of ECLAIR on Rule 20.7. Nonetheless, ECLAIR can be configured to selectively deviate on each of the cases mentioned above by means of checker configuration. However, as I said, it only makes sense deviating the rule in the cases where you are not allowed to add the parentheses (e.g., because both the macro definition and the macro invocations are in legacy code you are not allowed to touch).

In contrast, cppcheck is no more than a toy when MISRA compliance is concerned. It claims to support 153 out of 175 MISRA C:2012 guidelines. For 103 of those 153 it has a significant number of false negatives (FN) and false positives (FP). I recently participated to an evaluation of cppcheck 2.8 and here is a summary I can disclose:

Rule 1.3               FP
Rule 2.1               FN
Rule 2.2               FN+FP
Rule 2.4               FN+FP
Rule 2.5               FP
Rule 2.7               FP
Rule 3.2               FN
Rule 4.2               FN
Rule 5.1               FP
Rule 5.3               FN
Rule 5.6               FN+FP
Rule 5.7               FN+FP
Rule 5.8               FN+FP
Rule 5.9               FN+FP
Rule 6.1               FN+FP
Rule 7.1               FN
Rule 7.3               FN
Rule 7.4               FN+FP
Rule 8.1               FN
Rule 8.2               FN+FP
Rule 8.3               FN
Rule 8.4               FP
Rule 8.5               FN+FP
Rule 8.6               FP
Rule 8.7               FN
Rule 8.8               FN
Rule 8.9               FN
Rule 8.10              FN
Rule 8.13              FN
Rule 8.14              FP
Rule 9.1               FN+FP
Rule 9.3               FN
Rule 10.1              FN
Rule 10.2              FN
Rule 10.3              FN+FP
Rule 10.4              FP
Rule 10.5              FN+FP
Rule 10.6              FP
Rule 10.7              FN+FP
Rule 10.8              FP
Rule 11.1              FN+FP
Rule 11.2              FN
Rule 11.3              FN+FP
Rule 11.4              FP
Rule 11.5              FP
Rule 11.7              FN
Rule 11.8              FN+FP
Rule 11.9              FN
Rule 12.1              FN
Rule 12.2              FP
Rule 12.3              FP
Rule 13.1              FN
Rule 13.2              FN
Rule 13.4              FP
Rule 13.5              FN
Rule 13.6              FP
Rule 14.2              FN
Rule 14.3              FN
Rule 15.5              FN+FP
Rule 15.6              FN+FP
Rule 16.1              FN
Rule 16.3              FN
Rule 16.6              FP
Rule 16.7              FP
Rule 17.1              FP
Rule 17.2              FN+FP
Rule 17.4              FN
Rule 17.5              FN
Rule 17.7              FP
Rule 18.1              FN
Rule 18.3              FN
Rule 18.4              FP
Rule 19.1              FN
Rule 19.2              FP
Rule 20.2              FN
Rule 20.4              FP
Rule 20.5              FN
Rule 20.7              FP
Rule 20.9              FN
Rule 20.10             FP
Rule 20.12             FP
Rule 21.1              FN+FP
Rule 21.2              FN
Rule 21.3              FP
Rule 21.6              FP
Rule 21.8              FN+FP
Rule 21.12             FN
Rule 21.13             FP
Rule 21.14             FN
Rule 21.15             FN
Rule 21.16             FN+FP
Rule 21.17             FN
Rule 21.18             FN
Rule 21.19             FN
Rule 21.20             FN
Rule 22.1              FP
Rule 22.2              FN+FP
Rule 22.5              FN
Rule 22.6              FN
Rule 22.7              FN
Rule 22.8              FN+FP
Rule 22.9              FN+FP
Rule 22.10             FP

These results are clearly relative to the testsuite employed:
while very large, it cannot of course reach 100% coverage. For instance, if you noticed Rule 20.7 reports given by ECLAIR and not by cppcheck, then maybe line

Rule 20.7 FP

should be

Rule 20.7 FN+FP

If you can let me have an indication of the code that ECLAIR is flagging for Rule 20.7 and cppcheck does not flag, I will be happy to double-check.

While the sheer amount of false negatives of cppcheck 2.8 precludes its use for safety-related development, the many false positives are also a big problem: people will waste time investigating them and, unless they have been properly trained on the MISRA guidelines so as to be able to recognize false positives, they might be tempted to change the code when there is no reason to do so. When the latter thing happens, code quality will typically decrease.

Kind regards,

Roberto

Quoted 77 lines · Xenia Ragiadakou
Quoted 75 lines · Stefano Stabellini
Quoted 56 lines · Bertrand Marquis
Quoted 55 lines

Hi Xenia,

Quoted 51 lines · Xenia Ragiadakou
Quoted 5 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3
and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule.
There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given, my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses.
But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator).
While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 8 lines
Quoted 5 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function.
Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

#33 lore raw
Quoted 27 lines · Roberto Bagnara
Quoted 6 lines · Stefano Stabellini

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments
[...]
- macro parameter used as lhs in assignment

You can obtain different semantics depending on whether parentheses are or are not used (in the macro call and/or macro expansion depending on the case):

#include <stdio.h>

void g(int v) {
   printf("%d\n", v);
}

#define m1(x, y, ...) g(y)

void f1(int x, int y, ...) {
   g(y);
}

#define p 0, 1

Quoted 8 lines
void test1() {
   m1(p, 2);
   f1(p, 2);
}

#define m4(x) x = 4

void f4(int &x) {

Let's focus on C here.

Quoted 36 lines
   x = 4;
}
void test4() {
   int y;
   int z;
   z = 3;
   m4(y = z);
   printf("%d\n", z);
   z = 3;
   f4(y = z);
   printf("%d\n", z);
}
int main() {
   test1();
   test4();
}
Quoted 1 line

- macro parameters used as macro arguments

Please note that Rule 20.7 depends on the final expansion:
so whether parentheses are or are not used in a certain macro body is irrelevant, the point being that, at the end of all expansions, expressions resulting from the expansion of macro parameters are enclosed in parentheses.

Quoted 1 line

- macro parameter used as array index

This is safe today, but my understanding is that in C++23 the [] operator will accept more than one expression. A similar change might (who knows?) be considered for C26 or even offered before (intentionally or by mistake) by some C compiler.

Quoted 7 lines
Quoted 4 lines

Some of these cases are interesting because they should function correctly even without parenthesis, hence the discussion. In particular parenthesis don't seem necessary at least for the function argument case.

This is not the right spirit for MISRA compliance: why would you want splitting hairs when inserting a pair of parentheses is so easy?

I think I've said so before - too many parentheses harm readability.

Quoted 10 lines

C and C++ are very complex languages, and the MISRA coding standards are the result of a (very difficult!) compromise between simplicity and effectiveness: rules that are exactly targeted to all and only all the problematic instances would be very difficult to express and to remember. So, yes: in many cases you might spend time to demonstrate that a particular (real) MISRA violation does not imply the existence of a real issue, but this time is not well spent. Critical code must be boring and obviously right, in the sense that whomever is reading the code should not be distracted by thoughts like "there are no parentheses here: am I sure nothing bad can happen?"

I also did indicate before that "(x) = ..." visually (but not syntactically) can raise the question of whether the left side actually is an lvalue.

Jan

#34 lore raw
In reply to Roberto Bagnara

Hi Roberto,

Quoted 33 lines · Roberto Bagnara
Quoted 3 lines · Stefano Stabellini

+Roberto

I think we need Roberto's advice on Rule 20.7. (Full thread below.)

Hi there, sorry for the delay: I missed this message. Please see below, where I took the freedom of rearranging the cases.

Quoted 6 lines

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments
[...]
- macro parameter used as lhs in assignment

You can obtain different semantics depending on whether parentheses are or are not used (in the macro call and/or macro expansion depending on the case):

#include <stdio.h>

void g(int v) {
  printf("%d\n", v);
}

#define m1(x, y, ...) g(y)

void f1(int x, int y, ...) {
  g(y);
}

Quoted 7 lines

#define p 0, 1

void test1() {
  m1(p, 2);
  f1(p, 2);
}

In the example above something bothers me. Let me explain.

Running the above example gives:
2
1

The results differ mainly because m1() is substituted before p. Thus, adding parentheses around the macro parameter 'y' of m1() i.e #define m1(x, y, ...) g((y))
has no impact.

If the example is changed into the following:

#include <stdio.h>

void g(int v) {
printf("%d\n", v);
}

#define m1(y, ...) g(y)

void f1(int y, ...) {
g(y);
}

#define p 0, 1

void test1() {
    m1(p, 2);
    f1(p, 2);
}

if no parentheses are added around 'y' in the definition of m1(), the compiler complains with "too many arguments to function g". If parentheses are added around 'y', the compiler does not complain but the behavior will still differ and the result will be 1
0

This happens because in the case of m1(), p is interpreted as an expression (due to the parentheses added there) and the comma is evaluated as a comma operator, while in f1(), p is interpreted as a list of expressions and the comma is evaluated as a comma separator.

Hence, in my opinion, parentheses should not be added around macro parameters used as function arguments because they can hide a bug due to missing parentheses around the entire macro definition. Since macro 'p' is supposed to represent an expression, and the semantics of the comma token are those of a comma operator and not a comma separator, then parentheses need to be placed around the entire macro definition i.e #define p (0, 1)

AFAIK, there is no requirement in MISRA C guidelines to add parentheses around the entire macro definition when it is used as an expression and this is something I cannot understand. Unless I got it all wrong I guess ...

Quoted 31 lines

#define m4(x) x = 4

void f4(int &x) {
  x = 4;
}

void test4() {
  int y;
  int z;
  z = 3;
  m4(y = z);
  printf("%d\n", z);
  z = 3;
  f4(y = z);
  printf("%d\n", z);
}

int main() {
  test1();
  test4();
}

Quoted 1 line

- macro parameters used as macro arguments

Please note that Rule 20.7 depends on the final expansion:
so whether parentheses are or are not used in a certain macro body is irrelevant, the point being that, at the end of all expansions, expressions resulting from the expansion of macro parameters are enclosed in parentheses.

Quoted 8 lines
Quoted 1 line

- macro parameter used as array index

This is safe today, but my understanding is that in C++23 the [] operator will accept more than one expression. A similar change might (who knows?) be considered for C26 or even offered before (intentionally or by mistake) by some C compiler.

Can a deviation being added in the basis of C99 standard since according to the standard, E1[E2] is identical to (*((E1)+(E2))), and therefore, macro parameters used as subscript expressions are implicitly parenthesized and can be exempted from the rule.

Quoted 148 lines
Quoted 4 lines

Some of these cases are interesting because they should function correctly even without parenthesis, hence the discussion. In particular parenthesis don't seem necessary at least for the function argument case.

This is not the right spirit for MISRA compliance: why would you want splitting hairs when inserting a pair of parentheses is so easy? C and C++ are very complex languages, and the MISRA coding standards are the result of a (very difficult!) compromise between simplicity and effectiveness: rules that are exactly targeted to all and only all the problematic instances would be very difficult to express and to remember. So, yes: in many cases you might spend time to demonstrate that a particular (real) MISRA violation does not imply the existence of a real issue, but this time is not well spent.  Critical code must be boring and obviously right, in the sense that whomever is reading the code should not be distracted by thoughts like "there are no parentheses here: am I sure nothing bad can happen?"

Quoted 3 lines

Regardless of the MISRA C interpretation, Xenia noticed that Eclair reports violations on these cases (cppcheck does not, I don't know other checkers).

I am not aware of any false positives (or flse negatives) for the current version of ECLAIR on Rule 20.7.  Nonetheless, ECLAIR can be configured to selectively deviate on each of the cases mentioned above by means of checker configuration.  However, as I said, it only makes sense deviating the rule in the cases where you are not allowed to add the parentheses (e.g., because both the macro definition and the macro invocations are in legacy code you are not allowed to touch).

In contrast, cppcheck is no more than a toy when MISRA compliance is concerned.  It claims to support 153 out of 175 MISRA C:2012 guidelines. For 103 of those 153 it has a significant number of false negatives (FN) and false positives (FP).  I recently participated to an evaluation of cppcheck 2.8 and here is a summary I can disclose:

Rule 1.3               FP
Rule 2.1               FN
Rule 2.2               FN+FP
Rule 2.4               FN+FP
Rule 2.5               FP
Rule 2.7               FP
Rule 3.2               FN
Rule 4.2               FN
Rule 5.1               FP
Rule 5.3               FN
Rule 5.6               FN+FP
Rule 5.7               FN+FP
Rule 5.8               FN+FP
Rule 5.9               FN+FP
Rule 6.1               FN+FP
Rule 7.1               FN
Rule 7.3               FN
Rule 7.4               FN+FP
Rule 8.1               FN
Rule 8.2               FN+FP
Rule 8.3               FN
Rule 8.4               FP
Rule 8.5               FN+FP
Rule 8.6               FP
Rule 8.7               FN
Rule 8.8               FN
Rule 8.9               FN
Rule 8.10              FN
Rule 8.13              FN
Rule 8.14              FP
Rule 9.1               FN+FP
Rule 9.3               FN
Rule 10.1              FN
Rule 10.2              FN
Rule 10.3              FN+FP
Rule 10.4              FP
Rule 10.5              FN+FP
Rule 10.6              FP
Rule 10.7              FN+FP
Rule 10.8              FP
Rule 11.1              FN+FP
Rule 11.2              FN
Rule 11.3              FN+FP
Rule 11.4              FP
Rule 11.5              FP
Rule 11.7              FN
Rule 11.8              FN+FP
Rule 11.9              FN
Rule 12.1              FN
Rule 12.2              FP
Rule 12.3              FP
Rule 13.1              FN
Rule 13.2              FN
Rule 13.4              FP
Rule 13.5              FN
Rule 13.6              FP
Rule 14.2              FN
Rule 14.3              FN
Rule 15.5              FN+FP
Rule 15.6              FN+FP
Rule 16.1              FN
Rule 16.3              FN
Rule 16.6              FP
Rule 16.7              FP
Rule 17.1              FP
Rule 17.2              FN+FP
Rule 17.4              FN
Rule 17.5              FN
Rule 17.7              FP
Rule 18.1              FN
Rule 18.3              FN
Rule 18.4              FP
Rule 19.1              FN
Rule 19.2              FP
Rule 20.2              FN
Rule 20.4              FP
Rule 20.5              FN
Rule 20.7              FP
Rule 20.9              FN
Rule 20.10             FP
Rule 20.12             FP
Rule 21.1              FN+FP
Rule 21.2              FN
Rule 21.3              FP
Rule 21.6              FP
Rule 21.8              FN+FP
Rule 21.12             FN
Rule 21.13             FP
Rule 21.14             FN
Rule 21.15             FN
Rule 21.16             FN+FP
Rule 21.17             FN
Rule 21.18             FN
Rule 21.19             FN
Rule 21.20             FN
Rule 22.1              FP
Rule 22.2              FN+FP
Rule 22.5              FN
Rule 22.6              FN
Rule 22.7              FN
Rule 22.8              FN+FP
Rule 22.9              FN+FP
Rule 22.10             FP

These results are clearly relative to the testsuite employed:
while very large, it cannot of course reach 100% coverage. For instance, if you noticed Rule 20.7 reports given by ECLAIR and not by cppcheck, then maybe line

Rule 20.7              FP

Quoted 7 lines

should be

Rule 20.7              FN+FP

If you can let me have an indication of the code that ECLAIR is flagging for Rule 20.7 and cppcheck does not flag, I will be happy to double-check.

ECLAIR flags as violations of Rule 20.7 the cases where unparenthesized macro parameters are used as (1) function arguments or (2) array indexes, while cppcheck does not.

For instance:
(1) in xen/arch/arm/include/asm/atomic.h

#define read_atomic(p) ({                                               \
    union { typeof(*(p)) val; char c[0]; } x_;                          \
    read_atomic_size(p, x_.c, sizeof(*(p)));                            \
    x_.val;                                                             \

})
ECLAIR flags as violations missing parentheses around 'p', when used as an argument of read_atomic_size().

(2) in xen/arch/arm/arm64/cpufeature.c

#define SANITIZE_REG(field, num, reg)  \
	sanitize_reg(&system_cpuinfo.field.bits[num], new->field.bits[num], \
				 #reg, ftr_##reg)

ECLAIR flags as violations missing parentheses around 'num'.

Quoted 91 lines

While the sheer amount of false negatives of cppcheck 2.8 precludes its use for safety-related development, the many false positives are also a big problem: people will waste time investigating them and, unless they have been properly trained on the MISRA guidelines so as to be able to recognize false positives, they might be tempted to change the code when there is no reason to do so.  When the latter thing happens, code quality will typically decrease.

Kind regards,

   Roberto

Quoted 77 lines · Xenia Ragiadakou
Quoted 75 lines · Stefano Stabellini
Quoted 56 lines · Bertrand Marquis
Quoted 55 lines

Hi Xenia,

Quoted 51 lines · Xenia Ragiadakou
Quoted 5 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3
and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule.
There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given,  my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses.
But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator).
While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 8 lines
Quoted 5 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function.
Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

Signature · 1 line
Xenia
#35 lore raw

Hi Xenia. Please see below.

Quoted 70 lines · Xenia Ragiadakou
Quoted 24 lines · Roberto Bagnara
Quoted 4 lines

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments

 > [...]
 > - macro parameter used as lhs in assignment

You can obtain different semantics depending on whether parentheses are or are not used (in the macro call and/or macro expansion depending on the case):

#include <stdio.h>

void g(int v) {
   printf("%d\n", v);
}

#define m1(x, y, ...) g(y)

void f1(int x, int y, ...) {
   g(y);
}

Quoted 7 lines

#define p 0, 1

void test1() {
   m1(p, 2);
   f1(p, 2);
}

In the example above something bothers me. Let me explain.

Running the above example gives:
2
1

The results differ mainly because m1() is substituted before p. Thus, adding parentheses around the macro parameter 'y' of m1() i.e #define m1(x, y, ...) g((y))
has no impact.

If the example is changed into the following:

#include <stdio.h>

void g(int v) {
   printf("%d\n", v);
}

#define m1(y, ...) g(y)

void f1(int y, ...) {
   g(y);
}

#define p 0, 1

void test1() {
    m1(p, 2);
    f1(p, 2);
}

if no parentheses are added around 'y' in the definition of m1(), the compiler complains with "too many arguments to function g". If parentheses are added around 'y', the compiler does not complain but the behavior will still differ and the result will be 1
0

Quoted 5 lines

This happens because in the case of m1(), p is interpreted as an expression (due to the parentheses added there) and the comma is evaluated as a comma operator, while in f1(), p is interpreted as a list of expressions and the comma is evaluated as a comma separator.

Hence, in my opinion, parentheses should not be added around macro parameters used as function arguments because they can hide a bug due to missing parentheses around the entire macro definition. Since macro 'p' is supposed to represent an expression, and the semantics of the comma token are those of a comma operator and not a comma separator, then parentheses need to be placed around the entire macro definition i.e #define p (0, 1)

Your analysis is correct: the example was meant only to show that the use of a macro or a function with the same actual parameters and apparently equivalent bodies can make a difference and that the addition of parentheses (around the body of p, as you suggest, or around the occurrence of p in the call to f1()) can avoid this problem. All this, however, is outside the scope of Rule 20.7, so the example may have been confusing: sorry about that.

Quoted 2 lines

AFAIK, there is no requirement in MISRA C guidelines to add parentheses around the entire macro definition when it is used as an expression and this is something I cannot understand. Unless I got it all wrong I guess ...

Yes, this is known and it is has also been brought to the attention of the MISRA C working group.

Quoted 2 lines

Can a deviation being added in the basis of C99 standard since according to the standard, E1[E2] is identical to (*((E1)+(E2))), and therefore, macro parameters used as subscript expressions are implicitly parenthesized and can be exempted from the rule.

Sure, you can always deviate any non-mandatory guideline: just be ware of the fact that complying is often cheaper than deviating.

Quoted 15 lines
Quoted 5 lines

For instance, if you noticed Rule 20.7 reports given by ECLAIR and not by cppcheck, then maybe line

Rule 20.7              FP

Quoted 7 lines

should be

Rule 20.7              FN+FP

If you can let me have an indication of the code that ECLAIR is flagging for Rule 20.7 and cppcheck does not flag, I will be happy to double-check.

ECLAIR flags as violations of Rule 20.7 the cases where unparenthesized macro parameters are used as (1) function arguments or (2) array indexes, while cppcheck does not.

Quoted 8 lines

For instance:
(1) in xen/arch/arm/include/asm/atomic.h
#define read_atomic(p) ({                                               \
    union { typeof(*(p)) val; char c[0]; } x_;                          \
    read_atomic_size(p, x_.c, sizeof(*(p)));                            \
    x_.val;                                                             \
})
ECLAIR flags as violations missing parentheses around 'p', when used as an argument of read_atomic_size().

ECLAIR is right in reporting these violations of Rule 20.7; these are false negatives of cppcheck.

Quoted 5 lines

(2) in xen/arch/arm/arm64/cpufeature.c
#define SANITIZE_REG(field, num, reg)  \
    sanitize_reg(&system_cpuinfo.field.bits[num], new->field.bits[num], \
                 #reg, ftr_##reg)
ECLAIR flags as violations missing parentheses around 'num'.

Same as above.

I am probably repeating myself, but the MISRA guidelines are the result of carefully-chosen compromises between the simplicity of the guideline and its ability to protect against the targeted bad thing. As Rule 20.7 is required, any violation will have to be deviated by projects that have MISRA-compliance among their objectives.
Kind regards,

  Roberto

Quoted 78 lines
Quoted 77 lines
Quoted 77 lines · Xenia Ragiadakou
Quoted 75 lines · Stefano Stabellini
Quoted 56 lines · Bertrand Marquis
Quoted 55 lines

Hi Xenia,

Quoted 51 lines · Xenia Ragiadakou
Quoted 5 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3
and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule.
There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given,  my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses.
But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator).
While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 8 lines
Quoted 5 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function.
Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

#36 lore raw

Hi Roberto,

Quoted 118 lines · Roberto Bagnara
Quoted 70 lines · Xenia Ragiadakou
Quoted 24 lines · Roberto Bagnara
Quoted 4 lines

The question is on the interpretation of Rule 20.7. Are parenthesis required by Rule 20.7 in the following cases:

- macro parameters used as function arguments

 > [...]
 > - macro parameter used as lhs in assignment

You can obtain different semantics depending on whether parentheses are or are not used (in the macro call and/or macro expansion depending on the case):

#include <stdio.h>

void g(int v) {
   printf("%d\n", v);
}

#define m1(x, y, ...) g(y)

void f1(int x, int y, ...) {
   g(y);
}

Quoted 7 lines

#define p 0, 1

void test1() {
   m1(p, 2);
   f1(p, 2);
}

In the example above something bothers me. Let me explain.

Running the above example gives:
2
1

The results differ mainly because m1() is substituted before p. Thus, adding parentheses around the macro parameter 'y' of m1() i.e #define m1(x, y, ...) g((y))
has no impact.

If the example is changed into the following:

#include <stdio.h>

void g(int v) {
    printf("%d\n", v);
}

#define m1(y, ...) g(y)

void f1(int y, ...) {
    g(y);
}

#define p 0, 1

void test1() {
     m1(p, 2);
     f1(p, 2);
}

if no parentheses are added around 'y' in the definition of m1(), the compiler complains with "too many arguments to function g". If parentheses are added around 'y', the compiler does not complain but the behavior will still differ and the result will be 1
0

Quoted 5 lines

This happens because in the case of m1(), p is interpreted as an expression (due to the parentheses added there) and the comma is evaluated as a comma operator, while in f1(), p is interpreted as a list of expressions and the comma is evaluated as a comma separator.

Hence, in my opinion, parentheses should not be added around macro parameters used as function arguments because they can hide a bug due to missing parentheses around the entire macro definition. Since macro 'p' is supposed to represent an expression, and the semantics of the comma token are those of a comma operator and not a comma separator, then parentheses need to be placed around the entire macro definition i.e #define p (0, 1)

Your analysis is correct: the example was meant only to show that the use of a macro or a function with the same actual parameters and apparently equivalent bodies can make a difference and that the addition of parentheses (around the body of p, as you suggest, or around the occurrence of p in the call to f1()) can avoid this problem. All this, however, is outside the scope of Rule 20.7, so the example may have been confusing: sorry about that.

Quoted 2 lines

AFAIK, there is no requirement in MISRA C guidelines to add parentheses around the entire macro definition when it is used as an expression and this is something I cannot understand. Unless I got it all wrong I guess ...

Yes, this is known and it is has also been brought to the attention of the MISRA C working group.

Quoted 2 lines

Can a deviation being added in the basis of C99 standard since according to the standard, E1[E2] is identical to (*((E1)+(E2))), and therefore, macro parameters used as subscript expressions are implicitly parenthesized and can be exempted from the rule.

Sure, you can always deviate any non-mandatory guideline: just be ware of the fact that complying is often cheaper than deviating.

Quoted 21 lines
Quoted 5 lines

For instance, if you noticed Rule 20.7 reports given by ECLAIR and not by cppcheck, then maybe line

Rule 20.7              FP

Quoted 7 lines

should be

Rule 20.7              FN+FP

If you can let me have an indication of the code that ECLAIR is flagging for Rule 20.7 and cppcheck does not flag, I will be happy to double-check.

ECLAIR flags as violations of Rule 20.7 the cases where unparenthesized macro parameters are used as (1) function arguments or (2) array indexes, while cppcheck does not.

For instance:
(1) in xen/arch/arm/include/asm/atomic.h
#define read_atomic(p) ({                                               \
     union { typeof(*(p)) val; char c[0]; } x_;                          \
     read_atomic_size(p, x_.c, sizeof(*(p)));                            \

Quoted 7 lines
Quoted 3 lines

x_.val;                                                             \
})
ECLAIR flags as violations missing parentheses around 'p', when used as an argument of read_atomic_size().

ECLAIR is right in reporting these violations of Rule 20.7; these are false negatives of cppcheck.

AFAIU, the rationale of Rule 20.7 is to ensure that the precedence of the expression, produced after the macro parameter expansion, will be higher (a parenthesized expression is a primary expression and has the highest precedence) than the precedence of any operator performed on that expression after the substitution. These two examples refer to cases where either no operator is applied to the expression or the applied operator precedence is the lowest possible (comma operator) and for this reason the rationale of the guideline may be considered insufficient to justify the need of parentheses. I guess, that ECLAIR flags the above as violations because there is no formal exemption in the body of the rule.
Cppcheck intentionally considers those cases compliant but unfortunately there is no justification in the commit messages of the respective changes.

Quoted 6 lines
Quoted 5 lines

(2) in xen/arch/arm/arm64/cpufeature.c
#define SANITIZE_REG(field, num, reg)  \
     sanitize_reg(&system_cpuinfo.field.bits[num], new->field.bits[num], \
                  #reg, ftr_##reg)
ECLAIR flags as violations missing parentheses around 'num'.

Quoted 7 lines

Same as above.

I am probably repeating myself, but the MISRA guidelines are the result of carefully-chosen compromises between the simplicity of the guideline and its ability to protect against the targeted bad thing.As Rule 20.7 is required, any violation will have to be deviated by projects that have MISRA-compliance among their objectives.

There are two things that have come to my attention and may cause confusion around Rule 20.7. They may have also been brought to the attention of the MISRA C working group.

1) Rule 12.1 (Advisory), which suggests the use of parentheses to make operator precedence explicit, does not require the operands of a comma operator/separator to be parenthesized because it recognizes that overuse of parentheses can clutter the code. Since both Rules 20.7 and 12.1 aim to address basically the same issue, why they are not aligned?

2) Rule 20.7 maps to the CERT-C Rule PRE01 which has a formal exemption (PRE01-C-EX1) for the above case. Maybe this sounds irrelevant but it struck me as odd.

I forgot in my previous email to thank you for your help that it's really much appreciated and needed. Thanks a lot.

Quoted 82 lines

Kind regards,

  Roberto

Quoted 78 lines
Quoted 77 lines
Quoted 77 lines · Xenia Ragiadakou
Quoted 75 lines · Stefano Stabellini
Quoted 56 lines · Bertrand Marquis
Quoted 55 lines

Hi Xenia,

Quoted 51 lines · Xenia Ragiadakou
Quoted 5 lines · Stefano Stabellini

Patches 1, 4, and 6 are already committed. I plan to commit patches 2, 3
and 5 in the next couple of days.
Patch 7 needs further discussions and it is best addressed during the next MISRA C sync-up.

I would like to share here, before the next MISRA C sync, my understandings that will hopefully resolve a wrong impression of mine, that I may have spread around, regarding this rule.
There was a misunderstanding regarding the rule 20.7 from my part and I think that Jan is absolutely right that parenthesizing macro parameters used as function arguments is not required by the rule.

The rule 20.7 states "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses" and in the rationale of the rule states "If a macro parameter is not being used as an expression then the parentheses are not necessary because no operators are involved.".

Initially, based on the title, my understanding was that it requires for the expression resulting from the expansion of the macro to be enclosed in parentheses. Then, based on the rule explanation and the examples given,  my understanding was that it requires the macro parameters that are used as expressions to be enclosed in parentheses.
But, after re-thinking about it, the most probable and what makes more sense, is that it require parentheses around the macro parameters that are part of an expression and not around those that are used as expressions.

Therefore, macro parameters being used as function arguments are not required to be enclosed in parentheses, because the function arguments are part of an expression list, not of an expression (comma is evaluated as separator, not as operator).
While, macro parameters used as rhs and lhs expressions of the assignment operator are required to be enclosed in parentheses because they are part of an assignment expression.

I verified that the violation reported by cppcheck is not due to missing parentheses around the function argument (though still I have not understood the origin of the warning). Also, Eclair does not report it.

Hence, it was a misunderstanding of mine and there is no inconsistency, with respect to this rule, in adding parentheses around macro parameters used as rhs of assignments. The rule does not require adding parentheses around macro parameters used as function arguments and neither cppcheck nor Eclair report violation for missing parentheses around macro parameters used as function arguments.

Quoted 8 lines
Quoted 5 lines

Thanks a lot for the detailed explanation :-)

What you say does make sense and I agree with your analysis here, only protect when part of an expression and not use as a subsequent parameter (for a function or an other macro).

Yeah I also agree with your analysis, and many thanks for double-checking the cppcheck and Eclair's reports.

Unfortunately in the specific case that I checked, it was not reported because it was actually an argument to a macro, not a function.
Eclair does report as violations of Rule 20.7 the macro parameters that are used as function arguments and are not enclosed in parentheses.

So, one tool reports it as violation and the other one not.

The same goes, also, for the case where a macro parameter is used as index to an array. Eclair reports it as violation while cppcheck does not.

Signature · 1 line
Xenia