octocode 0.11.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
Documentation
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! C++ language implementation for the indexer

use crate::indexer::languages::Language;
use tree_sitter::Node;

pub struct Cpp {}

impl Language for Cpp {
	fn name(&self) -> &'static str {
		"cpp"
	}

	fn get_ts_language(&self) -> tree_sitter::Language {
		tree_sitter_cpp::LANGUAGE.into()
	}

	fn get_meaningful_kinds(&self) -> Vec<&'static str> {
		vec![
			"function_definition",
			"declaration", // For function declarations in headers
			"class_specifier",
			"struct_specifier",
			"enum_specifier",
			"namespace_definition",
			"preproc_include", // For #include statements
		]
	}

	fn extract_symbols(&self, node: Node, contents: &str) -> Vec<String> {
		let mut symbols = Vec::new();

		match node.kind() {
			"function_definition" => {
				// Find function name
				for child in node.children(&mut node.walk()) {
					if child.kind() == "function_declarator" {
						for decl_child in child.children(&mut child.walk()) {
							if decl_child.kind() == "identifier" {
								if let Ok(name) = decl_child.utf8_text(contents.as_bytes()) {
									symbols.push(name.to_string());
								}
								break;
							}
						}
						break;
					}
				}

				// Extract variables from function body
				for child in node.children(&mut node.walk()) {
					if child.kind() == "compound_statement" {
						self.extract_cpp_variables(child, contents, &mut symbols);
						break;
					}
				}
			}
			"declaration" => {
				// Handle both function declarations and variable declarations
				let mut found_function = false;

				// First, check if this is a function declaration
				for child in node.children(&mut node.walk()) {
					if child.kind() == "function_declarator" {
						found_function = true;
						for decl_child in child.children(&mut child.walk()) {
							if decl_child.kind() == "identifier" {
								if let Ok(name) = decl_child.utf8_text(contents.as_bytes()) {
									symbols.push(name.to_string());
								}
								break;
							}
						}
						break;
					}
				}

				// If not a function declaration, extract variable names
				if !found_function {
					for child in node.children(&mut node.walk()) {
						if child.kind() == "init_declarator" || child.kind() == "declarator" {
							for decl_child in child.children(&mut child.walk()) {
								if decl_child.kind() == "identifier" {
									if let Ok(name) = decl_child.utf8_text(contents.as_bytes()) {
										symbols.push(name.to_string());
									}
									break;
								}
							}
						}
					}
				}
			}
			"class_specifier" | "struct_specifier" | "enum_specifier" => {
				// Find class/struct/enum name
				for child in node.children(&mut node.walk()) {
					if child.kind() == "name" || child.kind() == "type_identifier" {
						if let Ok(name) = child.utf8_text(contents.as_bytes()) {
							symbols.push(name.to_string());
						}
						break;
					}
				}

				// Extract member names
				self.extract_cpp_members(node, contents, &mut symbols);
			}
			"namespace_definition" => {
				// Find namespace name
				for child in node.children(&mut node.walk()) {
					if child.kind() == "identifier" || child.kind() == "namespace_identifier" {
						if let Ok(name) = child.utf8_text(contents.as_bytes()) {
							symbols.push(name.to_string());
						}
						break;
					}
				}
			}
			_ => self.extract_identifiers(node, contents, &mut symbols),
		}

		// Deduplicate symbols before returning
		symbols.sort();
		symbols.dedup();

		symbols
	}

	fn extract_identifiers(&self, node: Node, contents: &str, symbols: &mut Vec<String>) {
		let kind = node.kind();
		// Check if this is a valid identifier
		if kind == "identifier" || kind == "type_identifier" || kind == "field_identifier" {
			if let Ok(text) = node.utf8_text(contents.as_bytes()) {
				let t = text.trim();
				if !t.is_empty() && !symbols.contains(&t.to_string()) {
					symbols.push(t.to_string());
				}
			}
		}

		// Continue with recursive traversal
		let mut cursor = node.walk();
		if cursor.goto_first_child() {
			loop {
				self.extract_identifiers(cursor.node(), contents, symbols);
				if !cursor.goto_next_sibling() {
					break;
				}
			}
		}
	}

	fn are_node_types_equivalent(&self, type1: &str, type2: &str) -> bool {
		// Direct match
		if type1 == type2 {
			return true;
		}

		// C++-specific semantic groups
		let semantic_groups = [
			// Functions and methods
			&["function_definition"] as &[&str],
			// Type definitions
			&["class_specifier", "struct_specifier", "enum_specifier"],
			// Namespaces
			&["namespace_definition"],
			// Templates
			&["template_declaration"],
			// Variable declarations (group standalone variable declarations together)
			&["declaration"],
			// Preprocessor directives
			&[
				"preproc_include",
				"preproc_define",
				"preproc_ifdef",
				"preproc_ifndef",
			],
		];

		// Check if both types belong to the same semantic group
		for group in &semantic_groups {
			let contains_type1 = group.contains(&type1);
			let contains_type2 = group.contains(&type2);

			if contains_type1 && contains_type2 {
				return true;
			}
		}

		false
	}

	fn get_node_type_description(&self, node_type: &str) -> &'static str {
		match node_type {
			"function_definition" => "function declarations",
			"class_specifier" => "class declarations",
			"struct_specifier" => "struct declarations",
			"enum_specifier" => "enum declarations",
			"namespace_definition" => "namespace declarations",
			"template_declaration" => "template declarations",
			"declaration" => "variable declarations",
			"preproc_include" | "preproc_define" | "preproc_ifdef" | "preproc_ifndef" => {
				"preprocessor directives"
			}
			_ => "declarations",
		}
	}

	fn extract_imports_exports(&self, node: Node, contents: &str) -> (Vec<String>, Vec<String>) {
		let mut imports = Vec::new();
		let mut exports = Vec::new();

		match node.kind() {
			// Extract imports from #include statements
			"preproc_include" => {
				if let Ok(include_text) = node.utf8_text(contents.as_bytes()) {
					if let Some(header) = Self::parse_cpp_include(include_text) {
						imports.push(header);
					}
				}
			}
			// Extract exports from function definitions and declarations
			"function_definition" | "declaration" => {
				// For function definitions, always consider them as exports
				if node.kind() == "function_definition" {
					if let Some(function_name) = self.extract_function_name(node, contents) {
						exports.push(function_name);
					}
				} else {
					// For declarations, check if it's a function declaration
					let mut is_function_declaration = false;
					for child in node.children(&mut node.walk()) {
						if child.kind() == "function_declarator" {
							is_function_declaration = true;
							if let Some(function_name) = self.extract_function_name(node, contents)
							{
								exports.push(function_name);
							}
							break;
						}
					}

					// If not a function declaration, it might be a variable declaration
					// Export global variables and constants
					if !is_function_declaration {
						if let Some(var_names) = self.extract_variable_names(node, contents) {
							exports.extend(var_names);
						}
					}
				}
			}
			// Extract exports from class, struct, and enum declarations
			"class_specifier" | "struct_specifier" | "enum_specifier" => {
				if let Some(type_name) = self.extract_type_name(node, contents) {
					exports.push(type_name);
				}
			}
			// Extract exports from namespace declarations
			"namespace_definition" => {
				if let Some(namespace_name) = self.extract_namespace_name(node, contents) {
					exports.push(namespace_name);
				}
			}
			// Extract exports from template declarations
			"template_declaration" => {
				// Template declarations can contain functions, classes, etc.
				// Look for the actual declaration inside the template
				for child in node.children(&mut node.walk()) {
					match child.kind() {
						"function_definition" | "class_specifier" | "struct_specifier" => {
							if let Some(template_name) = self.extract_template_name(child, contents)
							{
								exports.push(format!("template<{}>", template_name));
							}
						}
						_ => {}
					}
				}
			}
			// Extract exports from macro definitions
			"preproc_define" => {
				if let Some(macro_name) = self.extract_macro_name(node, contents) {
					exports.push(macro_name);
				}
			}
			// Extract exports from typedef declarations
			"type_definition" => {
				if let Some(typedef_name) = self.extract_typedef_name(node, contents) {
					exports.push(typedef_name);
				}
			}
			_ => {
				// For other node types, don't extract anything
				// This prevents noise and focuses on meaningful exports
			}
		}

		(imports, exports)
	}

	fn resolve_import(
		&self,
		import_path: &str,
		source_file: &str,
		all_files: &[String],
	) -> Option<String> {
		use super::resolution_utils::FileRegistry;

		let registry = FileRegistry::new(all_files);

		if import_path.starts_with('"') && import_path.ends_with('"') {
			// Local include: #include "header.h"
			let header_name = &import_path[1..import_path.len() - 1];
			self.resolve_local_include(header_name, source_file, &registry)
		} else if import_path.starts_with('<') && import_path.ends_with('>') {
			// System include: #include <header.h> - try to find in project
			let header_name = &import_path[1..import_path.len() - 1];
			registry.find_exact_file(header_name)
		} else {
			// Direct path or unquoted header - try local resolution first, then search common directories
			if let Some(local_result) =
				self.resolve_local_include(import_path, source_file, &registry)
			{
				Some(local_result)
			} else if let Some(exact_result) = registry.find_exact_file(import_path) {
				Some(exact_result)
			} else {
				// Search in common include directories
				let include_dirs = ["include", "inc", "headers", "lib"];
				for include_dir in &include_dirs {
					let full_path = std::path::Path::new(include_dir).join(import_path);
					if let Some(result) = registry.find_exact_file(&full_path.to_string_lossy()) {
						return Some(result);
					}
				}
				None
			}
		}
	}

	fn get_file_extensions(&self) -> Vec<&'static str> {
		vec!["cpp", "cc", "cxx", "c++", "c", "h", "hpp"]
	}
}

