go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
//! PDG (Program Dependence Graph) construction.
//!
//! Builds a PDG by combining CFG and DFG, computing control dependencies
//! from the CFG structure.
//!
//! # Control Dependency Computation
//!
//! A statement S is control-dependent on a predicate P if:
//! 1. There exists a path from P to S where S executes
//! 2. There exists another path from P where S does not execute
//!
//! In practice, we compute this from CFG edges:
//! - For True/False edges: target block lines are control-dependent on source condition
//! - For Loop edges: body lines are control-dependent on loop header
//! - For Exception edges: handler lines are control-dependent on try block

use std::collections::{HashMap, HashSet};

use crate::cfg::types::{BlockId, CFGInfo, EdgeType};
use crate::dfg::types::DFGInfo;
use crate::pdg::types::{BranchType, ControlDependence, PDGInfo};

/// Builder for constructing Program Dependence Graphs.
///
/// Combines CFG (control flow) and DFG (data flow) into a unified PDG
/// with computed control dependencies.
pub struct PDGBuilder {
    cfg: CFGInfo,
    dfg: DFGInfo,
    control_deps: Vec<ControlDependence>,

    /// Cache: block ID -> (start_line, end_line)
    block_line_ranges: HashMap<BlockId, (usize, usize)>,
}

impl PDGBuilder {
    /// Create a new PDGBuilder from CFG and DFG.
    pub fn new(cfg: CFGInfo, dfg: DFGInfo) -> Self {
        // Pre-compute block line ranges for efficient lookup
        let block_line_ranges: HashMap<BlockId, (usize, usize)> = cfg
            .blocks
            .iter()
            .map(|(id, block)| (*id, (block.start_line, block.end_line)))
            .collect();

        Self {
            cfg,
            dfg,
            control_deps: Vec::new(),
            block_line_ranges,
        }
    }

    /// Build the PDG with computed control dependencies.
    pub fn build(mut self) -> PDGInfo {
        self.compute_control_dependencies();
        PDGInfo::new(self.cfg, self.dfg, self.control_deps)
    }

    /// Compute control dependencies from CFG structure.
    ///
    /// For each conditional edge (True/False/Loop/Exception), the target block's
    /// lines are control-dependent on the source block's condition line.
    fn compute_control_dependencies(&mut self) {
        // Track which blocks are in each branch to compute proper control dependencies
        // For now, use the simpler approach: direct edge targets are control-dependent

        // Collect transitive dependency computation requests to avoid borrow conflict
        let mut transitive_deps: Vec<(BlockId, BlockId, usize, BranchType)> = Vec::new();

        for edge in &self.cfg.edges {
            // Skip unconditional edges - they don't create control dependencies
            let branch_type = match edge.edge_type {
                EdgeType::True => BranchType::True,
                EdgeType::False | EdgeType::Else => BranchType::False,
                EdgeType::BackEdge | EdgeType::Loop | EdgeType::Iterate => BranchType::Loop,
                EdgeType::Exception | EdgeType::ExceptionGroup => BranchType::Exception,
                // These edge types also create control dependencies
                EdgeType::Enter | EdgeType::Case | EdgeType::PatternMatch | EdgeType::Match => {
                    BranchType::True // Treat as conditional
                }
                EdgeType::OkSome | EdgeType::ErrNone => BranchType::True, // Rust Result/Option
                EdgeType::Pass | EdgeType::Fail => BranchType::True,     // Assert
                _ => continue, // Skip unconditional/sequential edges
            };

            // Get source block's condition line (usually the block's start line)
            let condition_line = self.get_condition_line(edge.from);

            // Get all lines in the target block and add control dependencies
            let (target_start, target_end) = match self.block_line_ranges.get(&edge.to) {
                Some(&range) => range,
                None => continue,
            };

            // For each line in the target block, it's control-dependent on the condition
            for line in target_start..=target_end {
                // Don't create self-dependencies
                if line != condition_line {
                    self.control_deps.push(ControlDependence {
                        condition_line,
                        dependent_line: line,
                        branch_type,
                    });
                }
            }

            // For branches, also add control dependencies for all blocks reachable
            // only through this branch (until they reconverge)
            if matches!(branch_type, BranchType::True | BranchType::False) {
                transitive_deps.push((edge.from, edge.to, condition_line, branch_type));
            }
        }

        // Process transitive control dependencies after releasing the immutable borrow
        for (source_block, start_block, condition_line, branch_type) in transitive_deps {
            self.add_transitive_control_deps(source_block, start_block, condition_line, branch_type);
        }

        // Deduplicate control dependencies
        self.deduplicate_control_deps();
    }

