質問

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?

役に立ちましたか?

解決

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.

他のヒント

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

change the p into dot(.) so that it means any character

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top