Misra: Added checker for misra rule 17.7

This commit is contained in:
Daniel Marjamäki 2019-05-22 21:39:33 +02:00
parent 149c07f475
commit 54be0bc3cf
3 changed files with 31 additions and 8 deletions

View File

@ -299,6 +299,7 @@ class Scope:
nestedInId = None
nestedIn = None
type = None
isExecutable = None
def __init__(self, element):
self.Id = element.get('id')
@ -310,6 +311,7 @@ class Scope:
self.nestedInId = element.get('nestedIn')
self.nestedIn = None
self.type = element.get('type')
self.isExecutable = (self.type in ('Function', 'If', 'Else', 'For', 'While', 'Do', 'Switch', 'Try', 'Catch', 'Unconditional', 'Lambda'))
def setId(self, IdMap):
self.bodyStart = IdMap[self.bodyStartId]

View File

@ -1528,6 +1528,21 @@ class MisraChecker:
self.reportError(token, 17, 6)
def misra_17_7(self, data):
for token in data.tokenlist:
if not token.scope.isExecutable:
continue
if token.str != '(' or token.astParent:
continue
if not token.previous.isName or token.previous.varId:
continue
if token.valueType is None:
continue
if token.valueType.type == 'void' and token.valueType.pointer == 0:
continue
self.reportError(token, 17, 7)
def misra_17_8(self, data):
for token in data.tokenlist:
if not (token.isAssignmentOp or (token.str in {'++', '--'})):
@ -2186,6 +2201,7 @@ class MisraChecker:
self.misra_17_1(cfg)
if cfgNumber == 1:
self.misra_17_6(data.rawTokens)
self.misra_17_7(cfg)
self.misra_17_8(cfg)
self.misra_18_5(cfg)
self.misra_18_8(cfg)

View File

@ -219,7 +219,7 @@ void misra_11_7(int *p, float f) {
}
char * misra_11_8(const char *str) {
misra_11_8(str); // no-warning
(void)misra_11_8(str); // no-warning
return (char *)str; // 11.8
}
@ -491,6 +491,11 @@ void misra_17_1() {
void misra_17_6(int x[static 20]) {} // 17.6
int calculation(int x) { return x + 1; }
void misra_17_7(void) {
calculation(123); // 17.7
}
void misra_17_8(int x) {
x = 3; // 17.8
}
@ -536,20 +541,20 @@ void misra_21_3() {
}
void misra_21_7() {
atof(str); // 21.7
atoi(str); // 21.7
atol(str); // 21.7
atoll(str); // 21.7
(void)atof(str); // 21.7
(void)atoi(str); // 21.7
(void)atol(str); // 21.7
(void)atoll(str); // 21.7
}
void misra_21_8() {
abort(); // 21.8
getenv("foo"); // 21.8
system(""); // 21.8
(void)getenv("foo"); // 21.8
(void)system(""); // 21.8
exit(-1); // 21.8
}
void misra_21_9() {
bsearch(key,base,num,size,cmp); // 21.9
(void)bsearch(key,base,num,size,cmp); // 21.9
qsort(base,num,size,cmp); // 21.9
}