Skip to contentSkip to conversation

docs/misra: add 14.3 and 14.4

PATCH Superseded Stefano Stabellini 3 participants 9 messages Aug 30, 2023 – Aug 31, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs/misra: add 14.3 and 14.4

From: Stefano Stabellini <[email protected]>

Add 14.3, with a project-wide deviations on if statements. Add 14.4, clarifying that implicit conversions of integers, chars and pointers to bool are allowed.

Also take the opportunity to clarify that parameters of function pointer types are expected to have names (Rule 8.2).

---
 docs/misra/rules.rst | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
docs/misra/rules.rst +19 -1
index db30632b93..6cde4feeae 100644
@@ -234,7 +234,7 @@ maintainers if you want to suggest a change.
    * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
      - Required
      - Function types shall be in prototype form with named parameters
-     -
+     - Function pointer types shall have named parameters too.
 
    * - `Rule 8.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_03.c>`_
      - Required
@@ -332,6 +332,24 @@ maintainers if you want to suggest a change.
      - A loop counter shall not have essentially floating type
      -
 
+   * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
+     - Required
+     - Controlling expressions shall not be invariant
+     - Due to the extensive usage of IS_ENABLED, sizeof compile-time
+       checks, and other constructs that are detected as errors by MISRA
+       C scanners, managing the configuration of a MISRA C scanner for
+       this rule would be unmanageable. Thus, this rule is adopted with
+       a project-wide deviation on 'if' statements. The rule only
+       applies to while, for, do ... while, ?:, and switch statements.
+
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
+
    * - `Rule 16.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_07.c>`_
      - Required
      - A switch-expression shall not have essentially Boolean type
Signature · 1 line
2.25.1
#1 lore raw

docs/misra/rules.rst maintainers if you want to suggest a change. · line 237

Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +1 -1
@@ -234,7 +234,7 @@ maintainers if you want to suggest a change.
    * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
      - Required
      - Function types shall be in prototype form with named parameters
-     -
+     - Function pointer types shall have named parameters too.

This isn't an exception; do we really need to state such? I would have expected something to appear here only if we intended to deviate certain constructs.

docs/misra/rules.rst maintainers if you want to suggest a change. · line 343

Quoted 13 lines
docs/misra/rules.rst +9 -0
@@ -332,6 +332,24 @@ maintainers if you want to suggest a change.
      - A loop counter shall not have essentially floating type
      -
 
+   * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
+     - Required
+     - Controlling expressions shall not be invariant
+     - Due to the extensive usage of IS_ENABLED, sizeof compile-time
+       checks, and other constructs that are detected as errors by MISRA
+       C scanners, managing the configuration of a MISRA C scanner for
+       this rule would be unmanageable. Thus, this rule is adopted with
+       a project-wide deviation on 'if' statements. The rule only
+       applies to while, for, do ... while, ?:, and switch statements.

The sizeof() aspect mentioned particularly applies to switch() as well. Furthermore ?: is really only shorthand for simple if(), so I don't see treating it different from if() as helpful.

That said, I'd be a little hesitant to give an ack here anyway. If you'd split 14.3 and 14.4, I'd be happy to ack 14.4's addition.

Jan

Quoted 11 lines
docs/misra/rules.rst +8 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
+
    * - `Rule 16.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_07.c>`_
      - Required
      - A switch-expression shall not have essentially Boolean type
#2 lore raw
Quoted 13 lines · Jan Beulich

docs/misra/rules.rst maintainers if you want to suggest a change. · line 237

Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +1 -1
@@ -234,7 +234,7 @@ maintainers if you want to suggest a change.
    * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
      - Required
      - Function types shall be in prototype form with named parameters
-     -
+     - Function pointer types shall have named parameters too.

This isn't an exception; do we really need to state such? I would have expected something to appear here only if we intended to deviate certain constructs.

Yes, it is not an exception. However, as there was genuine confusion in the community about whether the rule should apply or not to function pointer types I think it would be good to clarify. To avoid any doubts in the future. My preference is to keep this as clarification.

Quoted 4 lines
Quoted 4 lines
docs/misra/rules.rst +0 -0
@@ -332,6 +332,24 @@ maintainers if you want to suggest a change.
      - A loop counter shall not have essentially floating type
      -
 
Quoted 13 lines

