Misra: Add rule 11.3

This commit is contained in:
Daniel Marjamäki 2017-04-16 12:13:30 +02:00
parent a92ce98cab
commit 85a6e9ce59
3 changed files with 38 additions and 18 deletions

View File

@ -69,6 +69,11 @@ class ValueType:
def setId(self, IdMap): def setId(self, IdMap):
self.typeScope = IdMap[self.typeScopeId] self.typeScope = IdMap[self.typeScopeId]
def isIntegral(self):
return self.type == 'bool' or self.type == 'char' or self.type == 'short' or self.type == 'int' or self.type == 'long' or self.type == 'long long'
def isFloat(self):
return self.type == 'float' or self.type == 'double' or self.type == 'long double'
class Token: class Token:
""" """

View File

@ -24,6 +24,10 @@ void misra_7_3() {
int x = 12lu; // 7.3 int x = 12lu; // 7.3
} }
void misra_11_3(u8* p) {
x = (u64*)p; // 11.3
}
void misra_11_4(u8*p) { void misra_11_4(u8*p) {
u64 y = (u64)p; // 11.4 u64 y = (u64)p; // 11.4
} }

View File

@ -247,24 +247,16 @@ def misra_7_3(rawTokens):
if re.match(r'^[0-9]+l', tok.str): if re.match(r'^[0-9]+l', tok.str):
reportError(tok, 7, 3) reportError(tok, 7, 3)
def misra_11_3(data):
def misra_12_1_sizeof(rawTokens): for token in data.tokenlist:
state = 0 if not isCast(token):
for tok in rawTokens: continue
if tok.str.startswith('//') or tok.str.startswith('/*'): vt1 = token.valueType
continue vt2 = token.astOperand1.valueType
if tok.str == 'sizeof': if not vt1 or not vt2:
state = 1 continue
elif state == 1: if vt1.pointer==vt2.pointer and vt1.pointer>0 and vt1.type != vt2.type and vt1.isIntegral() and vt2.isIntegral():
if re.match(r'^[a-zA-Z_]',tok.str): reportError(token, 11, 3)
state = 2
else:
state = 0
elif state == 2:
if tok.str in ['+','-','*','/','%']:
reportError(tok, 12, 1)
else:
state = 0
def misra_11_4(data): def misra_11_4(data):
for token in data.tokenlist: for token in data.tokenlist:
@ -335,6 +327,24 @@ def misra_11_9(data):
if value == '((void*)0)': if value == '((void*)0)':
reportError(directive, 11, 9) reportError(directive, 11, 9)
def misra_12_1_sizeof(rawTokens):
state = 0
for tok in rawTokens:
if tok.str.startswith('//') or tok.str.startswith('/*'):
continue
if tok.str == 'sizeof':
state = 1
elif state == 1:
if re.match(r'^[a-zA-Z_]',tok.str):
state = 2
else:
state = 0
elif state == 2:
if tok.str in ['+','-','*','/','%']:
reportError(tok, 12, 1)
else:
state = 0
def misra_12_1(data): def misra_12_1(data):
for token in data.tokenlist: for token in data.tokenlist:
p = getPrecedence(token) p = getPrecedence(token)
@ -776,6 +786,7 @@ for arg in sys.argv[1:]:
if cfgNumber == 1: if cfgNumber == 1:
misra_7_1(data.rawTokens) misra_7_1(data.rawTokens)
misra_7_3(data.rawTokens) misra_7_3(data.rawTokens)
misra_11_3(cfg)
misra_11_4(cfg) misra_11_4(cfg)
misra_11_5(cfg) misra_11_5(cfg)
misra_11_6(cfg) misra_11_6(cfg)