Dead Code Detection in DeusData: Finding Functions with Zero Callers While Excluding Entry Points
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:
- Candidate identification – The system collects every node labeled
FunctionorMethodfrom the knowledge graph. - 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 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 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,exportin JavaScript, orpublicin C# - Predefined naming conventions – Well-known names including
main,init, andstart
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:
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– BuildsCALLSedges between functions during the indexing phase, establishing the complete call graph topology.src/pipeline/pass_entry_points.c– Detects language-specific entry points and sets theentry_point=trueproperty on applicable nodes.
CLI Tool Execution
The Dead code detection tool defined in 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:
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:
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 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 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
CALLSedges 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.cfor graph construction andsrc/pipeline/pass_entry_points.cfor entry point detection. - The Cypher query filters nodes where
NOT f.entry_pointand no incomingCALLSrelationships exist. - Performance averages approximately 150 milliseconds for typical codebases.
- Tests in
tests/test_ui.cverify 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 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 constructs the call graph edges, src/pipeline/pass_entry_points.c marks entry points, and src/cli/cli.c executes the dead code detection query and returns results.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →