Writing rules: minor tweaks
This commit is contained in:
parent
cdcd3c4a8f
commit
55e12659ff
|
@ -118,48 +118,50 @@
|
||||||
<section>
|
<section>
|
||||||
<title>Regular expressions</title>
|
<title>Regular expressions</title>
|
||||||
|
|
||||||
<para>Simple rules can be defined through regular expressions. Cppcheck
|
<para>Simple rules can be defined through regular expressions.</para>
|
||||||
uses PCRE to handle regular expressions.</para>
|
|
||||||
|
<para>Cppcheck uses the <literal>PCRE</literal> library to handle regular
|
||||||
|
expressions. <literal>PCRE</literal> stands for "Perl Compatible Regular
|
||||||
|
Expressions". The homepage for <literal>PCRE</literal> is
|
||||||
|
<literal>http://www.pcre.org</literal>.</para>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<title>Creating regular expression</title>
|
<title>Creating the regular expression</title>
|
||||||
|
|
||||||
<para>Let's create a regular expression that checks for:</para>
|
<para>Let's create a regular expression that checks for:</para>
|
||||||
|
|
||||||
<programlisting>if (p)
|
<programlisting>if (p)
|
||||||
free(p);</programlisting>
|
free(p);</programlisting>
|
||||||
|
|
||||||
<para>The condition is often redundant, on most implementations it is
|
<para>For such code the condition is often redundant, on most
|
||||||
valid to free a NULL pointer.</para>
|
implementations it is valid to free a NULL pointer.</para>
|
||||||
|
|
||||||
<para>The regular expression must match the simplified code. Create a
|
<para>The regular expression must be written for the simplified code. To
|
||||||
source file that has the bad code:</para>
|
see what the simplified code looks like you can create a source file
|
||||||
|
with some code:</para>
|
||||||
|
|
||||||
<programlisting>void f() {
|
<programlisting>void f() {
|
||||||
if (p)
|
if (p)
|
||||||
free(p);
|
free(p);
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>To see the simplified code you can use <literal>cppcheck
|
<para>Save that code as <literal>dealloc.cpp</literal> and use
|
||||||
--rule=".+" dealloc.cpp</literal>:</para>
|
<literal>cppcheck --rule=".+" dealloc.cpp</literal>:</para>
|
||||||
|
|
||||||
<programlisting>$ ./cppcheck --rule=".+" dealloc.cpp
|
<programlisting>$ ./cppcheck --rule=".+" dealloc.cpp
|
||||||
Checking dealloc.cpp...
|
Checking dealloc.cpp...
|
||||||
[dealloc.cpp:1]: (style) found ' void f ( ) { if ( p ) { free ( p ) ; } }'</programlisting>
|
[dealloc.cpp:1]: (style) found ' void f ( ) { if ( p ) { free ( p ) ; } }'</programlisting>
|
||||||
|
|
||||||
<para>In short, the simplified code is:</para>
|
<para>From that output we can see that the simplified code is:</para>
|
||||||
|
|
||||||
<programlisting> void f ( ) { if ( p ) { free ( p ) ; } }</programlisting>
|
<programlisting> void f ( ) { if ( p ) { free ( p ) ; } }</programlisting>
|
||||||
|
|
||||||
<para>Now that the simplified code is known we can create a regular
|
<para>Now that we know how the simplified code looks for a simple test
|
||||||
expression:</para>
|
case, we can create a regular expression:</para>
|
||||||
|
|
||||||
<programlisting>$ cppcheck --rule="if \( p \) { free \( p \) ; }" dealloc.cpp
|
<programlisting>$ cppcheck --rule="if \( p \) { free \( p \) ; }" dealloc.cpp
|
||||||
Checking dealloc.cpp...
|
Checking dealloc.cpp...
|
||||||
[dealloc.cpp:2]: (style) found 'if ( p ) { free ( p ) ; }'</programlisting>
|
[dealloc.cpp:2]: (style) found 'if ( p ) { free ( p ) ; }'</programlisting>
|
||||||
|
|
||||||
<para>Feel free to improve the pattern. Above, the pointer name must be
|
|
||||||
"p" to get a match.</para>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
|
@ -169,12 +171,11 @@ Checking dealloc.cpp...
|
||||||
|
|
||||||
<itemizedlist>
|
<itemizedlist>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>a pattern to search for.</para>
|
<para>a pattern to search for</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>an error message that is reported when pattern is found - this
|
<para>an error message that is reported when pattern is found</para>
|
||||||
is optional, if none is given a default message is written.</para>
|
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
|
||||||
|
@ -190,10 +191,6 @@ Checking dealloc.cpp...
|
||||||
</message>
|
</message>
|
||||||
</rule></programlisting>
|
</rule></programlisting>
|
||||||
|
|
||||||
<para>The <literal>message</literal>, <literal>id</literal>,
|
|
||||||
<literal>severity</literal> and <literal>summary</literal> elements are
|
|
||||||
optional. But highly recommended.</para>
|
|
||||||
|
|
||||||
<para>If you save that xml data in <literal>dealloc.rule</literal> you
|
<para>If you save that xml data in <literal>dealloc.rule</literal> you
|
||||||
can test this rule:</para>
|
can test this rule:</para>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue