How to define unused macro definitions and typedefs?

It's easy to get a list of unused functions and variables with linker feedback, but how can I detect these unused macro definitions and typedefs? Should I view the code line by line and git grep throughout the project?

+8
c ++ c
source share
4 answers

Static analysis tools for C and C ++ programs may include checking for unused preprocessor macros.

For example, see PC-Lint .

Another possibility would be to go to specific include files and use #ifdef 0 to delete large sections of macros, and then look at the compiler errors using its separation and rest algorithm.

However, I would expect the static analysis tool to be much better, as the size of the source code will become large.

+7
source share

For macros defined in the source files, you can try the -Wunused-macros gcc / clang flag. There are also -Wunused-local-typedefs in gcc.

+7
source share

For unused macros, you can take a look at coan . It has options that could help with this task. From about page :

What characters will appear in active preprocessor directives in a given configuration?

(The preprocessor directive is active if it is not included in the scope of any false #if). Suppose you are interested in the source C in the application, you can display a list of these characters with file names and line numbers with the command:

$> characters coan --recurse --locate --active --once --filter c, h app

It has options for removing conditional code snippets ( #if 0 and friends) and many other useful functions for working with the C preprocessor. I would use it to collect all #define d characters and all #ifdef or defined characters and friends. I would sort and unique these two collections of characters and distinguish them. This is a pretty good way to find typos. Then I will take the histogram separately and start with the least frequent and work on the lists.

For unused typedefs, this is another problem. You can use a program such as cross-references, such as OpenGrok or GNU Global , but this is not very automatic.

+2
source share

There is cscout (now open source):

https://github.com/dspinellis/cscout

which finds unused "extern", #define.

+1
source share

All Articles