Skip to main content

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.

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 using or typedef
  • 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_transform and yield_value, and the await_ready, await_suspend and await_resume members of what they return. A promise type chosen by a specialization of std::coroutine_traits is found by name instead.
  • A variable at namespace scope, or a static data member, whose initializer calls a function that is not constexpr or consteval is a root, because that initializer runs before main. A googletest TEST is 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 20261130064240

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for assert-cpp-definition-is-used 20261130064240
File Size Uploaded Provenance Actions
assert_cpp_definition_is_used-20261130064240.tar.gz 62.6 kB With provenance Details for assert_cpp_definition_is_used-20261130064240.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for assert-cpp-definition-is-used 20261130064240
File Interpreter ABI Platform Actions
assert_cpp_definition_is_used-20261130064240-py3-none-any.whl Python 3 none any Details for assert_cpp_definition_is_used-20261130064240-py3-none-any.whl

Total release size: 82.2 kB

Release files / assert_cpp_definition_is_used-20261130064240.tar.gz

Download URL assert_cpp_definition_is_used-20261130064240.tar.gz
Size 62.6 kB
Tags Source
SHA-256 checksum
How to use checksums
35d97c38fb5d34480c1d47431c2381a7b2708d4386d60bf3b0d24801fc7f6b3f
BLAKE2b-256 checksum
How to use checksums
8b64eea586ca4b3cca196d818c15e8554be843a8fe42ea1fa46d9bd84bada87f
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 log

Release files / assert_cpp_definition_is_used-20261130064240-py3-none-any.whl

Download URL assert_cpp_definition_is_used-20261130064240-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f13eaf72f45bf2318a5ee89f8b4bdb4863978c7c6a59155a776702e0176aa421
BLAKE2b-256 checksum
How to use checksums
1f4c5ae8e32209ec7e429daffcf697823d002482d704b2840825a03cdb4f99df
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 log

Security for assert-cpp-definition-is-used 20261130064240

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.

Select from 2 files

Attestation for assert_cpp_definition_is_used-20261130064240.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page