Remove Unused Header Files

The Perl scripts below are included in source/ (as of r8223) to help deal with headers included by .cpp and other header files. These scripts rely on the workhorse perl module.

For each .cpp file in the current directory, remove unused header files. These are identified by omitting the relevant #include statement from the body of the code and looking for compilation errors. Headers included by both the .cpp file and another header are removed from the .cpp file. Headers imported from preprocessor #if-blocks are ignored. A known limitation is that include statements issued from within #define--#undef blocks can be erroneously omitted; case in point, physconst.cpp. Some files may have to be edited by hand, or even reverted. Caution is advised.

The number of threads can be specified on the command line. A list of source files may be issued after it; by default, the script operates on all the source files in the directory. The script admits the -d option, before the number of threads, to perform a dry run: a summary file is produced, but no source files are modified. It is useful to run prior to any updates.


$ ./ 16
239 of 348 files contained redundant headers
headers included: 3482, redundant:  689, duplicates:    9

The above command takes about 3 minutes to run on nephos.

Note that the script generates a number of temporary files (one per thread), eg, test-0.cpp, to carry out the compilations. In the event of an aborted run, these would have to be removed by hand before re-running the script.

Minimally compile each header file to determine if other header files are required for a successful compilation. Error messages generated are parsed for "not declared" variables, and the appropriate headers are included. This process is repeated until no errors occur. By default, cddefines.h is included in the compilation, but not reported.

The number of threads may be issued on the command line.


$ ./ 16
         gammas.h:       thermal.h
      mole_priv.h:       mole.h
 physconst_priv.h:       physconst.h

The above command completed in about 4 seconds on nephos.

As for, make sure you delete all test-*.cpp following an aborted run, before re-running this script.

Display a tree of header calls for the entire project, or the header files issued on the command line.


$ ./ quantumstate.h 
  |       |_____algorithm

Display a list of all the headers listed in the call-tree of a header file. If no headers are issued on the command line, the script operates on all the headers present in the current directory.


$ ./ quantumstate.h 
   quantumstate.h:       energy.h proxy_iterator.h algorithm

List all the headers directly included by the header file in question. The script operates on all headers in the current directory, if none are issued on the command line.


$ ./ quantumstate.h 
   quantumstate.h:       energy.h proxy_iterator.h

Like, it produces a Christmas tree of header dependencies for the specified source files. By default, it operates on all source files in the directory.


$ ./ called.cpp noexec.cpp