https://github.com/wojciechmula/cleanup-headers
Remove unnecessary includes from C/C++ source files
https://github.com/wojciechmula/cleanup-headers
c cpp dependencies includes
Last synced: about 2 months ago
JSON representation
Remove unnecessary includes from C/C++ source files
- Host: GitHub
- URL: https://github.com/wojciechmula/cleanup-headers
- Owner: WojciechMula
- Created: 2018-01-20T09:40:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-04T17:23:15.000Z (over 7 years ago)
- Last Synced: 2025-04-01T16:21:14.715Z (about 1 year ago)
- Topics: c, cpp, dependencies, includes
- Language: Python
- Size: 13.7 KB
- Stars: 27
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
================================================================================
Cleanup C++/C headers
================================================================================
This simple script helps in removing unnecessary includes from C/C++ files.
A huge code base or legacy code usually means that implementation files are
full of includes pilled up over years. Likewise, creating a new project by
forking an old one ends up with tons of leftovers.
How it works?
-----------------------------------------------------------
The only thing you need is **the full command** that creates an object file.
The script systematically comments out one include file at once and recompiles
source. When program/object file still compiles, then the commented out include
is considered unneeded.
**Caveat 1** (noticed by my colleague Leszek): such a mechanical way of removing
includes may lead to creating indirect dependencies. Some symbols required by
implementation might be provided (accidentally) by includes present in headers
files. When one remove include from the implementation file, then later changes
to the header file might break the compilation.
**Caveat 2**: the script doesn't interpret any other preprocessor directive
than ``#include``. Conditional includes enabled by ``ifdefs`` will be removed
unconditionally.
How to use it?
-----------------------------------------------------------
Here's an example from my toy project https://github.com/WojciechMula/avx512popcnt-superoptimizer
The head of the main program::
$ head -n 15 avx512popcnt.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "binary.cpp"
When run ``make``, we capture the command which builds the program::
$ make avx512popcnt
g++ -Wall -Wextra -pedantic -std=c++14 -O3 avx512popcnt.cpp -o lineavx512popcnt
Now the script comes::
$ python cleanup.py g++ -Wall -Wextra -pedantic -std=c++14 -O3 avx512popcnt.cpp -o avx512popcnt
Checking compilation of avx512popcnt.cpp... OK
Removing cstdint (1/12)... OK
Removing cstdlib (2/12)... OK
Removing cstdio (3/12)... OK
Removing cassert (4/12)... not possible
Removing vector (5/12)... OK
Removing memory (6/12)... not possible
Removing random (7/12)... not possible
Removing algorithm (8/12)... OK
Removing bitset (9/12)... OK
Removing sys/types.h (10/12)... OK
Removing unistd.h (11/12)... not possible
Removing binary.cpp (12/12)... not possible
avx512popcnt.cpp: not required cstdint, cstdlib, cstdio, vector, algorithm, bitset, sys/types.h
avx512popcnt.cpp was updated
It turned out that eight includes weren't needed at all.
Configuration
-----------------------------------------------------------
You can control behaviour of the script via a config file. The config
file must be located either in ``~/.config/cleanup-headers/config.ini``
or its path must be provided by the environment variable
``CLEANUP_HEADERS_CONFIG``. Please refer to the sample ``config.ini``
for more details.