repotoire 0.7.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! Value store and query API for constant propagation.
//!
//! `ValueStore` holds resolved symbolic values extracted during parsing and
//! provides O(1) lookups for detectors. `RawParseValues` is the intermediate
//! representation produced by parsers before ingestion into the store.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use super::types::{BinOp, LiteralValue, SymbolicValue};

/// A single variable assignment observed inside a function body.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Assignment {
    /// Local variable name (unqualified).
    pub variable: String,
    /// Resolved or partially resolved value.
    pub value: SymbolicValue,
    /// Source line (1-indexed).
    pub line: u32,
    /// Source column (0-indexed).
    pub column: u32,
}

/// Raw values extracted from a single file during parsing.
///
/// Parsers populate this struct per-file, then the pipeline calls
/// `ValueStore::ingest` to merge it into the global store.
#[derive(Debug, Default, Clone)]
pub struct RawParseValues {
    /// Module/class-level constants: `(qualified_name, value)`.
    pub module_constants: Vec<(String, SymbolicValue)>,
    /// Per-function assignment lists: `func_qn -> assignments`.
    pub function_assignments: HashMap<String, Vec<Assignment>>,
    /// Per-function return expressions: `func_qn -> return value`.
    pub return_expressions: HashMap<String, SymbolicValue>,
}

/// Central store for all resolved symbolic values in the analysed codebase.
///
/// Provides O(1) lookups by qualified name and line-scoped variable resolution.
pub struct ValueStore {
    /// Module/class-level constants keyed by qualified name.
    pub(crate) constants: HashMap<String, SymbolicValue>,
    /// Per-function assignment lists keyed by function qualified name.
    pub(crate) function_values: HashMap<String, Vec<Assignment>>,
    /// Resolved return values keyed by function qualified name.
    pub(crate) return_values: HashMap<String, SymbolicValue>,
}

/// Sentinel empty slice returned by `assignments_in` when a function has no
/// recorded assignments.
static EMPTY_ASSIGNMENTS: &[Assignment] = &[];

impl ValueStore {
    /// Create an empty value store.
    pub fn new() -> Self {
        Self {
            constants: HashMap::new(),
            function_values: HashMap::new(),
            return_values: HashMap::new(),
        }
    }

    /// Resolve a variable at a given source line inside a function.
    ///
    /// Returns the value of the *last* assignment to `var_name` whose line is
    /// `<=` the query `line`. If no matching assignment exists (or the function
    /// is unknown), returns `SymbolicValue::Unknown`.
    pub fn resolve_at(&self, func_qn: &str, var_name: &str, line: u32) -> SymbolicValue {
        let Some(assignments) = self.function_values.get(func_qn) else {
            return SymbolicValue::Unknown;
        };

        assignments
            .iter()
            .rev()
            .find(|a| a.variable == var_name && a.line <= line)
            .map(|a| a.value.clone())
            .unwrap_or(SymbolicValue::Unknown)
    }

    /// Return the resolved return value for a function, or `Unknown`.
    pub fn return_value(&self, func_qn: &str) -> SymbolicValue {
        self.return_values
            .get(func_qn)
            .cloned()
            .unwrap_or(SymbolicValue::Unknown)
    }

    /// Return all assignments recorded inside a function body.
    ///
    /// Returns an empty slice if the function is not present in the store.
    pub fn assignments_in(&self, func_qn: &str) -> &[Assignment] {
        self.function_values
            .get(func_qn)
            .map(|v| v.as_slice())
            .unwrap_or(EMPTY_ASSIGNMENTS)
    }

    /// Resolve a module/class-level constant by qualified name, or `Unknown`.
    pub fn resolve_constant(&self, qualified_name: &str) -> SymbolicValue {
        self.constants
            .get(qualified_name)
            .cloned()
            .unwrap_or(SymbolicValue::Unknown)
    }

    /// Attempt to fully evaluate a symbolic value down to a concrete literal.
    ///
    /// Returns `None` if the value cannot be reduced (e.g. contains `Unknown`,
    /// `Parameter`, `FieldAccess`, or unresolvable variables).
    pub fn try_evaluate(&self, value: &SymbolicValue) -> Option<LiteralValue> {
        match value {
            SymbolicValue::Literal(lit) => Some(lit.clone()),

            SymbolicValue::BinaryOp(op, lhs, rhs) => {
                let lhs_lit = self.try_evaluate(lhs)?;
                let rhs_lit = self.try_evaluate(rhs)?;
                evaluate_binary_op(*op, &lhs_lit, &rhs_lit)
            }

            SymbolicValue::Concat(parts) => {
                let mut buf = String::new();
                for part in parts {
                    let lit = self.try_evaluate(part)?;
                    buf.push_str(&literal_to_string(&lit)?);
                }
                Some(LiteralValue::String(buf))
            }

            SymbolicValue::Variable(name) => {
                let resolved = self.resolve_constant(name);
                if resolved == SymbolicValue::Unknown {
                    return None;
                }
                self.try_evaluate(&resolved)
            }

            // Parameter, FieldAccess, Index, Call, Phi, Unknown — cannot evaluate.
            _ => None,
        }
    }

    /// Merge raw parse values into the store.
    ///
    /// Constants are inserted (last-write wins for duplicates). Function
    /// assignments and return expressions are appended / overwritten per
    /// qualified name.
    pub fn ingest(&mut self, raw: RawParseValues) {
        for (name, value) in raw.module_constants {
            self.constants.insert(name, value);
        }
        for (func_qn, mut assignments) in raw.function_assignments {
            self.function_values
                .entry(func_qn)
                .or_default()
                .append(&mut assignments);
        }
        for (func_qn, value) in raw.return_expressions {
            self.return_values.insert(func_qn, value);
        }
    }
}

