iwyu (Include What You Use) has just been accepted in Debian.
This tool, based on LLVM/Clang, checks the headers of a C/C++ file. It will detect which include directives are useless, what could be improve and add comments to explain why #include are there.
Let's take this C file:
#include <stdio.h>
#include <malloc.h>
#include <limits.h>
int foo(void) {
fprintf(stderr, "an error\n");
return INT_MAX;
}
$ iwyu foo.c
will return:
foo.c should remove these lines:
- #include <malloc.h> // lines 2-2
The full include-list for foo.c:
#include <limits.h> // for INT_MAX
#include <stdio.h> // for fprintf, stderr
---
Package on Debian