LinuxCommandLibrary

cppclean

Clean C++ code by removing unused includes

TLDR

Run in a project's directory

$ cppclean [path/to/project]
copy

Run on a project where the headers are in the inc1/ and inc2/ directories
$ cppclean [path/to/project] --include-path [inc1] --include-path [inc2]
copy

Run on a specific file main.cpp
$ cppclean [main.cpp]
copy

Run on the current directory, excluding the "build" directory
$ cppclean [.] --exclude [build]
copy

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)

Copied to clipboard