Skip to content

Commit 4f9a563

Browse files
committed
Fixed #10583 (False positive: misra-15.6)
1 parent 8804277 commit 4f9a563

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

addons/misra.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,24 +2832,51 @@ def misra_15_6(self, rawTokens):
28322832
state = 0
28332833
indent = 0
28342834
tok1 = None
2835+
def tokAt(tok,i):
2836+
while i < 0 and tok:
2837+
tok = tok.previous
2838+
if tok.str.startswith('//') or tok.str.startswith('/*'):
2839+
continue
2840+
i += 1
2841+
while i > 0 and tok:
2842+
tok = tok.next
2843+
if tok.str.startswith('//') or tok.str.startswith('/*'):
2844+
continue
2845+
i -= 1
2846+
return tok
2847+
2848+
def strtokens(tok, i1, i2):
2849+
tok1 = tokAt(tok, i1)
2850+
tok2 = tokAt(tok, i2)
2851+
tok = tok1
2852+
s = ''
2853+
while tok != tok2:
2854+
if tok.str.startswith('//') or tok.str.startswith('/*'):
2855+
tok = tok.next
2856+
continue
2857+
s += ' ' + tok.str
2858+
tok = tok.next
2859+
s += ' ' + tok.str
2860+
return s[1:]
2861+
28352862
for token in rawTokens:
28362863
if token.str in ['if', 'for', 'while']:
2837-
if simpleMatch(token.previous, '# if'):
2864+
if strtokens(token,-1,0) == '# if':
28382865
continue
2839-
if simpleMatch(token.previous, "} while"):
2866+
if strtokens(token,-1,0) == "} while":
28402867
# is there a 'do { .. } while'?
2841-
start = rawlink(token.previous)
2842-
if start and simpleMatch(start.previous, 'do {'):
2868+
start = rawlink(tokAt(token,-1))
2869+
if start and strtokens(start, -1, 0) == 'do {':
28432870
continue
28442871
if state == 2:
28452872
self.reportError(tok1, 15, 6)
28462873
state = 1
28472874
indent = 0
28482875
tok1 = token
28492876
elif token.str == 'else':
2850-
if simpleMatch(token.previous, '# else'):
2877+
if strtokens(token,-1,0) == '# else':
28512878
continue
2852-
if simpleMatch(token, 'else if'):
2879+
if strtokens(token,0,1) == 'else if':
28532880
continue
28542881
if state == 2:
28552882
self.reportError(tok1, 15, 6)

addons/test/misra/misra-test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,16 @@ static void misra_15_6(void) {
14141414
do {} while (x<0); // no-warning
14151415
}
14161416

1417+
static void misra_15_6_fp(void)
1418+
{
1419+
uint8_t value = 0U;
1420+
do // Test
1421+
{
1422+
value++;
1423+
}
1424+
while (value < 2U);
1425+
}
1426+
14171427
#if defined(M_20_9) && M_20_9 > 1 // no-warning (#10380)
14181428
#endif
14191429

0 commit comments

Comments
 (0)