Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Deep" header dependency analysis

Tags:

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.

like image 548
Reinderien Avatar asked May 04 '11 18:05

Reinderien


1 Answers

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 called SomeClass.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 #includes 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.

like image 118
richq Avatar answered Oct 06 '22 01:10

richq