如何在宏中排除 lcov 分支

How to exclude lcov branches within a macro

本文关键字:排除 lcov 分支      更新时间:2023-10-16

我的代码中有一些日志记录宏,如下所示:

#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