Skip to content

Fix #686: Add warning for bad line directives - #687

Merged
glankk merged 20 commits into
cppcheck-opensource:masterfrom
glankk:686
Jul 24, 2026
Merged

Fix #686: Add warning for bad line directives#687
glankk merged 20 commits into
cppcheck-opensource:masterfrom
glankk:686

Conversation

@glankk

Copy link
Copy Markdown
Collaborator

readfile now takes a DUI with information about which standard to use, which is needed to determine if a warning should be issued or not. preprocess now also includes outputs from makeTokenLists which calls readfile. Some tests had to be fixed to have an ending newline because of this in order to pass.

Comment threadtest.cpp Outdated
Comment threadtest.cpp
Comment threadsimplecpp.cpp Outdated
Comment threadsimplecpp.cpp Outdated
Comment threadsimplecpp.cpp Outdated
Comment threadsimplecpp.cpp Outdated
glankkand others added 2 commits July 23, 2026 11:42
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
Comment threadsimplecpp.cpp Outdated
glankkand others added 2 commits July 23, 2026 13:24
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
Comment threadsimplecpp.cpp Outdated
Comment threadsimplecpp.cpp Outdated
try {
line = std::stoul(ppTok->str());
} catch (...) {
line = std::numeric_limits<unsigned long>::max();

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 value might not work well.. it means tokens might get line number 0 which cppcheck interprets as "no line".

I suggest that the value passed in the lineDirective call will be 1. After reporting the error message.. change it to 1?

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.

or assign maxvalue+1?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Actually it might be better that we just ignore the line number and use the current one when it's out of range, what do you think?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Or just discard the line directive entirely.

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 would be fine with both those options.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I've changed it to just use the maximum possible line number, I think that's alright. It's guaranteed to always be representable, and it's also the most lenient option in terms of conditional support.

Comment threadtest.cpp
@glankk
glankk merged commit df97c30 into cppcheck-opensource:masterJul 24, 2026
26 checks passed
@glankk
glankk deleted the 686 branch July 24, 2026 12:58
Sign up for freeto 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

@glankk@danmar@ludviggunne