assert-cpp-definition-is-used
Assert that every C++ definition in a tree is reached from a root of the program.
Why
A function whose last caller is deleted does not disappear. Its tests still pass and the coverage gate over its file stays green, because a test that calls a function runs its lines. Nothing in a normal toolchain reports it.
A loose check asks whether the definition's name is written anywhere
else. In C++ that question proves nothing. Overloads and members such as
size or get are everywhere, and two dead functions that call each
other both pass. C++ names can be tied to definitions, because the
compiler knows which definition each name refers to, so this tool asks
the stricter question: can the definition be reached from where the
program starts?
Installation
pip install assert-cpp-definition-is-used
The package depends on the libclang wheel, which carries a libclang
shared library and clang's Python bindings.
Usage
cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# Definitions in src/ that main and the exported C interface never reach
assert-cpp-definition-is-used --build-dir build \
--roots-declared-in include/vpi_user.h \
--roots-declared-in include/svdpi.h \
src
# Shared test code that no test reaches
assert-cpp-definition-is-used --build-dir build \
--sources test --sources lib/cpp \
lib/cpp
Each translation unit named in compile_commands.json whose source file
lies under a --sources tree is parsed with its own command line. Those
units make up the program. The positional trees are the ones whose
definitions are checked; a definition outside them, such as one in the
standard library, is followed but never reported.
Options
| Option | Effect |
|---|---|
--build-dir DIR |
The directory holding compile_commands.json. Required. |
--sources TREE |
Parse only units under this tree. Repeatable. Defaults to the trees. |
--root NAME |
A fully qualified function the program is entered through. Repeatable. Defaults to main. |
--roots-declared-in HEADER |
Every function this header declares is a root. Repeatable. |
--libclang PATH |
The libclang shared library to parse with. |
--jobs N |
How many units are parsed at once. Defaults to the processors. |
--exclude PATTERNS |
Comma-separated globs. A matching source is not parsed, and a definition in a matching file is not reported. |
--quiet |
Print nothing; report through the exit code. |
--count |
Print only how many definitions are dead. |
--verbose |
Print each unit parsed, each fallback on a dependent name, and a summary. |
--fail-fast |
Report only the first dead definition. |
--warn-only |
Always exit 0. |
Exit codes
| Code | Meaning |
|---|---|
| 0 | Nothing is dead |
| 1 | Something is dead |
| 2 | The build directory or its compile_commands.json is missing or unreadable, a unit failed to parse, libclang could not be loaded, or a --root or --roots-declared-in named nothing |
Output
Each dead definition prints on its own line, sorted by path and then by line:
src/main.cpp:3:6: function pong is reached from no root
src/shapes.h:12:21: member function Square::size is reached from no root
Parsing with the build's clang
The bundled libclang carries none of clang's own headers, such as
stddef.h and stdarg.h, so a unit that includes the standard library
may fail to parse with it. Point --libclang at the libclang of an
installed LLVM, which finds its headers beside itself:
assert-cpp-definition-is-used --build-dir build \
--libclang /usr/lib/llvm-18/lib/libclang.so.1 src
Using the same version as the compiler in compile_commands.json also
keeps the parse from tripping over flags or language features the bundled
version does not know. A unit that does not parse is an error, not a
skipped file, because whatever it alone uses would be reported as dead.
The bundled Python bindings do not list every kind of node libclang can
hand back: they miss the flag_enum attribute, which standard library
headers use, and every kind added after LLVM 18. A node of a kind they do
not list is searched for references inside a definition and passed over
outside one, as an unexposed node is.
What a definition is
Every one of these is checked:
- free functions and function templates
- member functions, static ones included, and constructors, destructors and conversion functions
- classes, structs, unions, class templates and their partial specializations
- enumerations and their enumerators
- type aliases and alias templates, whether written with
usingortypedef - variables and constants at namespace scope
- static and non-static data members
Local variables and parameters are left out, since the compiler's own unused warnings already cover them. An anonymous class, union or enumeration has no name to report, so it is never reported itself, though its named members are.
A definition is one entity however many units include it. The tool keys
each entity by its USR, the unified symbol resolution string clang gives
every declaration, so an inline function in a header that 50 files
include is one definition, and two overloads of one name are two. An
entity with internal linkage, such as a static function or one in an
anonymous namespace, is also keyed by the file that declares it.
What counts as a use
A definition is used when it is reachable. Every root is reachable, and
so is every entity that a reachable definition references. A reference is
anything the compiler resolves to the entity: a call, taking its address
to register a callback, naming a type, a base class or a template
argument, reading or writing a member, an enumerator in a case label,
an operator such as a + b or x[i], and the begin, end and
iterator operators a range for calls.
A definition referenced only by itself, or only by definitions that are
themselves unreachable, is dead. A unit outside --sources is never
parsed, so a call from a test never counts.
A use of a template's instantiation is a use of the template: calling
Box<int>::get reaches Box::get. Using a member reaches the class that
holds it.
Calls the compiler makes
Some calls appear in no source text. These rules cover them:
- When a class is reachable, so are its destructor, its constructors and
every one of its virtual member functions. Code outside the parsed
program, such as a library, may call a virtual through a base class,
and a constructor is often called from inside a standard template such
as
std::make_unique. - When a reachable function is a coroutine, so are its promise type and
the members of it the compiler calls:
get_return_object,initial_suspend,final_suspend,return_void,return_value,unhandled_exception,await_transformandyield_value, and theawait_ready,await_suspendandawait_resumemembers of what they return. A promise type chosen by a specialization ofstd::coroutine_traitsis found by name instead. - A variable at namespace scope, or a static data member, whose
initializer calls a function that is not
constexprorconstevalis a root, because that initializer runs beforemain. A googletestTESTis one: it defines a static data member whose initializer registers the test. A variable whose initializer is constant is used only when something references it.
Dependent names
Inside a template, a name that depends on a template parameter cannot be
resolved until the template is instantiated, and clang's tree of the
template's own text leaves it unresolved. The tool counts such a name as
a use of every entity with that name: machine.start() in a template
reaches every start, and a dependent a < b reaches every operator<.
A range for over a dependent range does the same for begin, end and
the iterator operators.
Only this fallback makes the result a lower bound rather than an exact
answer, and --verbose names each place it happens inside the checked
trees:
Falling back to every entity named start: src/launch.h:3:46
GitHub Actions
- name: Assert every definition is reached from a root
uses: 10U-Labs/assert-cpp-definition-is-used@latest
with:
build-dir: build
libclang: /usr/lib/llvm-18/lib/libclang.so.1
roots-declared-in: include/vpi_user.h include/svdpi.h
trees: src
verbose: true
Over shared test code, where the test programs are the program:
- name: Assert every test helper is reached from a test
uses: 10U-Labs/assert-cpp-definition-is-used@latest
with:
build-dir: build
libclang: /usr/lib/llvm-18/lib/libclang.so.1
sources: test lib/cpp
trees: lib/cpp
License
Apache-2.0
Metadata
Release files for assert-cpp-definition-is-used 20261130073847
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| assert_cpp_definition_is_used-20261130073847.tar.gz | 63.8 kB | With provenance | Details for assert_cpp_definition_is_used-20261130073847.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| assert_cpp_definition_is_used-20261130073847-py3-none-any.whl | Python 3 | none | any | Details for assert_cpp_definition_is_used-20261130073847-py3-none-any.whl |
Total release size: 83.6 kB
Release files / assert_cpp_definition_is_used-20261130073847.tar.gz
| Download URL | assert_cpp_definition_is_used-20261130073847.tar.gz |
|---|---|
| Size | 63.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d6a0c0740b88989c7a9d75081ec16dacbdd942b2868281ec90ed431d27db5f22
|
|
BLAKE2b-256 checksum How to use checksums |
20d5f1d2900ac972f69fd87b75640e1592a97076b8fb02dec527e5b60df9ca26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logRelease files / assert_cpp_definition_is_used-20261130073847-py3-none-any.whl
| Download URL | assert_cpp_definition_is_used-20261130073847-py3-none-any.whl |
|---|---|
| Size | 19.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0ea277690cb0b97757044fc4757aa5996b26f1eaf80ff872a7a103786a09d8da
|
|
BLAKE2b-256 checksum How to use checksums |
7fa1687a287107c8deddd2240dc463ab15fa1be5f3fdeab2d734c604d6a2d5f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logSecurity for assert-cpp-definition-is-used 20261130073847
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for assert_cpp_definition_is_used-20261130073847.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.