Skip to content

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

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

Fix #686: Add warning for bad line directives#687
glankk wants to merge 13 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
";\n";
simplecpp::OutputList outputList;
makeTokenList(code, dui, &outputList);
ASSERT_EQUALS("file0,2,portability_line_directive,Line number out of range: 18446744073709551617.\n",

@danmar danmar Jul 22, 2026

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 feel that ideally the warning would write the range permitted by the standard. But I am not sure exactly how I want to say it..

Can the warning say that it's UB according to the standard , and write the used standard...

If latest C++ standard is used it would be nice to say that it is conditionally supported instead of UB. However I don't know if the C++ standard allows negative value at all maybe we should say that negative values are unspecified or something..

@glankk glankk Jul 22, 2026

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.

Negative values are not even allowed syntactically according to the standard, they should fail to compile due to syntax error.

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.

My point being that the standard doesn't cover the case of negative numbers, so I think we can ignore them.

Comment thread test.cpp

{
const char code[] =
"#line 0\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.

could we add a negative value test and check what the output is. syntax error or such portability warnings would be fine ... it's not possible to detect such code with cppcheck so I feel we should add this in simplecpp.

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.

We could output a syntax error and stop processing when the value is negative, that would be the behavior ordained by the standard. 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.

On the other hand, simplecpp is generally pretty lenient when it comes to syntax errors, so it feels weirdly specific to add an error for this. For example, #line 1 garbage here is accepted without error.

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.

In the current implementation, line directives with negative numbers are silently ignored, which I think is okay. Since the code shouldn't even compile according to the standard, you shouldn't really need cppcheck to find such errors. cppcheck doesn't generally check for syntax errors as far as I'm aware, since they're caught by the compiler.

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