impl Cpp {
	/// Extract variable declarations in C++ compound statements
	#[allow(clippy::only_used_in_recursion)]
	fn extract_cpp_variables(&self, node: Node, contents: &str, symbols: &mut Vec<String>) {
		let mut cursor = node.walk();
		if cursor.goto_first_child() {
			loop {
				let child = cursor.node();

				match child.kind() {
					"declaration" => {
						// Handle variable declarations
						for decl_child in child.children(&mut child.walk()) {
							if decl_child.kind() == "init_declarator"
								|| decl_child.kind() == "declarator"
							{
								for init_child in decl_child.children(&mut decl_child.walk()) {
									if init_child.kind() == "identifier" {
										if let Ok(name) = init_child.utf8_text(contents.as_bytes())
										{
											if !symbols.contains(&name.to_string()) {
												symbols.push(name.to_string());
											}
										}
										break;
									}
								}
							}
						}
					}
					"compound_statement" => {
						// Recursively process nested blocks
						self.extract_cpp_variables(child, contents, symbols);
					}
					"if_statement" | "for_statement" | "while_statement" | "do_statement" => {
						// Process compound statements in control structures
						for stmt_child in child.children(&mut child.walk()) {
							if stmt_child.kind() == "compound_statement" {
								self.extract_cpp_variables(stmt_child, contents, symbols);
							}
						}
					}
					_ => {}
				}

				if !cursor.goto_next_sibling() {
					break;
				}
			}
		}
	}

	/// Extract members from class/struct/enum
	fn extract_cpp_members(&self, node: Node, contents: &str, symbols: &mut Vec<String>) {
		let mut cursor = node.walk();
		if cursor.goto_first_child() {
			loop {
				let child = cursor.node();

				match child.kind() {
					"field_declaration" => {
						// Extract field names
						for field_child in child.children(&mut child.walk()) {
							if field_child.kind() == "field_identifier"
								|| field_child.kind() == "identifier"
							{
								if let Ok(name) = field_child.utf8_text(contents.as_bytes()) {
									if !symbols.contains(&name.to_string()) {
										symbols.push(name.to_string());
									}
								}
							}
						}
					}
					"function_definition" => {
						// Handle method definitions
						for fn_child in child.children(&mut child.walk()) {
							if fn_child.kind() == "function_declarator" {
								for decl_child in fn_child.children(&mut fn_child.walk()) {
									if decl_child.kind() == "identifier" {
										if let Ok(name) = decl_child.utf8_text(contents.as_bytes())
										{
											if !symbols.contains(&name.to_string()) {
												symbols.push(name.to_string());
											}
										}
										break;
									}
								}
								break;
							}
						}
					}
					"enum_specifier" => {
						// Extract enum constant names
						for enum_child in child.children(&mut child.walk()) {
							if enum_child.kind() == "enumerator_list" {
								for enum_list_child in enum_child.children(&mut enum_child.walk()) {
									if enum_list_child.kind() == "enumerator" {
										for enumerator_child in
											enum_list_child.children(&mut enum_list_child.walk())
										{
											if enumerator_child.kind() == "identifier" {
												if let Ok(name) =
													enumerator_child.utf8_text(contents.as_bytes())
												{
													if !symbols.contains(&name.to_string()) {
														symbols.push(name.to_string());
													}
												}
												break;
											}
										}
									}
								}
							}
						}
					}
					_ => {}
				}

				if !cursor.goto_next_sibling() {
					break;
				}
			}
		}
	}

