简体   繁体   English

如何配置CppCheck以阻止使用功能

[英]How to configure CppCheck to discourage usage of a function

Reading CppCheck's List of Checks and the checkfunctions.h I noticed the feature: 阅读CppCheck的检查清单checkfunctions.h我注意到该功能:

Warn if a function is called whose usage is discouraged 警告不鼓励使用函数的函数被调用

I did not understand how to configure this, though. 我不知道如何配置它。 More specifically I want 更具体地说,我要

  1. One of my own implemented functions to be discouraged 不鼓励我自己执行的功能之一
  2. A 3rdparty function (eg OpenCV's cv::imwrite() ) to be discouraged. 不建议使用第3方函数(例如OpenCV的cv::imwrite() )。 I am linking the pre-builts of this library so it would be hard (but not impossible) to change the source code to achieve it 我正在链接该库的预构建版本,因此很难(但并非不可能)更改源代码来实现它

How can I annotate these functions or how can I add them to CppCheck's list of "functions non grata"? 如何注释这些功能,或者如何将它们添加到CppCheck的“非功能”列表中?

I am not certain if there is a generic way to check the usage of all the third party functions. 我不确定是否存在检查所有第三方功能使用情况的通用方法。 Probably @Daniel Marjamäki is the best person to answer this. @DanielMarjamäki可能是回答此问题的最佳人选。 But did you try creating a rule for it? 但是您是否尝试为此创建规则?

If you wish to check for the exact signature of the function, you could have something like this: 如果您希望检查函数的确切签名 ,则可以使用以下代码:

<?xml version="1.0"?>
<rule version="1">
    <pattern>cv::imwrite\(\)</pattern>
    <message>
        <id>discouragedFunction</id>
        <summary>The use of the function cv::imwrite is discouraged.</summary>
    </message>
</rule>

Or if you want something more on the generic side , you could have something like this: 或者,如果您想要在通用方面更多一些,您可以使用以下代码:

<?xml version="1.0"?>
<rule version="1">
    <pattern>cv::[_a-zA-Z][_a-zA-Z0-9]+\(\)</pattern>
    <message>
        <id>discouragedFunction</id>
        <summary>The use of the function opencv functions are discouraged.</summary>
    </message>
</rule>

The check uses configuration. 检查使用配置。 Nothing is hardcoded. 什么都没有硬编码。 Write a custom cfg file and use --library to load that. 编写一个自定义cfg文件,并使用--library加载该文件。

You can write the cfg file manually, it is xml format. 您可以手动编写cfg文件,它是xml格式。 Or you can use the GUI (it is not the best GUI ever but imho it works). 或者,您也可以使用GUI(这不是有史以来最好的GUI,但是恕我直言,它可以工作)。

If you have a function foo that is deprecated then you will write something like: 如果您不赞成使用函数foo,那么您将编写如下内容:

<function name="foo">
  <warn severity="style" alternatives="bar" reason="Deprecated"/>
  <arg nr="1"/>
</function>

You can specify a custom warning message also: 您还可以指定自定义警告消息:

<function name="foo">
  <warn severity="warning">Do not use foo(). Use bar() instead.</warn>
  <arg nr="1"/>
</function>

For each argument that the function takes you need to provide a <arg> . 对于该函数需要的每个参数,您需要提供一个<arg>

Let me know if you have problems. 如果您有问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM