Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,14 @@ def numberOfParentheses(tok1, tok2):
def findGotoLabel(gotoToken):
label = gotoToken.next.str
tok = gotoToken.next.next
functionScope = gotoToken.scope
while functionScope and functionScope.type != 'Function':
functionScope = functionScope.nestedIn
functionEnd = functionScope.bodyEnd if functionScope else None
while tok:
if tok.str == '}' and tok.scope.type == 'Function':
if tok is functionEnd:
break
if functionEnd is None and tok.str == '}' and tok.scope and tok.scope.type == 'Function':
break
Comment on lines +1179 to 1180

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.

The fix looks correct to me. I ran the CI commands (cppcheck --dump -DDUMMY ... misra/misra-test.c --std=c89 --platform=unix64 + misra.py -verify): with the old misra.py the new test gives Not expected: misra/misra-test.c:1476 15.2, and with this PR it passes. This was the only place in misra.py that used the '}' + scope.type == 'Function' heuristic.

Optional: a goto is always inside a function, so functionScope should never be None here. The fallback keeps the old heuristic that caused this bug. Maybe drop it and just stop at functionEnd, to keep the function simpler.

if tok.str == label and tok.next.str == ':':
return tok
Expand Down
17 changes: 17 additions & 0 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,23 @@ static void misra_15_3(int a) {
}
}

struct misra_15_2_s {
int a;
int b;
};

static void misra_15_2_initializer_list(void) {
int err = 0;
if (err == 0) {
err = 1;
goto done; // 15.1
}
struct misra_15_2_s s = { 1, 2 };
(void)s;
done:
return;
}

static void misra_15_4(void) {
misra_15_4_label:
return;
Expand Down
Loading