    /// Add transitive control dependencies for blocks only reachable through a branch.
    ///
    /// This handles nested statements inside if/else blocks, loops, etc.
    fn add_transitive_control_deps(
        &mut self,
        source_block: BlockId,
        start_block: BlockId,
        condition_line: usize,
        branch_type: BranchType,
    ) {
        // Find all blocks reachable from start_block
        let mut visited: HashSet<BlockId> = HashSet::new();
        let mut to_visit: Vec<BlockId> = vec![start_block];

        // Find the reconvergence point (where branches merge)
        // For simplicity, we limit depth and avoid loops
        let mut depth = 0;
        const MAX_DEPTH: usize = 50;

        while let Some(block_id) = to_visit.pop() {
            if visited.contains(&block_id) || depth > MAX_DEPTH {
                continue;
            }

            // Don't go back to the source block (would create cycles)
            if block_id == source_block {
                continue;
            }

            visited.insert(block_id);
            depth += 1;

            // Get successors of this block
            for &succ in self.cfg.successors(block_id) {
                // Skip back edges to avoid infinite loops
                if let Some(edge) = self.cfg.edges.iter().find(|e| e.from == block_id && e.to == succ) {
                    if edge.edge_type == EdgeType::BackEdge {
                        continue;
                    }
                }
                to_visit.push(succ);
            }
        }

        // For all visited blocks (except start_block which we already handled),
        // add control dependencies for their lines
        for block_id in visited {
            if block_id == start_block {
                continue;
            }

            let (start_line, end_line) = match self.block_line_ranges.get(&block_id) {
                Some(&range) => range,
                None => continue,
            };

            for line in start_line..=end_line {
                if line != condition_line {
                    self.control_deps.push(ControlDependence {
                        condition_line,
                        dependent_line: line,
                        branch_type,
                    });
                }
            }
        }
    }

    /// Get the condition line for a block (typically the block's start line).
    fn get_condition_line(&self, block_id: BlockId) -> usize {
        self.cfg
            .blocks
            .get(&block_id)
            .map(|b| b.start_line)
            .unwrap_or(1)
    }

    /// Remove duplicate control dependencies.
    fn deduplicate_control_deps(&mut self) {
        let mut seen: HashSet<(usize, usize)> = HashSet::new();
        self.control_deps.retain(|dep| {
            let key = (dep.condition_line, dep.dependent_line);
            seen.insert(key)
        });
    }
}

/// Build a PDG from file and function name.
///
/// # Arguments
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
/// PDGInfo containing combined CFG, DFG, and control dependencies.
pub fn build_pdg(file: &str, function: &str) -> crate::error::Result<PDGInfo> {
    build_pdg_with_language(file, function, None)
}

/// Build a PDG from file and function name with explicit language specification.
///
/// This function allows overriding the language auto-detection, which is useful
/// for files without extensions or with non-standard extensions.
///
/// # Arguments
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
/// * `language` - Optional language override (e.g., "python", "typescript", "rust").
///   If `None`, language is auto-detected from file extension.
///
/// # Returns
/// PDGInfo containing combined CFG, DFG, and control dependencies.
///
/// # Example
/// ```ignore
/// use go_brrr::pdg::build_pdg_with_language;
///
/// // Auto-detect language from file extension
/// let pdg = build_pdg_with_language("main.py", "process", None)?;
///
/// // Force Python language for extensionless file
/// let pdg = build_pdg_with_language("Makefile.inc", "target", Some("python"))?;
/// ```
pub fn build_pdg_with_language(
    file: &str,
    function: &str,
    language: Option<&str>,
) -> crate::error::Result<PDGInfo> {
    let cfg = crate::cfg::extract_with_language(file, function, language)?;
    let dfg = crate::dfg::extract_with_language(file, function, language)?;
    Ok(PDGBuilder::new(cfg, dfg).build())
}

