# Dead Code Detection in DeusData: Finding Functions with Zero Callers While Excluding Entry Points

> DeusData's dead code detection finds unused functions by querying its knowledge graph for zero-caller nodes, excluding essential entry points. Uncover hidden inefficiencies now.

- Repository: [Martin Vogel/codebase-memory-mcp](https://github.com/DeusData/codebase-memory-mcp)
- Tags: deep-dive
- Published: 2026-07-24

---

**Dead code detection in DeusData identifies unused functions by querying the internal knowledge graph for nodes with zero incoming `CALLS` edges while filtering out functions marked as entry points through properties, decorators, or predefined naming conventions.**

DeusData's `codebase-memory-mcp` repository implements sophisticated dead code detection to help developers identify unreachable functions across multiple programming languages. This system traverses an internal **knowledge graph** to distinguish between truly unused code and legitimate entry points that must remain intact. The detection mechanism combines graph traversal algorithms with language-specific heuristics to deliver accurate results in approximately 150 milliseconds.

## How Dead Code Detection Works

The dead code detection system operates through a two-step filtering process that analyzes the codebase's call graph representation.

### The Two-Step Filtering Process

**Codebase-Memory-MCP** applies a rigorous two-stage filter to identify genuinely dead code:

1. **Candidate identification** – The system collects every node labeled `Function` or `Method` from the knowledge graph.
2. **Entry point exclusion** – Functions are eliminated from consideration if they qualify as entry points through specific metadata or naming conventions.

After filtering, the system queries for functions with **zero incoming `CALLS` edges**, indicating no other code references them as a callee.

### Identifying Candidate Functions

During the indexing phase, the pipeline in [`src/pipeline/pass_calls.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_calls.c) constructs the `CALLS` edge set by analyzing function invocations throughout the codebase. Each function node stores its relationships in an in-memory SQLite-backed graph database, enabling fast traversal and querying of the call topology.

### Excluding Entry Points

The [`src/pipeline/pass_entry_points.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_entry_points.c) module marks functions as entry points using three distinct criteria:

- **Explicit property flag** – Functions carrying `entry_point = true`
- **Language-specific decorators** – Annotations like `#[allow(dead_code)]` in Rust, `export` in JavaScript, or `public` in C#
- **Predefined naming conventions** – Well-known names including `main`, `init`, and `start`

## The Graph Query Implementation

The core detection logic executes a **Cypher query** against the graph database to isolate functions with no callers while respecting entry point exclusions.

### Cypher Query Structure

The dead code detection tool constructs the following query in [`src/cli/cli.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/cli/cli.c):

```cypher
MATCH (f:Function)
WHERE NOT f.entry_point               // exclude entry points
  AND NOT EXISTS { (f)<-[:CALLS]-() } // no callers
RETURN f

```

This query filters out entry-point nodes and verifies the absence of incoming `CALLS` relationships, returning only truly unreachable functions.

## Implementation Architecture

The detection system spans multiple components across the codebase, from pipeline passes to the command-line interface.

### Pipeline Components

Two critical pipeline passes prepare the graph for dead code analysis:

- **[`src/pipeline/pass_calls.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_calls.c)** – Builds `CALLS` edges between functions during the indexing phase, establishing the complete call graph topology.
- **[`src/pipeline/pass_entry_points.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_entry_points.c)** – Detects language-specific entry points and sets the `entry_point=true` property on applicable nodes.

### CLI Tool Execution

The **Dead code detection** tool defined in [`src/cli/cli.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/cli/cli.c) orchestrates the query execution. When invoked, it builds the Cypher query, sends it to the in-memory graph, and returns the qualified names of dead functions according to the source code implementation.

## Practical Usage Examples

Developers can interact with dead code detection through the CLI, programmatic API, or configuration files.

### CLI Usage

Execute dead code detection for a specific project:

```bash
codebase-memory-mcp cli dead_code_detection --project myproj

```

This command returns a list of functions with zero callers, excluding all detected entry points.

### Programmatic API

Integrate dead code detection directly into custom tools using the C library API:

```c
const char *query =
    "MATCH (f:Function) "
    "WHERE NOT f.entry_point "
    "  AND NOT EXISTS { (f)<-[:CALLS]-() } "
    "RETURN f.qualified_name AS name";

cbm_query_result_t *res = cbm_query_graph(project, query);

```

This approach allows embedding detection logic into CI/CD pipelines or custom analysis tools.

### Custom Entry Point Configuration

Projects can override default entry point detection by creating a [`.codebase-memory.json`](https://github.com/DeusData/codebase-memory-mcp/blob/main/.codebase-memory.json) configuration file. Adding the `entry_point=true` property to specific functions ensures the MCP parser treats them as legitimate entry points during dead code analysis.

## Verification and Testing

The test suite in [`tests/test_ui.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/tests/test_ui.c) validates the detection accuracy. At lines 590-601, the layout test creates a node named `deadfn` with no callers and asserts that the system reports its status as `"dead"`. This confirms that the graph query correctly omits entry points while flagging true dead code, as implemented in the DeusData source.

## Summary

- **Dead code detection** in DeusData identifies functions with zero incoming `CALLS` edges in the knowledge graph.
- The system excludes entry points using three mechanisms: property flags, language-specific decorators, and predefined naming conventions.
- Implementation spans [`src/pipeline/pass_calls.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_calls.c) for graph construction and [`src/pipeline/pass_entry_points.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_entry_points.c) for entry point detection.
- The **Cypher query** filters nodes where `NOT f.entry_point` and no incoming `CALLS` relationships exist.
- Performance averages approximately **150 milliseconds** for typical codebases.
- Tests in [`tests/test_ui.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/tests/test_ui.c) verify that functions without callers are correctly classified as dead while entry points remain excluded.

## Frequently Asked Questions

### How does DeusData distinguish between dead code and entry points?

DeusData applies a multi-layered filter that checks for the `entry_point=true` property, language-specific export decorators like `#[allow(dead_code)]` or `export`, and well-known entry point names such as `main` or `init`. Only functions failing all these checks and having zero callers are flagged as dead code.

### What query language does the dead code detection use?

The system uses **Cypher** to query the internal knowledge graph. The specific query matches `Function` nodes where the `entry_point` property is false and no incoming `CALLS` relationships exist, effectively isolating unreachable functions.

### Can I customize which functions are treated as entry points?

Yes. You can add the `entry_point=true` property to functions in a [`.codebase-memory.json`](https://github.com/DeusData/codebase-memory-mcp/blob/main/.codebase-memory.json) configuration file. The MCP parser honors these overrides during the dead code detection pass, ensuring custom entry points are excluded from the dead code report.

### Where is the dead code detection logic implemented in the source code?

The logic is distributed across three main files: [`src/pipeline/pass_calls.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_calls.c) constructs the call graph edges, [`src/pipeline/pass_entry_points.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/pipeline/pass_entry_points.c) marks entry points, and [`src/cli/cli.c`](https://github.com/DeusData/codebase-memory-mcp/blob/main/src/cli/cli.c) executes the dead code detection query and returns results.