问题
I've poked around for a while, and can't find this anywhere. I have found a nice example of a cppcheck rule-file that shows a simple pattern;
<?xml version="1.0"?>
<rule version="1">
<pattern>if \( p \) { free \( p \) ; }</pattern>
<message>
<id>redundantCondition</id>
<severity>style</severity>
<summary>Redundant condition. It is valid to free a NULL pointer.</summary>
</message>
</rule>
Which works nicely, as long as all the pointers are named 'p' and the call is 'free'. How do I change 'p' to match any identifier? How do I check for "'free' or 'delete'"? Is the pattern a grep/awk/sed pattern?
回答1:
I am a Cppcheck developer.
Cppcheck uses PCRE. So use a regular expression that follows the Perl rules.
I'm not really good at Perl regular expressions so I can't answer how/if you can match any identifier (since it should be matched twice).
.. hope that helps a little at least.
回答2:
From my regex experience, I'd try this
<?xml version="1.0"?>
<rule version="1">
<pattern>if \( (\w+) \) { free \( \1 \) ; }</pattern>
<message>
<id>redundantCondition</id>
<severity>style</severity>
<summary>Redundant condition. It is valid to free a NULL pointer.</summary>
</message>
</rule>
Where these PCRE features replace the p
of your original example:
(\w)
is a word pattern (of alphanumeric chars and'_'
), caught in group #1\1
is the back-reference to the text which matched the pattern
An elaborated description can be found at Martin Moene's Blog: Find code patterns with CppCheck
回答3:
change the p into dot(.) so that it means any character
来源:https://stackoverflow.com/questions/10836794/what-is-cppcheck-rule-file-pattern-syntax