docs/misra/rules.rst maintainers if you want to suggest a change. · line 343

Quoted 9 lines
docs/misra/rules.rst +9 -0
+   * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
+     - Required
+     - Controlling expressions shall not be invariant
+     - Due to the extensive usage of IS_ENABLED, sizeof compile-time
+       checks, and other constructs that are detected as errors by MISRA
+       C scanners, managing the configuration of a MISRA C scanner for
+       this rule would be unmanageable. Thus, this rule is adopted with
+       a project-wide deviation on 'if' statements. The rule only
+       applies to while, for, do ... while, ?:, and switch statements.

The sizeof() aspect mentioned particularly applies to switch() as well. Furthermore ?: is really only shorthand for simple if(), so I don't see treating it different from if() as helpful.

I'll answer in another email.

Quoted 17 lines

That said, I'd be a little hesitant to give an ack here anyway. If you'd split 14.3 and 14.4, I'd be happy to ack 14.4's addition.

Jan

Quoted 11 lines
docs/misra/rules.rst +8 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
+
    * - `Rule 16.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_07.c>`_
      - Required
      - A switch-expression shall not have essentially Boolean type
#3 lore raw
In reply to Stefano Stabellini

Hi Stefano,

Quoted 21 lines · Stefano Stabellini

From: Stefano Stabellini <[email protected]>

Add 14.3, with a project-wide deviations on if statements. Add 14.4, clarifying that implicit conversions of integers, chars and pointers to bool are allowed.

Also take the opportunity to clarify that parameters of function pointer types are expected to have names (Rule 8.2).

---
docs/misra/rules.rst | 20 +++++++++++++++++++-

1 file changed, 19 insertions(+), 1 deletion(-)
docs/misra/rules.rst +0 -0
index db30632b93..6cde4feeae 100644

docs/misra/rules.rst maintainers if you want to suggest a change. · line 237

Quoted 5 lines
docs/misra/rules.rst +1 -1
@@ -234,7 +234,7 @@ maintainers if you want to suggest a change.
   * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
     - Required
     - Function types shall be in prototype form with named parameters
-     -
+     - Function pointer types shall have named parameters too.

I would just modify to Function and Function pointers types shall be ...

Quoted 6 lines
docs/misra/rules.rst +0 -0
   * - `Rule 8.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_03.c>`_
     - Required
@@ -332,6 +332,24 @@ maintainers if you want to suggest a change.
     - A loop counter shall not have essentially floating type
     -

docs/misra/rules.rst maintainers if you want to suggest a change. · line 343

Quoted 10 lines
docs/misra/rules.rst +9 -0
 
+   * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
+     - Required
+     - Controlling expressions shall not be invariant
+     - Due to the extensive usage of IS_ENABLED, sizeof compile-time
+       checks, and other constructs that are detected as errors by MISRA
+       C scanners, managing the configuration of a MISRA C scanner for
+       this rule would be unmanageable. Thus, this rule is adopted with
+       a project-wide deviation on 'if' statements. The rule only
+       applies to while, for, do ... while, ?:, and switch statements.

Didn't we also said that we would accept while(0) and while(1) ?
Also i agree with Jan, ? is really the same as if so we should not treat it differently.

docs/misra/rules.rst maintainers if you want to suggest a change. · line 351

Quoted 8 lines
docs/misra/rules.rst +8 -0
+
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed

I am a bit wondering here what is remaining after this deviation.

Quoted 7 lines
docs/misra/rules.rst +1 -0
+
   * - `Rule 16.7 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_16_07.c>`_
     - Required
     - A switch-expression shall not have essentially Boolean type
Signature · 1 line
2.25.1

Cheers
Bertrand

#4 lore raw
Quoted 10 lines · Bertrand Marquis

docs/misra/rules.rst maintainers if you want to suggest a change. · line 351

Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +7 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed

I am a bit wondering here what is remaining after this deviation.

Hmm, good point - floating point (and alike) types, which we don't use anyway.

Jan

#5 lore raw

Hi,

Quoted 12 lines · Jan Beulich
Quoted 9 lines · Bertrand Marquis
Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +7 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
Quoted 3 lines
Quoted 1 line

I am a bit wondering here what is remaining after this deviation.

Hmm, good point - floating point (and alike) types, which we don't use anyway.

