diff --git a/crates/codegraph-bench/fixtures/cuda/kernel.cu b/crates/codegraph-bench/fixtures/cuda/kernel.cu new file mode 100644 index 00000000..45fc4646 --- /dev/null +++ b/crates/codegraph-bench/fixtures/cuda/kernel.cu @@ -0,0 +1,18 @@ +__global__ void add_kernel(int* out, const int* a, const int* b) { + int i = 0; + out[i] = a[i] + b[i]; +} + +template +__global__ void scale_kernel(T* x, T factor) { + x[0] = x[0] * factor; +} + +DEFINE_FLASH_FORWARD_KERNEL(my_kernel, int n) { + int i = n; +} + +void launch(int* out, const int* a, const int* b, float* data) { + add_kernel<<>>(out, a, b); + scale_kernel<<>>(data, 2.0f); +} diff --git a/crates/codegraph-bench/fixtures/metal/shader.metal b/crates/codegraph-bench/fixtures/metal/shader.metal new file mode 100644 index 00000000..fdb400c8 --- /dev/null +++ b/crates/codegraph-bench/fixtures/metal/shader.metal @@ -0,0 +1,24 @@ +struct float4 { + float x; + float y; + float z; + float w; +}; + +struct float2 { + float u; + float v; +}; + +struct VertexIn { + float4 position [[position]]; + float2 uv [[user(locn0)]]; +}; + +float4 tint(float4 color) { + return color; +} + +vertex float4 vertex_main(VertexIn in [[stage_in]]) { + return tint(in.position); +} diff --git a/crates/codegraph-bench/tests/equivalence.rs b/crates/codegraph-bench/tests/equivalence.rs index b2afe4a2..9f978623 100644 --- a/crates/codegraph-bench/tests/equivalence.rs +++ b/crates/codegraph-bench/tests/equivalence.rs @@ -79,6 +79,43 @@ fn cpp_db_is_self_equivalent_to_cpp_golden() { assert_equivalent(&cpp_db(), &cpp_golden_dir()).unwrap(); } +#[test] +fn generated_golden_matches_committed_metal_fixture() { + // Guards Metal (#1121): `.metal`→cpp mapping + the `[[attribute]]` blank that + // prevents the spurious `VertexIn extends float4` inheritance edge. + let tempdir = TestDir::new("generated-golden-metal"); + write_golden(&metal_db(), tempdir.path()).unwrap(); + + let expected = load_golden(&metal_golden_dir()).unwrap(); + let actual = load_golden(tempdir.path()).unwrap(); + + diff_canonical(&expected, &actual, None).unwrap(); +} + +#[test] +fn metal_db_is_self_equivalent_to_metal_golden() { + assert_equivalent(&metal_db(), &metal_golden_dir()).unwrap(); +} + +#[test] +fn generated_golden_matches_committed_cuda_fixture() { + // Guards CUDA (#1172 CUDA-lang parts): `.cu`→cpp mapping, the `<<<…>>>` + // launch-config blank that preserves the host→kernel Calls edge (plain + + // templated), and macro-defined-kernel name recovery (`my_kernel`). + let tempdir = TestDir::new("generated-golden-cuda"); + write_golden(&cuda_db(), tempdir.path()).unwrap(); + + let expected = load_golden(&cuda_golden_dir()).unwrap(); + let actual = load_golden(tempdir.path()).unwrap(); + + diff_canonical(&expected, &actual, None).unwrap(); +} + +#[test] +fn cuda_db_is_self_equivalent_to_cuda_golden() { + assert_equivalent(&cuda_db(), &cuda_golden_dir()).unwrap(); +} + #[test] fn tier1_node_drift_is_reported() { let expected = load_golden(&mini_golden_dir()).unwrap(); @@ -157,6 +194,22 @@ fn cpp_golden_dir() -> PathBuf { workspace_root().join("reference/golden/cpp") } +fn metal_db() -> PathBuf { + workspace_root().join("reference/golden/metal/colby.db") +} + +fn metal_golden_dir() -> PathBuf { + workspace_root().join("reference/golden/metal") +} + +fn cuda_db() -> PathBuf { + workspace_root().join("reference/golden/cuda/colby.db") +} + +fn cuda_golden_dir() -> PathBuf { + workspace_root().join("reference/golden/cuda") +} + struct TestDir { path: PathBuf, } diff --git a/crates/codegraph-extract/src/engine.rs b/crates/codegraph-extract/src/engine.rs index 01fa73b9..aa71de28 100644 --- a/crates/codegraph-extract/src/engine.rs +++ b/crates/codegraph-extract/src/engine.rs @@ -58,6 +58,10 @@ pub fn builtin_language_for_ext(ext: &str) -> Option { "java" => Language::Java, "c" | "h" => Language::C, "cpp" | "cc" | "cxx" | "hpp" | "hxx" => Language::Cpp, + // Metal Shading Language (≈ C++14) and CUDA (≈ C++ + dialect tokens) both + // ride the C++ grammar with a dialect-specific pre-parse blank; no new + // `Language` variant (upstream maps all three to `cpp`). + "metal" | "cu" | "cuh" => Language::Cpp, "cs" => Language::CSharp, "php" | "module" | "install" | "theme" | "inc" => Language::Php, "rb" | "rake" => Language::Ruby, @@ -203,7 +207,7 @@ pub fn extract_source( duration_ms: start.elapsed().as_millis() as i64, }; } - let parsed_source = spec.pre_parse(source); + let parsed_source = spec.pre_parse(source, file_path); let Some(tree) = parser.parse(&parsed_source, None) else { return ExtractionResult { nodes: Vec::new(), @@ -859,6 +863,14 @@ mod tests { assert_eq!(detect_language("src/lib.rs"), Language::Rust); } + #[test] + fn metal_cu_cuh_map_to_cpp() { + assert_eq!(detect_language("s.metal"), Language::Cpp); + assert_eq!(detect_language("k.cu"), Language::Cpp); + assert_eq!(detect_language("k.cuh"), Language::Cpp); + assert_eq!(Language::ALL.len(), 36); + } + #[test] fn extract_source_unknown_language_yields_empty_no_error() { let result = extract_source("mystery.unknownext", "content", None); diff --git a/crates/codegraph-extract/src/lang/c.rs b/crates/codegraph-extract/src/lang/c.rs index a498e410..e4e16d10 100644 --- a/crates/codegraph-extract/src/lang/c.rs +++ b/crates/codegraph-extract/src/lang/c.rs @@ -79,6 +79,13 @@ impl LanguageSpec for CSpec { fn extract_import(&self, node: Node<'_>, source: &str) -> Option { include_import(node, source) } + fn pre_parse(&self, source: &str, _file_path: &str) -> String { + if crate::lang::cpp::looks_like_cuda_source(source) { + crate::lang::cpp::blank_cuda_constructs_str(source) + } else { + source.to_string() + } + } } pub(crate) fn include_import(node: Node<'_>, source: &str) -> Option { @@ -177,3 +184,21 @@ fn valid_ident(text: &str) -> bool { .is_some_and(|c| c == '_' || c.is_ascii_alphabetic()) && chars.all(|c| c == '_' || c.is_ascii_alphanumeric()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn c_header_cuda_content_blanked() { + let out = CSpec.pre_parse("__device__ int helper() { return 0; }", "h.h"); + assert!(!out.contains("__device__")); + assert_eq!(out.len(), "__device__ int helper() { return 0; }".len()); + } + + #[test] + fn c_plain_untouched() { + let src = "int add(int a, int b) { return a + b; }"; + assert_eq!(CSpec.pre_parse(src, "m.c"), src); + } +} diff --git a/crates/codegraph-extract/src/lang/cpp.rs b/crates/codegraph-extract/src/lang/cpp.rs index fdb339e1..2f9f95a2 100644 --- a/crates/codegraph-extract/src/lang/cpp.rs +++ b/crates/codegraph-extract/src/lang/cpp.rs @@ -67,6 +67,9 @@ impl LanguageSpec for CppSpec { "type" } fn resolve_name(&self, node: Node<'_>, source: &str) -> Option { + if let Some(name) = recover_cpp_macro_defined_name(node, source) { + return Some(name); + } let qid = declarator_qualified_id(child_by_field(node, "declarator")?)?; node_text(qid, source) .rsplit("::") @@ -116,8 +119,8 @@ impl LanguageSpec for CppSpec { fn extract_import(&self, node: Node<'_>, source: &str) -> Option { include_import(node, source) } - fn pre_parse(&self, source: &str) -> String { - pre_parse_cpp_source(source) + fn pre_parse(&self, source: &str, file_path: &str) -> String { + pre_parse_cpp_source(source, file_path) } } @@ -128,10 +131,133 @@ impl LanguageSpec for CppSpec { /// ASCII spaces — tree-sitter consumes byte offsets, and every blanked span lies /// on char boundaries, so byte length (and thus line/column) is preserved. Each /// pass is `contains`-gated, so macro-free C++ is returned byte-identical. -fn pre_parse_cpp_source(source: &str) -> String { +fn pre_parse_cpp_source(source: &str, file_path: &str) -> String { let bytes = blank_cpp_annotation_macro_calls(blank_cpp_inline_annotation_macros( blank_cpp_api_prefix_macros(source.as_bytes().to_vec()), )); + let lower = file_path.to_ascii_lowercase(); + let bytes = if lower.ends_with(".metal") { + blank_metal_attributes(bytes) + } else if lower.ends_with(".cu") || lower.ends_with(".cuh") || looks_like_cuda_source(source) { + blank_cuda_constructs(bytes) + } else { + bytes + }; + String::from_utf8(bytes).unwrap_or_else(|_| source.to_string()) +} + +/// Blank Metal Shading Language `[[attribute]]` annotations (`[[position]]`, +/// `[[buffer(0)]]`, comma-lists like `[[buffer(0), raster_order_group(0)]]`) to +/// equal-length spaces. MSL puts attributes AFTER the declarator +/// (`float4 position [[position]];`), which tree-sitter-cpp misparses into a +/// spurious `extends` ref from the struct to the field's own type (#1121). +/// `[[`-gated fast-exit; offset-preserving. The `regex` crate is lookahead-free +/// so the tight shape (after `[[`, an identifier then `]]`) alone excludes a +/// subscripted lambda `arr[[]{…}()]`. +fn blank_metal_attributes(bytes: Vec) -> Vec { + let source = match std::str::from_utf8(&bytes) { + Ok(source) => source, + Err(_) => return bytes, + }; + if !source.contains("[[") { + return bytes; + } + static RE: OnceLock = OnceLock::new(); + let re = RE.get_or_init(|| { + Regex::new( + r"\[\[\s*[A-Za-z_]\w*(?:\s*\([^()\n]*\))?(?:\s*,\s*[A-Za-z_]\w*(?:\s*\([^()\n]*\))?)*\s*\]\]", + ) + .expect("metal-attribute regex") + }); + let spans: Vec<(usize, usize)> = re.find_iter(source).map(|m| (m.start(), m.end())).collect(); + let mut bytes = bytes; + for (start, end) in spans { + blank_span(&mut bytes, start, end); + } + bytes +} + +/// Blank CUDA-specific constructs (`.cu`/`.cuh` or content-detected) so the +/// residual parses as plain C++ (#387, CUDA-lang parts of #1172): +/// - `__launch_bounds__(...)` then execution-space/storage specifiers +/// (`__global__` family) — gated by `__`; blanked to equal-length spaces. +/// `__restrict__` is deliberately excluded (grammar parses it natively). +/// - `<<>>` launch configs — gated by `<<<`; +/// the chevrons otherwise lex as shift operators and destroy the host→kernel +/// call edge. Blanking the span leaves a plain `kernel(args)` call. Only a +/// BRACE-BALANCED match is blanked (a stray `<<<` from a merge-conflict marker +/// opens braces it never closes and is left untouched), matching upstream's +/// char-scan replacer. Offset-preserving. +fn blank_cuda_constructs(bytes: Vec) -> Vec { + let source = match std::str::from_utf8(&bytes) { + Ok(source) => source, + Err(_) => return bytes, + }; + let mut spans: Vec<(usize, usize)> = Vec::new(); + if source.contains("__") { + static BOUNDS_RE: OnceLock = OnceLock::new(); + let bounds_re = BOUNDS_RE.get_or_init(|| { + Regex::new(r"\b__launch_bounds__\s*\([^()\n]*\)").expect("bounds regex") + }); + static SPEC_RE: OnceLock = OnceLock::new(); + let spec_re = SPEC_RE.get_or_init(|| { + Regex::new( + r"\b__(?:global|device|host|constant|shared|managed|grid_constant|forceinline|noinline|launch_bounds)__\b", + ) + .expect("specifier regex") + }); + spans.extend(bounds_re.find_iter(source).map(|m| (m.start(), m.end()))); + spans.extend(spec_re.find_iter(source).map(|m| (m.start(), m.end()))); + } + if source.contains("<<<") { + static LAUNCH_RE: OnceLock = OnceLock::new(); + let launch_re = + LAUNCH_RE.get_or_init(|| Regex::new(r"<<<[^;]{0,400}?>>>").expect("launch regex")); + for m in launch_re.find_iter(source) { + if is_brace_balanced(m.as_str()) { + spans.push((m.start(), m.end())); + } + } + } + let mut bytes = bytes; + for (start, end) in spans { + blank_span(&mut bytes, start, end); + } + bytes +} + +/// True when every `{` in `text` is matched by a later `}` and no `}` precedes +/// its opener (net depth stays non-negative and ends at zero). Used to reject a +/// launch-config match that spills across a merge-conflict marker. +fn is_brace_balanced(text: &str) -> bool { + let mut depth: i32 = 0; + for b in text.bytes() { + if b == b'{' { + depth += 1; + } else if b == b'}' { + depth -= 1; + if depth < 0 { + return false; + } + } + } + depth == 0 +} + +/// Strong content markers for CUDA in files without a `.cu`/`.cuh` extension +/// (much CUDA lives in `.h`/`.hpp`). The dunder specifiers are nvcc-only and +/// `cudaStream_t` is the runtime stream handle; none is valid C++ anywhere, so +/// a content-triggered blank on a non-CUDA file is inert. Weak markers (`dim3`, +/// `<<<`) are deliberately excluded. +pub(crate) fn looks_like_cuda_source(source: &str) -> bool { + source.contains("__global__") + || source.contains("__device__") + || source.contains("__constant__") + || source.contains("cudaStream_t") +} + +pub(crate) fn blank_cuda_constructs_str(source: &str) -> String { + let bytes = blank_cuda_constructs(source.as_bytes().to_vec()); String::from_utf8(bytes).unwrap_or_else(|_| source.to_string()) } @@ -301,6 +427,58 @@ fn blank_cpp_annotation_macro_calls(bytes: Vec) -> Vec { bytes } +/// Recover the real function name from the macro-definition idiom +/// `MACRO_NAME(real_name, typed args…) { body }` (flash-attention's +/// `DEFINE_FLASH_FORWARD_KERNEL(flash_fwd_kernel, bool Is_dropout, …) { … }`): +/// tree-sitter parses it as a `function_definition` named after the macro, so +/// every such kernel collapses onto one name (#1172). Narrow gate — ALL of: +/// the parsed name is macro-shaped (`^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$`); the +/// first "parameter" is a LONE lowercase-bearing `type_identifier` (the name); +/// ≥2 params and no OTHER param is a lone identifier (so gtest `TEST_F(Fixture, +/// Name)` / `PYBIND11_MODULE(ext, m)` / `BENCHMARK_DEFINE_F(Fix, name)` bail). +fn recover_cpp_macro_defined_name(node: Node<'_>, source: &str) -> Option { + if node.kind() != "function_definition" { + return None; + } + let declarator = child_by_field(node, "declarator")?; + if declarator.kind() != "function_declarator" { + return None; + } + let inner = child_by_field(declarator, "declarator")?; + if inner.kind() != "identifier" { + return None; + } + let macro_name = node_text(inner, source); + static RE: OnceLock = OnceLock::new(); + let re = RE + .get_or_init(|| Regex::new(r"^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$").expect("macro-name regex")); + if !re.is_match(¯o_name) { + return None; + } + let params = child_by_field(declarator, "parameters")?; + let named: Vec> = params.named_children(&mut params.walk()).collect(); + if named.len() < 2 { + return None; + } + let lone_ident_text = |p: Node<'_>| -> Option { + if p.kind() != "parameter_declaration" || p.named_child_count() != 1 { + return None; + } + let child = p.named_child(0)?; + (child.kind() == "type_identifier").then(|| node_text(child, source)) + }; + let name = lone_ident_text(named[0])?; + if !name.chars().any(|c| c.is_ascii_lowercase()) { + return None; + } + for p in &named[1..] { + if lone_ident_text(*p).is_some() { + return None; + } + } + Some(name) +} + fn declarator_qualified_id<'tree>(declarator: Node<'tree>) -> Option> { let mut queue = vec![declarator]; while let Some(current) = queue.pop() { @@ -522,7 +700,7 @@ mod tests { #[test] fn blank_cpp_api_prefix_member() { let src = "ENGINE_API virtual void Tick();"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out.len(), src.len()); assert!(out.starts_with(" virtual void Tick();")); } @@ -530,14 +708,14 @@ mod tests { #[test] fn blank_cpp_api_prefix_bare_value_untouched() { let src = "int x = MY_API;"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out, src); } #[test] fn blank_cpp_annotation_macro_calls_ue() { let src = "UPROPERTY(EditAnywhere)\nint X;"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out.len(), src.len()); assert!(out.starts_with(" \nint X;")); } @@ -545,21 +723,21 @@ mod tests { #[test] fn blank_cpp_annotation_statement_call_untouched() { let src = "FOO(x);"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out, src); } #[test] fn blank_cpp_annotation_in_expression_untouched() { let src = "if (CHECK(x)) {}"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out, src); } #[test] fn blank_cpp_inline_annotation_umeta() { let src = "Foo UMETA(DisplayName=\"Foo\"),"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out.len(), src.len()); assert!(out.starts_with("Foo ")); assert!(out.ends_with(",")); @@ -569,7 +747,7 @@ mod tests { #[test] fn blank_cpp_inline_annotation_lowercase_untouched() { let src = "auto v = meta(1);"; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out, src); } @@ -584,7 +762,7 @@ mod tests { void Bar(); }; "#; - let out = pre_parse_cpp_source(src); + let out = pre_parse_cpp_source(src, "t.cpp"); assert_eq!(out.len(), src.len()); assert_eq!( out.bytes().filter(|&b| b == b'\n').count(), @@ -601,6 +779,117 @@ public: }; } "#; - assert_eq!(pre_parse_cpp_source(src), src); + assert_eq!(pre_parse_cpp_source(src, "t.cpp"), src); + } + + #[test] + fn blank_metal_attributes_blanks_field_attribute() { + let src = "float4 position [[position]];"; + let out = blank_metal_attributes(src.as_bytes().to_vec()); + let out = String::from_utf8(out).unwrap(); + assert_eq!(out.len(), src.len()); + assert_eq!(out, "float4 position ;"); + assert!(!out.contains("[[")); + } + + #[test] + fn blank_metal_attributes_ignores_no_double_bracket() { + let src = "float4 position;"; + let out = blank_metal_attributes(src.as_bytes().to_vec()); + assert_eq!(String::from_utf8(out).unwrap(), src); + } + + #[test] + fn blank_metal_attributes_ignores_lambda_subscript() { + let src = "arr[[]{return 0;}()]"; + let out = blank_metal_attributes(src.as_bytes().to_vec()); + assert_eq!(String::from_utf8(out).unwrap(), src); + } + + #[test] + fn metal_attribute_blanked_only_for_dot_metal() { + let metal = pre_parse_cpp_source("struct S { float4 p [[position]]; };", "s.metal"); + assert!(!metal.contains("[[position]]")); + assert!(metal.contains("float4 p")); + let cpp = "[[nodiscard]] int f();"; + assert_eq!(pre_parse_cpp_source(cpp, "s.cpp"), cpp); + } + + #[test] + fn blank_cuda_specifier() { + let src = "__global__ void f() {}"; + let out = blank_cuda_constructs(src.as_bytes().to_vec()); + let out = String::from_utf8(out).unwrap(); + assert_eq!(out.len(), src.len()); + assert!(out.starts_with(" void f()")); + assert!(!out.contains("__global__")); + } + + #[test] + fn blank_cuda_launch_config() { + let src = "f<<>>(x);"; + let out = blank_cuda_constructs(src.as_bytes().to_vec()); + let out = String::from_utf8(out).unwrap(); + assert_eq!(out.len(), src.len()); + assert_eq!(out, "f (x);"); + } + + #[test] + fn blank_cuda_launch_unbalanced_braces_untouched() { + let src = "a <<< b } c >>> d;"; + let out = blank_cuda_constructs(src.as_bytes().to_vec()); + assert_eq!(String::from_utf8(out).unwrap(), src); + } + + #[test] + fn looks_like_cuda_source_markers() { + assert!(looks_like_cuda_source("__global__ void f();")); + assert!(looks_like_cuda_source("cudaStream_t s;")); + assert!(!looks_like_cuda_source("int main() { return 0; }")); + } + + #[test] + fn cuda_blanked_for_dot_cu() { + let out = pre_parse_cpp_source("void h() { k<<>>(x); }", "k.cu"); + assert!(!out.contains("<<<")); + assert!(out.contains("k (x);")); + } + + #[test] + fn cuda_blanked_by_content_in_hpp() { + let out = pre_parse_cpp_source("__global__ void k() {}", "k.hpp"); + assert!(!out.contains("__global__")); + } + + #[test] + fn recover_macro_kernel_name() { + assert_eq!( + resolve_macro_name("DEFINE_KERNEL(my_kernel, int n) {}"), + Some("my_kernel".to_string()) + ); + } + + #[test] + fn recover_macro_ignores_gtest_shape() { + assert_eq!(resolve_macro_name("TEST_F(Fixture, Name) {}"), None); + } + + #[test] + fn recover_macro_ignores_lowercase_macro() { + assert_eq!(resolve_macro_name("define_x(a, int b) {}"), None); + } + + fn resolve_macro_name(src: &str) -> Option { + let mut parser = tree_sitter::Parser::new(); + parser + .set_language(&tree_sitter_cpp::LANGUAGE.into()) + .unwrap(); + let tree = parser.parse(src, None).unwrap(); + let mut cursor = tree.root_node().walk(); + let func = tree + .root_node() + .named_children(&mut cursor) + .find(|c| c.kind() == "function_definition")?; + recover_cpp_macro_defined_name(func, src) } } diff --git a/crates/codegraph-extract/src/lang/csharp.rs b/crates/codegraph-extract/src/lang/csharp.rs index c72ecfaf..936437e4 100644 --- a/crates/codegraph-extract/src/lang/csharp.rs +++ b/crates/codegraph-extract/src/lang/csharp.rs @@ -71,7 +71,7 @@ impl LanguageSpec for CSharpSpec { fn return_field(&self) -> &'static str { "returns" } - fn pre_parse(&self, source: &str) -> String { + fn pre_parse(&self, source: &str, _file_path: &str) -> String { blank_csharp_preprocessor_directives(source) } fn get_return_type(&self, node: Node<'_>, source: &str) -> Option { diff --git a/crates/codegraph-extract/src/spec.rs b/crates/codegraph-extract/src/spec.rs index 3761d736..2b87fe09 100644 --- a/crates/codegraph-extract/src/spec.rs +++ b/crates/codegraph-extract/src/spec.rs @@ -57,7 +57,8 @@ pub trait LanguageSpec: Sync { fn params_field(&self) -> &'static str; fn return_field(&self) -> &'static str; - fn pre_parse(&self, source: &str) -> String { + fn pre_parse(&self, source: &str, file_path: &str) -> String { + let _ = file_path; source.to_string() } diff --git a/crates/codegraph-extract/src/walker.rs b/crates/codegraph-extract/src/walker.rs index 0ce993b2..3384bde1 100644 --- a/crates/codegraph-extract/src/walker.rs +++ b/crates/codegraph-extract/src/walker.rs @@ -4631,6 +4631,63 @@ void f() { Calculator* c = new Calculator(0); } assert!(has_ref(&refs, EdgeKind::Instantiates, "Calculator")); } + // #1172 — CUDA kernel-launch call edge survives the `<<<…>>>` blank. + #[test] + fn cuda_kernel_launch_call_edge() { + let src = r#" +__global__ void add_kernel(int* p) {} +void host() { + int* p; + add_kernel<<>>(p); +} +"#; + let (nodes, refs) = run("k.cu", src, Language::Cpp); + assert!(has_node(&nodes, NodeKind::Function, "add_kernel")); + assert!( + has_ref(&refs, EdgeKind::Calls, "add_kernel"), + "host launch should Call add_kernel, got: {:?}", + refs.iter() + .map(|r| (r.reference_kind, r.reference_name.as_str())) + .collect::>() + ); + } + + #[test] + fn cuda_templated_launch_call_edge() { + let src = r#" +template +__global__ void scale_kernel(T* x) {} +void host() { + float* x; + scale_kernel<<>>(x); +} +"#; + let (_, refs) = run("k.cu", src, Language::Cpp); + assert!( + has_ref(&refs, EdgeKind::Calls, "scale_kernel"), + "templated launch should Call scale_kernel (template args stripped), got: {:?}", + refs.iter() + .map(|r| (r.reference_kind, r.reference_name.as_str())) + .collect::>() + ); + } + + #[test] + fn cuda_macro_defined_kernel_recovered() { + let src = r#" +DEFINE_FLASH_FORWARD_KERNEL(my_kernel, int n) { } +"#; + let (nodes, _) = run("k.cu", src, Language::Cpp); + assert!( + has_node(&nodes, NodeKind::Function, "my_kernel"), + "macro-defined kernel should be named my_kernel, got: {:?}", + nodes + .iter() + .map(|n| (n.kind, n.name.as_str())) + .collect::>() + ); + } + #[test] fn cpp_primitive_declaration_no_instantiates() { // NEGATIVE: a plain primitive/local declaration must not create an edge. diff --git a/docs/equivalence.md b/docs/equivalence.md index f1bd31d8..3a15ba2e 100644 --- a/docs/equivalence.md +++ b/docs/equivalence.md @@ -304,6 +304,68 @@ Like the Ruby fixture, both the index and the dump are byte-stable, and the `cpp_db_is_self_equivalent_to_cpp_golden` tests in `crates/codegraph-bench/tests/equivalence.rs` enforce it. +### Metal fixture + +A fifth golden fixture, `reference/golden/metal/`, guards Metal Shading Language +support (upstream #1121 / `cc89146`). MSL ≈ C++14 and rides the existing +`tree-sitter-cpp` grammar — `.metal` maps to `Language::Cpp` with **no new +`Language` variant**. It guards the `.metal`-gated `[[attribute]]` blank: MSL's +post-declarator attributes (`float4 position [[position]];`) otherwise misparse a +struct field into a spurious `extends` edge from the struct to the field's own +type. The corpus (`crates/codegraph-bench/fixtures/metal/shader.metal`) defines +`float4`/`float2` structs, a `VertexIn` struct whose fields carry +`[[position]]`/`[[user(locn0)]]` attributes on those self-defined types, and a +`vertex_main` function that calls a `tint` helper. The golden must show: + +- `shader.metal` with `"language": "cpp"`; +- `VertexIn`/`float4`/`float2` as ordinary structs with **no `Extends` edge** + (the attribute blank prevents the spurious `VertexIn extends float4`); +- the intra-shader `vertex_main` → `tint` `Calls` edge. + +The `[[attribute]]` blank fires ONLY for `.metal` files; a `.cpp`/`.hpp` with a +regular `[[nodiscard]]` attribute is byte-identical through pre-parse (proven by +the `metal_attribute_blanked_only_for_dot_metal` unit test in `lang/cpp.rs`). + +### CUDA fixture + +A sixth golden fixture, `reference/golden/cuda/`, guards CUDA support (the +CUDA-language parts of upstream #1172 / `e1a8d88`). CUDA ≈ C++ + dialect tokens +and likewise rides `tree-sitter-cpp` — `.cu`/`.cuh` map to `Language::Cpp` with +**no new `Language` variant**. It guards the CUDA pre-parse blank (execution-space +specifiers + `<<>>` launch configs, offset-preserving and +brace-balance-checked) and macro-defined-kernel name recovery. The corpus +(`crates/codegraph-bench/fixtures/cuda/kernel.cu`) defines a `__global__ void +add_kernel`, a templated `__global__ scale_kernel`, a +`DEFINE_FLASH_FORWARD_KERNEL(my_kernel, …)` macro-defined kernel, and a `launch` +host function with a plain launch and a templated launch. The golden must show: + +- `kernel.cu` with `"language": "cpp"`; +- `add_kernel`, `scale_kernel`, `my_kernel`, `launch` as functions — the + macro kernel under its real name `my_kernel`, NOT `DEFINE_FLASH_FORWARD_KERNEL`; +- host→kernel `Calls` edges `launch` → `add_kernel` and `launch` → `scale_kernel` + (the `<<<…>>>` blank restores the call; the templated launch rides the + already-landed template-argument strip). + +The CUDA blank fires for `.cu`/`.cuh` files OR any C/C++-family file whose content +carries a strong CUDA marker (`__global__`/`__device__`/`__constant__`/ +`cudaStream_t`), so CUDA living in `.h`/`.hpp` headers is recognized. + +Regenerate both new fixtures reproducibly (identical recipe to the C++ fixture, +substituting `metal`/`cuda`): + +```bash +rm -rf /tmp/cg-fixture-metal && cp -r crates/codegraph-bench/fixtures/metal /tmp/cg-fixture-metal +cargo build --release -p codegraph-rs +CODEGRAPH_NO_DAEMON=1 CODEGRAPH_NO_WATCH=1 ./target/release/codegraph init /tmp/cg-fixture-metal +cp /tmp/cg-fixture-metal/.codegraph/codegraph.db reference/golden/metal/colby.db +cargo run -p codegraph-bench --bin bench -- --gen-golden reference/golden/metal/colby.db reference/golden/metal +# …and the same for cuda. +``` + +The `generated_golden_matches_committed_{metal,cuda}_fixture` and +`{metal,cuda}_db_is_self_equivalent_to_{metal,cuda}_golden` tests in +`crates/codegraph-bench/tests/equivalence.rs` enforce byte-stability. + ### KNOWN_DIFFS.md format Tier-3 differences are allowlisted by grep-able lines in repo-root diff --git a/reference/golden/cuda/colby.db b/reference/golden/cuda/colby.db new file mode 100644 index 00000000..0981683f Binary files /dev/null and b/reference/golden/cuda/colby.db differ diff --git a/reference/golden/cuda/edges.json b/reference/golden/cuda/edges.json new file mode 100644 index 00000000..4edd05cf --- /dev/null +++ b/reference/golden/cuda/edges.json @@ -0,0 +1,62 @@ +[ + { + "col": 4, + "kind": "calls", + "line": 16, + "metadata": { + "confidence": 0.9, + "resolvedBy": "exact-match" + }, + "provenance": null, + "source": "function:8fcfc4f43bf1b07028d15edababdbdf7", + "target": "function:290ca38de34bc728b62b2c22dce9be77" + }, + { + "col": 4, + "kind": "calls", + "line": 17, + "metadata": { + "confidence": 0.9, + "resolvedBy": "exact-match" + }, + "provenance": null, + "source": "function:8fcfc4f43bf1b07028d15edababdbdf7", + "target": "function:d566ed55561eb4c96162668d7f78d9fb" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:kernel.cu", + "target": "function:290ca38de34bc728b62b2c22dce9be77" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:kernel.cu", + "target": "function:8ad1cefc2328c8b073c2e06a7d984a1d" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:kernel.cu", + "target": "function:8fcfc4f43bf1b07028d15edababdbdf7" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:kernel.cu", + "target": "function:d566ed55561eb4c96162668d7f78d9fb" + } +] diff --git a/reference/golden/cuda/files.json b/reference/golden/cuda/files.json new file mode 100644 index 00000000..e8c574ba --- /dev/null +++ b/reference/golden/cuda/files.json @@ -0,0 +1,10 @@ +[ + { + "content_hash": "3f2656febed2ea1c1ae2498e3361ad5d667edc05d4abf561a0b189eddfe4d113", + "errors": null, + "language": "cpp", + "node_count": 5, + "path": "kernel.cu", + "size": 452 + } +] diff --git a/reference/golden/cuda/nodes.json b/reference/golden/cuda/nodes.json new file mode 100644 index 00000000..cdfb2900 --- /dev/null +++ b/reference/golden/cuda/nodes.json @@ -0,0 +1,112 @@ +[ + { + "decorators": null, + "docstring": null, + "end_column": 0, + "end_line": 19, + "file_path": "kernel.cu", + "id": "file:kernel.cu", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "file", + "language": "cpp", + "name": "kernel.cu", + "qualified_name": "kernel.cu", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 1, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 4, + "file_path": "kernel.cu", + "id": "function:290ca38de34bc728b62b2c22dce9be77", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "add_kernel", + "qualified_name": "add_kernel", + "return_type": null, + "signature": null, + "start_column": 11, + "start_line": 1, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 13, + "file_path": "kernel.cu", + "id": "function:8ad1cefc2328c8b073c2e06a7d984a1d", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "my_kernel", + "qualified_name": "my_kernel", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 11, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 18, + "file_path": "kernel.cu", + "id": "function:8fcfc4f43bf1b07028d15edababdbdf7", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "launch", + "qualified_name": "launch", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 15, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 9, + "file_path": "kernel.cu", + "id": "function:d566ed55561eb4c96162668d7f78d9fb", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "scale_kernel", + "qualified_name": "scale_kernel", + "return_type": null, + "signature": null, + "start_column": 11, + "start_line": 7, + "type_parameters": null, + "visibility": null + } +] diff --git a/reference/golden/cuda/refs.json b/reference/golden/cuda/refs.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/reference/golden/cuda/refs.json @@ -0,0 +1 @@ +[] diff --git a/reference/golden/cuda/schema.sql b/reference/golden/cuda/schema.sql new file mode 100644 index 00000000..d36218fe --- /dev/null +++ b/reference/golden/cuda/schema.sql @@ -0,0 +1,117 @@ +CREATE TABLE schema_versions ( +version INTEGER PRIMARY KEY, +applied_at INTEGER NOT NULL, +description TEXT +); +CREATE TABLE nodes ( +id TEXT PRIMARY KEY, +kind TEXT NOT NULL, +name TEXT NOT NULL, +qualified_name TEXT NOT NULL, +file_path TEXT NOT NULL, +language TEXT NOT NULL, +start_line INTEGER NOT NULL, +end_line INTEGER NOT NULL, +start_column INTEGER NOT NULL, +end_column INTEGER NOT NULL, +docstring TEXT, +signature TEXT, +visibility TEXT, +is_exported INTEGER DEFAULT 0, +is_async INTEGER DEFAULT 0, +is_static INTEGER DEFAULT 0, +is_abstract INTEGER DEFAULT 0, +decorators TEXT, -- JSON array +type_parameters TEXT, -- JSON array +return_type TEXT, -- normalized return/result type name (e.g. C++ method return, for receiver-type inference) +updated_at INTEGER NOT NULL +); +CREATE TABLE edges ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +source TEXT NOT NULL, +target TEXT NOT NULL, +kind TEXT NOT NULL, +metadata TEXT, -- JSON object +line INTEGER, +col INTEGER, +provenance TEXT DEFAULT NULL, +FOREIGN KEY (source) REFERENCES nodes(id) ON DELETE CASCADE, +FOREIGN KEY (target) REFERENCES nodes(id) ON DELETE CASCADE +); +CREATE TABLE sqlite_sequence(name,seq); +CREATE TABLE files ( +path TEXT PRIMARY KEY, +content_hash TEXT NOT NULL, +language TEXT NOT NULL, +size INTEGER NOT NULL, +modified_at INTEGER NOT NULL, +indexed_at INTEGER NOT NULL, +node_count INTEGER DEFAULT 0, +errors TEXT -- JSON array +); +CREATE TABLE unresolved_refs ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +from_node_id TEXT NOT NULL, +reference_name TEXT NOT NULL, +reference_kind TEXT NOT NULL, +line INTEGER NOT NULL, +col INTEGER NOT NULL, +candidates TEXT, -- JSON array +file_path TEXT NOT NULL DEFAULT '', +language TEXT NOT NULL DEFAULT 'unknown', +reference_subkind TEXT, +FOREIGN KEY (from_node_id) REFERENCES nodes(id) ON DELETE CASCADE +); +CREATE INDEX idx_nodes_kind ON nodes(kind); +CREATE INDEX idx_nodes_name ON nodes(name); +CREATE INDEX idx_nodes_qualified_name ON nodes(qualified_name); +CREATE INDEX idx_nodes_file_path ON nodes(file_path); +CREATE INDEX idx_nodes_language ON nodes(language); +CREATE INDEX idx_nodes_file_line ON nodes(file_path, start_line); +CREATE INDEX idx_nodes_lower_name ON nodes(lower(name)); +CREATE VIRTUAL TABLE nodes_fts USING fts5( +id, +name, +qualified_name, +docstring, +signature, +content='nodes', +content_rowid='rowid' +) +/* nodes_fts(id,name,qualified_name,docstring,signature) */; +CREATE TABLE 'nodes_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE 'nodes_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE 'nodes_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE 'nodes_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER nodes_ai AFTER INSERT ON nodes BEGIN +INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature) +VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature); +END; +CREATE TRIGGER nodes_ad AFTER DELETE ON nodes BEGIN +INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature) +VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature); +END; +CREATE TRIGGER nodes_au AFTER UPDATE ON nodes BEGIN +INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature) +VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature); +INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature) +VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature); +END; +CREATE INDEX idx_edges_kind ON edges(kind); +CREATE INDEX idx_edges_source_kind ON edges(source, kind); +CREATE INDEX idx_edges_target_kind ON edges(target, kind); +CREATE UNIQUE INDEX idx_edges_identity +ON edges(source, target, kind, IFNULL(line, -1), IFNULL(col, -1)); +CREATE INDEX idx_files_language ON files(language); +CREATE INDEX idx_files_modified_at ON files(modified_at); +CREATE INDEX idx_unresolved_from_node ON unresolved_refs(from_node_id); +CREATE INDEX idx_unresolved_name ON unresolved_refs(reference_name); +CREATE INDEX idx_unresolved_file_path ON unresolved_refs(file_path); +CREATE INDEX idx_unresolved_from_name ON unresolved_refs(from_node_id, reference_name); +CREATE INDEX idx_edges_provenance ON edges(provenance); +CREATE TABLE project_metadata ( +key TEXT PRIMARY KEY, +value TEXT NOT NULL, +updated_at INTEGER NOT NULL +); +CREATE TABLE sqlite_stat1(tbl,idx,stat); diff --git a/reference/golden/metal/colby.db b/reference/golden/metal/colby.db new file mode 100644 index 00000000..9d849d30 Binary files /dev/null and b/reference/golden/metal/colby.db differ diff --git a/reference/golden/metal/edges.json b/reference/golden/metal/edges.json new file mode 100644 index 00000000..e50eb624 --- /dev/null +++ b/reference/golden/metal/edges.json @@ -0,0 +1,59 @@ +[ + { + "col": 11, + "kind": "calls", + "line": 23, + "metadata": { + "confidence": 0.9, + "resolvedBy": "exact-match" + }, + "provenance": null, + "source": "function:06d476406c9dac14172f0784bdcf3614", + "target": "function:0dd9c7c67cf2c6ae7a23bbbd5b2e5078" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:shader.metal", + "target": "function:06d476406c9dac14172f0784bdcf3614" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:shader.metal", + "target": "function:0dd9c7c67cf2c6ae7a23bbbd5b2e5078" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:shader.metal", + "target": "struct:71307e3c8493876bbef9b6575c1dabc4" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:shader.metal", + "target": "struct:ce73931d0e937bddde9dd6094c56cfad" + }, + { + "col": null, + "kind": "contains", + "line": null, + "metadata": null, + "provenance": null, + "source": "file:shader.metal", + "target": "struct:ea772be38f713c9777b2e8c23ddf6638" + } +] diff --git a/reference/golden/metal/files.json b/reference/golden/metal/files.json new file mode 100644 index 00000000..84c7f7c8 --- /dev/null +++ b/reference/golden/metal/files.json @@ -0,0 +1,10 @@ +[ + { + "content_hash": "42a8ea57f43a0da7ea71ed53775560358fbfe499ed6414bb0be9a2cb43eeda30", + "errors": null, + "language": "cpp", + "node_count": 6, + "path": "shader.metal", + "size": 340 + } +] diff --git a/reference/golden/metal/nodes.json b/reference/golden/metal/nodes.json new file mode 100644 index 00000000..b1eec34a --- /dev/null +++ b/reference/golden/metal/nodes.json @@ -0,0 +1,134 @@ +[ + { + "decorators": null, + "docstring": null, + "end_column": 0, + "end_line": 25, + "file_path": "shader.metal", + "id": "file:shader.metal", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "file", + "language": "cpp", + "name": "shader.metal", + "qualified_name": "shader.metal", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 1, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 24, + "file_path": "shader.metal", + "id": "function:06d476406c9dac14172f0784bdcf3614", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "vertex_main", + "qualified_name": "vertex_main", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 22, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 20, + "file_path": "shader.metal", + "id": "function:0dd9c7c67cf2c6ae7a23bbbd5b2e5078", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "function", + "language": "cpp", + "name": "tint", + "qualified_name": "tint", + "return_type": "float4", + "signature": null, + "start_column": 0, + "start_line": 18, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 11, + "file_path": "shader.metal", + "id": "struct:71307e3c8493876bbef9b6575c1dabc4", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "struct", + "language": "cpp", + "name": "float2", + "qualified_name": "float2", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 8, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 6, + "file_path": "shader.metal", + "id": "struct:ce73931d0e937bddde9dd6094c56cfad", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "struct", + "language": "cpp", + "name": "float4", + "qualified_name": "float4", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 1, + "type_parameters": null, + "visibility": null + }, + { + "decorators": null, + "docstring": null, + "end_column": 1, + "end_line": 16, + "file_path": "shader.metal", + "id": "struct:ea772be38f713c9777b2e8c23ddf6638", + "is_abstract": 0, + "is_async": 0, + "is_exported": 0, + "is_static": 0, + "kind": "struct", + "language": "cpp", + "name": "VertexIn", + "qualified_name": "VertexIn", + "return_type": null, + "signature": null, + "start_column": 0, + "start_line": 13, + "type_parameters": null, + "visibility": null + } +] diff --git a/reference/golden/metal/refs.json b/reference/golden/metal/refs.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/reference/golden/metal/refs.json @@ -0,0 +1 @@ +[] diff --git a/reference/golden/metal/schema.sql b/reference/golden/metal/schema.sql new file mode 100644 index 00000000..d36218fe --- /dev/null +++ b/reference/golden/metal/schema.sql @@ -0,0 +1,117 @@ +CREATE TABLE schema_versions ( +version INTEGER PRIMARY KEY, +applied_at INTEGER NOT NULL, +description TEXT +); +CREATE TABLE nodes ( +id TEXT PRIMARY KEY, +kind TEXT NOT NULL, +name TEXT NOT NULL, +qualified_name TEXT NOT NULL, +file_path TEXT NOT NULL, +language TEXT NOT NULL, +start_line INTEGER NOT NULL, +end_line INTEGER NOT NULL, +start_column INTEGER NOT NULL, +end_column INTEGER NOT NULL, +docstring TEXT, +signature TEXT, +visibility TEXT, +is_exported INTEGER DEFAULT 0, +is_async INTEGER DEFAULT 0, +is_static INTEGER DEFAULT 0, +is_abstract INTEGER DEFAULT 0, +decorators TEXT, -- JSON array +type_parameters TEXT, -- JSON array +return_type TEXT, -- normalized return/result type name (e.g. C++ method return, for receiver-type inference) +updated_at INTEGER NOT NULL +); +CREATE TABLE edges ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +source TEXT NOT NULL, +target TEXT NOT NULL, +kind TEXT NOT NULL, +metadata TEXT, -- JSON object +line INTEGER, +col INTEGER, +provenance TEXT DEFAULT NULL, +FOREIGN KEY (source) REFERENCES nodes(id) ON DELETE CASCADE, +FOREIGN KEY (target) REFERENCES nodes(id) ON DELETE CASCADE +); +CREATE TABLE sqlite_sequence(name,seq); +CREATE TABLE files ( +path TEXT PRIMARY KEY, +content_hash TEXT NOT NULL, +language TEXT NOT NULL, +size INTEGER NOT NULL, +modified_at INTEGER NOT NULL, +indexed_at INTEGER NOT NULL, +node_count INTEGER DEFAULT 0, +errors TEXT -- JSON array +); +CREATE TABLE unresolved_refs ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +from_node_id TEXT NOT NULL, +reference_name TEXT NOT NULL, +reference_kind TEXT NOT NULL, +line INTEGER NOT NULL, +col INTEGER NOT NULL, +candidates TEXT, -- JSON array +file_path TEXT NOT NULL DEFAULT '', +language TEXT NOT NULL DEFAULT 'unknown', +reference_subkind TEXT, +FOREIGN KEY (from_node_id) REFERENCES nodes(id) ON DELETE CASCADE +); +CREATE INDEX idx_nodes_kind ON nodes(kind); +CREATE INDEX idx_nodes_name ON nodes(name); +CREATE INDEX idx_nodes_qualified_name ON nodes(qualified_name); +CREATE INDEX idx_nodes_file_path ON nodes(file_path); +CREATE INDEX idx_nodes_language ON nodes(language); +CREATE INDEX idx_nodes_file_line ON nodes(file_path, start_line); +CREATE INDEX idx_nodes_lower_name ON nodes(lower(name)); +CREATE VIRTUAL TABLE nodes_fts USING fts5( +id, +name, +qualified_name, +docstring, +signature, +content='nodes', +content_rowid='rowid' +) +/* nodes_fts(id,name,qualified_name,docstring,signature) */; +CREATE TABLE 'nodes_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE 'nodes_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE 'nodes_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE 'nodes_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER nodes_ai AFTER INSERT ON nodes BEGIN +INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature) +VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature); +END; +CREATE TRIGGER nodes_ad AFTER DELETE ON nodes BEGIN +INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature) +VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature); +END; +CREATE TRIGGER nodes_au AFTER UPDATE ON nodes BEGIN +INSERT INTO nodes_fts(nodes_fts, rowid, id, name, qualified_name, docstring, signature) +VALUES ('delete', OLD.rowid, OLD.id, OLD.name, OLD.qualified_name, OLD.docstring, OLD.signature); +INSERT INTO nodes_fts(rowid, id, name, qualified_name, docstring, signature) +VALUES (NEW.rowid, NEW.id, NEW.name, NEW.qualified_name, NEW.docstring, NEW.signature); +END; +CREATE INDEX idx_edges_kind ON edges(kind); +CREATE INDEX idx_edges_source_kind ON edges(source, kind); +CREATE INDEX idx_edges_target_kind ON edges(target, kind); +CREATE UNIQUE INDEX idx_edges_identity +ON edges(source, target, kind, IFNULL(line, -1), IFNULL(col, -1)); +CREATE INDEX idx_files_language ON files(language); +CREATE INDEX idx_files_modified_at ON files(modified_at); +CREATE INDEX idx_unresolved_from_node ON unresolved_refs(from_node_id); +CREATE INDEX idx_unresolved_name ON unresolved_refs(reference_name); +CREATE INDEX idx_unresolved_file_path ON unresolved_refs(file_path); +CREATE INDEX idx_unresolved_from_name ON unresolved_refs(from_node_id, reference_name); +CREATE INDEX idx_edges_provenance ON edges(provenance); +CREATE TABLE project_metadata ( +key TEXT PRIMARY KEY, +value TEXT NOT NULL, +updated_at INTEGER NOT NULL +); +CREATE TABLE sqlite_stat1(tbl,idx,stat);