Skip to content

add missing #line directive to misra.py - #8879

Open
driftregion wants to merge 1 commit into
cppcheck-opensource:mainfrom
driftregion:misra_line
Open

driftregion wants to merge 1 commit into
cppcheck-opensource:mainfrom
driftregion:misra_line

Conversation

@driftregion

Copy link
Copy Markdown
Contributor

Hello cppcheck maintainer. Thank you for your work.

The #line preprocessor directive (standard since C90) was missing from misra.py, leading to false positives in my analysis. I have added it here.

See:
https://en.cppreference.com/c/preprocessor/line

Comment thread addons/misra.py
dir = mo.group(1)
if dir not in ['define', 'elif', 'else', 'endif', 'error', 'if', 'ifdef', 'ifndef', 'include',
'pragma', 'undef', 'warning']:
'pragma', 'undef', 'warning', 'line']:

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 right to me: #line is a standard directive, and with this change the 20.13 false positive is gone.

Could you add a regression test? One option that doesn't shift any line numbers in addons/test/misra/misra-test.c is to replace the empty line after #else1 // 20.13 (line 1946) with:

#line 1947

I tried it with the same commands CI uses (cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test.c --std=c89 --platform=unix64 + misra.py -verify). Without your fix it reports Not expected: misra/misra-test.c:1946 20.13, and with your fix it passes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Daniel, thanks for your work maintaining cppcheck. I had a look at the AI reviews on #8879, #8876, and cppcheck-opensource/simplecpp#704 just now. They seem reasonable. Before I go about addressing them, I'd like to request your human opinion on whether you endorse the AI-proposed changes. This statement makes it clear that the review was AI-generated (great idea btw), but leaves me with some confusion about whether the human in the loop endorses its content:

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

Thanks!

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.

I looked quickly. Adding that test seems reasonable to me.

I will mostly trust peoples judgments about AI reviews.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no new problems. With this change, #line directives are no longer reported as misra-c2012-20.13, and I checked all the ways of spelling the directive that I tried. A malformed directive such as #lines 3 is still reported. The PR still doesn't add the regression test that was asked for in comments.md (#line 1947 in addons/test/misra/misra-test.c). That has already been raised, so I'm not repeating it as a separate comment.

Testing

  • I built cppcheck in pr/ with make -j8 cppcheck.
  • I wrote scratch/t.c with several spellings of the directive:
    #line 10
    # line 20 "x.c"
    #	line 30
    #line	40
    #  line 50
    #lines 3
    int x;
    I ran cppcheck --dump -q t.c and then python3 misra.py t.c.dump with both the PR's and the base's misra.py.
    • PR: 20.13 is reported only for #lines 3 (x.c:50), which is correct.
    • Base: 20.13 is reported for all 6 directives.
    • simplecpp turns tabs into single spaces in the directive string in the dump, so the space-only regex in misra_20_13 handles those cases correctly.
  • Existing MISRA verify test: I ran cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test.c --std=c89 --platform=unix64 followed by misra.py -verify misra/misra-test.c.dump. It reported no Not expected or Not detected lines.
  • pytest: python3 -m pytest addons/test/misra_test.py stopped with a collection error before running any tests, so that suite was not run. I did not look into the cause.

Automated review of d4aa765 by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants