Skip to content

feat(callgraph): C/C++ explicit type inference engines#673

Open
shivasurya wants to merge 1 commit intoshiva/cpp-module-registryfrom
shiva/cpp-type-inference
Open

feat(callgraph): C/C++ explicit type inference engines#673
shivasurya wants to merge 1 commit intoshiva/cpp-module-registryfrom
shiva/cpp-type-inference

Conversation

@shivasurya
Copy link
Copy Markdown
Owner

Summary

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

  • graph/callgraph/resolution/c_types.goCTypeInferenceEngine, CFunctionScope, CVariableBinding. Tracks return types and per-function variable scopes drawn directly from source declarations.
  • graph/callgraph/resolution/cpp_types.goCppTypeInferenceEngine 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

  • go build ./...
  • go test ./... — full suite green (25 packages)
  • go test -race ./graph/callgraph/resolution/... — clean
  • go vet ./...
  • golangci-lint run ./graph/callgraph/resolution/ — 0 issues
  • Coverage on changed lines: c_types.go 100%, cpp_types.go 100%
  • 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)

Add CTypeInferenceEngine and CppTypeInferenceEngine under the
resolution package. The engines index types that appear verbatim in
C/C++ source — function return types, variable declarations, class
method return types, and class field types — with Confidence=1.0 and
Source="declaration". No inference, deduction, or propagation; later
phases layer those on top.

Highlights:
  - Function-scoped variable bindings track reassignment history; the
    latest binding wins on lookup.
  - C++ engine embeds the C engine by value so callers can use
    ExtractReturnType / GetScope / GetVariable uniformly.
  - C++ `auto` is recognised by exact match and stored with
    Confidence=0.0, Source="unresolved_auto" so resolvers skip it
    until Phase 2 deduces a concrete type.
  - Thread-safe via two RWMutex pairs on the C engine and two
    additional pairs for class-method and class-field maps; verified
    under `go test -race`.

Sets up the typed receiver lookups consumed by PR-07 (C call graph)
and PR-08 (C++ call graph).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@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
@shivasurya shivasurya added enhancement New feature or request go Pull requests that update go code labels 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.28%. Comparing base (b9a6ef5) to head (47d63d3).

Additional details and impacted files
@@                      Coverage Diff                      @@
##           shiva/cpp-module-registry     #673      +/-   ##
=============================================================
+ Coverage                      85.18%   85.28%   +0.09%     
=============================================================
  Files                            180      182       +2     
  Lines                          26237    26399     +162     
=============================================================
+ Hits                           22351    22515     +164     
+ Misses                          3024     3023       -1     
+ Partials                         862      861       -1     

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

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