	// C++ has #include statements for imports

	// Helper function to parse C++ include statements
	fn parse_cpp_include(include_text: &str) -> Option<String> {
		let trimmed = include_text.trim();

		// Handle #include <header.h> or #include "header.h"
		if trimmed.starts_with("#include") {
			let include_part = trimmed.strip_prefix("#include").unwrap().trim(); // Remove "#include"

			// Handle <header.h>
			if include_part.starts_with('<') && include_part.ends_with('>') {
				return Some(include_part[1..include_part.len() - 1].to_string());
			}

			// Handle "header.h"
			if include_part.starts_with('"') && include_part.ends_with('"') {
				return Some(include_part[1..include_part.len() - 1].to_string());
			}
		}

		None
	}
}

impl Cpp {
	/// Extract function name from function definition or declaration
	fn extract_function_name(&self, node: Node, contents: &str) -> Option<String> {
		for child in node.children(&mut node.walk()) {
			if child.kind() == "function_declarator" {
				for decl_child in child.children(&mut child.walk()) {
					if decl_child.kind() == "identifier" {
						if let Ok(name) = decl_child.utf8_text(contents.as_bytes()) {
							return Some(name.to_string());
						}
					}
				}
			}
		}
		None
	}

	/// Extract variable names from declaration nodes
	fn extract_variable_names(&self, node: Node, contents: &str) -> Option<Vec<String>> {
		let mut var_names = Vec::new();

		for child in node.children(&mut node.walk()) {
			if child.kind() == "init_declarator" || child.kind() == "declarator" {
				for decl_child in child.children(&mut child.walk()) {
					if decl_child.kind() == "identifier" {
						if let Ok(name) = decl_child.utf8_text(contents.as_bytes()) {
							var_names.push(name.to_string());
						}
					}
				}
			}
		}

		if var_names.is_empty() {
			None
		} else {
			Some(var_names)
		}
	}

	/// Extract type name from class, struct, or enum declarations
	fn extract_type_name(&self, node: Node, contents: &str) -> Option<String> {
		for child in node.children(&mut node.walk()) {
			if child.kind() == "type_identifier" || child.kind() == "identifier" {
				if let Ok(name) = child.utf8_text(contents.as_bytes()) {
					return Some(name.to_string());
				}
			}
		}
		None
	}

	/// Extract namespace name from namespace definition
	fn extract_namespace_name(&self, node: Node, contents: &str) -> Option<String> {
		for child in node.children(&mut node.walk()) {
			if child.kind() == "identifier" || child.kind() == "namespace_identifier" {
				if let Ok(name) = child.utf8_text(contents.as_bytes()) {
					return Some(name.to_string());
				}
			}
		}
		None
	}

	/// Extract template name from template declarations
	fn extract_template_name(&self, node: Node, contents: &str) -> Option<String> {
		match node.kind() {
			"function_definition" => self.extract_function_name(node, contents),
			"class_specifier" | "struct_specifier" => self.extract_type_name(node, contents),
			_ => None,
		}
	}

	/// Extract macro name from preprocessor define
	fn extract_macro_name(&self, node: Node, contents: &str) -> Option<String> {
		// Look for the identifier after #define
		for child in node.children(&mut node.walk()) {
			if child.kind() == "identifier" {
				if let Ok(name) = child.utf8_text(contents.as_bytes()) {
					return Some(name.to_string());
				}
			}
		}
		None
	}

	/// Extract typedef name from type definition
	fn extract_typedef_name(&self, node: Node, contents: &str) -> Option<String> {
		// Look for the new type name in typedef
		for child in node.children(&mut node.walk()) {
			if child.kind() == "type_identifier" {
				if let Ok(name) = child.utf8_text(contents.as_bytes()) {
					return Some(name.to_string());
				}
			}
		}
		None
	}

	/// Resolve local includes relative to source file
	fn resolve_local_include(
		&self,
		header_name: &str,
		source_file: &str,
		registry: &super::resolution_utils::FileRegistry,
	) -> Option<String> {
		let source_path = std::path::Path::new(source_file);
		let source_dir = source_path.parent()?;
		let header_path = source_dir.join(header_name);

		// Try to normalize the path to handle .. components
		let normalized_path =
			crate::utils::path::PathNormalizer::normalize_path(&header_path.to_string_lossy());

		registry.find_exact_file(&normalized_path)
	}
}