feat(kernel): R7a C/C++ walker — dual-lang ccpp module, preParse hoist, 7 new blanks, c/cpp default-routed (#1346)

Parity: 0 diffs on redis/git/fmt/protobuf/ALS sweeps; full-init dumps
byte-identical on all five + linux at kernel scale (10.4M dump lines,
same sha256 both arms). Linux 2c/6GB envelope: kernel-arm 19.1min vs
wasm-arm 22.9min (parse 356s vs 435s) on a much richer graph (the new
blanks recover error-swallowed code: git 2x nodes, linux kernel/+mm/ 3x).
Deferral guard corrected by measurement (C/C++ error incidence 9-42%;
--max-deferral flag); defer-reuse memo kills the 3x re-blank/re-parse
cost deferred files paid.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-07-17 16:56:41 -05:00
committed by GitHub
co-authored by Claude Fable 5
parent 44561b6aad
commit 2d72891b59
20 changed files with 3211 additions and 80 deletions
+136
View File
@@ -11162,3 +11162,139 @@ import DataStore from '../data/DataStore';
});
});
});
// R7a preParse additions — the blanking passes added so macro-heavy C/C++
// parses clean enough for the kernel route (each also improves the wasm
// path's own graphs). Offset preservation is load-bearing everywhere.
describe('C/C++ kernel-port preParse blanks (R7a)', () => {
it('blankCCplusplusGuardBodies blanks extern-C guard bodies, keeps directives', async () => {
const { blankCCplusplusGuardBodies } = await import('../src/extraction/languages/c-cpp');
const src = [
'#ifdef __cplusplus',
'extern "C" {',
'#endif',
'int real_decl(void);',
'#ifdef __cplusplus',
'}',
'#endif',
'',
].join('\n');
const out = blankCCplusplusGuardBodies(src);
expect(out.length).toBe(src.length);
expect(out).not.toContain('extern "C"');
expect(out).toContain('#ifdef __cplusplus'); // directives stay
expect(out).toContain('int real_decl(void);');
// A guard with a nested directive bails (needs real preprocessing).
const nested = [
'#ifdef __cplusplus',
'#define EXTERNC extern "C"',
'#endif',
'',
].join('\n');
expect(blankCCplusplusGuardBodies(nested)).toBe(nested);
// The `#ifndef` inverse guard is C-visible and must be untouched.
const inverse = ['#ifndef __cplusplus', 'int c_only(void);', '#endif', ''].join('\n');
expect(blankCCplusplusGuardBodies(inverse)).toBe(inverse);
});
it('blankLoneMacroLines blanks namespace-management macros, spares expression operands', async () => {
const { blankLoneMacroLines } = await import('../src/extraction/languages/c-cpp');
const src = ['FMT_BEGIN_NAMESPACE', 'struct S { int x; };', 'FMT_END_NAMESPACE', ''].join('\n');
const out = blankLoneMacroLines(src);
expect(out.length).toBe(src.length);
expect(out).not.toContain('FMT_BEGIN_NAMESPACE');
expect(out).toContain('struct S { int x; };');
// An ALL-CAPS operand alone on a line inside a multi-line expression is
// NOT a lone macro — the next line starts with an operator.
const expr = ['int x = 0', ' | FLAG_ONE', ' | FLAG_TWO;', ''].join('\n');
expect(blankLoneMacroLines(expr)).toBe(expr);
const cont = ['int y =', 'SOME_FLAG', '| OTHER;', ''].join('\n');
expect(blankLoneMacroLines(cont)).toBe(cont);
// Underscore-free solid words are too risky and stay.
const bare = ['NDEBUG', 'int z;', ''].join('\n');
expect(blankLoneMacroLines(bare)).toBe(bare);
});
it('blankCStatementMacroCalls blanks indented iterator macros, keeps the block', async () => {
const { blankCStatementMacroCalls } = await import('../src/extraction/languages/c-cpp');
const src = [
'static void walk(struct list *head) {',
'\tlist_for_each_entry(pos, head, member) {',
'\t\tuse(pos);',
'\t}',
'}',
'',
].join('\n');
const out = blankCStatementMacroCalls(src);
expect(out.length).toBe(src.length);
expect(out).not.toContain('list_for_each_entry');
expect(out).toContain('use(pos);');
// A real call statement ends with `;` — untouched.
expect(out).toContain('use(pos);');
const call = ['void f(void) {', '\tdo_thing(a, b);', '}', ''].join('\n');
expect(blankCStatementMacroCalls(call)).toBe(call);
// Column-0 `name(args) {` is an implicit-int function definition — untouched.
const kandr = ['main(argc, argv)', '{', '\treturn 0;', '}', ''].join('\n');
expect(blankCStatementMacroCalls(kandr)).toBe(kandr);
// Control-flow keywords are never macros.
const ctrl = ['void g(int x) {', '\twhile (x) {', '\t\tx--;', '\t}', '}', ''].join('\n');
expect(blankCStatementMacroCalls(ctrl)).toBe(ctrl);
});
it('blankCTrailingParamAttrMacros blanks `name UNUSED` params, spares call args', async () => {
const { blankCTrailingParamAttrMacros } = await import('../src/extraction/languages/c-cpp');
const src = 'static int run(int argc UNUSED, const char **argv UNUSED)\n{\n\treturn 0;\n}\n';
const out = blankCTrailingParamAttrMacros(src);
expect(out.length).toBe(src.length);
expect(out).not.toContain('UNUSED');
expect(out).toContain('int argc ');
// A macro CONSTANT as a call argument is preceded by `,`/`(`, never by a
// bare identifier — untouched.
const call = 'void f(void) {\n\tconnect(sock, DEFAULT_TIMEOUT);\n}\n';
expect(blankCTrailingParamAttrMacros(call)).toBe(call);
});
it('blankCKernelAnnotations blanks sparse/section dunders, spares parameterized ones and real types', async () => {
const { blankCKernelAnnotations } = await import('../src/extraction/languages/c-cpp');
const src = [
'static int __init audit_init(void) { return 0; }',
'void copy(void __user *dst, const char *src);',
'__bpf_kfunc void bpf_iter_destroy(struct bpf_iter_num *it);',
'__printf(1, 2) void log_fmt(const char *fmt, ...);',
'struct e *entry = container_of(r, struct audit_entry, rule);',
'__u32 count = 0;',
'',
].join('\n');
const out = blankCKernelAnnotations(src);
expect(out.length).toBe(src.length);
expect(out).not.toContain('__init');
expect(out).not.toContain('__user');
expect(out).not.toContain('__bpf_kfunc');
// Parameterized annotations keep their name — blanking it would strand
// the argument list as a floating parenthesis.
expect(out).toContain('__printf(1, 2)');
// container_of's type-keyword argument blanks; other `struct` keywords stay.
expect(out).toContain('container_of(r, audit_entry, rule)');
expect(out).toContain('struct e *entry');
// Real dunder TYPES are not annotations.
expect(out).toContain('__u32 count');
});
it('restoreDirectiveLines keeps #define lines out of the blanking blast radius', async () => {
const { extractFromSource } = await import('../src/extraction');
// FMT_API matches the _API-suffix member blank; without the directive
// restore the #define loses its NAME and the file gains a parse error.
const src = [
'#define FMT_API FMT_VISIBILITY("default")',
'class Widget {',
' public:',
' int size() const { return 1; }',
'};',
'',
].join('\n');
const result = extractFromSource('lib.hpp', src, 'cpp');
expect(result.errors).toEqual([]);
expect(result.nodes.some((n) => n.kind === 'class' && n.name === 'Widget')).toBe(true);
expect(result.nodes.some((n) => n.kind === 'method' && n.name === 'size')).toBe(true);
});
});
@@ -0,0 +1,84 @@
/* Torture fixture for the C kernel walker (R7a) — exercises every c-path
* branch of the checklist: fn-ptr tables, typedef enum/struct, file-scope
* consts incl. multi-declarator, the macro-prototype misparse skip,
* value-refs (+ shadow prune), the leading-attr-macro preParse blank, and
* the C call shapes. Must parse ERROR-FREE post-preParse or the kernel arm
* defers. */
#include <stdio.h>
#include <sys/socket.h>
#include "local_ops.h"
/** Retry budget for the poller. */
static const int MAX_RETRIES = 3;
static const int LOW_WATER = 2, HIGH_WATER = 8;
static int counter = 0;
static const char *BANNER = "torture";
/* Bare identifier declarators are the macro-prototype misparse shape and are
* skipped by design (uninit scalars are the accepted loss). */
int bare_global;
MYLIB_API config_handle;
/* Leading attribute macro — blanked by preParseCSource (#1211), so the real
* name survives on both arms. */
SEC_ATTR UINT32 masked_entry(VOID) { return 0; }
typedef enum { STATE_IDLE, STATE_RUNNING, STATE_DONE } run_state_t;
typedef struct {
int fd;
void (*on_recv)(int);
} conn_t;
typedef struct conn_pool conn_pool_t;
typedef int (*cb_t)(int);
enum wire_flags { WIRE_A = 1, WIRE_B = 2 };
struct packet {
int len;
unsigned char body[64];
struct packet *next;
cb_t *async_cb;
};
/** Sum helper (docstring). */
static int add(int a, int b) { return a + b; }
static int cb_a(int x) { return add(x, 1); }
static int cb_b(int x) { return add(x, 2); }
/* fn-ptr table at file scope — the ungated 'list' capture positions. */
static cb_t DISPATCH_TABLE[] = { cb_a, cb_b };
static void handle_recv(int fd);
/* struct initializer — the ungated 'value' capture positions. */
static const struct handler_ops OPS = { .recv = handle_recv, .flags = WIRE_A };
static void handle_recv(int fd) {
struct packet pkt;
pkt.len = fd;
printf("fd=%d retries=%d\n", fd, MAX_RETRIES);
}
/* Local shadow of a file-scope const — the shadow prune drops HIGH_WATER as a
* value-ref target while LOW_WATER stays live. */
static int shadowed_reader(void) {
int HIGH_WATER = 99;
return HIGH_WATER + LOW_WATER;
}
static int use_table(int idx, int v) {
cb_t fn = DISPATCH_TABLE[idx];
int r = (*fn)(v);
conn_t c = { 1, 0 };
c.on_recv(r);
return counter + r;
}
static void spawn_workers(void) {
register_handler(cb_a);
signal_connect(&cb_b);
}
@@ -0,0 +1,140 @@
/// Torture fixture for the C++ kernel walker (R7a) — namespaces (incl. C++17
/// nested + anonymous), out-of-line Cls::method defs, templates + template
/// bases, operator definitions, stack construction, local fn-ptrs, UE-macro
/// shapes THROUGH the hoisted preParse, using-aliases, access specifiers,
/// static-member value reads, and the cpp call shapes. Must parse ERROR-FREE
/// post-preParse or the kernel arm defers (spaced operator CALL SITES live in
/// torture-defer.cpp — they produce ERROR nodes by design).
#include <vector>
#include "widget_base.hpp"
namespace app {
/** Engine config (docstring). */
class Config {
public:
int retries;
void apply();
int helper_count() const { return 2; }
private:
int secret;
};
void Config::apply() { retries = helper_count(); }
namespace detail {
struct Counter {
int value;
Counter *next;
};
} // namespace detail
int detail_probe() { return 1; }
} // namespace app
namespace app::net {
class Session {
public:
void open();
virtual ~Session() {}
};
void Session::open() {}
} // namespace app::net
namespace {
int hidden_helper() { return 3; }
} // namespace
template <typename T>
class Base {
public:
T item;
};
template <typename T>
class Box : public Base<T> {
public:
T get() const { return value_; }
T unwrap();
private:
T value_;
};
template <typename T>
T Box<T>::unwrap() {
return value_;
}
class Derived : public Base<int>, private app::Config {
public:
Derived() : total_(0) {}
int total() const { return total_; }
private:
int total_;
};
struct Vec2 {
float x, y;
Vec2 operator+(const Vec2 &o) const { return {x + o.x, y + o.y}; }
explicit operator bool() const { return x != 0 || y != 0; }
Vec2 origin();
};
enum class Mode : unsigned char { Off, On };
enum Legacy { LEGACY_A, LEGACY_B };
typedef struct {
int id;
} packet_t;
using Handle = app::Config;
// UE-macro shapes — every one below is recovered by the hoisted preParse
// (export macro, reflection markup, inline specifier, API member prefix).
class MYMODULE_API Widget : public app::Config {
public:
UPROPERTY(EditAnywhere, Category = "State")
float Health;
FORCEINLINE float GetHealth() const { return Health; }
ENGINE_API virtual void Tick(float Delta);
};
void Widget::Tick(float Delta) { Health += Delta; }
Config GlobalConfig;
int build_number = 7;
template <typename T>
T compute_seed(T v) {
return v + 1;
}
float drive_helper(float v) { return v; }
Widget *make_widget() { return new Widget(); }
float drive() {
Widget local;
app::Config cfg;
Vec2 a{1, 2};
Vec2 b(a);
Vec2 c2(1.5f, 2.5f);
float f = a.x + b.y + c2.x;
make_widget()->Tick(0.5f);
auto kernel = &compute_seed<float>;
if (f > 1) {
kernel = &drive_helper;
}
float r = kernel(f);
int flags = GlobalConfig.retries;
Mode m = Mode::Off;
int leg = LEGACY_A;
app::detail_probe();
compute_seed<int>(2);
auto mp = &app::Config::apply;
(void)mp;
(void)m;
return r + f + flags + leg;
}
@@ -0,0 +1,31 @@
// Torture header for the C++ kernel walker (R7a) — include guard, forward
// declarations (skipped, #1093), extern "C" prototypes, header templates,
// and a UE-reflection-shaped class recovered by the hoisted preParse.
#ifndef TORTURE_HPP
#define TORTURE_HPP
class Forward;
struct Opaque;
extern "C" {
int c_bridge(int value);
}
/// Reusable clamp helper.
template <typename T>
T clamp_value(T v, T lo, T hi) {
return v < lo ? lo : (v > hi ? hi : v);
}
class MYLIB_API Meter : public Forward {
public:
UPROPERTY(BlueprintReadOnly)
int Reading;
FORCEINLINE int Peek() const { return Reading; }
void Calibrate(int target);
Forward *owner();
};
inline void Meter::Calibrate(int target) { Reading = clamp_value(target, 0, 100); }
#endif
+182
View File
@@ -0,0 +1,182 @@
/**
* Kernel↔wasm C/C++ extraction parity (R7a of the kernel migration).
*
* Asserts the native walker (codegraph-kernel/src/ccpp/) produces the SAME
* ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
* unresolved refs compared as canonicalized multisets — over:
* - the checked-in torture fixtures (torture.c / torture.cpp / torture.hpp:
* fn-ptr tables, typedef enum/struct, multi-declarator consts, namespaces
* incl. C++17 nested, out-of-line Cls::method defs, templates + template
* bases, operators, stack construction, local fn-ptrs, UE-macro shapes
* through the hoisted preParse, using-aliases, value-ref shadowing), and
* - Metal/CUDA-shaped sources arriving as language 'cpp' — pinning that the
* route point applies the SAME extension/content-gated preParse blanks to
* the kernel arm (docs/design/ccpp-kernel-port-checklist.md, decision 1/2).
*
* Files with parse errors — including the spaced explicit-operator CALL-SITE
* shape (#1247), which rides an ERROR node — must DEFER to wasm (`defer:`),
* asserted below. The full-repo sweep lives in scripts/kernel-parity.mjs
* (redis/git/fmt et al., run for the §5 gate); this suite keeps the invariant
* alive in `npm test`. Skips when no kernel binary is staged;
* CODEGRAPH_KERNEL_EXPECT=1 turns that into a failure (kernel-scaffold.test.ts).
*/
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { extractFromSource } from '../src/extraction';
import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
import type { ExtractionResult, Language } from '../src/types';
const KERNEL_PATH = path.join(
__dirname,
'..',
'codegraph-kernel',
'prebuilds',
`${process.platform}-${process.arch}`,
'codegraph-kernel.node'
);
const kernelBuilt = fs.existsSync(KERNEL_PATH);
const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
return {
nodes: result.nodes
.map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
.sort(),
edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
refs: result.unresolvedReferences
.map((r) => JSON.stringify(r, Object.keys(r).sort()))
.sort(),
};
}
const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
let savedEnv: Record<string, string | undefined>;
describe.skipIf(!kernelBuilt)('kernel C/C++ extraction parity', () => {
beforeAll(async () => {
await initGrammars();
await loadGrammarsForLanguages(['c', 'cpp']);
});
beforeEach(() => {
savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
resetKernelForTests();
});
afterEach(() => {
for (const k of ENV_KEYS) {
if (savedEnv[k] === undefined) delete process.env[k];
else process.env[k] = savedEnv[k];
}
resetKernelForTests();
});
function assertParity(filePath: string, source: string, language: Language, minNodes = 3): void {
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
const viaKernel = tryKernelExtract(filePath, source, language);
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource(filePath, source, language);
delete process.env.CODEGRAPH_KERNEL;
const k = canon(viaKernel!);
const w = canon(viaWasm);
expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
// Meaningful comparison, not empty-vs-empty (the inline Metal/CUDA
// sources are deliberately small — they pass their exact node count).
expect(viaWasm.nodes.length).toBeGreaterThanOrEqual(minNodes);
}
it('torture fixture (c): fn-ptr tables, typedefs, file-scope consts, value-refs', () => {
const file = path.join(FIXTURE_DIR, 'torture.c');
assertParity('fixtures/torture.c', fs.readFileSync(file, 'utf8'), 'c');
});
it('torture fixture (cpp): namespaces, out-of-line methods, templates, fn-ptrs, UE macros', () => {
const file = path.join(FIXTURE_DIR, 'torture.cpp');
assertParity('fixtures/torture.cpp', fs.readFileSync(file, 'utf8'), 'cpp');
});
it('torture fixture (hpp): fwd decls, extern "C", header templates, reflection markup', () => {
const file = path.join(FIXTURE_DIR, 'torture.hpp');
assertParity('fixtures/torture.hpp', fs.readFileSync(file, 'utf8'), 'cpp');
});
// Metal rides the cpp route: `.metal` maps to language 'cpp' and the
// extension-gated `[[attribute]]` blank must reach the kernel arm through
// the route-point preParse hoist (filePath rides along for the gate).
it('metal-shaped source (.metal → cpp): attribute blanks applied on both arms', () => {
const metal = [
'struct VertexIn {',
' float3 position [[attribute(0)]];',
' float2 uv [[attribute(1)]];',
'};',
'static float2 scale_uv(float2 uv) { return uv; }',
'',
].join('\n');
assertParity('fixtures/shader.metal', metal, 'cpp');
});
// CUDA rides the cpp route too: specifier + launch-config blanks are gated
// by extension OR content, and both fire before the kernel call.
it('cuda-shaped source (.cu → cpp): specifier + launch blanks applied on both arms', () => {
const cuda = [
'__global__ void step_kernel(float *data) { data[0] += 1.0f; }',
'void launch(float *data) { step_kernel<<<1, 256>>>(data); }',
'',
].join('\n');
assertParity('fixtures/kern.cu', cuda, 'cpp');
});
// Every torture fixture again with CRLF line endings — the shape every
// Windows autocrlf checkout has. Derived in memory (not a checked-in CRLF
// file) so no platform or editor can silently normalize it away. Pins the
// JS-multiline-^ docstring semantics for the C comment markers (#1329).
it.each([
['torture.c', 'c'],
['torture.cpp', 'cpp'],
['torture.hpp', 'cpp'],
] as const)('torture fixture CRLF parity: %s', (name, lang) => {
const file = path.join(FIXTURE_DIR, name);
const crlf = fs.readFileSync(file, 'utf8').replace(/(?<!\r)\n/g, '\r\n');
assertParity(`fixtures/${name} (crlf)`, crlf, lang);
});
it('spaced explicit-operator call sites defer to the wasm extractor (#1247 rides an ERROR node)', () => {
const source = [
'struct It { int operator*() const { return 1; } };',
'int read_it(const It &it) { return it.operator *(); }',
'',
].join('\n');
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
expect(tryKernelExtract('src/op.cpp', source, 'cpp')).toBeNull();
// The seam still serves the file — through the wasm path, where the
// operator-call recovery emits the `it.operator*` ref.
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource('src/op.cpp', source, 'cpp');
delete process.env.CODEGRAPH_KERNEL;
expect(
viaWasm.unresolvedReferences.some((r) => r.referenceName === 'it.operator*')
).toBe(true);
});
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
const broken = 'void f( {\n return }} 12 (\n';
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
expect(tryKernelExtract('src/broken.c', broken, 'c')).toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource('src/broken.c', broken, 'c');
delete process.env.CODEGRAPH_KERNEL;
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
});
});
+1 -1
View File
@@ -36,7 +36,7 @@ const kernelBuilt = fs.existsSync(KERNEL_PATH);
// Every kernel-capable language. `jsx` shares the javascript grammar on BOTH
// paths (langs.rs mirrors WASM_GRAMMAR_FILES), so the distinct grammars are:
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go'];
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp'];
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
beforeAll(async () => {