Repository navigation
unused code
The is a lot of unused code in CFS, only a minor set might be of interest for further used. Most is dead code which not only slows down compilation but makes problems in refactoring code and often spoils output with warnings (it's often the bad code that is not used).
Be warned! The option -Wunused-function is not very helpfull: "Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall."
All code (beside cfs.o) resides as static libs in the < build >/lib directory.
We can list all content via
nm *.a | grep -E ' [TDBW] ' > all.txt
A sample line is
00000000 T _ZN12CoupledField18XMLMaterialHandler17ReadThermoelasticEPNS_12BaseMaterialEPNS_9ParamNodeE
which becomes via c++filt
00000000 T CoupledField::XMLMaterialHandler::ReadThermoelastic(CoupledField::BaseMaterial*, CoupledField::ParamNode*)
Removing the first 12/20 (32/64 bit machines) characters from the beginning of the lines is done via cut
Now, several (template) code occurs in multiple libs: e.g.
CoupledField::StdVector<CoupledField::ParamNode*>::GetSize() constwe solve this by first applying sort and the using uniq. Note that uniq only finds duplicated lines in sequence, therefore we have to sort first.
Altogether this is (executed in lib)
nm *.a | grep -E ' [TDBW] ' | c++filt | cut -b 12- | sort | uniq > lib.txt
Similar we get the used code via
nm cfs | grep -E ' [TDBW] ' | c++filt | cut -b 12- | sort | uniq > bin.txt
Clearly you could include further executables like cfstool.
One can compare bin.txt against lib.txt via kompare or any other diff tool. One can also use comm as
comm -1 -3 bin/bin.txt lib/lib.txt > dead_code.txt
This option is not really helpful but was research during this task - maybe it helps somehow.
What one has to do, is to compile with -fdata-sections -ffunction-sections which means: "Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section's name in the output file."
A way quick and dirty (temporary) way to do this, is in cmake_modules/compiler.cmake at the very last lines:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -ffunction-sections ${CFS_C_FLAGS} ${CFS_CXX_FLAGS} ${CFS_PROF_FLAGS} ${CFS_OPT_FLAGS} ${CFS_SUPPRESSIONS}")
Now one can automatically eliminate unused code by the linker option -gc-sections. One can also print what is elemeninated via --print-gc-sections (for the newer linkers). This can be added to the build process by
SET(CMAKE_EXE_LINKER_FLAGS ... -Wl,-gc-sections -Wl,--print-gc-sections")
But it seems that the output of --print-gc-sections does not do what one would expect.