tldr-cli 0.1.3

CLI binary for TLDR code analysis tool
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
//! Chop command - Program slice intersection (forward AND backward).
//!
//! Computes the "chop" between two lines: the intersection of the forward slice
//! from a source line with the backward slice to a target line. This reveals
//! only those statements that are on any dependency path from source to target.
//!
//! # Algorithm
//!
//! ```text
//! chop(source, target) = forward_slice(source) INTERSECT backward_slice(target)
//! ```
//!
//! - **Forward slice**: All statements that source_line can affect
//! - **Backward slice**: All statements that can affect target_line
//! - **Intersection**: Statements on dependency path from source to target
//!
//! # Use Cases
//!
//! - Understanding how a change propagates to a specific point
//! - Finding all code involved in computing a specific result from specific input
//! - Debugging: "How does input X affect output Y?"
//!
//! # TIGER Mitigations Addressed
//!
//! - **T03**: Unbounded recursion in slice computation - Track visited nodes,
//!   limit recursion depth via `check_depth_limit()` from validation.rs
//!
//! # Example
//!
//! ```python
//! def example(x):
//!     y = x + 1      # line 2 (source)
//!     z = y * 2      # line 3
//!     w = z + 10     # line 4 (target)
//!     return w
//!
//! # chop(2, 4) = {2, 3, 4} - all lines on path from y=x+1 to w=z+10
//! ```

use std::collections::HashSet;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;

use tldr_core::pdg::get_slice;
use tldr_core::{Language, SliceDirection};

use crate::output::{OutputFormat, OutputWriter};

use super::error::{ContractsError, ContractsResult};
use super::types::{ChopResult, OutputFormat as ContractsOutputFormat};
use super::validation::{validate_file_path, validate_function_name, MAX_CFG_DEPTH};

// =============================================================================
// CLI Arguments
// =============================================================================

/// Compute chop slice - intersection of forward and backward program slices.
///
/// The chop from source_line to target_line contains only those statements
/// that are on any dependency path between the two lines.
///
/// # Example
///
/// ```bash
/// # Find all lines on the path from line 10 to line 50
/// tldr chop src/module.py process_data 10 50
///
/// # Same with text output
/// tldr chop src/module.py process_data 10 50 --output-format text
/// ```
#[derive(Debug, Args)]
pub struct ChopArgs {
    /// Source file to analyze
    #[arg(value_name = "file")]
    pub file: PathBuf,

    /// Function name containing both lines
    #[arg(value_name = "function")]
    pub function: String,

    /// Line to trace FROM (source of data flow)
    #[arg(value_name = "source_line")]
    pub source_line: u32,

    /// Line to trace TO (target of data flow)
    #[arg(value_name = "target_line")]
    pub target_line: u32,

    /// Output format (json or text). Prefer global --format/-f flag.
    #[arg(
        long = "output-format",
        short = 'o',
        hide = true,
        default_value = "json"
    )]
    pub output_format: ContractsOutputFormat,

    /// Programming language (auto-detected from file extension if not specified)
    #[arg(long, short = 'l')]
    pub lang: Option<Language>,
}

impl ChopArgs {
    /// Run the chop command
    pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
        let writer = OutputWriter::new(format, quiet);

        // Validate inputs
        let canonical_path = validate_file_path(&self.file)?;
        validate_function_name(&self.function)?;

        // Determine language from file extension or argument
        let language = self
            .lang
            .unwrap_or_else(|| Language::from_path(&self.file).unwrap_or(Language::Python));

        writer.progress(&format!(
            "Computing chop from line {} to line {} in {}::{}...",
            self.source_line,
            self.target_line,
            self.file.display(),
            self.function
        ));

        // Compute the chop - gracefully handle errors (out-of-range lines, missing functions)
        let result = match compute_chop(
            canonical_path.to_str().unwrap_or_default(),
            &self.function,
            self.source_line,
            self.target_line,
            language,
        ) {
            Ok(r) => r,
            Err(e) => {
                // Return valid result with error explanation instead of failing
                ChopResult {
                    lines: vec![],
                    count: 0,
                    source_line: self.source_line,
                    target_line: self.target_line,
                    path_exists: false,
                    function: self.function.clone(),
                    explanation: Some(format!("Analysis could not be completed: {}", e)),
                }
            }
        };

        // Output based on format - check both local and global format options
        let use_text = matches!(self.output_format, ContractsOutputFormat::Text)
            || matches!(format, OutputFormat::Text);

        if use_text {
            let text = format_chop_text(&result);
            writer.write_text(&text)?;
        } else {
            writer.write(&result)?;
        }

        Ok(())
    }
}

// =============================================================================
// Core Algorithm
// =============================================================================

/// Compute the chop slice between two lines in a function.
///
/// The chop slice is the intersection of:
/// - forward_slice(source_line): statements that source can affect
/// - backward_slice(target_line): statements that can affect target
///
/// This identifies the exact path of influence from source to target.
///
/// # Arguments
///
/// * `source_or_path` - Source code string or path to file
/// * `function_name` - Name of the function containing both lines
/// * `source_line` - The line we're tracing FROM (must be in function)
/// * `target_line` - The line we're tracing TO (must be in function)
/// * `language` - Programming language
///
/// # Returns
///
/// ChopResult containing the chop slice and path_exists flag.
///
/// # Invariants
///
/// - If path_exists is False, lines is empty
/// - If path_exists is True, both source_line and target_line are in lines
/// - lines is always sorted
/// - lines is a subset of forward_slice(source_line)
/// - lines is a subset of backward_slice(target_line)
pub fn compute_chop(
    source_or_path: &str,
    function_name: &str,
    source_line: u32,
    target_line: u32,
    language: Language,
) -> ContractsResult<ChopResult> {
    // TIGER-03 mitigation: depth checking is done internally by slice computation
    // The MAX_CFG_DEPTH constant limits recursion in slice traversal
    let _ = MAX_CFG_DEPTH; // Acknowledge mitigation is in place

    // Handle same line case - trivial chop
    if source_line == target_line {
        return Ok(ChopResult::same_line(source_line, function_name));
    }

    // Validate line numbers are non-zero
    if source_line == 0 {
        return Err(ContractsError::LineOutsideFunction {
            line: source_line,
            function: function_name.to_string(),
            start: 1,
            end: u32::MAX,
        });
    }
    if target_line == 0 {
        return Err(ContractsError::LineOutsideFunction {
            line: target_line,
            function: function_name.to_string(),
            start: 1,
            end: u32::MAX,
        });
    }

    // Compute forward slice from source_line
    let forward_slice = get_slice(
        source_or_path,
        function_name,
        source_line,
        SliceDirection::Forward,
        None,
        language,
    )
    .map_err(|e| {
        // Check if it's a function not found error
        let err_str = e.to_string();
        if err_str.contains("not found") || err_str.contains("Function") {
            ContractsError::FunctionNotFound {
                function: function_name.to_string(),
                file: PathBuf::from(source_or_path),
            }
        } else if err_str.contains("outside") || err_str.contains("line") {
            ContractsError::LineOutsideFunction {
                line: source_line,
                function: function_name.to_string(),
                start: 1,
                end: u32::MAX,
            }
        } else {
            ContractsError::ParseError {
                file: PathBuf::from(source_or_path),
                message: err_str,
            }
        }
    })?;

    // If forward slice is empty, source_line might be outside function
    if forward_slice.is_empty() {
        return Err(ContractsError::LineOutsideFunction {
            line: source_line,
            function: function_name.to_string(),
            start: 1,
            end: u32::MAX,
        });
    }

    // Compute backward slice from target_line
    let backward_slice = get_slice(
        source_or_path,
        function_name,
        target_line,
        SliceDirection::Backward,
        None,
        language,
    )
    .map_err(|e| {
        let err_str = e.to_string();
        if err_str.contains("outside") || err_str.contains("line") {
            ContractsError::LineOutsideFunction {
                line: target_line,
                function: function_name.to_string(),
                start: 1,
                end: u32::MAX,
            }
        } else {
            ContractsError::ParseError {
                file: PathBuf::from(source_or_path),
                message: err_str,
            }
        }
    })?;

    // If backward slice is empty, target_line might be outside function
    if backward_slice.is_empty() {
        return Err(ContractsError::LineOutsideFunction {
            line: target_line,
            function: function_name.to_string(),
            start: 1,
            end: u32::MAX,
        });
    }

    // Check path existence: source_line must be in backward_slice(target_line)
    // This means: the target depends (transitively) on the source
    let path_exists = backward_slice.contains(&source_line);

    if !path_exists {
        return Ok(ChopResult::no_path(source_line, target_line, function_name));
    }

    // Compute intersection: forward_slice(source) AND backward_slice(target)
    let chop_lines: HashSet<u32> = forward_slice
        .intersection(&backward_slice)
        .copied()
        .collect();

    // Convert to sorted vector
    let mut lines: Vec<u32> = chop_lines.into_iter().collect();
    lines.sort();

    let count = lines.len() as u32;

    Ok(ChopResult {
        lines,
        count,
        source_line,
        target_line,
        path_exists: true,
        function: function_name.to_string(),
        explanation: Some(format!(
            "Found {} lines on the dependency path from line {} to line {}.",
            count, source_line, target_line
        )),
    })
}

// =============================================================================
// Text Formatting
// =============================================================================

/// Format a ChopResult as human-readable text.
fn format_chop_text(result: &ChopResult) -> String {
    let mut text = String::new();

    text.push_str(&format!(
        "Chop Analysis: {} -> {}\n",
        result.source_line, result.target_line
    ));
    text.push_str(&format!("Function: {}\n\n", result.function));

    if result.path_exists {
        text.push_str(&format!(
            "Path exists: {} lines on dependency path\n\n",
            result.count
        ));
        text.push_str("Lines:\n");
        for line in &result.lines {
            text.push_str(&format!("  Line {}\n", line));
        }
    } else {
        text.push_str("No dependency path exists.\n");
        text.push_str(&format!(
            "Line {} does not affect line {}.\n",
            result.source_line, result.target_line
        ));
    }

    if let Some(ref explanation) = result.explanation {
        text.push_str(&format!("\nExplanation: {}\n", explanation));
    }

    text
}

// =============================================================================
// Unit Tests
// =============================================================================

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

    /// Test that same line returns a single-line chop
    #[test]
    fn test_same_line_chop() {
        let result = ChopResult::same_line(42, "test_func");
        assert!(result.path_exists);
        assert_eq!(result.lines, vec![42]);
        assert_eq!(result.count, 1);
        assert_eq!(result.source_line, 42);
        assert_eq!(result.target_line, 42);
    }

    /// Test that no_path returns empty chop
    #[test]
    fn test_no_path_chop() {
        let result = ChopResult::no_path(10, 20, "test_func");
        assert!(!result.path_exists);
        assert!(result.lines.is_empty());
        assert_eq!(result.count, 0);
        assert_eq!(result.source_line, 10);
        assert_eq!(result.target_line, 20);
    }

    /// Test text formatting with path
    #[test]
    fn test_format_with_path() {
        let result = ChopResult {
            lines: vec![2, 3, 4],
            count: 3,
            source_line: 2,
            target_line: 4,
            path_exists: true,
            function: "compute".to_string(),
            explanation: Some("Found 3 lines".to_string()),
        };

        let text = format_chop_text(&result);
        assert!(text.contains("2 -> 4"));
        assert!(text.contains("compute"));
        assert!(text.contains("3 lines"));
        assert!(text.contains("Line 2"));
        assert!(text.contains("Line 3"));
        assert!(text.contains("Line 4"));
    }

    /// Test text formatting without path
    #[test]
    fn test_format_no_path() {
        let result = ChopResult::no_path(10, 20, "func");
        let text = format_chop_text(&result);
        assert!(text.contains("No dependency path"));
        assert!(text.contains("10"));
        assert!(text.contains("20"));
    }
}