Writing rules: Minor updates. Trying to make it easier.
This commit is contained in:
parent
eaea572683
commit
68f2c47c5c
|
@ -118,7 +118,8 @@
|
||||||
<section>
|
<section>
|
||||||
<title>Regular expressions</title>
|
<title>Regular expressions</title>
|
||||||
|
|
||||||
<para>Simple rules can be defined through regular expressions.</para>
|
<para>Simple rules can be defined through regular expressions. Cppcheck
|
||||||
|
uses PCRE to handle regular expressions.</para>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
<title>Creating regular expression</title>
|
<title>Creating regular expression</title>
|
||||||
|
@ -128,35 +129,42 @@
|
||||||
<programlisting>if (p)
|
<programlisting>if (p)
|
||||||
free(p);</programlisting>
|
free(p);</programlisting>
|
||||||
|
|
||||||
<para>The condition is often redundant, it is valid to free a NULL
|
<para>The condition is often redundant, on most implementations it is
|
||||||
pointer.</para>
|
valid to free a NULL pointer.</para>
|
||||||
|
|
||||||
<para>It is important to write the regular expression so it matches the
|
<para>The regular expression must match the simplified code. Create a
|
||||||
simplified code. Create a source file that has the bad code:</para>
|
source file that has the bad code:</para>
|
||||||
|
|
||||||
<programlisting>void f() {
|
<programlisting>void f() {
|
||||||
if (p) free(p);
|
if (p)
|
||||||
|
free(p);
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>I intentionally put the whole pattern on a single line. The
|
<para>To see the simplified code use <literal>cppcheck --debug
|
||||||
simplified code is written on a single line of code.</para>
|
dealloc.cpp</literal>.</para>
|
||||||
|
|
||||||
<para>To see the simplified code I use <literal>cppcheck --debug
|
|
||||||
file.cpp</literal>.</para>
|
|
||||||
|
|
||||||
<programlisting>##file dealloc.cpp
|
<programlisting>##file dealloc.cpp
|
||||||
1: void f ( ) {
|
1: void f ( ) {
|
||||||
2: if ( p ) { free ( p ) ; }
|
2: if ( p ) {
|
||||||
3: }</programlisting>
|
3: free ( p ) ; }
|
||||||
|
4: }</programlisting>
|
||||||
|
|
||||||
<para>I save that in a file <literal>test.txt</literal>.</para>
|
<para>In the <literal>--debug</literal> output there are line feeds and
|
||||||
|
line numbers. But the newlines and line numbers are only there to make
|
||||||
|
the output easier to read. The real simplified code is written on a
|
||||||
|
single line:</para>
|
||||||
|
|
||||||
<para>Now we can use <literal>grep</literal> to develop a regular
|
<programlisting>void f ( ) { if ( p ) { free ( p ) ; } }</programlisting>
|
||||||
expression.</para>
|
|
||||||
|
|
||||||
<programlisting>grep "if [(] p [)] { free [(] p [)] ; }" test.txt</programlisting>
|
<para>Now we can use <literal>cppcheck --rule</literal> to develop a
|
||||||
|
regular expression.</para>
|
||||||
|
|
||||||
<para>Feel free to improve the pattern.</para>
|
<programlisting>$ cppcheck --rule="if \( p \) { free \( p \) ; }" dealloc.cpp
|
||||||
|
Checking dealloc.cpp...
|
||||||
|
[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>
|
||||||
|
@ -170,8 +178,8 @@
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>an optional error message that is reported when pattern is
|
<para>an error message that is reported when pattern is found - this
|
||||||
found</para>
|
is optional, if none is given a default message is written.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
|
||||||
|
@ -179,7 +187,7 @@
|
||||||
|
|
||||||
<programlisting><?xml version="1.0"?>
|
<programlisting><?xml version="1.0"?>
|
||||||
<rule version="1">
|
<rule version="1">
|
||||||
<pattern>if [(] p [)] { free [(] p [)] ; }</pattern>
|
<pattern>if \( p \) { free \( p \) ; }</pattern>
|
||||||
<message>
|
<message>
|
||||||
<id>redundantCondition</id>
|
<id>redundantCondition</id>
|
||||||
<severity>style</severity>
|
<severity>style</severity>
|
||||||
|
@ -189,10 +197,14 @@
|
||||||
|
|
||||||
<para>The <literal>message</literal>, <literal>id</literal>,
|
<para>The <literal>message</literal>, <literal>id</literal>,
|
||||||
<literal>severity</literal> and <literal>summary</literal> elements are
|
<literal>severity</literal> and <literal>summary</literal> elements are
|
||||||
optional. If they are not written default values are used.</para>
|
optional. But highly recommended.</para>
|
||||||
|
|
||||||
<para>Now you can test this rule. Use the <literal>cppcheck
|
<para>If you save that xml data in <literal>dealloc.rule</literal> you
|
||||||
--rule-file=dealloc.rule test.cpp</literal> command.</para>
|
can test this rule:</para>
|
||||||
|
|
||||||
|
<programlisting>$ cppcheck --rule-file=dealloc.rule dealloc.cpp
|
||||||
|
Checking dealloc.cpp...
|
||||||
|
[dealloc.cpp:2]: (style) Redundant condition. It is valid to free a NULL pointer.</programlisting>
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
</article>
|
</article>
|
||||||
|
|
Loading…
Reference in New Issue