-
Notifications
You must be signed in to change notification settings - Fork 90
Includes of system headers are never implicitly relative to the source file #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
is it possible to write some test for this? Something similar to this maybe:
|
8be5251
to
2b73c45
Compare
Hope this will do - the test detects the difference between the old and new simplecpp behaviour. I found I had to create an empty |
@@ -3223,7 +3223,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL | |||
|
|||
const Token * const inctok = inc2.cfront(); | |||
|
|||
const bool systemheader = (inctok->op == '<'); | |||
const bool systemheader = (inctok->str()[0] == '<'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This indicates that code above which also checks op
for <
might never be executed,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is inctok->op
supposed to have any meaning here? By dumping out its value every time we reach this point in a run of the testrunner
binary, I can see it's a null character in every case except in include7()
and include8()
, which are both testing the case where the argument to #include
is derived by expanding a preprocessor macro; in these cases it holds a <
character. So perhaps it's left over from the macro expansion? (Apologies, I'm new to the cppcheck
innards, I'm not familiar with how it works.)
By chance, until I added my new test, these were also the only cases where the filespec in inctok->str()
used angle brackets, so for the cases tested by testrunner
so far, testing inctok->op
was adequate. I agree this looks wrong, but I'm unsure whether the fix should be to look at inctok->str()
instead, or whether to fix the initialisation of inctok->op
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial observation was based on a short look and was wrong. I am also not familiar in how it works.
op
is set to <
in that code above. Since with your change that is not used anymore that can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An additional note. It seems op
should only be set if it is a single character token. And if the token isn't already something like a name, number or comment. That wasn't done consistently though - I addressed that within #285.
This is scary. I finally got around to checking why the issue I introduced in #276 and fixed in #281 did not trigger any test failures. And it turns out it is caused by the same code you just fixed. simplecpp is completely void of tests with additional files at the moment. That has been a source of discussion in #261. There's also tests lacking in cppcheck which I have partially added locally. |
hmm.. I feel I can merge this. However when I try it locally it does not seem to work. I have this code in my file 1.c:
And 1.h contains:
when I run simplecpp with command It works as it should for you? |
That's why we need unit tests with headers/includes... |
The title of this PR says that "system headers are never relative to the source file". That is not true. See:
|
…e file Sometimes, #include directives with angle-bracket filespec delimiters are used (or abused) to defeat the preprocessor's behaviour where it tries to find a header file at a path relative to the file containing the directive. Without this fix, any non-root header file, foo/bar.h, which does #include <bar.h> while intending to include a root-level header file, will instead enter an infinite inclusion loop, terminating when the inclusion stack overflows with a "#include nested too deeply" error.
2b73c45
to
81808f8
Compare
That looks to have been caused by
OK, I have added the word "implicitly" into the commit summary, hope this is sufficient. For what it's worth, I found the following description in the gcc man page (I realise this isn't unversal to all compilers):
|
I will add such tests to Cppcheck soon with the referenced PR. |
Thanks I think this looks good. If CI is happy I'll merge it. Let's consider the testing later separately. I also think we could explore using pytest in simplecpp. |
Sometimes,
#include
directives with angle-bracket filespec delimiters are used (or abused) to defeat the preprocessor's behaviour where it tries to find a header file at a path relative to the file containing the directive.Without this fix, any non-root header file,
foo/bar.h
, which doeswhile intending to include a root-level header file, will instead enter an infinite inclusion loop, terminating when the inclusion stack overflows with a
#include nested too deeply
error.See also this cppcheck issue.