在 Delphi 中查找未使用的(又名“死的”)代码

发布于 2024-10-03 13:30:24 字数 260 浏览 11 评论 0原文

是否有任何工具可以确定是否使用了函数/过程/方法/整个类?

我见过一些工具可以帮助知识渊博的开发人员追踪未使用的代码片段(大多数适用于 Delphi 以外的语言),但大多数都有一个陡峭的学习曲线和/或需要大量的挖掘来确定是否使用了代码片段。

据称,Delphi 的智能链接器这样做是为了减少最终可执行文件的大小。不幸的是,智能链接器上给出的少量信息并没有表明检索从堆中剔除的内容的方法。

诚然,即使有可能找出智能链接器正在消除的内容,它的剔除也可能非常保守。

Are there any tools that can determine if a function/procedure/method/entire class is used?

I've seen tools that can help a knowledgeable developer track down unused fragments of code (most are for languages other than Delphi) but most have a steep learning curve and/or require considerable digging to determine if a code fragment is used or not.

Delphi's smart linker purportedly does this to reduce the size of the final executable. Unfortunately what little information is given on the smart linker doesn't indicate a way to retrieve what was culled from the hurd.

Admittedly, even if it is possible to find out what the smart linker is eliminating it may be very conservative with its culling.

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

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

发布评论

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

