Search code examples
c++misra

MISRA 5-2-1 rule requires postfix-expressions?


The rule 5-2-1 in MISRA-2008 states that

Each operand of a logical && or || shall be a postfix-expression. Exception: Where an expression consists of either a sequence of only logical && or a sequence of only logical ||, extra parentheses are not required.

And here are the examples, right from the document itself:

if (x == 0 && ishigh)      // Non-compliant
if (( x == 0 ) && ishigh)  // Compliant
if (x || y || z)           // Compliant by exception, if x, y and z bool
if (x || y && z)           // Non-compliant
if (x || (y && z))         // Compliant
if (x && !y)               // Non-compliant
if (x && (!y))             // Compliant
if (is_odd(y) && x)        // Compliant
if ((x > c1) && (y > c2)  && (z > c3))   // Compliant - exception
if ((x > c1) && (y > c2)  || (z > c3))   // Non-compliant
if ((x > c1) && ((y > c2) || (z > c3)))  // Compliant as extra() used

Could somebody point me where the posfix-expressions are there? They look as primary ones. I don't see anything like ++ or --.


Solution

  • All primary-expressions are postfix-expressions. In these examples, the only postfix-expression that's not a primary-expression is is_odd(y).

    The grammar for postfix-expressions is:

    postfix-expression:

    primary-expression
    postfix-expression [ expr-or-braced-init-list ]
    postfix-expression ( expression-listopt )
    simple-type-specifier ( expression-listopt )
    typename-specifier ( expression-listopt )
    simple-type-specifier braced-init-list
    typename-specifier braced-init-list
    postfix-expression . templateopt id-expression
    postfix-expression -> templateopt id-expression
    postfix-expression ++
    postfix-expression --
    dynamic_­cast < type-id > ( expression )
    static_­cast < type-id > ( expression )
    reinterpret_­cast < type-id > ( expression )
    const_­cast < type-id > ( expression )
    typeid ( expression )
    typeid ( type-id )

    and

    primary-expression:

    literal
    this
    ( expression )
    id-expression
    lambda-expression
    fold-expression
    requires-expression