Hi,
for my latest project I am trying to customize CppCheck by defining some additional analysis rules. But I have some problems with the regexp used by CppCheck. For example, I want to search for this expression:
float f = 1982;
I have written the following regexp (which I have successfully tested in an online regexp tester):
float(\s+)(\w+)(\s*)=(\s*)(\d+);
When running CppCheck, the C expression above is not found. Any advices or suggestions?
Thanks in advance
You'll need to show us the CppCheck code you're using. It could be a simple mistake in something else.
This is the rule XML file I use:
<?xml version="1.0"?> <rule version="1"> <pattern>float(\s+)(\w+)(\s*)=(\s*)(\d+);</pattern> <message> <id>intToFloatAssignment</id> <severity>error</severity> <summary>Do not assign int values to a float variable.</summary> </message> </rule>
Don't think it will work because cppcheck pre-processes the source first - if you have an example file example.c
void f() { float var = 1; }
and run cppcheck --rule=".+" example.c you will see it is processed to
[example.c:1]: (style) found ' void f ( ) { }'
i.e. it's removed that statement altogether! Hence your regex will never match.
(that technique is taken from writing-rules-1.pdf in https://sourceforge.net/projects/cppcheck/files/Articles/)
I don't know if there's a way to work around it.
Thanks for the hint. The output of CppCheck is quite strange. The expression 'float f = 123;' seems only to be supressed if a C type is used. When I define a local variable with a custom data type the content of the function 'foo' is shown in the output.
Example 1:
int foo() { int f = 1982; }
Output:
[testsnippets2.cpp:1]: (style) found ' int foo ( ) { }'
Example 2:
int foo() { bar f = 1982; }
Output:
[testsnippets2.cpp:1]: (style) found ' int foo ( ) { bar f ; f = 1982 ; }'
Maybe I should try another more reliable code analyser (e.g. Clang).