Metadata-Version: 2.4
Name: assert-cpp-definition-is-used
Version: 20261130080635
Summary: CLI tool to assert that every C++ definition in a tree is reached from a root of the program
Author-email: 10U Labs <dev@10ulabs.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/10U-Labs/assert-cpp-definition-is-used
Project-URL: Repository, https://github.com/10U-Labs/assert-cpp-definition-is-used
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: libclang>=18.1.1
Dynamic: license-file

# 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

```bash
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

```bash
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 or its syntax tree could not be read, 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:

```text
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:

```bash
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.

Each release is tested with LLVM 22's libclang, through the bundled
bindings and through LLVM 22's own, apt.llvm.org's `python3-clang-22`,
put ahead of them on `PYTHONPATH`.

## 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:

```text
Falling back to every entity named start: src/launch.h:3:46
```

## GitHub Actions

```yaml
- 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:

```yaml
- 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
