xunused

Find unused functions and methods

xunused is a tool to find unused C/C++ functions and methods across source files in the whole project. It is built upon clang to parse the source code.

Example

Just pass the compilation database as an argument to xunused.

xunused compile_commands.json
[8/342] Processing file /Users/dennis/projects/neomutt/email/globals.c
[1/342] Processing file /Users/dennis/projects/neomutt/sidebar/sidebar.c
[3/342] Processing file /Users/dennis/projects/neomutt/conn/zstrm.c
[2/342] Processing file /Users/dennis/projects/neomutt/postpone/postpone.c
[...]
color/parse_ansi.c:75: warning: Function 'ansi_skip_sequence' is unused
mutt/buffer.c:701: warning: Function 'buf_lower' is unused
/Users/dennis/projects/neomutt/mutt/buffer.h:81: note: declared here
mutt/buffer.c:715: warning: Function 'buf_upper' is unused
/Users/dennis/projects/neomutt/mutt/buffer.h:82: note: declared here
config/set.c:1083: warning: Function 'cs_str_delete' is unused
/Users/dennis/projects/neomutt/config/set.h:291: note: declared here
config/set.c:885: warning: Function 'cs_str_native_get' is unused
/Users/dennis/projects/neomutt/config/set.h:284: note: declared here
config/set.c:706: warning: Function 'cs_str_string_get' is unused
[...]

Note: Some platforms may need an extra parameter to locate headers such as stdbool.h

xunused -extra-arg=-I/usr/lib/clang/17/include compile_commands.json

Excluding test code

To find functions that are only used by tests but not any other code we need to exclude our test code.

In theory the -filter could be used for that. Unfortunately that only works for including paths and not for excluding. And the underlying regular expression engine doesn’t support negative lookahead so something like -filter='.+^((?!test).)*$' also doesn’t help.

The easy brute force solution is to simply remove the test folder before running xunused.

Search by Algolia