CppCheck regexp
Kevin Adrian


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):


When running CppCheck, the C expression above is not found. Any advices or suggestions?

Thanks in advance

Edgar Reynaldo

You'll need to show us the CppCheck code you're using. It could be a simple mistake in something else.

Kevin Adrian

This is the rule XML file I use:

<?xml version="1.0"?>
<rule version="1">
    <summary>Do not assign int values to a float variable.</summary>

Peter Hull

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.

Kevin Adrian

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;


[testsnippets2.cpp:1]: (style) found ' int foo ( ) { }'

Example 2:

int foo() {
  bar f = 1982;


[testsnippets2.cpp:1]: (style) found ' int foo ( ) { bar f ; f = 1982 ; }'

Maybe I should try another more reliable code analyser (e.g. Clang).

Thread #617873. Printed from Allegro.cc