在 Delphi 中查找未使用的(又名“死的”)代码
是否有任何工具可以确定是否使用了函数/过程/方法/整个类?
我见过一些工具可以帮助知识渊博的开发人员追踪未使用的代码片段(大多数适用于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
虽然笨拙且不是全局的,但您可以检查页边空白处的点的源代码,如下所示。您可以在这些行上设置断点,因为代码尚未消除死代码。
我相信,这种情况发生在程序层面。我不知道有什么方法可以在不太粗略的水平上确定这一点。
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.
该线程讨论删除未使用的单元,并讨论 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?
您还可以查看最常用的商业分析器/覆盖工具 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/
您可以使用代码覆盖率工具来查找未使用的代码。只需运行应用程序并手动执行所有部分即可。代码覆盖率报告将显示哪些部分没有被触及。 (在单元测试上运行该工具没有帮助,因为未使用的代码仍然可以进行单元测试)。
此处提供了免费的(开源)Delphi 代码覆盖率工具。运行后,检查报告中是否有红线,这些是尚未到达的线。
也许作者可以在未来的版本中添加“搜索死代码”功能。
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.
Maybe the author can add a 'search for dead code' feature in a future version.
过去,我使用 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?
查看 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.
也许 CodeHealer 可以帮助您处理未使用的代码
Maybe CodeHealer could help you with unused code
对于单位,请使用 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).