![]() |
|
MISRA C:2012 Rule-10.1: (Required) |
|
||||
![]() | |||||||
Amplification:
In the following table a number within a cell indicates where a restriction applies to the use of an essential type as an operand to an operator. These numbers correspond to paragraphs in the Rationale section and indicate why each restriction is imposed.
| Operator | Operand | Essential type category of arithmetic operand | |||||
| Boolean | character | enum | signed | unsigned | floating | ||
| [ ] | integer | 3 | 4 | 1 | |||
| +(unary) | 3 | 4 | 5 | ||||
| -(unary) | 3 | 4 | 5 | 8 | |||
| + - | either | 3 | 5 | ||||
| * / | either | 3 | 4 | 5 | |||
| % | either | 3 | 4 | 5 | 1 | ||
| < > <= >= | either | 3 | |||||
| == != | either | ||||||
| ! && || | any | 2 | 2 | 2 | 2 | 2 | |
| << >> | left | 3 | 4 | 5, 6 | 6 | 1 | |
| << >> | right | 3 | 4 | 7 | 7 | 1 | |
| ~ & | ^ | any | 3 | 4 | 5, 6 | 6 | 1 | |
| ? : | 1st | 2 | 2 | 2 | 2 | 2 | |
| ? : | 2nd and 3rd | ||||||
Under this rule the ++ and -- operators behave the same way as the binary + and - operators.
Other rules place further restrictions on the combination of essential types that may be used within an expression.
Exception:
A non-negative integer constant expression of essentially signed type may be used as the right hand operand to a shift operator.
Example Code:
#pragma PRQA_MESSAGES_OFF 2830,2832,2912,2982,2984,2985,2986,2995,2996,3112
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#include <stdbool.h>
#include "misra.h"
#include "m3cmex.h"
#ifndef BEXP
#define BEXP bla
#endif
extern int16_t rule_1001_Boolean( void )
{
uint16_t buf[10] = {0};
buf[BEXP]; /* 4500 */
BEXP + u16a; /* 4501 */
BEXP - u16a; /* 4501 */
BEXP * u16a; /* 4501 */
BEXP / u16a; /* 4501 */
BEXP % u16a; /* 4501 */
u16a + BEXP; /* 4501 */
u16a - BEXP; /* 4501 */
u16a * BEXP; /* 4501 */
u16a / BEXP; /* 4501 */
u16a % BEXP; /* 4501 */
~BEXP; /* 4502 */
BEXP & u16a; /* 4502 */
BEXP | u16a; /* 4502 */
BEXP ^ u16a; /* 4502 */
u16a & BEXP; /* 4502 */
u16a | BEXP; /* 4502 */
u16a ^ BEXP; /* 4502 */
BEXP << 0u; /* 4503 */
BEXP << 1u; /* 4503 */
BEXP << 3u; /* 4503 */
BEXP >> 0u; /* 4503 */
BEXP >> 1u; /* 4503 */
BEXP >> 3u; /* 4503 */
u16a << BEXP; /* 4504 */
u16a >> BEXP; /* 4504 */
BEXP < u16a; /* 4505 */
BEXP <= u16a; /* 4505 */
BEXP >= u16a; /* 4505 */
BEXP > u16a; /* 4505 */
u16a < BEXP; /* 4505 */
u16a <= BEXP; /* 4505 */
u16a >= BEXP; /* 4505 */
u16a > BEXP; /* 4505 */
BEXP == bla; /* */
BEXP != bla; /* */
bla == BEXP; /* */
bla != BEXP; /* */
++bla; /* 4507 */
--bla; /* 4507 */
bla++; /* 4507 */
bla--; /* 4507 */
!BEXP; /* */
BEXP && bla; /* */
BEXP || bla; /* */
bla && BEXP; /* */
bla || BEXP; /* */
BEXP ? u16a : u16b; /* */
(u16a > u16b) ? BEXP : bla; /* */
return 1;
}
Example Code:
#pragma PRQA_MESSAGES_OFF 1812,2790,2791,2830,2921,2984,2985,2995,2996,3112,3344
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#include "misra.h"
#include "m3cmex.h"
#ifndef CEXP
#define CEXP pca
#endif
extern int16_t rule_1001_character( void )
{
uint16_t buf[300] = {0};
buf[CEXP]; /* 4510 */
CEXP + u16a; /* */
CEXP - u16a; /* */
CEXP * u16a; /* 4511 */
CEXP / u16a; /* 4511 */
CEXP % u16a; /* 4511 */
u16a + CEXP; /* */
u16a - CEXP; /* */
u16a * CEXP; /* 4511 */
u16a / CEXP; /* 4511 */
u16a % CEXP; /* 4511 */
~CEXP; /* 4512 */
CEXP & u16a; /* 4512 */
CEXP | u16a; /* 4512 */
CEXP ^ u16a; /* 4512 */
u16a & CEXP; /* 4512 */
u16a | CEXP; /* 4512 */
u16a ^ CEXP; /* 4512 */
CEXP << 0u; /* 4513 */
CEXP << 1u; /* 4513 */
CEXP << 3u; /* 4513 */
CEXP >> 0u; /* 4513 */
CEXP >> 1u; /* 4513 */
CEXP >> 3u; /* 4513 */
u16a << CEXP; /* 4514 */
u16a >> CEXP; /* 4514 */
CEXP < 'z'; /* */
CEXP <= 'z'; /* */
CEXP >= 'z'; /* */
CEXP > 'z'; /* */
'z' < CEXP; /* */
'z' <= CEXP; /* */
'z' >= CEXP; /* */
'z' > CEXP; /* */
CEXP == 'z'; /* */
CEXP != 'z'; /* */
'z' == CEXP; /* */
'z' != CEXP; /* */
++pca; /* */
--pca; /* */
pca++; /* */
pca--; /* */
!CEXP; /* 4518 */
CEXP && bla; /* 4518 */
CEXP || bla; /* 4518 */
bla && CEXP; /* 4518 */
bla || CEXP; /* 4518 */
CEXP ? u16a : u16b; /* 4519 */
(u16a > u16b) ? CEXP : 'p'; /* */
return 1;
}
Example Code:
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#pragma PRQA_MESSAGES_OFF 1812,2790,2830,2912,2984,2985,2986,2995,2996,3112,3322,3344
#include "misra.h"
#include "m3cmex.h"
#ifndef EEXP
#define EEXP n1a
#endif
extern int16_t rule_1001_enum( void )
{
uint16_t buf[300] = {0};
buf[EEXP]; /* */
EEXP + u16a; /* 4521 */
EEXP - u16a; /* 4521 */
EEXP * u16a; /* 4521 */
EEXP / u16a; /* 4521 */
EEXP % u16a; /* 4521 */
u16a + EEXP; /* 4521 */
u16a - EEXP; /* 4521 */
u16a * EEXP; /* 4521 */
u16a / EEXP; /* 4521 */
u16a % EEXP; /* 4521 */
~EEXP; /* 4522 */
EEXP & u16a; /* 4522 */
EEXP | u16a; /* 4522 */
EEXP ^ u16a; /* 4522 */
u16a & EEXP; /* 4522 */
u16a | EEXP; /* 4522 */
u16a ^ EEXP; /* 4522 */
EEXP << 0u; /* 4523 */
EEXP << 1u; /* 4523 */
EEXP << 3u; /* 4523 */
EEXP >> 0u; /* 4523 */
EEXP >> 1u; /* 4523 */
EEXP >> 3u; /* 4523 */
u16a << EEXP; /* 4524 */
u16a >> EEXP; /* 4524 */
EEXP < AMBER; /* */
EEXP <= AMBER; /* */
EEXP >= AMBER; /* */
EEXP > AMBER; /* */
RED < EEXP; /* */
RED <= EEXP; /* */
RED >= EEXP; /* */
RED > EEXP; /* */
EEXP == RED; /* */
EEXP != RED; /* */
RED == EEXP; /* */
RED != EEXP; /* */
++n1a; /* 4527 */
--n1a; /* 4527 */
n1a++; /* 4527 */
n1a--; /* 4527 */
!EEXP; /* 4528 */
EEXP && bla; /* 4528 */
EEXP || bla; /* 4528 */
bla && EEXP; /* 4528 */
bla || EEXP; /* 4528 */
EEXP ? u16a : u16b; /* 4529 */
(u16a > u16b) ? EEXP : n1a; /* */
return 1;
}
Example Code:
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#pragma PRQA_MESSAGES_OFF 1800,1804,2830,3112,3344,3377
#include "misra.h"
#include "m3cmex.h"
#ifndef FEXP
#define FEXP fta
#endif
extern int16_t rule_1001_floating( void )
{
uint16_t buf[300] = {0};
buf[FEXP]; /* 0453 2753 */
FEXP + u16a; /* */
FEXP - u16a; /* */
FEXP * u16a; /* */
FEXP / u16a; /* */
FEXP % u16a; /* 0495 */
u16a + FEXP; /* */
u16a - FEXP; /* */
u16a * FEXP; /* */
u16a / FEXP; /* */
u16a % FEXP; /* 0496 */
FEXP & u16a; /* 0495 */
FEXP | u16a; /* 0495 */
FEXP ^ u16a; /* 0495 */
u16a & FEXP; /* 0496 */
u16a | FEXP; /* 0496 */
u16a ^ FEXP; /* 0496 */
FEXP << 0u; /* 0495 */
FEXP << 1u; /* 0495 */
FEXP << 3u; /* 0495 */
FEXP >> 0u; /* 0495 */
FEXP >> 1u; /* 0495 */
FEXP >> 3u; /* 0495 */
u16a << FEXP; /* 0496 */
u16a >> FEXP; /* 0496 */
FEXP < 2.34F; /* */
FEXP <= 2.34F; /* */
FEXP >= 2.34F; /* */
FEXP > 2.34F; /* */
2.34F < FEXP; /* */
2.34F <= FEXP; /* */
2.34F >= FEXP; /* */
2.34F > FEXP; /* */
FEXP == 2.34F; /* */
FEXP != 2.34F; /* */
2.34F == FEXP; /* */
2.34F != FEXP; /* */
++fta; /* */
--fta; /* */
fta++; /* */
fta--; /* */
FEXP && bla; /* 4568 */
FEXP || bla; /* 4568 */
bla && FEXP; /* 4568 */
bla || FEXP; /* 4568 */
FEXP ? u16a : u16b; /* 4569 */
(u16a > u16b) ? FEXP : u16a; /* */
return 1;
}
Example Code:
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#pragma PRQA_MESSAGES_OFF 1056,1820,1821,1830,1831,1840,1841,2790,2830,2832,2840,2860,2890,2911,2984,2985,2995,3112,3322,3344,3377
#include "misra.h"
#include "m3cmex.h"
#ifndef SEXP
#define SEXP s16a
#endif
extern int16_t rule_1001_signed( void )
{
uint16_t buf[300] = {0};
buf[SEXP]; /* */
SEXP + u16a; /* */
SEXP - u16a; /* */
SEXP * u16a; /* */
SEXP / u16a; /* */
SEXP % u16a; /* */
u16a + SEXP; /* */
u16a - SEXP; /* */
u16a * SEXP; /* */
u16a / SEXP; /* */
u16a % SEXP; /* */
~SEXP; /* 4532 */
SEXP & u16a; /* 4532 */
SEXP | u16a; /* 4532 */
SEXP ^ u16a; /* 4532 */
u16a & SEXP; /* 4532 */
u16a | SEXP; /* 4532 */
u16a ^ SEXP; /* 4532 */
SEXP << 0u; /* 4533 */
SEXP << 1u; /* 4533 */
SEXP << 3u; /* 4533 */
SEXP >> 0u; /* 4533 */
SEXP >> 1u; /* 4533 */
SEXP >> 3u; /* 4533 */
u16a << SEXP; /* 4534 */
u16a >> SEXP; /* 4534 */
SEXP < 35; /* */
SEXP <= 35; /* */
SEXP >= 35; /* */
SEXP > 35; /* */
35 < SEXP; /* */
35 <= SEXP; /* */
35 >= SEXP; /* */
35 > SEXP; /* */
SEXP == 35; /* */
SEXP != 35; /* */
35 == SEXP; /* */
35 != SEXP; /* */
++sia; /* */
--sia; /* */
sia++; /* */
sia--; /* */
!SEXP; /* 4538 */
SEXP && bla; /* 4538 */
SEXP || bla; /* 4538 */
bla && SEXP; /* 4538 */
bla || SEXP; /* 4538 */
SEXP ? u16a : u16b; /* 4539 */
(u16a > u16b) ? SEXP : sib; /* */
/**************************/
buf[3]; /* */
3 + u16a; /* */
3 - u16a; /* */
3 * u16a; /* */
3 / u16a; /* */
3 % u16a; /* */
u16a + 3; /* */
u16a - 3; /* */
u16a * 3; /* */
u16a / 3; /* */
u16a % 3; /* */
~3; /* 4542 */
3 & u16a; /* 4542 */
3 | u16a; /* 4542 */
3 ^ u16a; /* 4542 */
u16a & 3; /* 4542 */
u16a | 3; /* 4542 */
u16a ^ 3; /* 4542 */
3 << 0u; /* 4543 */
3 << 1u; /* 4543 */
3 << 3u; /* 4543 */
3 >> 0u; /* 4543 */
3 >> 1u; /* 4543 */
3 >> 3u; /* 4543 */
u16a << 3; /* 4544 */
u16a >> 3; /* 4544 */
3 < 35; /* */
3 <= 35; /* */
3 >= 35; /* */
3 > 35; /* */
35 < 3; /* */
35 <= 3; /* */
35 >= 3; /* */
35 > 3; /* */
3 == 35; /* */
3 != 35; /* */
35 == 3; /* */
35 != 3; /* */
++sia; /* */
--sia; /* */
sia++; /* */
sia--; /* */
!3; /* 4548 */
3 && bla; /* 4548 */
3 || bla; /* 4548 */
bla && 3; /* 4548 */
bla || 3; /* 4548 */
3 ? u16a : u16b; /* 4549 */
(u16a > u16b) ? 3 : sib; /* */
return 1;
}
Example Code:
/************************************************************************************
* MISRA-C3 Rule 10.1 Requirement
* ==============================
*
* Bool char enum sgnd unsd fltg
* ----- ----- ----- ----- ----- -----
* [ ] X X - - - X
* + unary X X X - - -
* - unary X X X - X -
* + - either X - X - - -
* * / either X X X - - -
* % either X X X - - X
* < <= >= > either X - - - - -
* == != either - - - - - -
* ! && || any - X X X X X
* << >> left X X X X - X
* << >> right X X X X - X
* ~ & | ^ any X X X X - X
* ? : 1 - X X X X X
* ? : 2 and 3 - - - - - -
*
************************************************************************************/
#pragma PRQA_MESSAGES_OFF 1056,1820,1821,1830,1831,1840,1841,2790,2830,2832,2860,2890,2912,2985,3112,3322,3344,3377,4571
#include "misra.h"
#include "m3cmex.h"
extern int16_t rule_1001_unsigned( void )
{
int16_t buf[300] = {0};
buf[u16b]; /* */
+u16b; /* */
-u16b; /* 3101 */
-u8b; /* 3102 */
-(u8a + u8b); /* 3102 */
u16b + u16a; /* */
u16b - u16a; /* */
u16b * u16a; /* */
u16b / u16a; /* */
u16b % u16a; /* */
u16a + u16b; /* */
u16a - u16b; /* */
u16a * u16b; /* */
u16a / u16b; /* */
u16a % u16b; /* */
u16b & u16a; /* */
u16b | u16a; /* */
u16b ^ u16a; /* */
u16a & u16b; /* */
u16a | u16b; /* */
u16a ^ u16b; /* */
u16b << 0u; /* */
u16b << 1u; /* */
u16b << 3u; /* */
u16b >> 0u; /* */
u16b >> 1u; /* */
u16b >> 3u; /* */
u16a << u16b; /* */
u16a >> u16b; /* */
u16b < 35U; /* */
u16b <= 35U; /* */
u16b >= 35U; /* */
u16b > 35U; /* */
35U < u16b; /* */
35U <= u16b; /* */
35U >= u16b; /* */
35U > u16b; /* */
u16b == 35U; /* */
u16b != 35U; /* */
35U == u16b; /* */
35U != u16b; /* */
++u16a; /* */
--u16a; /* */
u16a++; /* */
u16a--; /* */
u16b && bla; /* 4558 */
u16b || bla; /* 4558 */
bla && u16b; /* 4558 */
bla || u16b; /* 4558 */
u16b ? u16a : u16b; /* 4559 */
(u16a > u16b) ? u16b : u16a; /* */
return 1;
}
QAC messages that encompass this guideline:
| 3101 | Unary '-' applied to an operand of type unsigned int or unsigned long gives an unsigned result. |
| 3102 | Unary '-' applied to an operand whose underlying type is unsigned. |
| 4500 | An expression of 'essentially Boolean' type (%1s) is being used as an array subscript. |
| 4501 | An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s). |
| 4502 | An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this bitwise operator (%3s). |
| 4503 | An expression of 'essentially Boolean' type (%1s) is being used as the left-hand operand of this shift operator (%2s). |
| 4504 | An expression of 'essentially Boolean' type (%1s) is being used as the right-hand operand of this shift operator (%2s). |
| 4505 | An expression of 'essentially Boolean' type (%1s) is being used as the %2s operand of this relational operator (%3s). |
| 4507 | An expression of 'essentially Boolean' type (%1s) is being used as the operand of this increment/decrement operator (%2s). |
| 4510 | An expression of 'essentially character' type (%1s) is being used as an array subscript. |
| 4511 | An expression of 'essentially character' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s). |
| 4512 | An expression of 'essentially character' type (%1s) is being used as the %2s operand of this bitwise operator (%3s). |
| 4513 | An expression of 'essentially character' type (%1s) is being used as the left-hand operand of this shift operator (%2s). |
| 4514 | An expression of 'essentially character' type (%1s) is being used as the right-hand operand of this shift operator (%2s). |
| 4518 | An expression of 'essentially character' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4519 | An expression of 'essentially character' type (%1s) is being used as the first operand of this conditional operator (%2s). |
| 4521 | An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this arithmetic operator (%3s). |
| 4522 | An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this bitwise operator (%3s). |
| 4523 | An expression of 'essentially enum' type (%1s) is being used as the left-hand operand of this shift operator (%2s). |
| 4524 | An expression of 'essentially enum' type (%1s) is being used as the right-hand operand of this shift operator (%2s). |
| 4527 | An expression of 'essentially enum' type is being used as the operand of this increment/decrement operator. |
| 4528 | An expression of 'essentially enum' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4529 | An expression of 'essentially enum' type (%1s) is being used as the first operand of this conditional operator (%2s). |
| 4532 | An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s). |
| 4533 | An expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s). |
| 4534 | An expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s). |
| 4538 | An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4539 | An expression of 'essentially signed' type (%1s) is being used as the first operand of this conditional operator (%2s). |
| 4542 | A non-negative constant expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s). |
| 4543 | A non-negative constant expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s). |
| 4544 | A non-negative constant expression of 'essentially signed' type (%1s) is being used as the right-hand operand of this shift operator (%2s). |
| 4548 | A non-negative constant expression of 'essentially signed' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4549 | A non-negative constant expression of 'essentially signed' type (%1s) is being used as the first operand of this conditional operator (%2s). |
| 4558 | An expression of 'essentially unsigned' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4559 | An expression of 'essentially unsigned' type (%1s) is being used as the first operand of this conditional operator (%2s). |
| 4568 | An expression of 'essentially floating' type (%1s) is being used as the %2s operand of this logical operator (%3s). |
| 4569 | An expression of 'essentially floating' type (%1s) is being used as the first operand of this conditional operator (%2s). |
|
||
(c) The Motor Industry Research Association, 2012
|
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0 © 2013 Programming Research www.programmingresearch.com |
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |