It would be awesome if there would be a Remove unnecessary #includes - Feature for C++
I know this feature was often wished but it was too hard for you guys to implement. Why do you not choose a brute force approach to find out which includes are needed and which are not. When manually done you just comment out one #include after another and do a recompile to find out if a certain include file is really needed. Some time ago I thought about programming a small VB Macro within Visual Studio that just does this automatically - just find each line that contains a #include -> uncomment -> recompile -> if it worked -> just add a comment behind that header file like "VAssistX Detected, that this header file is not needed and commented it out for that reason"
In simple cases this approach should work - the final result can look like this
#include "car.h"
#include "wheel.h"
// #include "foo.h" /* VAssistX Detected, that this header file is not needed and commented it out for that reason */
#include "bar.h"
Things get more complicated if for example wheel.h needs to know something about car.h but car.h and wheel.h are not needed - what I mean is that both header files could be safely removed without a compilation error, but when only one header file is removed the simple brute force approach doen't work.
What you really need to do is to uncomment all permutations of header files to find out what is really necessary and what is not
Example:
#include "h1.h"
#include "h2.h"
#include "h3.h"
#include "h4.h"
All permutations:
0000 (0 = comment header file out, 1 = leave it unchanged)
0001
0010
0011
..
1111
Some permutation can be skipped because one file can only depend on another if its included later than the file it depends on - also its more likely that depending header files are grouped together, so a combination of 110111111111111111011 is not as mush likely as 110011111111111111111.
Mabye you gather some statistical data from real world projects to find more common patterns - maybe you can allow the user to check for some patterns first.
Recompilation time shouldn't be a big problem (you change only one .cpp file and maybe it is possible to do parallel compilation)
There should be a dialog showing the currently checked header file code (110111111111111111011)
It should be possible to abort this brute force search. Even if it take a long time this is not a problem because it can be started when leaving work an run overnight - in the morning the ongoing search can be aborted and the results can be viewed.
I have real world project with moth than 30 includes in some implementation files. I am always too lazy to check out manually which header files are needed - I am sure there at least 5 files that are not needed but it cost to mush time to find out which ones are not needed - the downside is that I have unnecessary decencies in my code.