/// Build a PDG from existing CFG and DFG.
pub fn build_pdg_from_graphs(cfg: CFGInfo, dfg: DFGInfo) -> PDGInfo {
    PDGBuilder::new(cfg, dfg).build()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cfg::types::{BlockType, CFGBlock, CFGEdge};
    use crate::dfg::types::{DataflowEdge, DataflowKind};

    /// Create a test CFG representing:
    /// ```text
    /// def test(x):        # Block 0, line 1 (entry)
    ///     if x > 0:       # Block 1, line 2 (branch)
    ///         result = x  # Block 2, line 3 (true branch)
    ///     else:
    ///         result = 0  # Block 3, line 5 (false branch)
    ///     return result   # Block 4, line 6 (exit)
    /// ```
    fn create_test_cfg() -> CFGInfo {
        let mut blocks = HashMap::new();

        blocks.insert(
            BlockId(0),
            CFGBlock {
                id: BlockId(0),
                label: "entry".to_string(),
                block_type: BlockType::Entry,
                statements: vec!["def test(x):".to_string()],
                func_calls: vec![],
                start_line: 1,
                end_line: 1,
            },
        );

        blocks.insert(
            BlockId(1),
            CFGBlock {
                id: BlockId(1),
                label: "if x > 0".to_string(),
                block_type: BlockType::Branch,
                statements: vec!["if x > 0:".to_string()],
                func_calls: vec![],
                start_line: 2,
                end_line: 2,
            },
        );

        blocks.insert(
            BlockId(2),
            CFGBlock {
                id: BlockId(2),
                label: "true branch".to_string(),
                block_type: BlockType::Body,
                statements: vec!["result = x".to_string()],
                func_calls: vec![],
                start_line: 3,
                end_line: 3,
            },
        );

        blocks.insert(
            BlockId(3),
            CFGBlock {
                id: BlockId(3),
                label: "false branch".to_string(),
                block_type: BlockType::Body,
                statements: vec!["result = 0".to_string()],
                func_calls: vec![],
                start_line: 5,
                end_line: 5,
            },
        );

        blocks.insert(
            BlockId(4),
            CFGBlock {
                id: BlockId(4),
                label: "exit".to_string(),
                block_type: BlockType::Return,
                statements: vec!["return result".to_string()],
                func_calls: vec![],
                start_line: 6,
                end_line: 6,
            },
        );

        let edges = vec![
            CFGEdge::unconditional(BlockId(0), BlockId(1)),
            CFGEdge::new(BlockId(1), BlockId(2), EdgeType::True),
            CFGEdge::new(BlockId(1), BlockId(3), EdgeType::False),
            CFGEdge::unconditional(BlockId(2), BlockId(4)),
            CFGEdge::unconditional(BlockId(3), BlockId(4)),
        ];

        CFGInfo {
            function_name: "test".to_string(),
            blocks,
            edges,
            entry: BlockId(0),
            exits: vec![BlockId(4)],
            decision_points: 1,
            comprehension_decision_points: 0,
            nested_cfgs: HashMap::new(),
            is_async: false,
            await_points: 0,
            blocking_calls: Vec::new(),
            ..Default::default()
        }
    }

    /// Create a test DFG for the same function.
    fn create_test_dfg() -> DFGInfo {
        DFGInfo::new(
            "test".to_string(),
            vec![
                // x flows from parameter to line 3 (true branch)
                DataflowEdge {
                    variable: "x".to_string(),
                    from_line: 1,
                    to_line: 3,
                    kind: DataflowKind::Use,
                },
                // result flows from line 3 to return (line 6)
                DataflowEdge {
                    variable: "result".to_string(),
                    from_line: 3,
                    to_line: 6,
                    kind: DataflowKind::Definition,
                },
                // result flows from line 5 to return (line 6)
                DataflowEdge {
                    variable: "result".to_string(),
                    from_line: 5,
                    to_line: 6,
                    kind: DataflowKind::Definition,
                },
            ],
            [
                ("x".to_string(), vec![1]),
                ("result".to_string(), vec![3, 5]),
            ]
            .into_iter()
            .collect(),
            [("x".to_string(), vec![3]), ("result".to_string(), vec![6])]
                .into_iter()
                .collect(),
        )
    }

    #[test]
    fn test_pdg_builder_creates_control_deps() {
        let cfg = create_test_cfg();
        let dfg = create_test_dfg();

        let pdg = PDGBuilder::new(cfg, dfg).build();

        // Line 3 (true branch) should be control-dependent on line 2 (condition)
        assert!(
            pdg.is_control_dependent(3, 2),
            "Line 3 should be control-dependent on line 2 (true branch)"
        );

        // Line 5 (false branch) should be control-dependent on line 2 (condition)
        assert!(
            pdg.is_control_dependent(5, 2),
            "Line 5 should be control-dependent on line 2 (false branch)"
        );
    }

    #[test]
    fn test_pdg_has_both_control_and_data_edges() {
        let cfg = create_test_cfg();
        let dfg = create_test_dfg();

        let pdg = PDGBuilder::new(cfg, dfg).build();

        // Should have control dependencies
        assert!(pdg.control_dep_count() > 0, "PDG should have control dependencies");

        // Should have data flow edges
        assert!(pdg.data_edge_count() > 0, "PDG should have data flow edges");
    }

    #[test]
    fn test_control_deps_for_line() {
        let cfg = create_test_cfg();
        let dfg = create_test_dfg();

        let pdg = PDGBuilder::new(cfg, dfg).build();

        // Line 3 should have line 2 as its control dependency
        let deps = pdg.control_deps_for(3);
        assert!(
            deps.contains(&2),
            "Line 3's control deps should include line 2"
        );
    }

    #[test]
    fn test_lines_controlled_by() {
        let cfg = create_test_cfg();
        let dfg = create_test_dfg();

        let pdg = PDGBuilder::new(cfg, dfg).build();

        // Line 2 should control lines 3 and 5
        let controlled = pdg.lines_controlled_by(2);
        assert!(controlled.contains(&3), "Line 2 should control line 3");
        assert!(controlled.contains(&5), "Line 2 should control line 5");
    }
}