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

Can I include cppcheck suppression within a function header?

本文关键字:cppcheck 包含 函数 我可以      更新时间:2023-10-16

我添加了一个内联注释来抑制函数的cppcheck unusedFunction警告,但我希望将其包含在函数头中,以便Doxygen可以记录所有未使用的函数(我正在实现API,因此我有许多函数将不会在我的源代码中使用)。我不希望抑制所有未使用的函数错误,而是基于每个函数。

我想做这样的事情:

/**
 * 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();
}

但当我这样做时,cppcheck不会"看到"内联抑制。如果我把它移到标头之外,但就在函数声明之前,那么抑制就起作用了。cppcheck文档似乎暗示了抑制需要直接在生成then错误的行之前。

有人成功了吗?

查看cppcheck源(文件preprocessor.cpp函数RemoveComments()),似乎无法做到这一点。

识别注释的代码是:

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

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

当发现注释时,管理警告抑制的代码是:

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);
    }
}

因此,cppcheck将跳过空格并立即检查///*之后的第一个令牌。

不幸的是,Doxygen的特殊注释块以/**////*!//!开头,第三个字符阻止了"正确匹配"。

更改:

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

进入:

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

应该允许你想要的:

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

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

你也许可以在http://trac.cppcheck.net/