misra.py: Check switch-clauses in R15.3 (#2538)

* misra.py: Check switch-clauses in R15.3

* break on violation
This commit is contained in:
Georgy Komarov 2020-02-16 02:03:18 +03:00 committed by GitHub
parent 320cb9008f
commit 9155f3a83a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -1877,6 +1877,26 @@ class MisraChecker:
scope = scope.nestedIn
if not scope:
self.reportError(token, 15, 3)
# Jump crosses from one switch-clause to another is non-compliant
elif scope.type == 'Switch':
# Search for start of a current case block
tcase_start = token
while tcase_start and tcase_start.str not in ('case', 'default'):
tcase_start = tcase_start.previous
# Make sure that goto label doesn't occurs in the other
# switch-clauses
if tcase_start:
t = scope.bodyStart
in_this_case = False
while t and t != scope.bodyEnd:
if t == tcase_start:
in_this_case = True
if in_this_case and t.str not in ('case', 'default'):
in_this_case = False
if t == tok and not in_this_case:
self.reportError(token, 15, 3)
break
t = t.next
def misra_15_5(self, data):
for token in data.tokenlist:

View File

@ -696,6 +696,28 @@ void misra_15_3() {
L1:
} else {}
} else {}
switch (x) {
case 0:
if (x == y) {
goto L2; // 15.3 15.1
}
goto L2; // 15.3 15.1
L3:
foo();
if (a == 0x42) {
// Compliant:
goto L3; // 15.1 15.2
}
break;
case 1:
y = x;
L2:
++x;
break;
default:
break;
}
}
int misra_15_5() {