Skip to contentSkip to conversation

misra: deviations of Rule 16.4

PATCH In review Federico Serafini 2 participants 5 messages May 3, 2024

lore
Share & tools
lore
#0 lore raw

cover misra: deviations of Rule 16.4

Define "switch-clause" in terms of the C specification. Deviate Rule 16.4.

Federico Serafini (2):
docs/misra: add Terms & Definitions section to rules.rst automation/eclair: add deviation for Rule 16.4

 automation/eclair_analysis/ECLAIR/deviations.ecl |  8 ++++++++
 docs/misra/deviations.rst                        | 13 +++++++++++++
 docs/misra/rules.rst                             | 14 ++++++++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 docs/misra: add Terms & Definitions section to rules.rst

Add a section for terms and definitions used by MISRA but expressed in terms of the C specification.

Add a definition of "switch clause" to the newly-introduced section.

Link the first use of the term "switch clause" in the document to its definition.

---
Jan you were not completely satisfied by the definition but I didn't find a better one.

---
 docs/misra/rules.rst | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
docs/misra/rules.rst +12 -2
index b7b447e152..d3b70fdf04 100644
@@ -489,8 +489,7 @@ maintainers if you want to suggest a change.
 
    * - `Rule 16.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_03.c>`_
      - Required
-     - An unconditional break statement shall terminate every
-       switch-clause
+     - An unconditional break statement shall terminate every switch-clause_
      - In addition to break, also other unconditional flow control statements
        such as continue, return, goto are allowed.
 
@@ -712,3 +711,14 @@ maintainers if you want to suggest a change.
      - The value of a pointer to a FILE shall not be used after the associated
        stream has been closed
      -
+
+Terms & Definitions
+-------------------
+
+.. _switch-clause:
+
+A *switch clause* can be defined as:
+"the non-empty list of statements which follows a non-empty list of
+case/default labels".
+A formal definition is available within the amplification of MISRA C:2012
+Rule 16.1.
Signature · 1 line
2.34.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [XEN PATCH 1/2] docs/misra: add Terms & Definitions section to rules.rst

Quoted 10 lines · Federico Serafini

Add a section for terms and definitions used by MISRA but expressed in terms of the C specification.

Add a definition of "switch clause" to the newly-introduced section.

Link the first use of the term "switch clause" in the document to its definition.

Quoted 39 lines

---
Jan you were not completely satisfied by the definition but I didn't find a better one.

---
 docs/misra/rules.rst | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
docs/misra/rules.rst +12 -2
index b7b447e152..d3b70fdf04 100644
@@ -489,8 +489,7 @@ maintainers if you want to suggest a change.
 
    * - `Rule 16.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_03.c>`_
      - Required
-     - An unconditional break statement shall terminate every
-       switch-clause
+     - An unconditional break statement shall terminate every switch-clause_
      - In addition to break, also other unconditional flow control statements
        such as continue, return, goto are allowed.
 
@@ -712,3 +711,14 @@ maintainers if you want to suggest a change.
      - The value of a pointer to a FILE shall not be used after the associated
        stream has been closed
      -
+
+Terms & Definitions
+-------------------
+
+.. _switch-clause:
+
+A *switch clause* can be defined as:
+"the non-empty list of statements which follows a non-empty list of
+case/default labels".
+A formal definition is available within the amplification of MISRA C:2012
+Rule 16.1.
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/2 automation/eclair: add deviation for Rule 16.4

In reply to Federico Serafini

MISRA C:2012 Rule 16.4 states that "Every switch statement shall have a default label".
Update ECLAIR configuration to take into account the deviations agreed during MISRA meetings.

---
 automation/eclair_analysis/ECLAIR/deviations.ecl |  8 ++++++++
 docs/misra/deviations.rst                        | 13 +++++++++++++
 2 files changed, 21 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +8 -0
index d21f112a9b..f09ad71acf 100644
@@ -384,6 +384,14 @@ explicit comment indicating the fallthrough intention is present."
 -config=MC3R1.R16.3,reports+={safe, "any_area(end_loc(any_exp(text(^(?s).*/\\* [fF]all ?through.? \\*/.*$,0..1))))"}
 -doc_end
 