So we accept the rule but we deviate all cases that would apply. I do not think we should do that.

Bertrand

Quoted 3 lines

Jan

#6 lore raw
Quoted 14 lines · Bertrand Marquis

Hi,

Quoted 12 lines · Jan Beulich
Quoted 9 lines · Bertrand Marquis
Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +7 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I am a bit wondering here what is remaining after this deviation.

Hmm, good point - floating point (and alike) types, which we don't use anyway.

So we accept the rule but we deviate all cases that would apply. I do not think we should do that.

Right; Stefano - I withdraw my offer to ack a 14.4-only patch.

Jan

#7 lore raw
In reply to Bertrand Marquis
Quoted 12 lines · Bertrand Marquis
Quoted 12 lines · Jan Beulich
Quoted 9 lines · Bertrand Marquis
Quoted 8 lines · Stefano Stabellini
docs/misra/rules.rst +7 -0
+   * - `Rule 14.4 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c>`_
+     - Required
+     - The controlling expression of an if statement and the controlling
+       expression of an iteration-statement shall have essentially
+       Boolean type
+     - Implicit conversions of integers, pointers, and chars to boolean
+       are allowed
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I am a bit wondering here what is remaining after this deviation.

Hmm, good point - floating point (and alike) types, which we don't use anyway.

So we accept the rule but we deviate all cases that would apply. I do not think we should do that.

In the past we have been accepting rules that don't apply to Xen because it is not much that they don't apply. It is that we have no violations and we don't think there is any risk of getting violations in the future.

But theoretically they apply, and for example if we end up with floating points in Xen at some point in the future we would want rules affecting floating points to apply, hence we accepted rules about floating points.

This rule is a bit different though: we deviate most of the rule and the remaining part is small. In addition, we have no violations and we don't think there is any risk of getting violations in the future for what's left of the rule.

So I don't know if it is worth adding the rule or not. I think we should ask Roberto next time. For now, I remove it from this patch.

#8 lore raw
In reply to Bertrand Marquis
Quoted 18 lines · Bertrand Marquis

Hi Stefano,

Quoted 16 lines · Stefano Stabellini

From: Stefano Stabellini <[email protected]>

Add 14.3, with a project-wide deviations on if statements. Add 14.4, clarifying that implicit conversions of integers, chars and pointers to bool are allowed.

Also take the opportunity to clarify that parameters of function pointer types are expected to have names (Rule 8.2).

---
docs/misra/rules.rst | 20 +++++++++++++++++++-

1 file changed, 19 insertions(+), 1 deletion(-)
Quoted 13 lines

docs/misra/rules.rst maintainers if you want to suggest a change. · line 237

Quoted 10 lines
docs/misra/rules.rst +1 -1
index db30632b93..6cde4feeae 100644
@@ -234,7 +234,7 @@ maintainers if you want to suggest a change.
   * - `Rule 8.2 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_02.c>`_
     - Required
     - Function types shall be in prototype form with named parameters
-     -
+     - Function pointer types shall have named parameters too.

I would just modify to Function and Function pointers types shall be ...

Sure, I can do that.

Quoted 7 lines
Quoted 7 lines
docs/misra/rules.rst +0 -0
   * - `Rule 8.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_08_03.c>`_
     - Required
@@ -332,6 +332,24 @@ maintainers if you want to suggest a change.
     - A loop counter shall not have essentially floating type
     -
 
Quoted 12 lines

docs/misra/rules.rst maintainers if you want to suggest a change. · line 343

Quoted 9 lines
docs/misra/rules.rst +9 -0
+   * - `Rule 14.3 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_03.c>`_
+     - Required
+     - Controlling expressions shall not be invariant
+     - Due to the extensive usage of IS_ENABLED, sizeof compile-time
+       checks, and other constructs that are detected as errors by MISRA
+       C scanners, managing the configuration of a MISRA C scanner for
+       this rule would be unmanageable. Thus, this rule is adopted with
+       a project-wide deviation on 'if' statements. The rule only
+       applies to while, for, do ... while, ?:, and switch statements.

Didn't we also said that we would accept while(0) and while(1) ?
Also i agree with Jan, ? is really the same as if so we should not treat it differently.

I took the list of things the rule applies to from the text of the rule itself. However, I think you are right about the ?: and it should be deviated together with if. I can also add while(0) and while(1).