Rule 12.5: OR sequence without parentheses not compliant?

6.12 Expressions

Moderators: misra-c, david ward

Post Reply
pkrebs
Posts: 2
Joined: Sun Oct 04, 2009 12:30 pm
Company: Vienna Institute for Safety & Systems Engineering

Rule 12.5: OR sequence without parentheses not compliant?

Post by pkrebs » Fri Sep 17, 2010 8:51 am

Hello,

I have a question regarding Rule 12.5: "The operands of a logical && or || shall be primary-expressions."

I often use constructs of the form "(condition1) || (condition2) || ... || (condition n)" to test for illegal values of a set of input parameters in functions (see example below), which should be compliant as far as I understand the rule text without the need of extra parentheses to group the conditions (parenthesised expression, sequence of only logical ||). However, my analysis tool (flexelint, v9.00e) complains about this construct ("non-primary expression"). Did I got something wrong about the definition of "primary expression" or is this a false positive?

Thanks in advance for any answers.

Example:

Code: Select all

if ((start >= end) || (start > 7U) || (end > 8U))      /* raises rule violation */
{
	/* do something */
}
else
{
	/* do something different*/
}


if ((start >= end) || ((start > 7U) || (end > 8U)))      /* does not raise violation, note extra parentheses around second and third condition */
{
	/* do something */
}
else
{
	/* do something different*/
}

William Forbes
Posts: 29
Joined: Wed Dec 17, 2008 9:48 pm
Company: TRW Automotive

Re: Rule 12.5: OR sequence without parentheses not compliant

Post by William Forbes » Sat Sep 18, 2010 12:49 pm

It is my understanding that Rule 12.5 allows exceptions if the operands are "effectively Boolean"
So you first example is allowed as the expression (start >= end) || (start > 7U) is effectively Boolean.
As indeed is the expression (start > 7U) || (end > 8U)
So which ever way you look at it, it will be allowed under rule 12.5.

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 12.5: OR sequence without parentheses not compliant

Post by misra-c » Wed Sep 22, 2010 8:52 am

The last sentence of the first paragraph of Rule 12.5 reads:
Where an expression consists of either a sequence of only logical && or a sequence of only logical || , extra parentheses are not required.
Therefore the code is compliant with Rule 12.5.

Your tool appears to be checking correctly against the headline rule (the extra parentheses make the right operand of the first || into a primary expression) but has not taken the whole text of the rule into account.
---
Posted by and on behalf of
the MISRA C Working Group

Achim Olaf Zacher
Posts: 4
Joined: Wed Sep 14, 2005 9:59 am
Location: Germany

Re: Rule 12.5: OR sequence without parentheses not compliant

Post by Achim Olaf Zacher » Mon Sep 17, 2012 2:06 pm

Hello,

Rule 12.1 explicitly mentions C's operator precedence in expressions.
Rule 12.5 is a special case of rule 12.1.

What about compiler switches or preprocessor statements?

#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)

are braces (parentheses) required here? (found in vendor header files)

Thanks in advance

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 12.5: OR sequence without parentheses not compliant

Post by misra-c » Tue Sep 25, 2012 9:45 am

Yes, the rule applies to preprocessing expressions in exactly the same way as it would to other expressions.

Since the defined operator is a unary operator, it will need parentheses, in much the same way as a function call would require parentheses in a non-preprocessing expression. So, the example would need to be written:

Code: Select all

#if (!defined(__cplusplus)) || (defined(__STDC_LIMIT_MACROS))
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.12 Expressions”