+-doc_begin="Switch statements having a controlling expression of enum type deliberately do not have a default case: gcc -Wall enables -Wswitch which warns (and breaks the build as we use -Werror) if one of the enum labels is missing from the switch."
+-config=MC3R1.R16.4,reports+={deliberate,'any_area(kind(context)&&^.* has no `default.*$&&stmt(node(switch_stmt)&&child(cond,skip(__non_syntactic_paren_stmts,type(canonical(enum_underlying_type(any())))))))'}
+-doc_end
+
+-doc_begin="A switch statement with a single switch clause and no default label may be used in place of an equivalent if statement if it is considered to improve readability."
+-config=MC3R1.R16.4,switch_clauses+={deliberate,"switch(1)&&default(0)"}
+-doc_end
+
 -doc_begin="A switch statement with a single switch clause and no default label may be used in place of an equivalent if statement if it is considered to improve readability."
 -config=MC3R1.R16.6,switch_clauses+={deliberate, "default(0)"}
 -doc_end
docs/misra/deviations.rst +13 -0
index ed0c1e8ed0..39cc321a27 100644
@@ -334,6 +334,19 @@ Deviations related to MISRA C:2012 Rules:
          - /\* Fallthrough \*/
          - /\* Fallthrough. \*/
 
+   * - R16.4
+     - Switch statements having a controlling expression of enum type
+       deliberately do not have a default case: gcc -Wall enables -Wswitch
+       which warns (and breaks the build as we use -Werror) if one of the enum
+       labels is missing from the switch.
+     - Tagged as `deliberate` for ECLAIR.
+
+   * - R16.4
+     - A switch statement with a single switch clause and no default label may
+       be used in place of an equivalent if statement if it is considered to
+       improve readability.
+     - Tagged as `deliberate` for ECLAIR.
+
    * - R16.6
      - A switch statement with a single switch clause and no default label may
        be used in place of an equivalent if statement if it is considered to
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [XEN PATCH 2/2] automation/eclair: add deviation for Rule 16.4

Quoted 6 lines · Federico Serafini

MISRA C:2012 Rule 16.4 states that "Every switch statement shall have a default label".
Update ECLAIR configuration to take into account the deviations agreed during MISRA meetings.

Quoted 51 lines
---
 automation/eclair_analysis/ECLAIR/deviations.ecl |  8 ++++++++
 docs/misra/deviations.rst                        | 13 +++++++++++++
 2 files changed, 21 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +8 -0
index d21f112a9b..f09ad71acf 100644
@@ -384,6 +384,14 @@ explicit comment indicating the fallthrough intention is present."
 -config=MC3R1.R16.3,reports+={safe, "any_area(end_loc(any_exp(text(^(?s).*/\\* [fF]all ?through.? \\*/.*$,0..1))))"}
 -doc_end
 
+-doc_begin="Switch statements having a controlling expression of enum type deliberately do not have a default case: gcc -Wall enables -Wswitch which warns (and breaks the build as we use -Werror) if one of the enum labels is missing from the switch."
+-config=MC3R1.R16.4,reports+={deliberate,'any_area(kind(context)&&^.* has no `default.*$&&stmt(node(switch_stmt)&&child(cond,skip(__non_syntactic_paren_stmts,type(canonical(enum_underlying_type(any())))))))'}
+-doc_end
+
+-doc_begin="A switch statement with a single switch clause and no default label may be used in place of an equivalent if statement if it is considered to improve readability."
+-config=MC3R1.R16.4,switch_clauses+={deliberate,"switch(1)&&default(0)"}
+-doc_end
+
 -doc_begin="A switch statement with a single switch clause and no default label may be used in place of an equivalent if statement if it is considered to improve readability."
 -config=MC3R1.R16.6,switch_clauses+={deliberate, "default(0)"}
 -doc_end
docs/misra/deviations.rst +13 -0
index ed0c1e8ed0..39cc321a27 100644
@@ -334,6 +334,19 @@ Deviations related to MISRA C:2012 Rules:
          - /\* Fallthrough \*/
          - /\* Fallthrough. \*/
 
+   * - R16.4
+     - Switch statements having a controlling expression of enum type
+       deliberately do not have a default case: gcc -Wall enables -Wswitch
+       which warns (and breaks the build as we use -Werror) if one of the enum
+       labels is missing from the switch.
+     - Tagged as `deliberate` for ECLAIR.
+
+   * - R16.4
+     - A switch statement with a single switch clause and no default label may
+       be used in place of an equivalent if statement if it is considered to
+       improve readability.
+     - Tagged as `deliberate` for ECLAIR.
+
    * - R16.6
      - A switch statement with a single switch clause and no default label may
        be used in place of an equivalent if statement if it is considered to
Signature · 1 line
2.34.1