misra: python refactorings
This commit is contained in:
parent
0edc4feb57
commit
8c7ae14f0b
@ -102,9 +102,9 @@ def getEssentialTypeCategory(expr):
|
|||||||
typeToken = expr.variable.typeStartToken
|
typeToken = expr.variable.typeStartToken
|
||||||
while typeToken:
|
while typeToken:
|
||||||
if typeToken.valueType:
|
if typeToken.valueType:
|
||||||
if typeToken.valueType.type in {'bool'}:
|
if typeToken.valueType.type == 'bool':
|
||||||
return typeToken.valueType.type
|
return typeToken.valueType.type
|
||||||
if typeToken.valueType.type in {'float', 'double', 'long double'}:
|
if typeToken.valueType.type in ('float', 'double', 'long double'):
|
||||||
return "float"
|
return "float"
|
||||||
if typeToken.valueType.sign:
|
if typeToken.valueType.sign:
|
||||||
return typeToken.valueType.sign
|
return typeToken.valueType.sign
|
||||||
@ -117,8 +117,8 @@ def getEssentialTypeCategory(expr):
|
|||||||
def getEssentialCategorylist(operand1, operand2):
|
def getEssentialCategorylist(operand1, operand2):
|
||||||
if not operand1 or not operand2:
|
if not operand1 or not operand2:
|
||||||
return None, None
|
return None, None
|
||||||
if (operand1.str in {'++', '--'} or
|
if (operand1.str in ('++', '--') or
|
||||||
operand2.str in {'++', '--'}):
|
operand2.str in ('++', '--')):
|
||||||
return None, None
|
return None, None
|
||||||
if ((operand1.valueType and operand1.valueType.pointer) or
|
if ((operand1.valueType and operand1.valueType.pointer) or
|
||||||
(operand2.valueType and operand2.valueType.pointer)):
|
(operand2.valueType and operand2.valueType.pointer)):
|
||||||
@ -134,11 +134,11 @@ def getEssentialType(expr):
|
|||||||
if expr.variable:
|
if expr.variable:
|
||||||
typeToken = expr.variable.typeStartToken
|
typeToken = expr.variable.typeStartToken
|
||||||
while typeToken and typeToken.isName:
|
while typeToken and typeToken.isName:
|
||||||
if typeToken.str in {'char', 'short', 'int', 'long', 'float', 'double'}:
|
if typeToken.str in ('char', 'short', 'int', 'long', 'float', 'double'):
|
||||||
return typeToken.str
|
return typeToken.str
|
||||||
typeToken = typeToken.next
|
typeToken = typeToken.next
|
||||||
|
|
||||||
elif expr.astOperand1 and expr.astOperand2 and expr.str in {'+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":"}:
|
elif expr.astOperand1 and expr.astOperand2 and expr.str in ('+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":"):
|
||||||
if expr.astOperand1.valueType and expr.astOperand1.valueType.pointer > 0:
|
if expr.astOperand1.valueType and expr.astOperand1.valueType.pointer > 0:
|
||||||
return None
|
return None
|
||||||
if expr.astOperand2.valueType and expr.astOperand2.valueType.pointer > 0:
|
if expr.astOperand2.valueType and expr.astOperand2.valueType.pointer > 0:
|
||||||
@ -205,10 +205,10 @@ def hasExternalLinkage(var):
|
|||||||
|
|
||||||
|
|
||||||
def countSideEffects(expr):
|
def countSideEffects(expr):
|
||||||
if not expr or expr.str in {',', ';'}:
|
if not expr or expr.str in (',', ';'):
|
||||||
return 0
|
return 0
|
||||||
ret = 0
|
ret = 0
|
||||||
if expr.str in {'++', '--', '='}:
|
if expr.str in ('++', '--', '='):
|
||||||
ret = 1
|
ret = 1
|
||||||
return ret + countSideEffects(expr.astOperand1) + countSideEffects(expr.astOperand2)
|
return ret + countSideEffects(expr.astOperand1) + countSideEffects(expr.astOperand2)
|
||||||
|
|
||||||
@ -270,7 +270,7 @@ def isFloatCounterInWhileLoop(whileToken):
|
|||||||
continue
|
continue
|
||||||
if token.isAssignmentOp and token.astOperand1.str == counterToken.str:
|
if token.isAssignmentOp and token.astOperand1.str == counterToken.str:
|
||||||
return True
|
return True
|
||||||
if token.str == counterToken.str and token.astParent and token.astParent.str in {'++', '--'}:
|
if token.str == counterToken.str and token.astParent and token.astParent.str in ('++', '--'):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -288,7 +288,7 @@ def hasSideEffectsRecursive(expr):
|
|||||||
e = e.astOperand1
|
e = e.astOperand1
|
||||||
if e and e.str == '.':
|
if e and e.str == '.':
|
||||||
return False
|
return False
|
||||||
if expr.str in {'++', '--', '='}:
|
if expr.str in ('++', '--', '='):
|
||||||
return True
|
return True
|
||||||
# Todo: Check function calls
|
# Todo: Check function calls
|
||||||
return hasSideEffectsRecursive(expr.astOperand1) or hasSideEffectsRecursive(expr.astOperand2)
|
return hasSideEffectsRecursive(expr.astOperand1) or hasSideEffectsRecursive(expr.astOperand2)
|
||||||
@ -322,7 +322,7 @@ def isUnsignedInt(expr):
|
|||||||
return False
|
return False
|
||||||
if expr.isNumber:
|
if expr.isNumber:
|
||||||
return 'u' in expr.str or 'U' in expr.str
|
return 'u' in expr.str or 'U' in expr.str
|
||||||
if expr.str in {'+', '-', '*', '/', '%'}:
|
if expr.str in ('+', '-', '*', '/', '%'):
|
||||||
return isUnsignedInt(expr.astOperand1) or isUnsignedInt(expr.astOperand2)
|
return isUnsignedInt(expr.astOperand1) or isUnsignedInt(expr.astOperand2)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -332,15 +332,15 @@ def getPrecedence(expr):
|
|||||||
return 16
|
return 16
|
||||||
if not expr.astOperand1 or not expr.astOperand2:
|
if not expr.astOperand1 or not expr.astOperand2:
|
||||||
return 16
|
return 16
|
||||||
if expr.str in {'*', '/', '%'}:
|
if expr.str in ('*', '/', '%'):
|
||||||
return 12
|
return 12
|
||||||
if expr.str in {'+', '-'}:
|
if expr.str in ('+', '-'):
|
||||||
return 11
|
return 11
|
||||||
if expr.str in {'<<', '>>'}:
|
if expr.str in ('<<', '>>'):
|
||||||
return 10
|
return 10
|
||||||
if expr.str in {'<', '>', '<=', '>='}:
|
if expr.str in ('<', '>', '<=', '>='):
|
||||||
return 9
|
return 9
|
||||||
if expr.str in {'==', '!='}:
|
if expr.str in ('==', '!='):
|
||||||
return 8
|
return 8
|
||||||
if expr.str == '&':
|
if expr.str == '&':
|
||||||
return 7
|
return 7
|
||||||
@ -352,7 +352,7 @@ def getPrecedence(expr):
|
|||||||
return 4
|
return 4
|
||||||
if expr.str == '||':
|
if expr.str == '||':
|
||||||
return 3
|
return 3
|
||||||
if expr.str in {'?', ':'}:
|
if expr.str in ('?', ':'):
|
||||||
return 2
|
return 2
|
||||||
if expr.isAssignmentOp:
|
if expr.isAssignmentOp:
|
||||||
return 1
|
return 1
|
||||||
@ -1005,7 +1005,7 @@ class MisraChecker:
|
|||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if token.str != '=' or not token.astOperand1 or not token.astOperand2:
|
if token.str != '=' or not token.astOperand1 or not token.astOperand2:
|
||||||
continue
|
continue
|
||||||
if (token.astOperand2.str not in {'+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~'} and
|
if (token.astOperand2.str not in ('+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~') and
|
||||||
not isCast(token.astOperand2)):
|
not isCast(token.astOperand2)):
|
||||||
continue
|
continue
|
||||||
vt1 = token.astOperand1.valueType
|
vt1 = token.astOperand1.valueType
|
||||||
@ -1039,7 +1039,7 @@ class MisraChecker:
|
|||||||
continue
|
continue
|
||||||
if not token.astOperand1.astOperand1:
|
if not token.astOperand1.astOperand1:
|
||||||
continue
|
continue
|
||||||
if token.astOperand1.str not in {'+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~'}:
|
if token.astOperand1.str not in ('+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~'):
|
||||||
continue
|
continue
|
||||||
if token.astOperand1.str != '~' and not token.astOperand1.astOperand2:
|
if token.astOperand1.str != '~' and not token.astOperand1.astOperand2:
|
||||||
continue
|
continue
|
||||||
@ -1110,7 +1110,7 @@ class MisraChecker:
|
|||||||
if vt1.pointer > 0 and vt1.type != 'void' and vt2.pointer == vt1.pointer and vt2.type == 'void':
|
if vt1.pointer > 0 and vt1.type != 'void' and vt2.pointer == vt1.pointer and vt2.type == 'void':
|
||||||
self.reportError(token, 11, 5)
|
self.reportError(token, 11, 5)
|
||||||
continue
|
continue
|
||||||
if token.astOperand1.astOperand1 and token.astOperand1.astOperand1.str in {'malloc', 'calloc', 'realloc', 'free'}:
|
if token.astOperand1.astOperand1 and token.astOperand1.astOperand1.str in ('malloc', 'calloc', 'realloc', 'free'):
|
||||||
continue
|
continue
|
||||||
vt1 = token.valueType
|
vt1 = token.valueType
|
||||||
vt2 = token.astOperand1.valueType
|
vt2 = token.astOperand1.valueType
|
||||||
@ -1223,7 +1223,7 @@ class MisraChecker:
|
|||||||
else:
|
else:
|
||||||
state = 0
|
state = 0
|
||||||
elif state == 2:
|
elif state == 2:
|
||||||
if tok.str in {'+', '-', '*', '/', '%'}:
|
if tok.str in ('+', '-', '*', '/', '%'):
|
||||||
self.reportError(tok, 12, 1)
|
self.reportError(tok, 12, 1)
|
||||||
else:
|
else:
|
||||||
state = 0
|
state = 0
|
||||||
@ -1246,7 +1246,7 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_12_2(self, data):
|
def misra_12_2(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if not (token.str in {'<<', '>>'}):
|
if not (token.str in ('<<', '>>')):
|
||||||
continue
|
continue
|
||||||
if (not token.astOperand2) or (not token.astOperand2.values):
|
if (not token.astOperand2) or (not token.astOperand2.values):
|
||||||
continue
|
continue
|
||||||
@ -1303,10 +1303,10 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_13_3(self, data):
|
def misra_13_3(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if token.str not in {'++', '--'}:
|
if token.str not in ('++', '--'):
|
||||||
continue
|
continue
|
||||||
astTop = token
|
astTop = token
|
||||||
while astTop.astParent and astTop.astParent.str not in {',', ';'}:
|
while astTop.astParent and astTop.astParent.str not in (',', ';'):
|
||||||
astTop = astTop.astParent
|
astTop = astTop.astParent
|
||||||
if countSideEffects(astTop) >= 2:
|
if countSideEffects(astTop) >= 2:
|
||||||
self.reportError(astTop, 13, 3)
|
self.reportError(astTop, 13, 3)
|
||||||
@ -1318,7 +1318,7 @@ class MisraChecker:
|
|||||||
continue
|
continue
|
||||||
if not token.astParent:
|
if not token.astParent:
|
||||||
continue
|
continue
|
||||||
if token.astOperand1.str == '[' and token.astOperand1.previous.str in {'{', ','}:
|
if token.astOperand1.str == '[' and token.astOperand1.previous.str in ('{', ','):
|
||||||
continue
|
continue
|
||||||
if not (token.astParent.str in [',', ';', '{']):
|
if not (token.astParent.str in [',', ';', '{']):
|
||||||
self.reportError(token, 13, 4)
|
self.reportError(token, 13, 4)
|
||||||
@ -1538,7 +1538,7 @@ class MisraChecker:
|
|||||||
continue
|
continue
|
||||||
tok2 = token
|
tok2 = token
|
||||||
while tok2:
|
while tok2:
|
||||||
if tok2.str in {'}', 'case'}:
|
if tok2.str in ('}', 'case'):
|
||||||
break
|
break
|
||||||
if tok2.str == '{':
|
if tok2.str == '{':
|
||||||
tok2 = tok2.link
|
tok2 = tok2.link
|
||||||
@ -1575,7 +1575,7 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_17_1(self, data):
|
def misra_17_1(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if isFunctionCall(token) and token.astOperand1.str in {'va_list', 'va_arg', 'va_start', 'va_end', 'va_copy'}:
|
if isFunctionCall(token) and token.astOperand1.str in ('va_list', 'va_arg', 'va_start', 'va_end', 'va_copy'):
|
||||||
self.reportError(token, 17, 1)
|
self.reportError(token, 17, 1)
|
||||||
elif token.str == 'va_list':
|
elif token.str == 'va_list':
|
||||||
self.reportError(token, 17, 1)
|
self.reportError(token, 17, 1)
|
||||||
@ -1652,7 +1652,7 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_17_8(self, data):
|
def misra_17_8(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if not (token.isAssignmentOp or (token.str in {'++', '--'})):
|
if not (token.isAssignmentOp or (token.str in ('++', '--'))):
|
||||||
continue
|
continue
|
||||||
if not token.astOperand1:
|
if not token.astOperand1:
|
||||||
continue
|
continue
|
||||||
@ -1741,7 +1741,7 @@ class MisraChecker:
|
|||||||
for directive in data.directives:
|
for directive in data.directives:
|
||||||
if not directive.str.startswith('#include '):
|
if not directive.str.startswith('#include '):
|
||||||
continue
|
continue
|
||||||
for pattern in {'\\', '//', '/*', "'"}:
|
for pattern in ('\\', '//', '/*', "'"):
|
||||||
if pattern in directive.str:
|
if pattern in directive.str:
|
||||||
self.reportError(directive, 20, 2)
|
self.reportError(directive, 20, 2)
|
||||||
break
|
break
|
||||||
@ -1848,7 +1848,7 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_21_3(self, data):
|
def misra_21_3(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if isFunctionCall(token) and (token.astOperand1.str in {'malloc', 'calloc', 'realloc', 'free'}):
|
if isFunctionCall(token) and (token.astOperand1.str in ('malloc', 'calloc', 'realloc', 'free')):
|
||||||
self.reportError(token, 21, 3)
|
self.reportError(token, 21, 3)
|
||||||
|
|
||||||
|
|
||||||
@ -1875,19 +1875,19 @@ class MisraChecker:
|
|||||||
|
|
||||||
def misra_21_7(self, data):
|
def misra_21_7(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if isFunctionCall(token) and (token.astOperand1.str in {'atof', 'atoi', 'atol', 'atoll'}):
|
if isFunctionCall(token) and (token.astOperand1.str in ('atof', 'atoi', 'atol', 'atoll')):
|
||||||
self.reportError(token, 21, 7)
|
self.reportError(token, 21, 7)
|
||||||
|
|
||||||
|
|
||||||
def misra_21_8(self, data):
|
def misra_21_8(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if isFunctionCall(token) and (token.astOperand1.str in {'abort', 'exit', 'getenv', 'system'}):
|
if isFunctionCall(token) and (token.astOperand1.str in ('abort', 'exit', 'getenv', 'system')):
|
||||||
self.reportError(token, 21, 8)
|
self.reportError(token, 21, 8)
|
||||||
|
|
||||||
|
|
||||||
def misra_21_9(self, data):
|
def misra_21_9(self, data):
|
||||||
for token in data.tokenlist:
|
for token in data.tokenlist:
|
||||||
if (token.str in {'bsearch', 'qsort'}) and token.next and token.next.str == '(':
|
if (token.str in ('bsearch', 'qsort')) and token.next and token.next.str == '(':
|
||||||
self.reportError(token, 21, 9)
|
self.reportError(token, 21, 9)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user