misra; implement rule 16.1

This commit is contained in:
Daniel Marjamäki 2021-07-20 20:40:42 +02:00
parent e72e59f934
commit b409d4a598
2 changed files with 35 additions and 1 deletions

View File

@ -2696,7 +2696,27 @@ class MisraChecker:
if not simpleMatch(tok, '} else'):
self.reportError(tok, 15, 7)
# TODO add 16.1 rule
def misra_16_1(self, cfg):
for scope in cfg.scopes:
if scope.type != 'Switch':
continue
in_case_or_default = False
tok = scope.bodyStart.next
while tok != scope.bodyEnd:
if not in_case_or_default:
if tok.str not in ('case', 'default'):
self.reportError(tok, 16, 1)
else:
in_case_or_default = True
else:
if simpleMatch(tok, 'break ;'):
in_case_or_default = False
tok = tok.next
if tok.str == '{':
tok = tok.link
if tok.scope.type == 'Unconditional' and simpleMatch(tok.previous.previous, 'break ;'):
in_case_or_default = False
tok = tok.next
def misra_16_2(self, data):
for token in data.tokenlist:
@ -3749,6 +3769,7 @@ class MisraChecker:
if cfgNumber == 0:
self.executeCheck(1506, self.misra_15_6, data.rawTokens)
self.executeCheck(1507, self.misra_15_7, cfg)
self.executeCheck(1601, self.misra_16_1, cfg)
self.executeCheck(1602, self.misra_16_2, cfg)
if cfgNumber == 0:
self.executeCheck(1603, self.misra_16_3, data.rawTokens)

View File

@ -140,6 +140,7 @@ static void foo(void)
}
} while(misra_5_2_func2()); //14.4
}
break;
}
}
@ -229,6 +230,7 @@ static void misra_5_5_func1(void)
}
} while(misra_5_5_func2()); //14.4
}
break;
}
}
@ -1246,6 +1248,17 @@ static void misra_15_7(void) {
if (a==2) {} else { if (b==4) {} } // no-warning
}
static void misra_16_1(int32_t i) {
switch (i) {
int8_t x; // 16.1
default: // 16.3 16.5
break;
if (i != 18) {} // 16.1
case 1: // 16.3
break;
}
}
static void misra_16_2(void) {
switch (x) {
default: