cargo-fa 0.11.1

Static analysis tool for framealloc - detect memory intent violations before runtime
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
//! Output formatting for cargo-fa diagnostics.

use crate::cli::{OutputFormat, Severity};
use crate::diagnostics::{Applicability, Diagnostic};
use colored::*;
use std::io::Write;

/// Format a header line
pub fn header(text: &str) -> String {
    format!("{} {}", "cargo-fa".cyan().bold(), text)
}

/// Print a diagnostic to the terminal
pub fn print_diagnostic(diag: &Diagnostic, format: &OutputFormat) {
    match format {
        OutputFormat::Terminal => print_terminal(diag),
        OutputFormat::Compact => print_compact(diag),
        OutputFormat::Json => print_json(diag),
        OutputFormat::Sarif => {}      // Handled at report level
        OutputFormat::Junit => {}      // Handled at report level  
        OutputFormat::Checkstyle => {} // Handled at report level
    }
}

fn print_terminal(diag: &Diagnostic) {
    let severity_str = match diag.severity {
        Severity::Error => "error".red().bold(),
        Severity::Warning => "warning".yellow().bold(),
        Severity::Hint => "hint".cyan().bold(),
    };
    
    // Main diagnostic line
    println!(
        "{}[{}]: {}",
        severity_str,
        diag.code.code.bold(),
        diag.message.bold()
    );
    
    // Location
    println!(
        "  {} {}:{}:{}",
        "-->".blue().bold(),
        diag.location.file.display(),
        diag.location.line,
        diag.location.column
    );
    
    // Show source context if possible
    if let Ok(source) = std::fs::read_to_string(&diag.location.file) {
        let lines: Vec<&str> = source.lines().collect();
        if diag.location.line > 0 && diag.location.line <= lines.len() {
            let line_num = diag.location.line;
            let line_str = format!("{}", line_num);
            let padding = " ".repeat(line_str.len());
            
            println!("   {} {}", padding, "|".blue().bold());
            println!(
                "   {} {} {}",
                line_num.to_string().blue().bold(),
                "|".blue().bold(),
                lines[line_num - 1]
            );
            
            // Underline the relevant part
            if diag.location.column > 0 {
                let underline_start = diag.location.column - 1;
                let underline_len = diag.location.end_column
                    .map(|e| e.saturating_sub(diag.location.column).max(1))
                    .unwrap_or(1);
                
                let underline = format!(
                    "{}{}",
                    " ".repeat(underline_start),
                    "^".repeat(underline_len)
                );
                
                let colored_underline = match diag.severity {
                    Severity::Error => underline.red().bold(),
                    Severity::Warning => underline.yellow().bold(),
                    Severity::Hint => underline.cyan().bold(),
                };
                
                println!(
                    "   {} {} {}",
                    padding,
                    "|".blue().bold(),
                    colored_underline
                );
            }
            
            println!("   {} {}", padding, "|".blue().bold());
        }
    }
    
    // Notes
    for note in &diag.notes {
        println!("   {} {}: {}", "=".blue().bold(), "note".bold(), note);
    }
    
    // Suggestion
    if let Some(ref suggestion) = diag.suggestion {
        let help_prefix = match suggestion.applicability {
            Applicability::MachineApplicable => "fix",
            _ => "help",
        };
        
        println!(
            "   {} {}: {}",
            "=".blue().bold(),
            help_prefix.green().bold(),
            suggestion.message
        );
        
        if let Some(ref replacement) = suggestion.replacement {
            println!("   {}  {}", " ".repeat(help_prefix.len()), replacement.green());
        }
    }
    
    // Related locations
    for related in &diag.related {
        println!(
            "   {} {}: {}",
            "=".blue().bold(),
            "related".bold(),
            related.message
        );
        println!(
            "      {} {}:{}:{}",
            "-->".blue(),
            related.location.file.display(),
            related.location.line,
            related.location.column
        );
    }
    
    // Documentation link
    println!(
        "   {} see: {}",
        "=".blue().bold(),
        format!("https://docs.rs/framealloc/diagnostics#{}", diag.code.code).dimmed()
    );
    
    println!();
}

fn print_compact(diag: &Diagnostic) {
    let severity = match diag.severity {
        Severity::Error => "error",
        Severity::Warning => "warning",
        Severity::Hint => "hint",
    };
    
    println!(
        "{}:{}:{}: {} [{}]: {}",
        diag.location.file.display(),
        diag.location.line,
        diag.location.column,
        severity,
        diag.code.code,
        diag.message
    );
}

fn print_json(diag: &Diagnostic) {
    if let Ok(json) = serde_json::to_string(diag) {
        println!("{}", json);
    }
}

/// Generate SARIF output for all diagnostics
pub fn generate_sarif(diagnostics: &[Diagnostic]) -> String {
    let results: Vec<serde_json::Value> = diagnostics
        .iter()
        .map(|d| {
            serde_json::json!({
                "ruleId": d.code.code,
                "level": match d.severity {
                    Severity::Error => "error",
                    Severity::Warning => "warning",
                    Severity::Hint => "note",
                },
                "message": {
                    "text": d.message
                },
                "locations": [{
                    "physicalLocation": {
                        "artifactLocation": {
                            "uri": d.location.file.to_string_lossy()
                        },
                        "region": {
                            "startLine": d.location.line,
                            "startColumn": d.location.column,
                            "endLine": d.location.end_line.unwrap_or(d.location.line),
                            "endColumn": d.location.end_column.unwrap_or(d.location.column + 1)
                        }
                    }
                }]
            })
        })
        .collect();
    
    let sarif = serde_json::json!({
        "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
        "version": "2.1.0",
        "runs": [{
            "tool": {
                "driver": {
                    "name": "cargo-fa",
                    "version": env!("CARGO_PKG_VERSION"),
                    "informationUri": "https://docs.rs/framealloc",
                    "rules": generate_rules()
                }
            },
            "results": results
        }]
    });
    
    serde_json::to_string_pretty(&sarif).unwrap_or_default()
}

fn generate_rules() -> Vec<serde_json::Value> {
    vec![
        rule("FA601", "frame-escape", "Frame allocation may escape frame scope"),
        rule("FA602", "loop-allocation", "Allocation in hot loop"),
        rule("FA603", "missing-frame-boundary", "Missing frame lifecycle calls"),
        rule("FA604", "retention-mismatch", "Retention policy mismatch"),
        rule("FA605", "discard-escape", "Discard policy but stored beyond frame"),
        rule("FA701", "async-frame", "Frame allocation in async function"),
        rule("FA702", "await-crossing", "Frame allocation crosses await point"),
        rule("FA703", "closure-capture", "FrameBox captured by closure/task"),
        rule("FA801", "tag-mismatch", "Allocation tag mismatch"),
        rule("FA802", "unknown-tag", "Unknown allocation tag"),
        rule("FA803", "cross-module", "Cross-module allocation"),
    ]
}

fn rule(id: &str, name: &str, description: &str) -> serde_json::Value {
    serde_json::json!({
        "id": id,
        "name": name,
        "shortDescription": { "text": description },
        "helpUri": format!("https://docs.rs/framealloc/diagnostics#{}", id)
    })
}

/// Print a summary line
pub fn print_summary(errors: usize, warnings: usize, hints: usize) {
    if errors == 0 && warnings == 0 && hints == 0 {
        println!("{}", "No issues found ✓".green().bold());
        return;
    }
    
    let mut parts = Vec::new();
    
    if errors > 0 {
        parts.push(format!("{} error{}", errors, if errors == 1 { "" } else { "s" }).red().bold().to_string());
    }
    if warnings > 0 {
        parts.push(format!("{} warning{}", warnings, if warnings == 1 { "" } else { "s" }).yellow().bold().to_string());
    }
    if hints > 0 {
        parts.push(format!("{} hint{}", hints, if hints == 1 { "" } else { "s" }).cyan().to_string());
    }
    
    println!("{}: {}", "Summary".bold(), parts.join(", "));
}

/// Generate JUnit XML output for test reporting systems
pub fn generate_junit(diagnostics: &[Diagnostic]) -> String {
    let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="cargo-fa" tests=""#);
    xml.push_str(&diagnostics.len().to_string());
    xml.push_str(r#"" failures=""#);
    xml.push_str(&diagnostics.iter().filter(|d| d.severity == Severity::Error).count().to_string());
    xml.push_str(r#"">
  <testsuite name="framealloc-analysis">
"#);
    
    for diag in diagnostics {
        xml.push_str(&format!(
            r#"    <testcase name="{}" classname="{}">
"#,
            escape_xml(&diag.code.code),
            escape_xml(&diag.location.file.to_string_lossy())
        ));
        
        match diag.severity {
            Severity::Error => {
                xml.push_str(&format!(
                    r#"      <failure message="{}" type="error">{}</failure>
"#,
                    escape_xml(&diag.message),
                    escape_xml(&format!("{}:{}:{}", 
                        diag.location.file.display(),
                        diag.location.line,
                        diag.location.column
                    ))
                ));
            }
            Severity::Warning => {
                xml.push_str(&format!(
                    r#"      <failure message="{}" type="warning">{}</failure>
"#,
                    escape_xml(&diag.message),
                    escape_xml(&format!("{}:{}:{}", 
                        diag.location.file.display(),
                        diag.location.line,
                        diag.location.column
                    ))
                ));
            }
            Severity::Hint => {
                xml.push_str(&format!(
                    r#"      <system-out>{}: {}</system-out>
"#,
                    escape_xml(&diag.code.code),
                    escape_xml(&diag.message)
                ));
            }
        }
        
        xml.push_str("    </testcase>\n");
    }
    
    xml.push_str("  </testsuite>\n</testsuites>");
    xml
}

/// Generate Checkstyle XML output for Jenkins and legacy CI
pub fn generate_checkstyle(diagnostics: &[Diagnostic]) -> String {
    let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
"#);
    
    // Group by file
    let mut by_file: std::collections::HashMap<String, Vec<&Diagnostic>> = std::collections::HashMap::new();
    for diag in diagnostics {
        let file = diag.location.file.to_string_lossy().to_string();
        by_file.entry(file).or_default().push(diag);
    }
    
    for (file, diags) in by_file {
        xml.push_str(&format!(r#"  <file name="{}">
"#, escape_xml(&file)));
        
        for diag in diags {
            let severity = match diag.severity {
                Severity::Error => "error",
                Severity::Warning => "warning",
                Severity::Hint => "info",
            };
            
            xml.push_str(&format!(
                r#"    <error line="{}" column="{}" severity="{}" message="{}" source="cargo-fa.{}"/>
"#,
                diag.location.line,
                diag.location.column,
                severity,
                escape_xml(&diag.message),
                diag.code.code
            ));
        }
        
        xml.push_str("  </file>\n");
    }
    
    xml.push_str("</checkstyle>");
    xml
}

fn escape_xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

/// Generate full JSON output for fa-insight IDE integration (v0.7.0).
/// 
/// This produces the structured JSON format that fa-insight expects,
/// including diagnostics array and summary statistics.
pub fn generate_json_report(diagnostics: &[Diagnostic], files_analyzed: usize, duration_ms: u64) -> String {
    let errors = diagnostics.iter().filter(|d| d.severity == Severity::Error).count();
    let warnings = diagnostics.iter().filter(|d| d.severity == Severity::Warning).count();
    let hints = diagnostics.iter().filter(|d| d.severity == Severity::Hint).count();
    
    let diag_json: Vec<serde_json::Value> = diagnostics
        .iter()
        .map(|d| {
            let mut obj = serde_json::json!({
                "code": {
                    "code": d.code.code,
                    "category": format!("{:?}", d.code.category)
                },
                "severity": match d.severity {
                    Severity::Error => "error",
                    Severity::Warning => "warning",
                    Severity::Hint => "hint",
                },
                "message": d.message,
                "location": {
                    "file": d.location.file.to_string_lossy(),
                    "line": d.location.line,
                    "column": d.location.column,
                    "end_line": d.location.end_line,
                    "end_column": d.location.end_column
                },
                "notes": d.notes,
                "related": d.related.iter().map(|r| {
                    serde_json::json!({
                        "location": {
                            "file": r.location.file.to_string_lossy(),
                            "line": r.location.line,
                            "column": r.location.column
                        },
                        "message": r.message
                    })
                }).collect::<Vec<_>>()
            });
            
            if let Some(ref suggestion) = d.suggestion {
                obj["suggestion"] = serde_json::json!({
                    "message": suggestion.message,
                    "replacement": suggestion.replacement,
                    "applicability": format!("{:?}", suggestion.applicability)
                });
            }
            
            obj
        })
        .collect();
    
    let report = serde_json::json!({
        "diagnostics": diag_json,
        "summary": {
            "errors": errors,
            "warnings": warnings,
            "hints": hints
        },
        "files_analyzed": files_analyzed,
        "duration_ms": duration_ms
    });
    
    serde_json::to_string_pretty(&report).unwrap_or_else(|_| "{}".to_string())
}