Skip to contentSkip to conversation

automation/eclair: update configuration of Rule 20.7

PATCH In review Federico Serafini 2 participants 5 messages Sep 10, 2024 – Sep 12, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover automation/eclair: update configuration of Rule 20.7

Update ECLAIR configuration to deviate some safe violations of Rule 20.7. Remove redundant comment-based deviations.

Federico Serafini (2):
automation/eclair: update configuration of Rule 20.7 xen/bitmap: remove redundant deviations

 automation/eclair_analysis/ECLAIR/deviations.ecl | 4 ++++
 docs/misra/deviations.rst                        | 5 +++++
 xen/include/xen/bitmap.h                         | 3 ---
 3 files changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 automation/eclair: update configuration of Rule 20.7

MISRA C:2012 Rule 20.7 states that "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". The rational of the rule is that if a macro argument expands to an expression, there may be problems related to operator precedence, e.g.,

define M(A, B) A * B

M(1+1, 2+2) will expand to: 1+1 * 2+2

Update ECLAIR configuration to tag as 'safe' the expansions of macro arguments surrounded tokens '{', '}' and ';', since in their presence problems related to operator precedence can not occur.

---
Changes from v1:
- more details to justify the deviation;
- split modifications in two patches.

---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 4 ++++
 docs/misra/deviations.rst                        | 5 +++++
 2 files changed, 9 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +4 -0
index 9051f41602..e0d9426b7a 100644
@@ -581,6 +581,10 @@ of this macro do not lead to developer confusion, and can thus be deviated."
 -config=MC3R1.R20.7,reports+={safe, "any_area(any_loc(any_exp(macro(^count_args_$))))"}
 -doc_end
 
+-doc_begin="Problems related to operator precedence can not occur if the expansion of the macro argument is surrounded by tokens '{', '}' and ';'."
+-config=MC3R1.R20.7,expansion_context+={safe, "left_right(^[\\{;]$,^[;\\}]$)"}
+-doc_end
+
 -doc_begin="Uses of variadic macros that have one of their arguments defined as
 a macro and used within the body for both ordinary parameter expansion and as an
 operand to the # or ## operators have a behavior that is well-understood and
docs/misra/deviations.rst +5 -0
index b66c271c4e..69f93a6a91 100644
@@ -534,6 +534,11 @@ Deviations related to MISRA C:2012 Rules:
        refactoring it to add parentheses breaks its functionality.
      - Tagged as `safe` for ECLAIR.
 
+   * - R20.7
+     - Problems related to operator precedence can not occur if the expansion
+       of the macro argument is surrounded by tokens '{', '}' and ';'.
+     - Tagged as `safe` for ECLAIR.
+
    * - R20.12
      - Variadic macros that use token pasting often employ the gcc extension
        `ext_paste_comma`, as detailed in `C-language-toolchain.rst`, which is
Signature · 1 line
2.34.1
Acked-by on v2 1/2
#2 lore raw
Quoted 7 lines · Federico Serafini

MISRA C:2012 Rule 20.7 states that "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". The rational of the rule is that if a macro argument expands to an expression, there may be problems related to operator precedence, e.g.,

define M(A, B) A * B

Quoted 7 lines

M(1+1, 2+2) will expand to: 1+1 * 2+2

Update ECLAIR configuration to tag as 'safe' the expansions of macro arguments surrounded tokens '{', '}' and ';', since in their presence problems related to operator precedence can not occur.

Quoted 43 lines

---
Changes from v1:
- more details to justify the deviation;
- split modifications in two patches.

---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 4 ++++
 docs/misra/deviations.rst                        | 5 +++++
 2 files changed, 9 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +4 -0
index 9051f41602..e0d9426b7a 100644
@@ -581,6 +581,10 @@ of this macro do not lead to developer confusion, and can thus be deviated."
 -config=MC3R1.R20.7,reports+={safe, "any_area(any_loc(any_exp(macro(^count_args_$))))"}
 -doc_end
 
+-doc_begin="Problems related to operator precedence can not occur if the expansion of the macro argument is surrounded by tokens '{', '}' and ';'."
+-config=MC3R1.R20.7,expansion_context+={safe, "left_right(^[\\{;]$,^[;\\}]$)"}
+-doc_end
+
 -doc_begin="Uses of variadic macros that have one of their arguments defined as
 a macro and used within the body for both ordinary parameter expansion and as an
 operand to the # or ## operators have a behavior that is well-understood and
docs/misra/deviations.rst +5 -0
index b66c271c4e..69f93a6a91 100644
@@ -534,6 +534,11 @@ Deviations related to MISRA C:2012 Rules:
        refactoring it to add parentheses breaks its functionality.
      - Tagged as `safe` for ECLAIR.
 
+   * - R20.7
+     - Problems related to operator precedence can not occur if the expansion
+       of the macro argument is surrounded by tokens '{', '}' and ';'.
+     - Tagged as `safe` for ECLAIR.
+
    * - R20.12
      - Variadic macros that use token pasting often employ the gcc extension
        `ext_paste_comma`, as detailed in `C-language-toolchain.rst`, which is
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 2/2 xen/bitmap: remove redundant deviations

In reply to Federico Serafini

Remove comment-based deviations since a project wide deviation that cover such cases is present.

---
Changes from v1:
- split modifications in two patches.

---
 xen/include/xen/bitmap.h | 3 ---
 1 file changed, 3 deletions(-)
xen/include/xen/bitmap.h +0 -3
index 5d668053b0..4b642cd420 100644
@@ -103,13 +103,10 @@ extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
 #define bitmap_switch(nbits, zero, small, large)			  \
 	unsigned int n__ = (nbits);					  \
 	if (__builtin_constant_p(nbits) && !n__) {			  \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		zero;							  \
 	} else if (__builtin_constant_p(nbits) && n__ <= BITS_PER_LONG) { \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		small;							  \
 	} else {							  \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		large;							  \
 	}
 
Signature · 1 line
2.34.1
Acked-by on v2 2/2
#4 lore raw

Re: [XEN PATCH v2 2/2] xen/bitmap: remove redundant deviations

Quoted 4 lines · Federico Serafini

Remove comment-based deviations since a project wide deviation that cover such cases is present.

Quoted 28 lines

---
Changes from v1:
- split modifications in two patches.

---
 xen/include/xen/bitmap.h | 3 ---
 1 file changed, 3 deletions(-)
xen/include/xen/bitmap.h +0 -3
index 5d668053b0..4b642cd420 100644
@@ -103,13 +103,10 @@ extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
 #define bitmap_switch(nbits, zero, small, large)			  \
 	unsigned int n__ = (nbits);					  \
 	if (__builtin_constant_p(nbits) && !n__) {			  \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		zero;							  \
 	} else if (__builtin_constant_p(nbits) && n__ <= BITS_PER_LONG) { \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		small;							  \
 	} else {							  \
-		/* SAF-7-safe Rule 20.7 non-parenthesized macro argument */ \
 		large;							  \
 	}
 
Signature · 1 line
2.34.1