评论(8

薄情伤 2024-10-10 13:30:24

虽然笨拙且不是全局的,但您可以检查页边空白处的点的源代码,如下所示。您可以在这些行上设置断点,因为代码尚未消除死代码。

我相信,这种情况发生在程序层面。我不知道有什么方法可以在不太粗略的水平上确定这一点。

替代文本

Although clumsy and not global, you can examine the source code for the dots in the margin, as shown below. These are lines on which you can set breakpoints because the code hasn't been deadcode eliminated.

This happens, I believe, at the procedure level. I don't know a way to determine this at a less coarse level.

alt text

女皇必胜 2024-10-10 13:30:24

该线程讨论删除未使用的单元,并讨论 Icarus Pascal Analyzer 等工具。该工具将满足您的所有需求,甚至更多。
如何从uses子句中“自动”删除未使用的单位?

This thread discusses removing unused units, and talks about tools like Icarus Pascal Analyzer. That tool will do all you need and more.
How to "automatically" remove unused units from uses clause?

七月上 2024-10-10 13:30:24

您还可以查看最常用的商业分析器/覆盖工具 AQTime,来自 http://www.automatedqa.com

以下是有关功能的视频:http://www.automatedqa.com/products/ aqtime/screencasts/coverage-profiling/

You could also check the most commonly used commercial profiler/coverage tool, AQTime from http://www.automatedqa.com

Here's a video on features: http://www.automatedqa.com/products/aqtime/screencasts/coverage-profiling/

罪歌 2024-10-10 13:30:24

您可以使用代码覆盖率工具来查找未使用的代码。只需运行应用程序并手动执行所有部分即可。代码覆盖率报告将显示哪些部分没有被触及。 (在单元测试上运行该工具没有帮助,因为未使用的代码仍然可以进行单元测试)。

此处提供了免费的(开源)Delphi 代码覆盖率工具。运行后,检查报告中是否有红线,这些是尚未到达的线。

Delphi代码覆盖率是一个简单的代码
用于创建的 Delphi 覆盖工具
代码覆盖率报告基于
详细的地图文件。

每个单元都会有一个
总结了unit.html
覆盖范围,其次是来源
已标记。绿线被覆盖。
红线不是被覆盖的线。这
其他行没有生成代码
为了它。还有一个
CodeCoverage_summary.html 文件
总结了覆盖范围并有链接
到生成的单位报告。

也许作者可以在未来的版本中添加“搜索死代码”功能。

You can use a Code Coverage tool to find unused code. Just run the application and execute all parts manually. The code coverage report will show which parts have not been touched. (Running the tool on the unit tests is not helpful because unused code still can have unit tests).

A free (open source) Code Coverage Tool for Delphi is available here. After running it, check for red lines in the reports, these are the lines which have not been reached.

Delphi Code Coverage is a simple Code
Coverage tool for Delphi that creates
code coverage reports based on
detailed MAP files.

For each unit there will be a
unit.html with a summary of the
coverage, followed by the source
marked up. Green lines were covered.
Red lines were not covered lines. The
other lines didn't have code generated
for it. There is also a
CodeCoverage_summary.html file that
summarizes the coverage and has links
to the generated unit reports.

Maybe the author can add a 'search for dead code' feature in a future version.

梦断已成空 2024-10-10 13:30:24

过去,我使用 Free Pascal 的“生成汇编器”功能编译了源代码,然后制作了一些对源代码进行操作的简单过滤器程序。 (因为知道智能链接是使用链接器“节”粒度完成的这一点很有用)

有时它会让您了解为什么某些内容没有智能链接出来(例如,因为某些表中可能有一个引用可能被初始化中的某些东西访问)

当然Delphi不是FPC,但是当你有提示要寻找什么时,只需在Delphi编译后查看页边空白处的点,看看它是否是智能链接的。同样,如果您想知道为什么某些代码没有被智能链接出来,分析一个使用 FPC 编译为汇编程序的小示例程序可以使原因显而易见。 (例如,您可以找到带有参考的 RTTI 表)

FPC 路线允许系统化的路线来搜索“为什么”?某些内容是否被消除,而映射文件和代码覆盖率仅告诉您它是否被链接,而不是为什么。

有关智能链接的简短说明,请参阅 Delphi:在uses子句中列出未使用的单元有哪些缺点?

In the past I've compiled sources with Free Pascal using the "generate assembler" functionality, and then made some simple filter programs that operate on the source. (for this bit is useful to know that smartlinking is done using linker "section" granularity)

It sometimes gives you insights why certain things are not smartlinked out (e.g. because there is a reference in some table that might be accessed by something in an initialization)

Of course Delphi is not FPC, but when you have hints what to look for, it is a matter of looking at the dots in the margin after a Delphi compile to see if it is smartlinked or not. Likewise, if you have wonder why certain code has (not) been smartlinked out, analyzing a small example program compiled to assembler with FPC can make the reason obvious. (e.g. you find RTTI tables with a reference to it)

The FPC route allows for a systematic route to search the WHY? something is eliminated or not, while map file and code coverage only tells you it is or is not linked in, not why.

For a short description of smart linking see Delphi: Which are the downsides of having unused units listed in the uses clause?

少钕鈤記 2024-10-10 13:30:24

查看 http://www.peganza.com/ 上的工具来帮助您完成一些任务。

Icarus 是免费软件,它会分析您的“使用”子句,以便您删除未使用的引用。

Pascal Analyzer 是完整版本,包括 Icarus 功能和大量其他工具。

Checkout the tools at http://www.peganza.com/ to help with some of your tasks.

Icarus is freeware and it analyzes your 'uses' clauses to allow you to remove unused references.

Pascal Analyzer is the full version which includes Icarus functionality and a ton of other tools.

蓬勃野心 2024-10-10 13:30:24

也许 CodeHealer 可以帮助您处理未使用的代码

CodeHealer 执行深入
分析源码,寻找
存在以下几个方面的问题:

审核:质量控制规则,例如未使用或无法访问的代码、使用
Delphi 指令名称和关键字为
标识符,隐藏他人的标识符
在更高的范围内具有相同的名称,
等等。
检查:潜在错误,例如未初始化或未引用
标识符,危险的类型转换,
自动类型转换,未定义
函数返回值,未使用
指定值等等。
指标:代码属性的量化,例如圈数
复杂性、对象之间的耦合
(数据抽象耦合),评论
比率、类数、行数
代码等等。

Maybe CodeHealer could help you with unused code

CodeHealer performs an in-depth
analysis of source code, looking for
problems in the following areas:

Audits: Quality control rules such as unused or unreachable code, use of
Delphi directive names and keywords as
identifiers, identifiers hiding others
of the same name at a higher scope,
and more.
Checks: Potential errors such as uninitialised or unreferenced
identifiers, dangerous type casting,
automatic type conversions, undefined
function return values, unused
assigned values, and more.
Metrics: Quantification of code properties such as cyclomatic
complexity, coupling between objects
(Data Abstraction Coupling), comment
ratio, number of classes, lines of
code, and more.

离线来电— 2024-10-10 13:30:24

对于单位,请使用 Pascal 分析器。您可能需要多次使用它。

对于在类工厂(等)中注册的对象/类,您需要手动仔细检查,因为它们将被完全编译。这样做的原因是编译器不知道您是否实际使用它们或不是。

对于方法,您需要检查蓝点。不太实用,所以还有另一种方法(根据我调查同一主题时被告知的情况)。您需要在启用详细地图文件的情况下进行完整构建。完成后,您需要比较源以查看映射文件中是否有条目。如果不是,则代码不会编译为 - 可能是死代码(可能是因为如果它是组件,那么您可能不使用该功能)。

For units, use Pascal Analyzer. You might need to use it a multiple of times.

For objects/classes that are registered in class factories (and the like), you will need to double check manually, as they will be fully compiled in. The reason for this is that the compiler doesn't know if you actually use them or not.

For methods, you need to check for the blue dots. Not very practical, so there is another way (from what I have been told when I investigate the same topic). You need to do a full build with a detailed map file enabled. Once that is done, you need to compare the source to see if there is an entry in the map file. If not, then the code is not compiled in - possibly dead code (possibly because if it is component, then it might be that you don't use that functionality).

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