MISRA rule 10.2 Expressions of essentially character type in additions and subtraction (#2897)

This commit is contained in:
Lars Even Almaas 2020-11-12 11:37:28 +01:00 committed by GitHub
parent 324e267559
commit 3a91b998d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 0 deletions

View File

@ -1523,6 +1523,41 @@ class MisraChecker:
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:
@ -3077,6 +3112,7 @@ class MisraChecker:
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)

View File

@ -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) {