impl Default for ValueStore {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

/// Evaluate a binary operation on two concrete literal values.
///
/// Returns `None` for type mismatches or unsupported operator/operand
/// combinations (e.g. division by zero).
fn evaluate_binary_op(op: BinOp, lhs: &LiteralValue, rhs: &LiteralValue) -> Option<LiteralValue> {
    match (lhs, rhs) {
        // Integer arithmetic
        (LiteralValue::Integer(a), LiteralValue::Integer(b)) => match op {
            BinOp::Add => Some(LiteralValue::Integer(a.checked_add(*b)?)),
            BinOp::Sub => Some(LiteralValue::Integer(a.checked_sub(*b)?)),
            BinOp::Mul => Some(LiteralValue::Integer(a.checked_mul(*b)?)),
            BinOp::Div => {
                if *b == 0 {
                    return None;
                }
                Some(LiteralValue::Integer(a.checked_div(*b)?))
            }
            BinOp::Mod => {
                if *b == 0 {
                    return None;
                }
                Some(LiteralValue::Integer(a.checked_rem(*b)?))
            }
            BinOp::Eq => Some(LiteralValue::Boolean(a == b)),
            BinOp::NotEq => Some(LiteralValue::Boolean(a != b)),
            BinOp::Lt => Some(LiteralValue::Boolean(a < b)),
            BinOp::Gt => Some(LiteralValue::Boolean(a > b)),
            BinOp::LtEq => Some(LiteralValue::Boolean(a <= b)),
            BinOp::GtEq => Some(LiteralValue::Boolean(a >= b)),
            _ => None,
        },

        // Float arithmetic
        (LiteralValue::Float(a), LiteralValue::Float(b)) => match op {
            BinOp::Add => Some(LiteralValue::Float(a + b)),
            BinOp::Sub => Some(LiteralValue::Float(a - b)),
            BinOp::Mul => Some(LiteralValue::Float(a * b)),
            BinOp::Div => {
                if *b == 0.0 {
                    return None;
                }
                Some(LiteralValue::Float(a / b))
            }
            _ => None,
        },

        // String concatenation
        (LiteralValue::String(a), LiteralValue::String(b)) => match op {
            BinOp::Add => Some(LiteralValue::String(format!("{a}{b}"))),
            _ => None,
        },

        // Boolean logic
        (LiteralValue::Boolean(a), LiteralValue::Boolean(b)) => match op {
            BinOp::And => Some(LiteralValue::Boolean(*a && *b)),
            BinOp::Or => Some(LiteralValue::Boolean(*a || *b)),
            _ => None,
        },

        _ => None,
    }
}

/// Convert a literal value to its string representation for `Concat` evaluation.
fn literal_to_string(lit: &LiteralValue) -> Option<String> {
    match lit {
        LiteralValue::String(s) => Some(s.clone()),
        LiteralValue::Integer(i) => Some(i.to_string()),
        LiteralValue::Float(f) => Some(f.to_string()),
        LiteralValue::Boolean(b) => Some(b.to_string()),
        LiteralValue::Null => Some("null".to_string()),
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper: create an assignment.
    fn assign(var: &str, value: SymbolicValue, line: u32) -> Assignment {
        Assignment {
            variable: var.to_string(),
            value,
            line,
            column: 0,
        }
    }

    /// Helper: create an integer literal symbolic value.
    fn int_val(n: i64) -> SymbolicValue {
        SymbolicValue::Literal(LiteralValue::Integer(n))
    }

    /// Helper: create a string literal symbolic value.
    fn str_val(s: &str) -> SymbolicValue {
        SymbolicValue::Literal(LiteralValue::String(s.to_string()))
    }

    #[test]
    fn test_resolve_at_finds_last_assignment() {
        let mut store = ValueStore::new();
        store.function_values.insert(
            "mod.func".to_string(),
            vec![
                assign("x", int_val(1), 5),
                assign("x", int_val(2), 10),
                assign("x", int_val(3), 20),
            ],
        );

        // At line 15, the last assignment at or before is line 10 → value 2.
        assert_eq!(store.resolve_at("mod.func", "x", 15), int_val(2));

        // At line 25, the last assignment at or before is line 20 → value 3.
        assert_eq!(store.resolve_at("mod.func", "x", 25), int_val(3));

        // Exactly on the assignment line should match.
        assert_eq!(store.resolve_at("mod.func", "x", 10), int_val(2));
    }

    #[test]
    fn test_resolve_at_unknown_for_missing_var() {
        let mut store = ValueStore::new();
        store
            .function_values
            .insert("mod.func".to_string(), vec![assign("x", int_val(1), 5)]);

        assert_eq!(
            store.resolve_at("mod.func", "y", 10),
            SymbolicValue::Unknown
        );
    }

    #[test]
    fn test_resolve_at_unknown_for_missing_func() {
        let store = ValueStore::new();

        assert_eq!(
            store.resolve_at("no.such.func", "x", 10),
            SymbolicValue::Unknown
        );
    }

    #[test]
    fn test_return_value() {
        let mut store = ValueStore::new();
        store
            .return_values
            .insert("mod.func".to_string(), int_val(42));

        assert_eq!(store.return_value("mod.func"), int_val(42));
        assert_eq!(store.return_value("mod.other"), SymbolicValue::Unknown);
    }

    #[test]
    fn test_assignments_in() {
        let mut store = ValueStore::new();
        let assignments = vec![assign("x", int_val(1), 5), assign("y", int_val(2), 10)];
        store
            .function_values
            .insert("mod.func".to_string(), assignments.clone());

        assert_eq!(store.assignments_in("mod.func"), assignments.as_slice());
        assert!(store.assignments_in("mod.other").is_empty());
    }

    #[test]
    fn test_resolve_constant() {
        let mut store = ValueStore::new();
        store
            .constants
            .insert("mod.MAX_SIZE".to_string(), int_val(100));

        assert_eq!(store.resolve_constant("mod.MAX_SIZE"), int_val(100));
        assert_eq!(
            store.resolve_constant("mod.MISSING"),
            SymbolicValue::Unknown
        );
    }

    #[test]
    fn test_try_evaluate_literal() {
        let store = ValueStore::new();

        assert_eq!(
            store.try_evaluate(&int_val(42)),
            Some(LiteralValue::Integer(42))
        );
    }

    #[test]
    fn test_try_evaluate_binary_op() {
        let store = ValueStore::new();
        let expr = SymbolicValue::BinaryOp(BinOp::Add, Box::new(int_val(1)), Box::new(int_val(2)));

        assert_eq!(store.try_evaluate(&expr), Some(LiteralValue::Integer(3)));
    }

    #[test]
    fn test_try_evaluate_unknown_returns_none() {
        let store = ValueStore::new();

        assert_eq!(store.try_evaluate(&SymbolicValue::Unknown), None);
    }

    #[test]
    fn test_try_evaluate_concat() {
        let store = ValueStore::new();
        let expr = SymbolicValue::Concat(vec![str_val("hello"), str_val(" world")]);

        assert_eq!(
            store.try_evaluate(&expr),
            Some(LiteralValue::String("hello world".to_string()))
        );
    }

    #[test]
    fn test_try_evaluate_variable_resolves_constant() {
        let mut store = ValueStore::new();
        store.constants.insert("mod.BASE".to_string(), int_val(100));

        let expr = SymbolicValue::Variable("mod.BASE".to_string());
        assert_eq!(store.try_evaluate(&expr), Some(LiteralValue::Integer(100)));
    }

    #[test]
    fn test_try_evaluate_nested_binary_op() {
        let store = ValueStore::new();
        // (2 * 3) + 4 = 10
        let expr = SymbolicValue::BinaryOp(
            BinOp::Add,
            Box::new(SymbolicValue::BinaryOp(
                BinOp::Mul,
                Box::new(int_val(2)),
                Box::new(int_val(3)),
            )),
            Box::new(int_val(4)),
        );

        assert_eq!(store.try_evaluate(&expr), Some(LiteralValue::Integer(10)));
    }

    #[test]
    fn test_try_evaluate_div_by_zero_returns_none() {
        let store = ValueStore::new();
        let expr = SymbolicValue::BinaryOp(BinOp::Div, Box::new(int_val(10)), Box::new(int_val(0)));

        assert_eq!(store.try_evaluate(&expr), None);
    }

    #[test]
    fn test_try_evaluate_string_concat_via_binop() {
        let store = ValueStore::new();
        let expr = SymbolicValue::BinaryOp(
            BinOp::Add,
            Box::new(str_val("foo")),
            Box::new(str_val("bar")),
        );

        assert_eq!(
            store.try_evaluate(&expr),
            Some(LiteralValue::String("foobar".to_string()))
        );
    }

    #[test]
    fn test_try_evaluate_boolean_ops() {
        let store = ValueStore::new();

        let and_expr = SymbolicValue::BinaryOp(
            BinOp::And,
            Box::new(SymbolicValue::Literal(LiteralValue::Boolean(true))),
            Box::new(SymbolicValue::Literal(LiteralValue::Boolean(false))),
        );
        assert_eq!(
            store.try_evaluate(&and_expr),
            Some(LiteralValue::Boolean(false))
        );

        let or_expr = SymbolicValue::BinaryOp(
            BinOp::Or,
            Box::new(SymbolicValue::Literal(LiteralValue::Boolean(true))),
            Box::new(SymbolicValue::Literal(LiteralValue::Boolean(false))),
        );
        assert_eq!(
            store.try_evaluate(&or_expr),
            Some(LiteralValue::Boolean(true))
        );
    }

    #[test]
    fn test_try_evaluate_comparison() {
        let store = ValueStore::new();
        let expr = SymbolicValue::BinaryOp(BinOp::Lt, Box::new(int_val(3)), Box::new(int_val(5)));

        assert_eq!(store.try_evaluate(&expr), Some(LiteralValue::Boolean(true)));
    }

    #[test]
    fn test_try_evaluate_float_arithmetic() {
        let store = ValueStore::new();
        let expr = SymbolicValue::BinaryOp(
            BinOp::Mul,
            Box::new(SymbolicValue::Literal(LiteralValue::Float(2.5))),
            Box::new(SymbolicValue::Literal(LiteralValue::Float(4.0))),
        );

        assert_eq!(store.try_evaluate(&expr), Some(LiteralValue::Float(10.0)));
    }

    #[test]
    fn test_ingest() {
        let mut store = ValueStore::new();

        let raw = RawParseValues {
            module_constants: vec![
                ("mod.A".to_string(), int_val(1)),
                ("mod.B".to_string(), int_val(2)),
            ],
            function_assignments: {
                let mut m = HashMap::new();
                m.insert("mod.func".to_string(), vec![assign("x", int_val(10), 5)]);
                m
            },
            return_expressions: {
                let mut m = HashMap::new();
                m.insert("mod.func".to_string(), int_val(10));
                m
            },
        };

        store.ingest(raw);

        assert_eq!(store.resolve_constant("mod.A"), int_val(1));
        assert_eq!(store.resolve_constant("mod.B"), int_val(2));
        assert_eq!(store.assignments_in("mod.func").len(), 1);
        assert_eq!(store.return_value("mod.func"), int_val(10));
    }

    #[test]
    fn test_ingest_appends_assignments() {
        let mut store = ValueStore::new();

        let raw1 = RawParseValues {
            module_constants: vec![],
            function_assignments: {
                let mut m = HashMap::new();
                m.insert("mod.func".to_string(), vec![assign("x", int_val(1), 5)]);
                m
            },
            return_expressions: HashMap::new(),
        };

        let raw2 = RawParseValues {
            module_constants: vec![],
            function_assignments: {
                let mut m = HashMap::new();
                m.insert("mod.func".to_string(), vec![assign("x", int_val(2), 10)]);
                m
            },
            return_expressions: HashMap::new(),
        };

        store.ingest(raw1);
        store.ingest(raw2);

        // Both ingested assignment lists should have been appended.
        assert_eq!(store.assignments_in("mod.func").len(), 2);
    }

    #[test]
    fn test_evaluate_binary_op_mod() {
        assert_eq!(
            evaluate_binary_op(
                BinOp::Mod,
                &LiteralValue::Integer(10),
                &LiteralValue::Integer(3)
            ),
            Some(LiteralValue::Integer(1))
        );
        // Mod by zero returns None.
        assert_eq!(
            evaluate_binary_op(
                BinOp::Mod,
                &LiteralValue::Integer(10),
                &LiteralValue::Integer(0)
            ),
            None
        );
    }

    #[test]
    fn test_evaluate_binary_op_float_div_by_zero() {
        assert_eq!(
            evaluate_binary_op(
                BinOp::Div,
                &LiteralValue::Float(1.0),
                &LiteralValue::Float(0.0)
            ),
            None
        );
    }

    #[test]
    fn test_concat_with_mixed_types() {
        let store = ValueStore::new();
        let expr = SymbolicValue::Concat(vec![
            str_val("count: "),
            int_val(42),
            str_val(", active: "),
            SymbolicValue::Literal(LiteralValue::Boolean(true)),
        ]);

        assert_eq!(
            store.try_evaluate(&expr),
            Some(LiteralValue::String("count: 42, active: true".to_string()))
        );
    }

    #[test]
    fn test_default_value_store() {
        let store = ValueStore::default();
        assert_eq!(store.resolve_constant("any"), SymbolicValue::Unknown);
        assert!(store.assignments_in("any").is_empty());
    }
}