Skip to content

feat(callgraph): C/C++ module registry and include resolution#672

Merged
shivasurya merged 1 commit intomainfrom
shiva/cpp-module-registry
May 3, 2026
Merged

feat(callgraph): C/C++ module registry and include resolution#672
shivasurya merged 1 commit intomainfrom
shiva/cpp-module-registry

Conversation

@shivasurya
Copy link
Copy Markdown
Owner

Summary

Adds the FQN foundation for the C/C++ call-graph builder.

  • graph/callgraph/core/c_module_types.goCModuleRegistry (file-prefix, includes, function index) and CppModuleRegistry (embeds C registry plus namespace and class indices).
  • graph/callgraph/registry/c_module.goBuildCModuleRegistry, BuildCppModuleRegistry, and BuildCIncludeMap walk the parsed CodeGraph and produce read-only registries.

FQN format

Shape Example
C function src/net/socket.c::connect_to_server
C++ free function (namespaced) src/utils.cpp::mylib::process
C++ class method src/socket.cpp::mylib::Socket::connect
C++ class method (no namespace) src/app.cpp::App::run
C++ free function (no namespace) src/main.cpp::main

Include resolution order

For #include \"...\", first match wins:

  1. Directory of the source file
  2. <projectRoot>/include/<header>
  3. <projectRoot>/src/<header>
  4. <projectRoot>/<header>

