cppclean
Clean C++ code by removing unused includes
TLDR
Run in a project's directory
Run on a project where the headers are in the inc1/ and inc2/ directories
Run on a specific file main.cpp
Run on the current directory, excluding the "build" directory
SYNOPSIS
cppclean [options] files|directories
PARAMETERS
-h, --help
Show this help message and exit
--version
Show program's version number and exit
-v, --verbose
Print extra diagnostic information
--ignore_includes=PATTERN
Regex to ignore matching includes
--ignore_func=PATTERN
Regex to ignore matching functions
--ignore_var=PATTERN
Regex to ignore matching variables
--ignore_type=PATTERN
Regex to ignore matching types
--ignore_namespace=PATTERN
Regex to ignore matching namespaces
DESCRIPTION
cppclean is a static analysis tool for identifying unused elements in C++ source code. It scans files or directories to detect unused includes, functions, variables, types, namespaces, and macros, helping developers remove dead code. This improves code maintainability, reduces build times, and enforces cleaner coding practices.
Using a lightweight parser, cppclean tracks symbol definitions and usages without requiring a full compiler. It prints warnings to stdout with line numbers and suggestions, allowing easy integration into build scripts or IDEs. Filters via regular expressions enable ignoring false positives or project-specific patterns.
Particularly valuable in large codebases, it complements tools like linters but focuses on deadcode removal. Run it recursively on directories for comprehensive cleanup. While not infallible with advanced C++ features like templates, it catches most common issues effectively.
CAVEATS
Uses simplified parsing; may miss complex cases like templates, lambdas, or heavy macros. Potential false positives/negatives. Requires Python 2/3.
EXAMPLE USAGE
cppclean -v *.cc
Scans C++ files verbosely, reports unused includes/functions.
cppclean --ignore_includes=google/ src/
Processes directory, ignores Google includes.
INSTALLATION
Clone from GitHub: git clone https://github.com/google/styleguide.git
Run cppclean.py directly (Python script). Some distros package it.
HISTORY
Developed by Google around 2010 for C++ style enforcement. Open-sourced on GitHub; maintained sporadically for legacy codebases.
SEE ALSO
cppcheck(1), clang-tidy(1), include-what-you-use(1)


