如何在宏中排除 lcov 分支
How to exclude lcov branches within a macro
我的代码中有一些日志记录宏,如下所示:
#define LOG_MSG (pri, msg, ... )
if (pri > PriorityLevel )
printf( msg, ##__VA_ARGS__);
我知道我可以使用LCOV_EXCL_START、LCOV_EXCL_STOP或LCOV_EXCL_LINE来抑制分支。 但这只有在我调用的每个地方都添加它时才有效LOG_MSG:
LOG_MSG(ERROR, "An Error has occurredn");//LCOV_EXCL_LINE
我想将该评论包含在宏中,但是如果我将其放在那里,LCOV 将无法识别它。 例如,此代码仍生成分支。
#define LOG_MSG (pri, msg, ... )
if (pri > PriorityLevel )
printf( msg, ##__VA_ARGS__);//LCOV_EXCL_LINE
有没有在宏本身中抑制这些分支的好方法?
新的LCOV版本1.11(或1.12)引入了LCOV_EXCL_BR_LINE关键字。所以在你的情况下:
LOG_MSG(ERROR, "An Error has occurredn"); //LCOV_EXCL_BR_LINE
或者,甚至更好:
LOG_MSG(ERROR, "An Error has occurredn"); (void)("LCOV_EXCL_BR_LINE");
在预编译器注释剥离中幸存下来。
为什么不把宏变成函数呢?
喜欢:
template <typename ... Ts>
void LOG_MSG(int priority, const std::string& message, Ts&&...ts)
{
if (priority > PriorityLevel)
printf(message.c_str(), std::forward<Ts>(ts)...);
// Or more appropriate stuff
}
我不知道如何将代码附加到答案上,但这是对@Jarod42解决方案的回应。 我没有使用 C++0x,所以我稍微修改了他的解决方案:
void LogMsgFunc( U32 pri, const char* msg, ... )
{
//LCOV_EXCL_START
va_list variableArgumentList;
va_start( variableArgumentList, msg );
if ( pri <= PriorityLevel )
{
vfprintf( stderr, msg, variableArgumentList );
}
va_end( variableArgumentList );
//LCOV_EXCL_STOP
}
#define LOG_MSG (pri, msg, ... )
LogMsgFunc(pri, msg, ##__VA_ARGS__);
怎么样
#define LOG_MSG__LCOV_EXCL_BR_LINE LOG_MSG
然后用新的宏LOG_MSG__LCOV_EXCL_BR_LINE
替换您不希望进行覆盖率测试的任何LOG_MSG
调用。这样行得通吗?
中提到的解决方案怎么样:https://github.com/linux-test-project/lcov/issues/44#issuecomment-427449082
更改 lcovrc 添加:
lcov_excl_br_line = LCOV_EXCL_BR_LINE|LOG_MSG
相关文章:
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- 如何从整个磁盘中排除要装载的一个文件
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- 如何在 C++ 的输出中排除 SENTINEL 值?
- 针对代码::块设置的 SFML 故障排除
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- C++:如何在从给定缓存中排除数字的同时生成随机数
- 遍历列表包含排除最后一个元素的内容
- 为什么带lcov的codecov在Travis上不能正常工作,而在我当地的Linux Mint上却不能正常工作
- C++排除通过派生类中的基类继承的类
- 在创建共享库时,是否可以明确地排除某些类
- 如何使用带有"--gcov tool"标志的lcov
- 对"无效的抽象返回类型"进行故障排除
- 需要帮助对内存泄漏源进行故障排除 - C++
- 可视C++ - 从链接器>输入>其他依赖项中排除文件
- 从参数包中排除前 n 个参数
- 使用许多嵌套的#include语句对编译器错误进行故障排除
- 如何在运行时以选择性方式排除代码块
- 如何在宏中排除 lcov 分支
- LCOV从代码覆盖率分析中排除整个包