mcp-cpp-server 0.2.2

A high-performance Model Context Protocol (MCP) server for C++ code analysis using clangd LSP integration
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//! Component index management for tracking file indexing states
//!
//! This module provides ComponentIndex which is a pure data structure for managing
//! the indexing state of files in a compilation database. It maps source files to
//! their index files and tracks the indexing status of each file without complex logic.

use super::hash::compute_file_hash;
use crate::clangd::version::ClangdVersion;
use crate::project::CompilationDatabase;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ComponentIndexError {
    #[error("Index directory does not exist: {path}")]
    IndexDirectoryNotFound { path: String },
    #[error("Unable to access index directory: {path}")]
    IndexDirectoryAccess { path: String },
    #[error("File not found in index: {path}")]
    FileNotFound { path: String },
    #[error("Path canonicalization failed for {path}: {error}")]
    PathCanonicalization { path: String, error: String },
}

/// File indexing states
#[derive(Debug, Clone, PartialEq, Default)]
pub enum FileIndexState {
    /// File is pending indexing (not yet processed)
    #[default]
    Pending,
    /// File is currently being indexed
    InProgress,
    /// File has been successfully indexed
    Indexed,
    /// File indexing failed with error message
    Failed(String),
}

/// Comprehensive indexing summary with detailed state information
#[derive(Debug, Clone)]
pub struct IndexingSummary {
    /// Total number of files in compilation database
    pub total_files: usize,
    /// Number of files successfully indexed
    pub indexed_count: usize,
    /// Number of files pending indexing
    pub pending_count: usize,
    /// Number of files currently being indexed
    pub in_progress_count: usize,
    /// Number of files that failed indexing
    pub failed_count: usize,
    /// Current coverage ratio (0.0 to 1.0)
    pub coverage: f32,
    /// Whether all files are indexed
    pub is_fully_indexed: bool,
    /// Whether any files are currently being processed
    pub has_active_indexing: bool,
    /// List of files that are pending indexing
    pub pending_files: Vec<PathBuf>,
    /// List of files currently being indexed
    pub in_progress_files: Vec<PathBuf>,
    /// List of files that have been indexed
    pub indexed_files: Vec<PathBuf>,
    /// List of files that failed with their error messages
    pub failed_files: Vec<(PathBuf, String)>,
}

/// Pure data structure for managing component index state
///
/// ComponentIndex is responsible for:
/// - Mapping source files to their index file paths
/// - Tracking indexing status for each compilation database file
/// - Providing simple queries and updates for file states
/// - Calculating coverage statistics
/// - Finding next files to process
///
/// This structure contains no complex logic - it's purely for data management.
pub struct ComponentIndex {
    /// Path to the index directory (.cache/clangd/index/)
    index_dir: PathBuf,
    /// Mapping from source file path to index file path
    file_to_index: HashMap<PathBuf, PathBuf>,
    /// Current indexing state for each file
    file_states: HashMap<PathBuf, FileIndexState>,
    /// Set of files from compilation database that should be indexed
    cdb_files: HashSet<PathBuf>,
    /// Clangd version for hash function selection
    format_version: u32,
}

impl ComponentIndex {
    /// Create a new ComponentIndex from a compilation database and clangd version
    ///
    /// All files are initialized as Pending - the ComponentIndexMonitor is responsible
    /// for checking disk state and updating file states appropriately.
    pub fn new(
        compilation_db: &CompilationDatabase,
        clangd_version: &ClangdVersion,
    ) -> Result<Self, ComponentIndexError> {
        let compilation_db_path = compilation_db.path();
        let compilation_db_dir = compilation_db_path
            .parent()
            .unwrap_or_else(|| Path::new("."));

        // Index directory is .cache/clangd/index/ relative to compilation database
        let index_dir = compilation_db_dir
            .join(".cache")
            .join("clangd")
            .join("index");

        let format_version = clangd_version.index_format_version();
        let mut file_to_index = HashMap::new();
        let mut file_states = HashMap::new();

        // Get canonical source files using the single source of truth for path canonicalization
        let canonical_files = compilation_db.canonical_source_files().map_err(|e| {
            ComponentIndexError::PathCanonicalization {
                path: "compilation database".to_string(),
                error: e.to_string(),
            }
        })?;

        let cdb_files: HashSet<PathBuf> = canonical_files.iter().cloned().collect();

        // Build mapping for each canonical file
        for canonical_source_file in canonical_files {
            // Compute hash for the canonical source file path
            let file_path_str = canonical_source_file.to_string_lossy();
            let hash = compute_file_hash(&file_path_str, format_version);

            // Extract basename from canonical path
            let basename = canonical_source_file
                .file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("unknown");

            // Construct index filename: basename.hash.idx
            let index_filename = format!("{basename}.{hash:016X}.idx");
            let index_path = index_dir.join(&index_filename);

            // Add mapping using canonical path as key
            file_to_index.insert(canonical_source_file.clone(), index_path);

            // Initialize all files as pending - ComponentIndexMonitor will update states based on disk
            file_states.insert(canonical_source_file, FileIndexState::Pending);
        }

        Ok(ComponentIndex {
            index_dir,
            file_to_index,
            file_states,
            cdb_files,
            format_version,
        })
    }

    /// Create a new ComponentIndex for testing without filesystem dependencies
    #[cfg(test)]
    pub fn new_for_test(
        compilation_db: &CompilationDatabase,
        clangd_version: &ClangdVersion,
    ) -> Self {
        let format_version = clangd_version.index_format_version();
        let mut file_to_index = HashMap::new();
        let mut file_states = HashMap::new();
        let mut cdb_files = HashSet::new();

        // Build mapping for each file in the compilation database
        for entry in compilation_db.entries() {
            let source_file = &entry.file;
            cdb_files.insert(source_file.to_path_buf());

            // Compute hash for the source file path
            let file_path_str = source_file.to_string_lossy();
            let hash = compute_file_hash(&file_path_str, format_version);

            // Extract basename from path
            let basename = source_file
                .file_name()
                .and_then(|name| name.to_str())
                .unwrap_or("unknown");

            // Construct index filename: basename.hash.idx (use a fake directory for tests)
            let index_filename = format!("{basename}.{hash:016X}.idx");
            let fake_index_dir = PathBuf::from("/fake/test/index");
            let index_path = fake_index_dir.join(&index_filename);

            // Add mapping - assume all files are pending initially in tests
            file_to_index.insert(source_file.to_path_buf(), index_path);
            file_states.insert(source_file.to_path_buf(), FileIndexState::Pending);
        }

        ComponentIndex {
            index_dir: PathBuf::from("/fake/test/index"),
            file_to_index,
            file_states,
            cdb_files,
            format_version,
        }
    }

    // File State Management Methods

    /// Mark a file as currently being indexed
    pub fn mark_file_in_progress(&mut self, source_file: &Path) -> bool {
        if let Some(state) = self.file_states.get_mut(source_file) {
            *state = FileIndexState::InProgress;
            true
        } else {
            false
        }
    }

    /// Mark a file as successfully indexed
    pub fn mark_file_indexed(&mut self, source_file: &Path) -> bool {
        if let Some(state) = self.file_states.get_mut(source_file) {
            *state = FileIndexState::Indexed;
            true
        } else {
            false
        }
    }

    /// Mark a file as failed to index with error message
    pub fn mark_file_failed(&mut self, source_file: &Path, error: String) -> bool {
        if let Some(state) = self.file_states.get_mut(source_file) {
            *state = FileIndexState::Failed(error);
            true
        } else {
            false
        }
    }

    /// Reset a file's state back to pending
    pub fn mark_file_pending(&mut self, source_file: &Path) -> bool {
        if let Some(state) = self.file_states.get_mut(source_file) {
            *state = FileIndexState::Pending;
            true
        } else {
            false
        }
    }

    // Query Methods

    /// Get the indexing state of a file
    pub fn get_file_state(&self, source_file: &Path) -> Option<&FileIndexState> {
        self.file_states.get(source_file)
    }

    /// Check if a source file has been indexed (index file exists and state is Indexed)
    pub fn is_file_indexed(&self, source_file: &Path) -> bool {
        matches!(
            self.file_states.get(source_file),
            Some(FileIndexState::Indexed)
        )
    }

