Skip to content

Commit

Permalink
MISRA rule 10.2 Expressions of essentially character type in addition…
Browse files Browse the repository at this point in the history
…s and subtraction (danmar#2897)
  • Loading branch information
larseven authored Nov 12, 2020
1 parent 324e267 commit 3a91b99
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
36 changes: 36 additions & 0 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,41 @@ def misra_10_1(self, data):
if e1_et == 'char' and e2_et == 'char':
self.reportError(token, 10, 1)

def misra_10_2(self, data):
def isEssentiallySignedOrUnsigned(op):
if op and op.valueType:
if op.valueType.sign in ['unsigned', 'signed']:
return True
return False

def isEssentiallyChar(op):
if op.isName:
return getEssentialType(op) == 'char'
return op.isChar

for token in data.tokenlist:
if not token.isArithmeticalOp or token.str not in ['+', '-']:
continue

operand1 = token.astOperand1
operand2 = token.astOperand2
if not operand1 or not operand2:
continue
if not operand1.isChar and not operand2.isChar:
continue

if token.str == '+':
if isEssentiallyChar(operand1) and not isEssentiallySignedOrUnsigned(operand2):
self.reportError(token, 10, 2)
if isEssentiallyChar(operand2) and not isEssentiallySignedOrUnsigned(operand1):
self.reportError(token, 10, 2)

if token.str == '-':
if not isEssentiallyChar(operand1):
self.reportError(token, 10, 2)
if not isEssentiallyChar(operand2) and not isEssentiallySignedOrUnsigned(operand2):
self.reportError(token, 10, 2)

def misra_10_4(self, data):
op = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'}
for token in data.tokenlist:
Expand Down Expand Up @@ -3077,6 +3112,7 @@ def parseDump(self, dumpfile):
self.executeCheck(814, self.misra_8_14, data.rawTokens)
self.executeCheck(905, self.misra_9_5, data.rawTokens)
self.executeCheck(1001, self.misra_10_1, cfg)
self.executeCheck(1002, self.misra_10_2, cfg)
self.executeCheck(1004, self.misra_10_4, cfg)
self.executeCheck(1006, self.misra_10_6, cfg)
self.executeCheck(1008, self.misra_10_8, cfg)
Expand Down
17 changes: 17 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,24 @@ void misra_10_1_ternary()
a = (get_bool(42) ? (get_bool(34) ? ui16 : ui8) : ui8) << (get_bool(19) ? ui16 : ui8); // 10.4
a = (get_bool(42) ? (get_bool(34) ? i16 : ui8) : ui8) << (get_bool(19) ? ui16 : ui8); // 10.1 10.4
a = (get_bool(42) ? (get_bool(34) ? ui16 : ui8) : ui8) << (get_bool(19) ? i16 : ui8); // 10.1 10.4
}

void misra_10_2() {
unsigned int u8a = 0;
signed char cha = 0;
signed int s8a = 0;
signed short s16a = 0;
float f32a = 0.0;
char res;

res = '0' + u8a; // 10.4
res = s8a + '0';
res = cha - '0';
res = '0' - s8a;
res = cha + ':';

res = s16a - 'a'; // 10.2
res = '0' + f32a; // 10.2 10.4
}

void misra_10_4(u32 x, s32 y) {
Expand Down

0 comments on commit 3a91b99

Please sign in to comment.