Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » CppCheck regexp

This thread is locked; no one can reply to it. rss feed Print
CppCheck regexp
Kevin Adrian
Member #7,087
April 2006
avatar

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

My mouth will speak words of wisdom; the utterance from my heart will give understanding. (Psalm 49:3)

Edgar Reynaldo
Member #8,592
May 2007
avatar

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

Kevin Adrian
Member #7,087
April 2006
avatar

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>

My mouth will speak words of wisdom; the utterance from my heart will give understanding. (Psalm 49:3)

Peter Hull
Member #1,136
March 2001

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
Member #7,087
April 2006
avatar

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

My mouth will speak words of wisdom; the utterance from my heart will give understanding. (Psalm 49:3)

Go to: