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
//! Available Expressions Analysis CLI command
//!
//! Computes available expressions at each program point for Common Subexpression
//! Elimination (CSE) detection.
//!
//! # Usage
//!
//! ```bash
//! tldr available <file> <function> [-f json|text]
//! tldr available src/main.py process_data --check "a + b"
//! tldr available src/main.py process_data --at_line 42
//! ```
//!
//! # Output
//!
//! - JSON: Full AvailableExprsInfo with avail_in/avail_out per block
//! - Text: Human-readable summary with redundant computations highlighted
//!
//! # Reference
//! - dataflow/spec.md (CAP-AE-01 through CAP-AE-12)

use std::path::PathBuf;

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

use tldr_core::dataflow::{compute_available_exprs_with_source_and_lang, AvailableExprsInfo};
use tldr_core::{get_cfg_context, get_dfg_context, Language};

use crate::output::OutputFormat;

/// Analyze available expressions for CSE detection
#[derive(Debug, Args)]
pub struct AvailableArgs {
    /// Source file to analyze
    pub file: PathBuf,

    /// Function name to analyze
    pub function: String,

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

    /// Check if a specific expression is available (e.g., "a + b")
    #[arg(long)]
    pub check: Option<String>,

    /// Show expressions available at a specific line number
    #[arg(long)]
    pub at_line: Option<usize>,

    /// Show what kills a specific expression
    #[arg(long)]
    pub killed_by: Option<String>,

    /// Show only CSE opportunities, skip per-block details
    #[arg(long)]
    pub cse_only: bool,
}

impl AvailableArgs {
    /// Run the available expressions analysis command
    pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
        use crate::output::OutputWriter;

        let writer = OutputWriter::new(format, quiet);

        // 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!(
            "Analyzing available expressions for {} in {}...",
            self.function,
            self.file.display()
        ));

        // Read source file - ensure it exists
        if !self.file.exists() {
            return Err(anyhow::anyhow!("File not found: {}", self.file.display()));
        }

        // Read source for line mapping
        let source = std::fs::read_to_string(&self.file)?;
        let source_lines: Vec<String> = source.lines().map(|s| s.to_string()).collect();

        // Get CFG for the function
        let cfg = get_cfg_context(
            self.file.to_str().unwrap_or_default(),
            &self.function,
            language,
        )?;

        // Get DFG for expression extraction
        let dfg = get_dfg_context(
            self.file.to_str().unwrap_or_default(),
            &self.function,
            language,
        )?;

        // Compute available expressions (with AST-based extraction for multi-language support)
        let result = compute_available_exprs_with_source_and_lang(
            &cfg,
            &dfg,
            &source_lines,
            Some(language),
        )?;

        // Handle specific queries
        if let Some(ref expr) = self.check {
            return self.handle_check_query(&result, expr, &writer);
        }

        if let Some(line) = self.at_line {
            return self.handle_at_line_query(&result, line, &writer);
        }

        if let Some(ref expr) = self.killed_by {
            return self.handle_killed_by_query(&result, expr, &writer);
        }

        // Default: output full result
        match format {
            OutputFormat::Json => {
                let json = serde_json::to_string_pretty(&result)
                    .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
                writer.write_text(&json)?;
            }
            OutputFormat::Text => {
                let text = self.format_text_output(&result);
                writer.write_text(&text)?;
            }
            OutputFormat::Compact => {
                let json = serde_json::to_string(&result)
                    .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
                writer.write_text(&json)?;
            }
            _ => {
                let json = serde_json::to_string_pretty(&result)
                    .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
                writer.write_text(&json)?;
            }
        }

        Ok(())
    }

    fn handle_check_query(
        &self,
        result: &AvailableExprsInfo,
        expr: &str,
        writer: &crate::output::OutputWriter,
    ) -> Result<()> {
        // Find blocks where this expression is available
        let mut available_in_blocks = Vec::new();

        for (block_id, exprs) in &result.avail_in {
            if exprs.iter().any(|e| e.text == expr) {
                available_in_blocks.push(*block_id);
            }
        }

        let output = serde_json::json!({
            "expression": expr,
            "available_in_blocks": available_in_blocks,
            "is_redundant": result.redundant_computations().iter().any(|(text, _, _)| text == expr),
        });

        let json = serde_json::to_string_pretty(&output)
            .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
        writer.write_text(&json)?;
        Ok(())
    }

    fn handle_at_line_query(
        &self,
        result: &AvailableExprsInfo,
        line: usize,
        writer: &crate::output::OutputWriter,
    ) -> Result<()> {
        // Find expressions available at the given line
        let mut available_exprs = Vec::new();

        for exprs in result.avail_in.values() {
            for expr in exprs {
                if expr.line <= line && !available_exprs.contains(&expr.text) {
                    available_exprs.push(expr.text.clone());
                }
            }
        }

        // Also check avail_out for completeness
        for exprs in result.avail_out.values() {
            for expr in exprs {
                if expr.line <= line && !available_exprs.contains(&expr.text) {
                    available_exprs.push(expr.text.clone());
                }
            }
        }

        let output = serde_json::json!({
            "line": line,
            "available_expressions": available_exprs,
        });

        let json = serde_json::to_string_pretty(&output)
            .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
        writer.write_text(&json)?;
        Ok(())
    }

    fn handle_killed_by_query(
        &self,
        result: &AvailableExprsInfo,
        expr: &str,
        writer: &crate::output::OutputWriter,
    ) -> Result<()> {
        // Find what variables kill this expression
        let mut killers = Vec::new();

        // Find the expression to get its operands
        for exprs in result.avail_in.values() {
            for e in exprs {
                if e.text == expr {
                    killers.extend(e.operands.iter().cloned());
                    break;
                }
            }
        }

        // Also check avail_out
        for exprs in result.avail_out.values() {
            for e in exprs {
                if e.text == expr {
                    for op in &e.operands {
                        if !killers.contains(op) {
                            killers.push(op.clone());
                        }
                    }
                    break;
                }
            }
        }

        let output = serde_json::json!({
            "expression": expr,
            "killed_by_redefinition_of": killers,
        });

        let json = serde_json::to_string_pretty(&output)
            .map_err(|e| anyhow::anyhow!("JSON serialization failed: {}", e))?;
        writer.write_text(&json)?;
        Ok(())
    }

    fn format_text_output(&self, result: &AvailableExprsInfo) -> String {
        let mut output = String::new();

        output.push_str(&format!(
            "Available Expressions Analysis: {} in {}\n\n",
            self.function,
            self.file.display()
        ));

        // Show redundant computations (CSE opportunities)
        // redundant_computations returns Vec<(text, first_line, redundant_line)>
        let redundant = result.redundant_computations();
        if !redundant.is_empty() {
            output.push_str("CSE Opportunities (redundant computations):\n");
            for (expr_text, first_line, redundant_line) in &redundant {
                output.push_str(&format!(
                    "  - '{}' first at line {}, redundant at line {}\n",
                    expr_text, first_line, redundant_line
                ));
            }
            output.push('\n');
        } else {
            output.push_str("No redundant computations detected.\n\n");
        }

        // Show available expressions per block (unless --cse-only)
        if !self.cse_only {
            output.push_str("Available expressions by block:\n");
            let mut blocks: Vec<_> = result.avail_in.keys().collect();
            blocks.sort();

            for block_id in blocks {
                if let Some(exprs) = result.avail_in.get(block_id) {
                    if !exprs.is_empty() {
                        let expr_strs: Vec<_> = exprs.iter().map(|e| e.text.as_str()).collect();
                        output.push_str(&format!(
                            "  Block {}: {}\n",
                            block_id,
                            expr_strs.join(", ")
                        ));
                    }
                }
            }
        }

        output
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::{HashMap, HashSet};
    use std::path::PathBuf;

    use tldr_core::dataflow::available::{
        AvailableExprsInfo, Confidence, ExprInstance, Expression,
    };

    /// Build an AvailableArgs with the given cse_only flag.
    fn make_args(cse_only: bool) -> AvailableArgs {
        AvailableArgs {
            file: PathBuf::from("test.py"),
            function: "example".to_string(),
            lang: None,
            check: None,
            at_line: None,
            killed_by: None,
            cse_only,
        }
    }

    /// Build an AvailableExprsInfo with one CSE opportunity and one block
    /// of available expressions.
    fn make_result_with_cse() -> AvailableExprsInfo {
        let expr_a = Expression::new("a + b", vec!["a", "b"], 2);
        let expr_b = Expression::new("c * d", vec!["c", "d"], 3);
        // Duplicate of expr_a at a later line -- triggers CSE
        let expr_a_dup = Expression::new("a + b", vec!["a", "b"], 4);

        let mut avail_in: HashMap<usize, HashSet<Expression>> = HashMap::new();
        let mut set = HashSet::new();
        set.insert(expr_a.clone());
        set.insert(expr_b.clone());
        avail_in.insert(0, set);

        let avail_out: HashMap<usize, HashSet<Expression>> = HashMap::new();

        let mut all_exprs = HashSet::new();
        all_exprs.insert(expr_a.clone());
        all_exprs.insert(expr_b.clone());

        // ExprInstance list that will trigger redundant_computations()
        let instances_with_blocks = vec![
            ExprInstance::new(expr_a.clone(), 0),
            ExprInstance::new(expr_b.clone(), 0),
            ExprInstance::new(expr_a_dup.clone(), 0),
        ];

        AvailableExprsInfo {
            avail_in,
            avail_out,
            all_exprs,
            entry_block: 0,
            expr_instances: vec![expr_a.clone(), expr_b.clone(), expr_a_dup.clone()],
            expr_instances_with_blocks: instances_with_blocks,
            defs_per_line: HashMap::new(),
            line_to_block: HashMap::new(),
            uncertain_exprs: Vec::new(),
            confidence: Confidence::High,
        }
    }

    #[test]
    fn test_cse_only_flag_hides_blocks() {
        let args = make_args(true);
        let result = make_result_with_cse();
        let output = args.format_text_output(&result);

        // CSE section should always be present
        assert!(
            output.contains("CSE Opportunities"),
            "CSE Opportunities section must be present with --cse-only. Got:\n{}",
            output,
        );

        // Per-block section should be hidden
        assert!(
            !output.contains("Available expressions by block:"),
            "Per-block section must be hidden with --cse-only. Got:\n{}",
            output,
        );
    }

    #[test]
    fn test_default_shows_blocks() {
        let args = make_args(false);
        let result = make_result_with_cse();
        let output = args.format_text_output(&result);

        // CSE section should be present
        assert!(
            output.contains("CSE Opportunities"),
            "CSE Opportunities section must be present by default. Got:\n{}",
            output,
        );

        // Per-block section should also be present
        assert!(
            output.contains("Available expressions by block:"),
            "Per-block section must be present by default. Got:\n{}",
            output,
        );
    }
}