在C ++代码中查找未实例化的模板 [英] Finding non-instantiated templates in C++ code

查看:55
本文介绍了在C ++代码中查找未实例化的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++代码中查找未实例化模板的最佳方法是什么?

What is the best way to find uninstantiated templates in C++ Code?

我有一个大量使用模板的代码库。
当然,我们要确保测试覆盖率很高。
对于所有使用的代码,使用 gcov 效果很好。

I have a code base that heavily uses templates. Of course, we want to make sure that the test coverage is high. For all used code, this works very well using gcov.

但是,会报告未使用的模板

However, unused templates are reported as non-executable by gcov.

经过一段时间的搜索之后,似乎没有办法强制 g ++ 为这些模板发出代码(这只是逻辑上的,编译器应该如何猜测任何类型?)
看来也没有办法使gcov识别出未实例化的模板代码作为可运行代码。

After some googling, it appears that there is no way to force g++ to emit code for these templates (which is only logical, how should the compiler guess any types?) It also appears that there is no way to make gcov recognize the uninstantiated template code as runnable code.

有没有开箱即用的东西,可以让我扩充GCC -ftest生成的文件-coverage -fprofile-arcs 工具?通过 GCC的gcov选项文档 ,将整个模板函数主体标记为一个块可能就足够了,因为执行永远不会在那里结束。

Is there anything "out of the box" that allows me to augment the files generated by the GCC -ftest-coverage -fprofile-arcs instrumentation? By the gcov option documentation of GCC, it would probably be enough to mark the whole template function body as one block since execution will never end up there.

EDIT (背景信息):我正在研究仅标头的模板库。
我的目的是查找未使用/未使用的函数。

EDIT (background information): I'm working on a header-only template library. My aim here is to find unused/untested functions.

我知道代码覆盖范围存在缺陷,但是找到未实例化的代码是迈向更好测试代码的非常重要的一步。
当前,我们将检查点宏放在每个函数的开头。
在测试模式下,它们扩展为将一对(文件,行)插入到一组已通过检查点的全局代码中的代码。
运行测试之后,我们手动读取所有文件,并将到达的检查点与所有可用检查点的集合进行比较。

I know that code coverage is flawed but finding uninstantiated code is a very important step towards better tested code. Currently, we are putting checkpoint macros at the beginning of each function. In testing mode, they expand to code that inserts a pair of (file, line) into a global set of passed checkpoints. After running the tests, we manually read all files and compare the reached checkpoints with the set of all available checkpoints.

查找未实例化的代码很重要,例如,由于C ++模板的优先级行为不直观,读者或什至作者可能希望在某些地方使用死代码。

Finding uninstantiated code is important, for example, because of unintuitive C++ template precedence behaviour, there might be dead code somewhere that the reader or even the author would expect to be used.

推荐答案

我认为我们的 C ++测试覆盖率工具(不是基于GCC的)确实可以从您的角度来看,这是正确的。

I think our C++ Test Coverage tool (not GCC-based) does this correctly from your point of view.

它会在编译器看到之前插入源代码。无论模板是否被使用,模板内部的代码都会获取覆盖探测器。该工具的测试覆盖率显示部分可知道所有探针的位置;如果未实例化模板代码,则显然无法执行,这将得到报告。

It instruments the source code before the compiler sees it; code inside templates get "coverage probes" regardless of whether the template gets used or not. The test coverage display portion of the tool knows where all the probes are; if the template code isn't instantiated, it clearly can't be executed that's what will get reported. You don't have to do any "custom" macro insertion or other, er, BS.

缺点是如果您有一个由几种不同类型参数化的模板,模板方法m1和m2是针对不同的实例化类型执行的,则m1和m2的覆盖率将为100%(毕竟,您执行了已检测的模板)。目前尚不清楚这很糟糕;只是它是如何解释的。

The downside is that if you have a template parameterized by several different types, and template method m1 and m2 are executed for different instantiated types, your coverage for m1 and m2 will be 100% (after all, you executed the instrumented template). It isn't clear this is bad; just that it is how this is interpreted.

这篇关于在C ++代码中查找未实例化的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