    /// Check if a source file is currently being indexed
    pub fn is_file_in_progress(&self, source_file: &Path) -> bool {
        matches!(
            self.file_states.get(source_file),
            Some(FileIndexState::InProgress)
        )
    }

    /// Check if a source file is pending indexing
    pub fn is_file_pending(&self, source_file: &Path) -> bool {
        matches!(
            self.file_states.get(source_file),
            Some(FileIndexState::Pending)
        )
    }

    /// Check if a source file indexing failed
    pub fn is_file_failed(&self, source_file: &Path) -> bool {
        matches!(
            self.file_states.get(source_file),
            Some(FileIndexState::Failed(_))
        )
    }

    /// Get the next file that needs indexing (in Pending state)
    pub fn get_next_uncovered_file(&self) -> Option<&Path> {
        self.cdb_files
            .iter()
            .find(|file| {
                matches!(
                    self.file_states.get(file.as_path()),
                    Some(FileIndexState::Pending)
                )
            })
            .map(|p| p.as_path())
    }

    /// Get all files in pending state
    pub fn get_pending_files(&self) -> Vec<&Path> {
        self.cdb_files
            .iter()
            .filter(|file| {
                matches!(
                    self.file_states.get(file.as_path()),
                    Some(FileIndexState::Pending)
                )
            })
            .map(|p| p.as_path())
            .collect()
    }

    /// Get all files in in-progress state
    pub fn get_in_progress_files(&self) -> Vec<&Path> {
        self.cdb_files
            .iter()
            .filter(|file| {
                matches!(
                    self.file_states.get(file.as_path()),
                    Some(FileIndexState::InProgress)
                )
            })
            .map(|p| p.as_path())
            .collect()
    }

    /// Get all files in indexed state
    pub fn get_indexed_files(&self) -> Vec<&Path> {
        self.cdb_files
            .iter()
            .filter(|file| {
                matches!(
                    self.file_states.get(file.as_path()),
                    Some(FileIndexState::Indexed)
                )
            })
            .map(|p| p.as_path())
            .collect()
    }

    /// Get all files in failed state
    pub fn get_failed_files(&self) -> Vec<(&Path, &String)> {
        self.cdb_files
            .iter()
            .filter_map(|file| {
                if let Some(FileIndexState::Failed(error)) = self.file_states.get(file.as_path()) {
                    Some((file.as_path(), error))
                } else {
                    None
                }
            })
            .collect()
    }

    // Statistics and Coverage Methods

    /// Get the number of files that have been indexed
    pub fn indexed_count(&self) -> usize {
        self.file_states
            .values()
            .filter(|state| matches!(state, FileIndexState::Indexed))
            .count()
    }

    /// Get the number of files that are pending indexing
    pub fn pending_count(&self) -> usize {
        self.file_states
            .values()
            .filter(|state| matches!(state, FileIndexState::Pending))
            .count()
    }

    /// Get the number of files that are currently being indexed
    pub fn in_progress_count(&self) -> usize {
        self.file_states
            .values()
            .filter(|state| matches!(state, FileIndexState::InProgress))
            .count()
    }

    /// Get the number of files that failed to index
    pub fn failed_count(&self) -> usize {
        self.file_states
            .values()
            .filter(|state| matches!(state, FileIndexState::Failed(_)))
            .count()
    }

    /// Get the total number of compilation database files
    pub fn total_files_count(&self) -> usize {
        self.cdb_files.len()
    }

    /// Get current indexing coverage as a ratio (0.0 to 1.0)
    pub fn coverage(&self) -> f32 {
        let total = self.total_files_count();
        if total == 0 {
            1.0
        } else {
            self.indexed_count() as f32 / total as f32
        }
    }

    /// Check if all files are indexed
    pub fn is_fully_indexed(&self) -> bool {
        self.pending_count() == 0 && self.in_progress_count() == 0
    }

    /// Check if any files are currently being processed
    pub fn has_active_indexing(&self) -> bool {
        self.in_progress_count() > 0
    }

    // File and index path methods

    /// Get the index file path for a given source file
    pub fn get_index_file(&self, source_file: &Path) -> Option<&Path> {
        self.file_to_index.get(source_file).map(|p| p.as_path())
    }

    /// Get all source files that are part of the compilation database
    pub fn source_files(&self) -> Vec<&Path> {
        self.cdb_files.iter().map(|p| p.as_path()).collect()
    }

