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

时间:2015-06-29 13:41:58

标签: c++ c header doxygen cppcheck

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

我想做这样的事情:

/**
 * 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 documentation似乎意味着抑制需要直接在生成然后错误的行之前。

有没有成功呢?

1 个答案:

答案 0 :(得分:2)

查看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/

上开票
相关问题