System includes (#include <...>) are skipped — they are owned by a future stdlib registry.

Design notes

  • Method-to-class association uses byte-range containment within the same file. The registry never reads parser-internal context state, so it stays composable across future parser refactors.
  • Files outside the project root (..-prefixed relative paths) are dropped at registry-build time.
  • FunctionIndex deliberately preserves duplicates across header and source files so the call-graph builder can choose between declaration and definition.
  • appendUnique dedupes per-key entries within a single file (defensive against repeated graph visits).

Test plan

  • go build ./...
  • go test ./... — full suite green (25 packages)
  • go vet ./...
  • golangci-lint run ./graph/callgraph/registry/ ./graph/callgraph/core/ — 0 issues
  • Coverage: core 94.3%, registry 91.7% on the new files
  • 9 spec test cases covered: empty graph, files+functions, duplicate across files, duplicate same file, outside project root, namespace+class index, method-without-namespace, on-disk include resolution (4 search dirs + system + missing), language filter, plus defensive paths (orphan method, empty header name, directory-named-as-header).

Stacked on

shiva/cpp-parser (#671)

@shivasurya shivasurya added enhancement New feature or request go Pull requests that update go code labels May 2, 2026
@shivasurya shivasurya self-assigned this May 2, 2026
@safedep
Copy link
Copy Markdown

safedep Bot commented May 2, 2026

SafeDep Report Summary

Green Malicious Packages Badge Green Vulnerable Packages Badge Green Risky License Badge

No dependency changes detected. Nothing to scan.

View complete scan results →

This report is generated by SafeDep Github App

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 2, 2026

Code Pathfinder Security Scan

Pass Critical High Medium Low Info

No security issues detected.

Metric Value
Files Scanned 4
Rules 205

Powered by Code Pathfinder

@codecov
Copy link
Copy Markdown

codecov Bot commented May 2, 2026

Codecov Report

❌ Patch coverage is 87.28324% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.19%. Comparing base (cd00b49) to head (7a98308).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
sast-engine/graph/callgraph/registry/c_module.go 86.41% 11 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #672      +/-   ##
==========================================
+ Coverage   85.18%   85.19%   +0.01%     
==========================================
  Files         178      180       +2     
  Lines       26064    26237     +173     
==========================================
+ Hits        22202    22353     +151     
- Misses       3012     3023      +11     
- Partials      850      861      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Owner Author

shivasurya commented May 3, 2026

Merge activity

  • May 3, 1:15 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • May 3, 1:23 PM UTC: Graphite rebased this pull request as part of a merge.
  • May 3, 1:24 PM UTC: @shivasurya merged this pull request with Graphite.

@shivasurya shivasurya changed the base branch from shiva/cpp-parser to graphite-base/672 May 3, 2026 13:21
@shivasurya shivasurya changed the base branch from graphite-base/672 to main May 3, 2026 13:22
Add CModuleRegistry and CppModuleRegistry types under callgraph/core
plus build helpers under callgraph/registry. The registries map source
files to project-relative FQN prefixes, index every function under its
bare name for cross-file resolution, and resolve project-local
#include "..." directives via a fixed search order (same dir, include/,
src/, project root). C++ adds namespace-qualified and class-qualified
indices, with method-to-class association via byte-range containment
so the registry stays decoupled from parser-internal context tracking.

Establishes the FQN foundation for the C/C++ call-graph builder: PR-07
(C) and PR-08 (C++) consume FunctionIndex, NamespaceIndex, ClassIndex,
and Includes to compute caller/callee FQNs and follow header-to-source
edges.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@shivasurya shivasurya force-pushed the shiva/cpp-module-registry branch from b9a6ef5 to 7a98308 Compare May 3, 2026 13:23
@shivasurya shivasurya merged commit ee277fb into main May 3, 2026
6 checks passed
@shivasurya shivasurya deleted the shiva/cpp-module-registry branch May 3, 2026 13:24
shivasurya added a commit that referenced this pull request May 3, 2026
## Summary

Adds the explicit type-tracking foundation for C/C++ call-graph resolution.

- **`graph/callgraph/resolution/c_types.go`** — `CTypeInferenceEngine`, `CFunctionScope`, `CVariableBinding`. Tracks return types and per-function variable scopes drawn directly from source declarations.
- **`graph/callgraph/resolution/cpp_types.go`** — `CppTypeInferenceEngine` embeds the C engine and adds class method / class field indices, plus `auto` handling.

### TypeInfo contract

| Source | Confidence | Used for |
|---|---|---|
| `declaration` | 1.0 | Explicit types from source — return types, variable decls, class members |
| `unresolved_auto` | 0.0 | C++ `auto x = ...` placeholders awaiting Phase 2 deduction |

### Design notes

- **Embedding**: `CppTypeInferenceEngine` embeds `CTypeInferenceEngine` by value, so every C-engine method (`ExtractReturnType`, `GetScope`, `GetVariable`, etc.) is callable on the C++ engine. The embedded `Registry` field aliases the C++ registry's `CModuleRegistry` facet so updates propagate.
- **Reassignment**: each variable name keeps a slice of bindings; `GetVariable` returns the latest. `GetAllBindings` exposes the history for future flow analysis.
- **`auto` detection**: exact equality on `\"auto\"`. Modifiers like `auto*` and `auto&` are concrete types and keep full confidence — they survive the override branch and route through the C engine unchanged.
- **Void returns**: explicitly dropped at registration so void functions never pollute downstream lookups (which gate on `GetReturnType != nil`).
- **Thread safety**: four `sync.RWMutex` instances guard `Scopes`, `ReturnTypes`, `ClassMethods`, `ClassFields`. Snapshot accessors (`GetAllReturnTypes`, `GetAllScopes`) return defensive copies.
- **Lazy scope creation**: `ExtractVariableType` creates the scope on first use; callers do not need to call `AddScope` before the first variable.

## Test plan

- [x] `go build ./...`
- [x] `go test ./...` — full suite green (25 packages)
- [x] `go test -race ./graph/callgraph/resolution/...` — clean
- [x] `go vet ./...`
- [x] `golangci-lint run ./graph/callgraph/resolution/` — 0 issues
- [x] Coverage on changed lines: `c_types.go` 100%, `cpp_types.go` 100%
- [x] Spec test cases covered: NewC*Engine, ExtractReturnType (success + void), AddReturnType, ExtractVariableType (basic + reassignment), GetScope miss, AddScope, concurrent access (-race), embedded methods, RegisterClassMethod (success + void/empty drops + redeclaration), RegisterClassField (success + empty drops), `auto` zero-confidence, `auto*`/`auto&` exact-match, complex C types (pointer/const/struct), complex C++ types (templates / refs / nested templates).

## Stacked on

`shiva/cpp-module-registry` (#672)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant