I recently came across an issue where cppcheck is reporting a syntax error in an included header from the nlohmann JSON library. This ends up stopping analysis of any source file that includes it.
It is interesting that there is already an inline suppression there. Has this possibly been reported before and I missed it?
In any case, I initially found this problem when using version 2.13.0 from Ubuntu 24.04, but it is also present in the latest version from the git repository (currently 73682dadd14f3f191f28549a1e32d2203d5b664a).
I came up with a hacky patch to address the problem, but it is probably not the best solution.
The name vector ends up being saved to the templateParameters vector. Then when the opening bracket < appears on line 49, it is interpreted as a less-than comparison operator because vector had previously been stored in the templateParameters vector.
The patch may address the initial problem, but there are still other problematic cases I have found such as:
In this case, the third < is not interpreted as a less-than operator because names are only stored in the templateParamaters vector when the depth is 1. I suppose the depth == 1 condition in findClosingBracket could be loosened if you were to store parameters for each depth. I initially thought about doing this, but then I realized that the following is also problematic:
constexprsize_tA=7;template<boolB=A<10>classbar;
This one couldn't really be solved by storing known template parameters because A is not a template parameter at all.
So I guess the main issue is that there does not seem to be an easy way to get previously defined symbols from within findClosingBracket in order to parse less-than operators correctly. But since I have only started diving into the cppcheck code very recently, I could have easily missed something. I should also note that I am not an expert in parsing C++, so there may be other solutions to this that I had not considered.
Any thoughts on this would be greatly appreciated. Thank you.
Last edit: Scott Ehlert 2024-07-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Came here to report same issue. This is not the first time I've encountered such a problem. The problem is more general than this particular example.
As an extensive Cppcheck user, I often do:
cppcheck --suppress=*:*lib/* ...
or
cppcheck --inline-suppr ...
And having this particular JSON library in lib directory. I have the false impression that everything is ok, but it's not. Which, as a result causes to potentially have undetected problems in code.
@danielmarjamaki@chrchr maybe Cppcheck needs some new kind of error type like "breaking". Which when triggered (like on [syntaxError] and other breaking analysis) will inform the user that the analysis is broken even if suppressions are used.
In production CI pipelines I used to do extra step to check if "configuration" is fine doing something like:
cppcheck <no explicit global suppressions like *:*lib/*> <no --enable=all to only report hard errors> <eventually --suppress=some_not_analysis_breaking_suppress if 3rd party has some issues> ...
Which then informed me if analysis is broken or not. But it would be great if we had some Cppcheck mechanism for this.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I recently came across an issue where cppcheck is reporting a syntax error in an included header from the nlohmann JSON library. This ends up stopping analysis of any source file that includes it.
That line can be found here: https://github.com/nlohmann/json/blob/960b763ecd144f156d05ec61f577b04107290137/include/nlohmann/json_fwd.hpp#L49
It is interesting that there is already an inline suppression there. Has this possibly been reported before and I missed it?
In any case, I initially found this problem when using version 2.13.0 from Ubuntu 24.04, but it is also present in the latest version from the git repository (currently 73682dadd14f3f191f28549a1e32d2203d5b664a).
I came up with a hacky patch to address the problem, but it is probably not the best solution.
The patch basically stops
Token::findClosingBracket
from saving names that appear after an equal sign in template parameters.The issue with the current code is that with a previous parameter on line 41 of the json_fwd header:
The name
vector
ends up being saved to thetemplateParameters
vector. Then when the opening bracket<
appears on line 49, it is interpreted as a less-than comparison operator becausevector
had previously been stored in thetemplateParameters
vector.The patch may address the initial problem, but there are still other problematic cases I have found such as:
In this case, the third
<
is not interpreted as a less-than operator because names are only stored in thetemplateParamaters
vector when the depth is 1. I suppose thedepth == 1
condition infindClosingBracket
could be loosened if you were to store parameters for each depth. I initially thought about doing this, but then I realized that the following is also problematic:This one couldn't really be solved by storing known template parameters because
A
is not a template parameter at all.So I guess the main issue is that there does not seem to be an easy way to get previously defined symbols from within
findClosingBracket
in order to parse less-than operators correctly. But since I have only started diving into the cppcheck code very recently, I could have easily missed something. I should also note that I am not an expert in parsing C++, so there may be other solutions to this that I had not considered.Any thoughts on this would be greatly appreciated. Thank you.
Last edit: Scott Ehlert 2024-07-09
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/12923
Please feel free to open a PR at https://github.com/danmar/cppcheck
Last edit: CHR 2024-07-10
Came here to report same issue. This is not the first time I've encountered such a problem. The problem is more general than this particular example.
As an extensive Cppcheck user, I often do:
And having this particular JSON library in
lib
directory. I have the false impression that everything is ok, but it's not. Which, as a result causes to potentially have undetected problems in code.@danielmarjamaki @chrchr maybe Cppcheck needs some new kind of error type like "breaking". Which when triggered (like on [syntaxError] and other breaking analysis) will inform the user that the analysis is broken even if suppressions are used.
In production CI pipelines I used to do extra step to check if "configuration" is fine doing something like:
Which then informed me if analysis is broken or not. But it would be great if we had some Cppcheck mechanism for this.
Today I found out that there is a
--safety
option that solves the problems I described.