我添加了一个内联注释来抑制函数的 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似乎暗示抑制需要直接在生成 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 的特殊注释 block 以 /**
、///
、/*!
或 //!< 开头
和第三个字符阻止“正确匹配”。
改变:
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/ 上开票
关于c++ - 我可以在函数头中包含 cppcheck 抑制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117285/