What is cppcheck rule-file <pattern> syntax?

爱⌒轻易说出口 提交于 2019-12-13 00:39:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!