Skip to content

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

Open
glankk wants to merge 18 commits into
cppcheck-opensource:masterfrom
glankk:686
Open

Fix #686: Add warning for bad line directives#687
glankk wants to merge 18 commits into
cppcheck-opensource:masterfrom
glankk:686

Conversation

@glankk

@glankk glankk commented Jul 22, 2026

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 thread test.cpp Outdated
Comment thread test.cpp
Comment thread simplecpp.cpp Outdated
Comment thread simplecpp.cpp Outdated
Comment thread simplecpp.cpp Outdated
Comment thread simplecpp.cpp Outdated
glankk and others added 2 commits July 23, 2026 11:42
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
Comment thread simplecpp.cpp Outdated
glankk and others added 2 commits July 23, 2026 13:24
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@gmail.com>
Comment thread simplecpp.cpp Outdated
Comment thread simplecpp.cpp
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
Collaborator Author

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
Collaborator Author

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
Collaborator Author

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.

Comment thread test.cpp
";\n";
simplecpp::OutputList outputList;
makeTokenList(code, dui, &outputList);
ASSERT_EQUALS("file0,2,portability_line_directive,Line number out of range: 18446744073709551617. Line numbers above 2147483647 are conditionally supported in " + std_name + ".\n",

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.

In theory this test will behave differently on a platform where unsigned long is 32 bits. I.e. on WIN32.
I don't have a strong opinion.. but it's a bit unfortunate.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

std::stoul which is used for parsing line numbers does not overflow but throws an out of range exception. So this will behave the same everywhere unsigned long is at least 32 bits.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I assume that's what you're referring to, if you mean the line number in the warning then it's just the token string.

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.

2 participants