Misra: Added rule 13.3

This commit is contained in:
Daniel Marjamäki 2017-04-13 19:38:25 +02:00
parent 5b0ee55964
commit e7483e1b2a
2 changed files with 37 additions and 0 deletions

View File

@ -44,6 +44,10 @@ void misra_13_1(int *p) {
int b[2] = {v,1}; // TODO int b[2] = {v,1}; // TODO
} }
void misra_13_3() {
x = y++; // 13.3
}
void misra_13_5() { void misra_13_5() {
if (x && (y++ < 123)){} // 13.5 if (x && (y++ < 123)){} // 13.5
} }

View File

@ -49,6 +49,27 @@ def bitsOfEssentialType(expr):
return INT_BITS return INT_BITS
return 0 return 0
def isFunctionCall(expr):
if not expr:
return False
if expr.str != '(' or not expr.astOperand1:
return False
if expr.astOperand1 != expr.previous:
return False
if expr.astOperand1.str in ['sizeof', 'if', 'switch', 'while']:
return False
return True
def countSideEffects(expr):
if not expr or expr.str in [',', ';']:
return 0
ret = 0
if expr.str in ['++', '--', '=']:
ret = 1
elif isFunctionCall(expr):
ret = 1
return ret + countSideEffects(expr.astOperand1) + countSideEffects(expr.astOperand2)
def hasSideEffectsRecursive(expr): def hasSideEffectsRecursive(expr):
if not expr: if not expr:
return False return False
@ -222,6 +243,17 @@ def misra_13_1(data):
if init and init.str == '{' and hasSideEffectsRecursive(init): if init and init.str == '{' and hasSideEffectsRecursive(init):
reportError(init,13,1) reportError(init,13,1)
def misra_13_3(data):
for token in data.tokenlist:
if not token.astOperand1:
continue
if token.astParent and token.astParent.str != ',':
continue
if token.str == ',':
continue
if countSideEffects(token) >= 2:
reportError(token, 13, 3)
def misra_13_5(data): def misra_13_5(data):
for token in data.tokenlist: for token in data.tokenlist:
if token.isLogicalOp and hasSideEffectsRecursive(token.astOperand2): if token.isLogicalOp and hasSideEffectsRecursive(token.astOperand2):
@ -264,6 +296,7 @@ for arg in sys.argv[1:]:
misra_12_3(cfg) misra_12_3(cfg)
misra_12_4(cfg) misra_12_4(cfg)
misra_13_1(cfg) misra_13_1(cfg)
misra_13_3(cfg)
misra_13_5(cfg) misra_13_5(cfg)
misra_14_4(cfg) misra_14_4(cfg)
misra_15_1(cfg) misra_15_1(cfg)