“深”头依赖分析
我从事一个中型 C/C++ 项目,我已经应用了 Doxygen+Graphviz。它的标题图很有用,但它们仅显示基于#include
的关系。我有兴趣找到一个(最好是基于 Linux 的)工具,它不仅基于 #include
还基于实际的符号使用来分析文件依赖关系。例如,这样的工具不仅会显示 a.cpp
包含 bh
,而且会显示 a.cpp
使用 SomeClass 是在
bh
包含的 ch
中声明的。它还能够建议可以修剪的标头包含。
I work on a mid-sized C/C++ project to which I've already applied Doxygen+Graphviz. Its header graphs are useful, but they only show relationships based on #include
. I'm interested in finding a (preferably linux-based) tool that analyzes file dependencies based not just on #include
, but on actual symbol usage. For example, such a tool would not only show that a.cpp
includes b.h
, but that a.cpp
uses SomeClass
that's declared in c.h
included by b.h
. It'd also be able to suggest header includes that could be pruned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我之前使用过包含您使用的内容,效果非常好。它使用 Clang 解析 C++ 代码,并建议添加要添加的前向声明和要删除的头文件。
一个缺点是它对代码的布局做出了假设 - 基本上是 Google 编码标准。因此,如果您有一个名为
SomeClass.cpp
的文件,它只会查看SomeFile.h
。此外,建议的包含使用项目根目录的完整路径(因此#include "src/SomeClass.h"
而不是#include "SomeClass.h"
)。最后,无论如何,我将我的代码更改为这个约定,因为它避免了歧义,但如果你尝试它,它需要注意。通常,您只需设置 CC=include-what-you-use 并重建即可获得结果 - 它使用所有 clang 机制来解析包含参数。有一个 python 程序使用结果自动更新 #include 行。
编辑:
另一个工具不那么复杂,但设置更简单,并且可以建议删除
#include
,它是去头。它的工作原理是将 C++ 文件复制到临时位置,删除#include
并重新编译。如果重新编译有效,则可以安全地删除该头文件。它不会建议前向声明或任何奇特的东西,但它可以减少实现文件中不必要的包含行。I've used Include What You Use before with pretty good results. It uses Clang to parse the C++ code and suggest forwards declarations to add and header files to remove.
One drawback is that it makes assumptions about the layout of your code - basically the Google coding standards. So it will only look at
SomeFile.h
if you have a file calledSomeClass.cpp
. Also the suggested includes use full paths from the root of your project (so#include "src/SomeClass.h"
instead of#include "SomeClass.h"
). In the end I changed my code to this convention anyway as it avoids ambiguity, but it needs a heads up in case you try it.Usually you can just set
CC=include-what-you-use
and rebuild to get the results - it uses all the clang machinery to parse-I
include arguments. There's a python program that uses the result to automatically update your #include lines.EDIT:
Another tool that is not as sophisticated, but is simpler to set up and can suggest
#include
s to remove is deheader. It works by copying your C++ file to a temporary location, removing an#include
and recompiling. If the recompile works, then it's safe to remove that header file. What it won't do is suggest forward declarations or anything fancy, but it can cut down on needless include lines in your implementation files.