    /// Get the index directory path
    pub fn index_directory(&self) -> &Path {
        &self.index_dir
    }

    /// Get the format version used for this index
    pub fn format_version(&self) -> u32 {
        self.format_version
    }

    /// Get all index file paths
    pub fn index_files(&self) -> Vec<&Path> {
        self.file_to_index.values().map(|p| p.as_path()).collect()
    }

    /// Get comprehensive indexing summary with detailed state information
    pub fn get_indexing_summary(&self) -> IndexingSummary {
        let pending_files: Vec<_> = self
            .get_pending_files()
            .iter()
            .map(|p| p.to_path_buf())
            .collect();
        let in_progress_files: Vec<_> = self
            .get_in_progress_files()
            .iter()
            .map(|p| p.to_path_buf())
            .collect();
        let indexed_files: Vec<_> = self
            .get_indexed_files()
            .iter()
            .map(|p| p.to_path_buf())
            .collect();
        let failed_files: Vec<_> = self
            .get_failed_files()
            .iter()
            .map(|(path, error)| (path.to_path_buf(), (*error).clone()))
            .collect();

        IndexingSummary {
            total_files: self.total_files_count(),
            indexed_count: self.indexed_count(),
            pending_count: self.pending_count(),
            in_progress_count: self.in_progress_count(),
            failed_count: self.failed_count(),
            coverage: self.coverage(),
            is_fully_indexed: self.is_fully_indexed(),
            has_active_indexing: self.has_active_indexing(),
            pending_files,
            in_progress_files,
            indexed_files,
            failed_files,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::clangd::version::ClangdVersion;
    use crate::project::CompilationDatabase;
    use std::fs;
    use std::io::Write;
    use tempfile::TempDir;

    fn create_test_compilation_database(dir: &Path) -> std::io::Result<CompilationDatabase> {
        let compile_commands_path = dir.join("compile_commands.json");
        let mut file = fs::File::create(&compile_commands_path)?;

        let content = r#"[
            {
                "directory": "/test/project",
                "command": "clang++ -c main.cpp -o main.o",
                "file": "/test/project/main.cpp"
            },
            {
                "directory": "/test/project",
                "command": "clang++ -c utils.cpp -o utils.o", 
                "file": "/test/project/utils.cpp"
            }
        ]"#;

        file.write_all(content.as_bytes())?;
        Ok(CompilationDatabase::new(compile_commands_path).unwrap())
    }

    fn create_test_version() -> ClangdVersion {
        ClangdVersion {
            major: 18,
            minor: 1,
            patch: 8,
            variant: None,
            date: None,
        }
    }

    #[test]
    fn test_component_index_creation() -> std::io::Result<()> {
        let temp_dir = TempDir::new()?;
        let build_dir = temp_dir.path();

        let compilation_db = create_test_compilation_database(build_dir)?;

        let version = create_test_version();
        let component_index = ComponentIndex::new(&compilation_db, &version).unwrap();

        // All files should start as pending (pure in-memory, no disk checks)
        assert_eq!(component_index.total_files_count(), 2);
        assert_eq!(component_index.pending_count(), 2);
        assert_eq!(component_index.indexed_count(), 0);
        assert_eq!(component_index.coverage(), 0.0);
        assert!(!component_index.is_fully_indexed());

        Ok(())
    }

    #[test]
    fn test_file_state_management() -> std::io::Result<()> {
        let temp_dir = TempDir::new()?;
        let build_dir = temp_dir.path();

        let compilation_db = create_test_compilation_database(build_dir)?;

        let version = create_test_version();
        let mut component_index = ComponentIndex::new(&compilation_db, &version).unwrap();

        let main_cpp = Path::new("/test/project/main.cpp");

        // Test marking file as in progress
        assert!(component_index.mark_file_in_progress(main_cpp));
        assert!(component_index.is_file_in_progress(main_cpp));
        assert_eq!(component_index.in_progress_count(), 1);

        // Test marking file as indexed
        assert!(component_index.mark_file_indexed(main_cpp));
        assert!(component_index.is_file_indexed(main_cpp));
        assert_eq!(component_index.indexed_count(), 1);
        assert_eq!(component_index.coverage(), 0.5);

        // Test marking file as failed
        assert!(component_index.mark_file_failed(main_cpp, "Test error".to_string()));
        assert!(component_index.is_file_failed(main_cpp));
        assert_eq!(component_index.failed_count(), 1);

        // Test marking file as pending again
        assert!(component_index.mark_file_pending(main_cpp));
        assert!(component_index.is_file_pending(main_cpp));
        assert_eq!(component_index.pending_count(), 2);

        Ok(())
    }

    #[test]
    fn test_next_uncovered_file() -> std::io::Result<()> {
        let temp_dir = TempDir::new()?;
        let build_dir = temp_dir.path();

        let compilation_db = create_test_compilation_database(build_dir)?;

        let version = create_test_version();
        let mut component_index = ComponentIndex::new(&compilation_db, &version).unwrap();

        // Should return one of the pending files (all start as pending now)
        let next_file = component_index.get_next_uncovered_file();
        assert!(next_file.is_some());

        // Mark first file as indexed
        let main_cpp = Path::new("/test/project/main.cpp");
        component_index.mark_file_indexed(main_cpp);

        // Should still return the remaining pending file
        let next_file = component_index.get_next_uncovered_file();
        assert!(next_file.is_some());
        assert_ne!(next_file.unwrap(), main_cpp);

        // Mark second file as indexed
        let utils_cpp = Path::new("/test/project/utils.cpp");
        component_index.mark_file_indexed(utils_cpp);

        // Should return None when all files are indexed
        assert!(component_index.get_next_uncovered_file().is_none());
        assert!(component_index.is_fully_indexed());

        Ok(())
    }

    #[test]
    fn test_file_collections() -> std::io::Result<()> {
        let temp_dir = TempDir::new()?;
        let build_dir = temp_dir.path();

        let compilation_db = create_test_compilation_database(build_dir)?;

        let version = create_test_version();
        let mut component_index = ComponentIndex::new(&compilation_db, &version).unwrap();

        let main_cpp = Path::new("/test/project/main.cpp");
        let utils_cpp = Path::new("/test/project/utils.cpp");

        // Set different states
        component_index.mark_file_in_progress(main_cpp);
        component_index.mark_file_failed(utils_cpp, "Test failure".to_string());

        // Test collections
        let in_progress = component_index.get_in_progress_files();
        assert_eq!(in_progress.len(), 1);
        assert_eq!(in_progress[0], main_cpp);

        let failed = component_index.get_failed_files();
        assert_eq!(failed.len(), 1);
        assert_eq!(failed[0].0, utils_cpp);
        assert_eq!(failed[0].1, "Test failure");

        let pending = component_index.get_pending_files();
        assert_eq!(pending.len(), 0);

        Ok(())
    }

    #[test]
    fn test_get_indexing_summary() -> std::io::Result<()> {
        let temp_dir = TempDir::new()?;
        let build_dir = temp_dir.path();

        let compilation_db = create_test_compilation_database(build_dir)?;

        let version = create_test_version();
        let mut component_index = ComponentIndex::new(&compilation_db, &version).unwrap();

        let main_cpp = Path::new("/test/project/main.cpp");
        let utils_cpp = Path::new("/test/project/utils.cpp");

        // Set up different states
        component_index.mark_file_in_progress(main_cpp);
        component_index.mark_file_failed(utils_cpp, "Test error".to_string());

        let summary = component_index.get_indexing_summary();

        // Verify summary contents
        assert_eq!(summary.total_files, 2);
        assert_eq!(summary.indexed_count, 0);
        assert_eq!(summary.pending_count, 0);
        assert_eq!(summary.in_progress_count, 1);
        assert_eq!(summary.failed_count, 1);
        assert_eq!(summary.coverage, 0.0);
        assert!(!summary.is_fully_indexed);
        assert!(summary.has_active_indexing);

        // Verify file lists
        assert_eq!(summary.in_progress_files.len(), 1);
        assert_eq!(summary.in_progress_files[0], main_cpp);
        assert_eq!(summary.failed_files.len(), 1);
        assert_eq!(summary.failed_files[0].0, utils_cpp);
        assert_eq!(summary.failed_files[0].1, "Test error");

        Ok(())
    }
}