8.1.15. Dead Code Analysis

8.1.15.1. Background

You have a C/C++ project and want to modify the default settings for dead code analysis.

8.1.15.2. What can be configured?

Useful things to consider:

  • Is the code you analyze a library DLL?

  • Do you have additional entry points, e.g. interrupts?

In addition, if you are using Qt, enable Frameworks-QtSupport to avoid dead-code messages for methods generated via the Q_OBJECT macro. This rule defines these methods as additional entry points.

8.1.15.3. What needs to be done?

Dead code analysis looks for so-called entry points into the code and starts a reachability analysis on the basis of the call graph information. It covers static and dynamic calls and accounts for virtual function calls. In addition, any function whose address is taken inside an alive function is considered alive.

On a per-project level:

  1. By convention, the default entry points are functions named main, DllMain, WinMain, etc.

  2. For DLLs on Windows, activate rule EntryPoints-EntriesByDLLExport to collect all DLL-exported functions as entries, technically speaking

    Setting parameters for DLLs.
    #define DLLEXPORT __declspec(dllexport)
    #define LIB_XY_API DLLEXPORT
    extern LIB_XY_API std::string base_dir();
    
  3. Define additional entry points with the other rules in the rule group EntryPoints, e.g. using the link name (R DMAHandler()) or via interrupts in code (__attribute__((interrupt))). Sample code:

    Setting entries via interrupts.
    #ifdef __AXIVION__
    #define INTERRUPT __attribute__((interrupt))
    #else
    #define INTERRUPT __interrupt
    #endif
    

On a cross-project level:

  • Things become more complicated and you will have to define a meta project that collects the results from all prior projects and that analyzes these results in addition. Note that you may want to suppress all other analyses.

  • Build all of your projects and collect the various RFG files from the projects.

  • Use the rule Architecture-CombinedDeadCode or Architecture-VariantDeadCode in order to calculate the dead code using the individual RFG files from the projects collected above.