Application of rule 14.10 for Autogenerated code

Forum for discussing and asking questions about the MISRA document MISRA AC AGC "Guidelines for the application of MISRA-C:2004 in the context of automatic code generation"

Moderators: david ward, GeoffFrost

Post Reply
ssauvage
Posts: 1
Joined: Thu Jan 10, 2013 9:43 am
Company: The MathWorks

Application of rule 14.10 for Autogenerated code

Post by ssauvage » Mon Jan 14, 2013 1:49 pm

I have recently received a question from a customer using our code generation tools (embedded coder).
The code that we generate for a block (saturate block) is like this:

Code: Select all

rtB_out = something;
if(rtB_out > maxlim) {
   rtB_out = maxlim;
}
else {
   if(rtB_out < minlim) {
      rtB_out = minlim;
   }
}
The customer asked whether the code above was MISRA compliant, in particular with respect to rule 14.10.
It is an “

Code: Select all

if ... else { if { ... } }
” construct and very close to the “

Code: Select all

if ... else if ...
” construct as described in the rule 14.10.
However, it can be read as a simple if being the statement in an else clause.

Personally, I find the code quite readable as it is and it is easy to check that there is no path where rtB_out is not assigned a value.
Would you recommend to have a final else in this context?

MISRA Reply
Posts: 52
Joined: Mon Dec 06, 2004 12:24 pm

Re: Application of rule 14.10 for Autogenerated code

Post by MISRA Reply » Mon Jan 21, 2013 9:28 pm

The code sample complies with 14.10 as the second if is nested within a compound statement. We would not recommend a final else in this context and would suggest that the code has been laid out in this manner by the autocode generator specifically to abide by Rule 14.10 and thus avoid the use of an empty else { ... } clause, the only reasonable alternative if a further final else clause were included.

Post Reply

Return to “MISRA AC AGC discussions”