简体   繁体   English

我可以在函数头中包含cppcheck抑制吗?

[英]Can I include cppcheck suppression within a function header?

I have added an inline comment to suppress a cppcheck unusedFunction warning for a function, but I would like to include this within the function header so that Doxygen can document all of the unused functions (I am implementing an API, so I have many functions that will not be used in my source). 我添加了一个内联注释来抑制函数的cppcheck unusedFunction警告,但我想在函数头中包含它,以便Doxygen可以记录所有未使用的函数(我正在实现一个API,所以我有很多函数,不会在我的来源中使用)。 I would prefer not to suppress all unusedFunction errors, but rather on a per-function basis. 我宁愿不压缩所有unusedFunction错误,而是基于每个函数。

I would like to do something like this: 我想做这样的事情:

/**
 * API function description
 * 
 * @param p1 function pointer to the ...
 * @return 0 if successful, -1 otherwise.
 * // cppcheck-suppress unusedFunction
 */
int CreateTask(Task_FuncPtr p1)
{    
    return doSomething();
}

But when I do this, cppcheck does not "see" the inline suppression. 但是当我这样做时,cppcheck没有“看到”内联抑制。 If I move it outside the header, but just before the function declaration then the suppression works. 如果我将它移到标题之外,但在函数声明之前,那么抑制就可以了。 The cppcheck documentation seems to imply that the suppression needs to be directly before the line generating then error. cppcheck文档似乎暗示抑制需要直接在生成然后错误的行之前。

Has any had success with this? 有没有成功呢?

Taking a look at cppcheck sources (file preprocessor.cpp function RemoveComments() ), it seems that you cannot do it. 看一下cppcheck源码(文件preprocessor.cpp函数RemoveComments() ),看来你做不到。

The code to identify comments is: 识别评论的代码是:

if (str.compare(i, 2, "//") == 0) { /* ... */ }

and

else if (str.compare(i, 2, "/*") == 0) { /* ... */ }

When a comment is found, the code that manages warning suppression is: 找到注释后,管理警告抑制的代码为:

if (_settings && _settings->_inlineSuppressions) {
    std::istringstream iss(comment);
    std::string word;
    iss >> word;
    if (word == "cppcheck-suppress") {
        iss >> word;
        if (iss)
            suppressionIDs.push_back(word);
    }
}

So cppcheck will skip spaces and check the first token immediately after // or /* . 所以cppcheck会跳过空格并在///*之后立即检查第一个标记。

Unfortunately Doxygen's special comment blocks start with /** , /// , /*! 不幸的是,Doxygen的特殊注释块以/**////*!开头/*! or //! //! and the third character prevents the "correct match". 第三个字符阻止“正确匹配”。

Changing: 更改:

if (word == "cppcheck-suppress") { /* ... */ }

into: 成:

if (contains(word, "cppcheck-suppress")) { /* ... */ }
// or if (ends_with(word, "cppcheck-suppress"))

should allow what you want: 应该允许你想要的东西:

/**
 * API function description
 *
 * @param p1 function pointer to the ...
 * @return 0 if successful, -1 otherwise.
 */
/** cppcheck-suppress unusedFunction */

or 要么

/// API function description
///
/// @param p1 function pointer to the ...
/// @return 0 if successful, -1 otherwise.
///
/// cppcheck-suppress unusedFunction

You can probably open a ticket on http://trac.cppcheck.net/ 您可以在http://trac.cppcheck.net/上打开一张票

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

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