1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::path::Path;
use tree_sitter::{Node, Parser};
use crate::semantic::adapter::LanguageAdapter;
use crate::semantic::common::{node_text, signature_up_to_body};
use crate::semantic::types::{ByteRange, ExtractedFile, Import, ImportKind, Symbol, SymbolKind};
/// Tree-sitter adapter for C++.
///
/// Built on the C adapter's vocabulary plus C++ extras:
/// - `class_specifier` → Class with method children walked.
/// Public-by-default visibility is inverted vs `struct`: a
/// `class` body without an access label has private members,
/// while `struct` is public-by-default.
/// - `namespace_definition` → recursed; nested classes/functions
/// carry the namespace as a textual parent prefix in their
/// signature line (no separate Namespace SymbolKind exists).
/// - `template_declaration` → unwraps to the inner function /
/// class so generic code shows up alongside non-generic.
/// - `function_definition` (top-level) → Function.
/// - `using_declaration` / `using_directive` → Imports (best-effort).
pub struct CppAdapter;
impl CppAdapter {
fn signature(&self, n: Node, s: &[u8]) -> String {
signature_up_to_body(n, s)
}
/// Recursive declarator walk shared with C — finds the
/// identifier buried under pointer/reference/parenthesized
/// wrappers.
#[allow(clippy::only_used_in_recursion)]
fn declarator_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
match n.kind() {
"identifier" | "field_identifier" | "destructor_name" | "operator_name" => {
Some(node_text(n, s).to_string())
}
"qualified_identifier" | "template_function" => {
// Last named child holds the name.
for i in (0..n.named_child_count()).rev() {
if let Some(c) = n.named_child(i)
&& let Some(name) = self.declarator_name(c, s)
{
return Some(name);
}
}
None
}
"function_declarator"
| "pointer_declarator"
| "reference_declarator"
| "parenthesized_declarator" => {
for i in 0..n.named_child_count() {
if let Some(c) = n.named_child(i)
&& let Some(name) = self.declarator_name(c, s)
{
return Some(name);
}
}
None
}
_ => None,
}
}
fn function_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
for i in 0..n.named_child_count() {
let c = n.named_child(i)?;
match c.kind() {
"function_declarator" | "pointer_declarator" | "reference_declarator" => {
if let Some(name) = self.declarator_name(c, s) {
return Some(name);
}
}
_ => {}
}
}
None
}
fn handle_function(&self, n: Node, s: &[u8], symbols: &mut Vec<Symbol>) {
let Some(name) = self.function_name(n, s) else {
return;
};
// Out-of-line method definition (`void Foo::bar() {}`)
// carries the receiving class via a qualified_identifier
// in the declarator. Detect that and emit a Method with
// the qualifier as parent_class so users can find these
// alongside in-class methods via list_symbols --parent Foo.
let parent = self.qualified_owner(n, s);
let kind = if parent.is_some() {
SymbolKind::Method
} else {
SymbolKind::Function
};
symbols.push(Symbol {
kind,
// C++ top-level functions are externally visible
// unless they're in an anonymous namespace or marked
// `static`. We don't recurse into anonymous namespace
// visibility tracking — treat as exported.
is_exported: true,
name,
range: ByteRange::from(n),
signature: self.signature(n, s),
parent_class: parent,
});
}
/// If the function's declarator is a `qualified_identifier`
/// (`Foo::bar` / `ns::Foo::bar`), return everything before the
/// last `::` as the parent_class. Returns None for plain
/// non-qualified function definitions.
fn qualified_owner(&self, n: Node, s: &[u8]) -> Option<String> {
for i in 0..n.named_child_count() {
let Some(decl) = n.named_child(i) else {
continue;
};
// Walk through the wrappers; we want the actual
// function_declarator's name child.
let inner = if matches!(
decl.kind(),
"function_declarator" | "pointer_declarator" | "reference_declarator"
) {
// function_declarator's `declarator` field holds the name.
decl.child_by_field_name("declarator").unwrap_or(decl)
} else {
continue;
};
if inner.kind() != "qualified_identifier" {
continue;
}
// Take all text before the trailing `::name`. tree-sitter
// exposes scope vs name fields, but we use raw text since
// it round-trips namespaces correctly.
let raw = node_text(inner, s);
if let Some((scope, _name)) = raw.rsplit_once("::") {
return Some(scope.to_string());
}
}
None
}
/// Walk a `field_declaration_list` (the body of `class`/`struct`)
/// collecting method declarations + inline definitions. C++
/// access (`public:` / `private:` / `protected:`) labels are
/// sibling nodes that change subsequent items' visibility; we
/// track the running label so each method gets the right
/// is_exported.
fn walk_class_body(
&self,
body: Node,
s: &[u8],
symbols: &mut Vec<Symbol>,
parent: &str,
default_public: bool,
) {
let mut public = default_public;
for i in 0..body.named_child_count() {
let Some(c) = body.named_child(i) else {
continue;
};
match c.kind() {
"access_specifier" => {
public = node_text(c, s).trim_end_matches(':') == "public";
}
"function_definition" | "declaration" => {
// Field name is reachable via the same
// declarator walk used for top-level functions.
if let Some(name) = self.function_name(c, s) {
symbols.push(Symbol {
kind: SymbolKind::Method,
is_exported: public,
name,
range: ByteRange::from(c),
signature: self.signature(c, s),
parent_class: Some(parent.to_string()),
});
}
}
"field_declaration" => {
// A field declaration may carry a method
// declarator (e.g. `virtual void f() = 0;`),
// a plain field, or a constructor proto.
if let Some(name) = self.function_name(c, s) {
symbols.push(Symbol {
kind: SymbolKind::Method,
is_exported: public,
name,
range: ByteRange::from(c),
signature: node_text(c, s).lines().next().unwrap_or("").to_string(),
parent_class: Some(parent.to_string()),
});
}
}
"class_specifier" | "struct_specifier" => {
// Nested type — recurse.
self.handle_class_or_struct(c, s, symbols, c.kind() == "struct_specifier");
}
_ => {}
}
}
}
fn class_or_struct_name<'a>(&self, n: Node<'a>, s: &'a [u8]) -> Option<String> {
for i in 0..n.named_child_count() {
if let Some(c) = n.named_child(i)
&& c.kind() == "type_identifier"
{
return Some(node_text(c, s).to_string());
}
}
None
}
fn handle_class_or_struct(
&self,
n: Node,
s: &[u8],
symbols: &mut Vec<Symbol>,
is_struct: bool,
) {
let Some(name) = self.class_or_struct_name(n, s) else {
return;
};
symbols.push(Symbol {
kind: SymbolKind::Class,
is_exported: true,
name: name.clone(),
range: ByteRange::from(n),
signature: node_text(n, s).lines().next().unwrap_or("").to_string(),
parent_class: None,
});
for i in 0..n.named_child_count() {
if let Some(c) = n.named_child(i)
&& c.kind() == "field_declaration_list"
{
self.walk_class_body(c, s, symbols, &name, is_struct);
}
}
}
/// `namespace ns { ... }` — recurse into its children at the
/// top-level walker. Symbols defined inside don't carry the
/// namespace prefix structurally, but their signature line
/// includes the `namespace ns {` context which is good enough
/// for the LLM to disambiguate.
fn handle_namespace(
&self,
n: Node,
s: &[u8],
symbols: &mut Vec<Symbol>,
imports: &mut Vec<Import>,
) {
for i in 0..n.named_child_count() {
if let Some(c) = n.named_child(i)
&& c.kind() == "declaration_list"
{
for j in 0..c.named_child_count() {
let Some(inner) = c.named_child(j) else {
continue;
};
self.dispatch_top(inner, s, symbols, imports);
}
}
}
}
fn handle_using(&self, n: Node, s: &[u8], imports: &mut Vec<Import>) {
// `using std::cout;` or `using namespace std;` — both carry
// an identifier/scoped name we can record.
for i in 0..n.named_child_count() {
if let Some(c) = n.named_child(i)
&& matches!(c.kind(), "qualified_identifier" | "identifier")
{
let path = node_text(c, s).to_string();
imports.push(Import {
names: vec![path.clone()],
source: path,
kind: ImportKind::Qualified,
});
return;
}
}
}
fn handle_include(&self, n: Node, s: &[u8], imports: &mut Vec<Import>) {
for i in 0..n.named_child_count() {
let Some(c) = n.named_child(i) else { continue };
match c.kind() {
"system_lib_string" => {
let raw = node_text(c, s);
let path = raw
.trim_matches(|ch: char| ch == '<' || ch == '>')
.to_string();
imports.push(Import {
names: vec![path.clone()],
source: path,
kind: ImportKind::Header,
});
return;
}
"string_literal" => {
for j in 0..c.named_child_count() {
if let Some(sub) = c.named_child(j)
&& sub.kind() == "string_content"
{
let path = node_text(sub, s).to_string();
imports.push(Import {
names: vec![path.clone()],
source: path,
kind: ImportKind::Header,
});
return;
}
}
}
_ => {}
}
}
}
fn dispatch_top(
&self,
c: Node,
bytes: &[u8],
symbols: &mut Vec<Symbol>,
imports: &mut Vec<Import>,
) {
match c.kind() {
"function_definition" => self.handle_function(c, bytes, symbols),
"class_specifier" => self.handle_class_or_struct(c, bytes, symbols, false),
"struct_specifier" => self.handle_class_or_struct(c, bytes, symbols, true),
"namespace_definition" => self.handle_namespace(c, bytes, symbols, imports),
"template_declaration" => {
// Unwrap to the inner function or class definition.
for i in 0..c.named_child_count() {
if let Some(inner) = c.named_child(i) {
self.dispatch_top(inner, bytes, symbols, imports);
}
}
}
"preproc_include" => self.handle_include(c, bytes, imports),
"using_declaration" | "using_directive" => self.handle_using(c, bytes, imports),
// `extern "C" { ... }` blocks. tree-sitter-cpp exposes
// them as `linkage_specification`. Walk the inner
// declaration_list as if it were the top level so FFI
// functions show up in list_symbols.
"linkage_specification" => {
for i in 0..c.named_child_count() {
if let Some(inner) = c.named_child(i)
&& inner.kind() == "declaration_list"
{
for j in 0..inner.named_child_count() {
if let Some(item) = inner.named_child(j) {
self.dispatch_top(item, bytes, symbols, imports);
}
}
} else if let Some(inner) = c.named_child(i) {
// Single-statement extern (no braces).
self.dispatch_top(inner, bytes, symbols, imports);
}
}
}
_ => {}
}
}
}
impl LanguageAdapter for CppAdapter {
fn extensions(&self) -> &[&str] {
// C++ source and header conventions. We claim `.h` headers
// when the build is C++ — but the C adapter also claims
// `.h`. The registry's `find_for_file` returns the first
// match, and adapters are inserted in alphabetical order in
// `SemanticManager` so C wins for `.h`. If a project is
// primarily C++ users can flip the file extension to
// `.hpp` / `.hh` / `.hxx` to route through here.
&[".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx"]
}
fn extract(&self, file_path: &Path, source: &str) -> Result<ExtractedFile, String> {
let lang: tree_sitter::Language = tree_sitter_cpp::LANGUAGE.into();
let mut parser = Parser::new();
parser
.set_language(&lang)
.map_err(|e| format!("Failed to set language: {e}"))?;
let tree = parser.parse(source, None).ok_or("Failed to parse source")?;
let root = tree.root_node();
let bytes = source.as_bytes();
let mut symbols = Vec::new();
let mut imports = Vec::new();
let mut warnings = Vec::new();
if root.has_error() {
warnings.push("tree-sitter reported syntax errors".to_string());
}
for i in 0..root.named_child_count() {
if let Some(c) = root.named_child(i) {
self.dispatch_top(c, bytes, &mut symbols, &mut imports);
}
}
let exports: Vec<String> = symbols
.iter()
.filter(|s| s.is_exported)
.map(|s| s.name.clone())
.collect();
Ok(ExtractedFile {
file_path: file_path.to_path_buf(),
symbols,
imports,
exports,
warnings,
mtime: std::time::SystemTime::now(),
size: 0,
head_hash: 0,
})
}
fn find_callees_in_range(
&self,
source: &str,
_file_path: &Path,
range: ByteRange,
) -> Result<Vec<String>, String> {
let lang: tree_sitter::Language = tree_sitter_cpp::LANGUAGE.into();
// Direct calls + member calls. Constructor calls
// (`new Foo()`) show up as `new_expression` with a
// `type_identifier`; capture that too.
let query_str = r#"
(call_expression function: (identifier) @callee)
(call_expression function: (field_expression field: (field_identifier) @callee))
(call_expression function: (qualified_identifier name: (identifier) @callee))
(new_expression type: (type_identifier) @callee)
"#;
crate::semantic::common::run_callee_query(&lang, query_str, source, range)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn pb(n: &str) -> std::path::PathBuf {
std::path::PathBuf::from(n)
}
#[test]
fn extracts_top_level_function() {
let src = "int add(int a, int b) { return a + b; }\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
let add = f.symbols.iter().find(|s| s.name == "add").unwrap();
assert!(matches!(add.kind, SymbolKind::Function));
}
#[test]
fn extracts_class_with_public_private_visibility() {
let src = "class App {\npublic:\n void hello() {}\nprivate:\n void helper() {}\n};\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
let app = f.symbols.iter().find(|s| s.name == "App").unwrap();
assert!(matches!(app.kind, SymbolKind::Class));
let hello = f.symbols.iter().find(|s| s.name == "hello").unwrap();
assert!(hello.is_exported, "hello is under public:");
let helper = f.symbols.iter().find(|s| s.name == "helper").unwrap();
assert!(!helper.is_exported, "helper is under private:");
assert_eq!(hello.parent_class.as_deref(), Some("App"));
}
#[test]
fn extracts_struct_default_public() {
// `struct` in C++ defaults to PUBLIC visibility.
let src = "struct V { int x; void show() {} };\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
let v = f.symbols.iter().find(|s| s.name == "V").unwrap();
assert!(matches!(v.kind, SymbolKind::Class));
let show = f.symbols.iter().find(|s| s.name == "show").unwrap();
assert!(show.is_exported, "struct member is public by default");
}
#[test]
fn extracts_namespaced_class() {
let src = "namespace ns {\nclass Inner { public: void m() {} };\n}\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
// Inner appears, walked through the namespace.
assert!(f.symbols.iter().any(|s| s.name == "Inner"));
let m = f.symbols.iter().find(|s| s.name == "m").unwrap();
assert_eq!(m.parent_class.as_deref(), Some("Inner"));
}
#[test]
fn extracts_template_function() {
let src = "template<typename T> T identity(T x) { return x; }\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
assert!(
f.symbols
.iter()
.any(|s| s.name == "identity" && matches!(s.kind, SymbolKind::Function))
);
}
#[test]
fn extracts_includes() {
let src = "#include <string>\n#include \"local.hpp\"\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
assert!(f.imports.iter().any(|i| i.source == "string"));
assert!(f.imports.iter().any(|i| i.source == "local.hpp"));
}
#[test]
fn find_callees_captures_direct_member_qualified_and_new() {
let src = "void run() { helper(); foo.bar(); std::cout(); auto p = new Foo(); }\nvoid helper() {}\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
let run = f.symbols.iter().find(|s| s.name == "run").unwrap();
let callees = CppAdapter
.find_callees_in_range(src, &pb("a.cpp"), run.range)
.unwrap();
assert!(callees.contains(&"helper".to_string()));
assert!(callees.contains(&"bar".to_string()));
assert!(callees.contains(&"Foo".to_string()));
}
/// `extern "C" { ... }` blocks — previously dropped entirely.
#[test]
fn extracts_extern_c_block_contents() {
let src = "extern \"C\" {\n int c_function(int x);\n void c_void(void);\n}\nint c_function(int x) { return x; }\nvoid c_void(void) {}\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
assert!(f.symbols.iter().any(|s| s.name == "c_function"));
assert!(f.symbols.iter().any(|s| s.name == "c_void"));
}
/// Out-of-line method definitions `void Foo::bar() {}` attach
/// to the qualifying class via parent_class.
#[test]
fn out_of_line_method_attaches_to_qualifying_class() {
let src = "class Foo { public: void bar(); };\nvoid Foo::bar() {}\n";
let f = CppAdapter.extract(&pb("a.cpp"), src).unwrap();
// There are two `bar` symbols: the declaration inside Foo,
// and the out-of-line definition. Both should be Methods
// with parent_class = "Foo".
let bars: Vec<_> = f.symbols.iter().filter(|s| s.name == "bar").collect();
assert!(!bars.is_empty(), "no bar symbols");
assert!(
bars.iter().all(|s| matches!(s.kind, SymbolKind::Method)),
"every bar should be a Method; got {:?}",
bars.iter()
.map(|s| (s.kind, s.parent_class.clone()))
.collect::<Vec<_>>(),
);
assert!(
bars.iter()
.all(|s| s.parent_class.as_deref() == Some("Foo")),
"every bar's parent_class should be Foo",
);
}
}