死码识别(C++)

发布于 2024-07-09 04:30:11 字数 1561 浏览 16 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

撕心裂肺的伤痛 2024-07-16 04:30:11

我知道 Gimpel 的 Lint 产品(PC-LintFlexelint) 将识别无法访问的代码和未使用/未引用的模块。

它们都属于静态分析工具的范畴。

我与 Gimpel 没有任何关系,只是一个满意的长期客户。

I know that Gimpel's Lint products (PC-Lint and Flexelint) will identify unreachable code and unused / unreferenced modules.

They both fall in the category of static analysis tools.

I have no affiliation w/ Gimpel, just a satisfied long-term customer.

好久不见√ 2024-07-16 04:30:11

您需要一些类似于 QA-C++ 的东西(http://www.programmingresearch.com/QACPP_MAIN. html),另请参阅http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis 对于类似产品。

您正在寻找一个静态代码分析工具来检测无法访问的代码; 许多编码指南(例如 MISRA-C++,如果我没记错的话)要求不存在无法访问的代码。 专门用于执行此类指南的分析工具将是您的最佳选择。

您还希望能够找到该工具的其他用途。

You'll want something along the lines of QA-C++ (http://www.programmingresearch.com/QACPP_MAIN.html), also see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis for similar products.

You're looking for a static code analysis tool that detects unreachable code; many coding guidelines (such as MISRA-C++, if I'm not mistaken) require that no unreachable code exists. An analysis tool geared specifically to enforce such a guideline would be your best bet.

And you'll like be able to find other uses for the tool as well.

难如初 2024-07-16 04:30:11

我不懂 Visual C,并且还推荐了 -Wunreachable-code 特定覆盖工具。 作为您情况的解决方案,我将尝试以下操作:

  1. 使用 ctags (或类似的程序)制作源代码中所有符号的列表
  2. 在编译器中启用死代码消除(我假设它默认为打开)
  3. 启用您的整个程序/链接时间优化(所以他知道模块中未使用的函数不需要其他外部函数并被丢弃)
  4. 从二进制文件中取出符号并将它们与1中的符号进行比较。

另一种方法可能是一些调用图生成工具(例如doxygen)。

I dont know Visual C, and had also recommended the -Wunreachable-code specific coverage tools. As solution for your situation I would try the following:

  1. Make with ctags (or similar programm) a list of all your symbols in your source
  2. Enable in your compiler the dead code elimination (I would assume it defaults to on)
  3. Enable your whole-program/link time optimizations (so he knows that not used functions in your moduls are not required by other externals and get discarded)
  4. Take the symbols from your binary and compare them with the symbols from 1.

Another approach could be some call graph generating tool (e.g. doxygen).

仅一夜美梦 2024-07-16 04:30:11

我建议您使用几种方法:
1. GCC 有一些有用的编译标志:

-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter

2. Cppcheck 有一些有用的功能,例如:

 --enable=unusedFunction

3. 按照之前的建议使用静态分析器。

I suggest you use a couple approaches:
1. GCC has some useful compilation flags:

-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter

2. Cppcheck has some useful features like:

 --enable=unusedFunction

3. Use static analyzer as was suggest before.

攀登最高峰 2024-07-16 04:30:11

对我有用的一种方法(使用 Delphi)是启用调试,并在调试器下运行程序。

当Delphi程序在调试器下运行时,IDE会在空白处显示哪些代码行可以设置为断点。 真正死的代码 - 即已被链接器/编译器删除的代码是显而易见的,因为无法在那里设置断点。

一些额外的注释,因为评论者似乎误解了这一点:

a:您不需要尝试在每一行上设置断点。 只需在 IDE 中打开源文件,然后快速滚动浏览即可。 死代码很容易被发现。

b:这不是“代码覆盖率”检查。 您不需要运行应用程序来查看它是否到达线路。

c:我对VS2008不够熟悉,所以不能说这个建议是否有效。

One approach that works for me - with Delphi - is to enable debugging, and run your program under the debugger.

When a Delphi program is run under the debugger, the IDE shows in the margin which lines of code can be set as breakpoints. Code which is truly dead - i.e., has been stripped out by the linker/compiler is obvious as breakpoints can't be set there.

Some additional notes, as commenters seem to misunderstand this:

a: You don't need to try setting a breakpoint on each line. Just open up the source file in the IDE, and quickly scroll through it. Dead code is easily spotted.

b: This is NOT a 'code coverage' check. You don't need to run the application to see if it reaches the lines.

c: I'm not familiar enough VS2008 so can't say if this suggestion will work.

黄昏下泛黄的笔记 2024-07-16 04:30:11

要么
1) MSVC 在内置静态分析工具中未得到充分利用。
2) MSVC marketplace 有很多工具,包括对大多数免费工具的支持,包括 CppCheck

您将需要最新版本的 Visual Studio 来运行市场应用程序,但免费的“< a href="https://www.visualstudio.com/free-developer-offers/" rel="nofollow noreferrer">社区版" 的许可非常宽松。

Either
1) MSVC's under-used in built static analysis tool.
2) The MSVC marketplace has lots of tools including support for most free tools, including CppCheck

You will need the latest version of Visual Studio for market place applications, but the free "Community Edition" has very lenient licencing.

原来分手还会想你 2024-07-16 04:30:11

编写一个脚本,随机删除一个函数(从源代码中)并从头开始重新编译所有内容。 如果它仍然可以编译 - 该函数就是死代码。

Write a script that randomly deletes a function (from the source code) and recompiles everything from scratch. If it still compiles - that function was dead code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文