Skip to content

feat: add C++ support to the C-family language plugins - #36

Merged
MCR-3 merged 6 commits into
mainfrom
34-cpp-language-support
Oct 22, 2026
Merged

MCR-3 merged 6 commits into
mainfrom
34-cpp-language-support

Conversation

@MCR-3

@MCR-3 MCR-3 commented Oct 22, 2026

Copy link
Copy Markdown
Member

Closes #34

Summary

A new cpp Language plugin reads .cc .cpp .cxx .hpp .hh .hxx, and a plugin can now claim an extension conditionally, so .h goes to C++ only when the picked root holds C++ files.

analyze_sources
  for each distinct extension (once per load)
    first plugin listing it in conditional_extensions whose claims() is true
      CppLanguage.claims(".h") = any path has a C++-only extension
    else its unconditional owner (C keeps .c and .h)
 backend/languages/
 ├── base.py        # + conditional_extensions, claims()
 ├── cfamily.py     # + C++ params, defaults, &/&&, packs
 ├── c.py
+├── cpp.py         # scopes, classes, templates, overloads, receiver typing
 └── __init__.py    # + CppLanguage registered
+sample_cpp_project/
 frontend/src/languages.ts   # + cpp entry (badge "C++"), Prism cpp grammar

Call resolution in cpp.py:

f(x)          class + bases -> enclosing namespaces -> using -> C's include visibility
Cls::m(), ns::f()   qualifier looked up outwards, then the name in it
obj.m(), p->m()     receiver's declared type (param, local, data member), bases walked;
                    type not visible (auto, template param) -> the one loaded method of that name
base->run()         Base::run when it has a body; never fans out to overrides
several candidates  link only if the argument count leaves exactly one

Calls inside a lambda belong to the enclosing Function, foo<int>(x) and Box<T>::get() hit the template's single card, and overloads get a parameter-type suffix only when they collide in one file.

Evidence

  • Before (main): python -m backend.analyzer sample_cpp_project reads only models.h, as C: 1 file, 0 functions, 0 edges.
    After: 11 files, 29 functions, 49 edges, every file cpp, including the .h.

  • C-only projects unchanged: analyzer output for sample_project, sample_rust_project, sample_typescript_project, sample_c_project and sample_rust_nested_project is byte-identical to main. .h stays c in sample_c_project.

  • Each commit stands alone: every commit imports the backend, analyzes sample_c_project and builds the frontend. npm run lint passes.

  • Spot checks on the fixture and a scratch probe:

base->run()                    -> Base::run (not Derived::run)
auto z = a; z.ping()           -> A::ping   (one loaded method)
auto z = a; z.get()            -> no edge   (A::get and B::get)
twice<int>(3), Box<int>.get()  -> the template's single Function
lambda body calls helper()     -> edge from the enclosing Function
add(1) with add(int)/add(double) -> no edge (argument count can't pick)

Merge Danger

Door: two-way. Additive, and reverting the squash commit restores the previous behavior.

Blast Radius: backend dependency

Adds tree-sitter-cpp to requirements.txt, so the Render image needs a rebuild and the module is imported at startup; if the wheel were missing, the whole backend would fail to import, not just C++. I installed it under Python 3.11 in the backend image and /api/health and /api/analyze work. .h files in a mixed folder now show the C++ badge instead of C.

Data members are typed from their declaration, so items_.size() on a std::vector links nothing instead of falling back to a unique loaded size. The issue lists "a field" among the cases that use the fallback, so this deviates from it on purpose, to avoid false wires. Unparseable macro-heavy headers keep the functions tree-sitter recovered and are flagged, as in C.

MCR-3 added 6 commits October 11, 2026 10:45
…ally

C owns `.h` outright, but a header in a project that also holds C++ files
is C++. The dispatcher mapped each extension to exactly one plugin, so it
had no way to express that.

A plugin can now list `conditional_extensions` and decide per load, in
`claims()`, whether it takes them. The dispatcher asks once per distinct
extension, in registry order, and falls back to the unconditional owner
when nobody claims it, so a load that has no claimant routes exactly as
before.
…helpers

The C++ plugin reuses these helpers, but a few C++ parameter and
declarator shapes were invisible to them: default arguments, `&` and
`&&` declarators, parameter packs and the bare `...` token. Teach
`extract_params` and the declarator walkers about them; none of these
nodes occur in a C tree, so the C plugin's output is unchanged.

`walk_top_level` now uses a stack instead of recursion so deeply nested
`#if` blocks cannot exhaust Python's recursion limit.
Reads `.cc .cpp .cxx .hpp .hh .hxx`, and claims `.h` when the picked
root holds any of those, with tree-sitter-cpp and the C-family helpers.
Functions are definitions with a body: free functions, in-class and
out-of-class methods, constructors, destructors and operators, named by
their scoped qualname, with a parameter-type suffix only for overloads
that collide in one file.

Calls resolve by C++'s own scoping (class and bases, namespaces, using
declarations, then the C plugin's include-based visibility) and member
calls by the receiver's declared type. When a type is not visible the
call links only to the one loaded method of that name, and a tie never
produces a guessed edge. Virtual calls link to the declared class's own
method, never fanning out to overrides.
A small catalogue app in the same spirit as the other sample projects,
written to exercise what the C++ plugin resolves: a namespace, a class
with in-class and out-of-class methods, a constructor, destructor and
operator, overloads, function and class templates, a lambda, a virtual
call through a base reference, an `auto` receiver, and a `.h` header that
is C++ because `.cpp` files sit beside it.
Registers the `cpp` language with its extensions, a badge color that
keeps the dark monogram text above 4.5:1 contrast, and the Prism `cpp`
grammar, and names C++ in the empty-state hints. `.h` stays on the C
entry: the backend decides per project which plugin reads a header, and
the frontend only displays the language each file carries.
Adds C++ to the supported-languages table, the sample projects and the
backend tour, with the plugin's limits, and the C++ fixture to the
analyzer regression check. Records the `.h` routing rule in CLAUDE.md
and the C/C++ separation in the glossary, since neither is visible from
reading a single plugin.

Closes #34
@MCR-3
MCR-3 merged commit 631bc2f into main Oct 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add C++ support to the C-family language plugins

1 participant