tldr-core 0.1.4

Core analysis engine 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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Alias Analysis Output Formatting
//!
//! This module provides output formatters for alias analysis results.
//! Supports JSON, human-readable text, and DOT graph formats.
//!
//! # Formats
//!
//! - **JSON**: Machine-readable, suitable for tooling integration
//! - **Text**: Human-readable, for terminal output and debugging
//! - **DOT**: Graphviz format for visualizing alias relationships
//!
//! # Example
//!
//! ```rust,ignore
//! use tldr_core::alias::{AliasInfo, AliasOutputFormat};
//!
//! let info = compute_alias_from_ssa(&ssa)?;
//!
//! // JSON output
//! println!("{}", info.to_json());
//!
//! // Human-readable text
//! println!("{}", info.to_text());
//!
//! // Graphviz DOT
//! println!("{}", info.to_dot());
//!
//! // Generic format dispatch
//! println!("{}", info.format(AliasOutputFormat::Text));
//! ```

use std::collections::{BTreeMap, BTreeSet};

use super::AliasInfo;

// =============================================================================
// Output Format Enum
// =============================================================================

/// Output format for alias analysis results.
///
/// Determines how the analysis results are serialized for display or storage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AliasOutputFormat {
    /// JSON format (default) - machine-readable
    #[default]
    Json,
    /// Human-readable text format
    Text,
    /// Graphviz DOT format for visualization
    Dot,
}

impl std::str::FromStr for AliasOutputFormat {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "json" => Ok(AliasOutputFormat::Json),
            "text" | "txt" => Ok(AliasOutputFormat::Text),
            "dot" | "graphviz" => Ok(AliasOutputFormat::Dot),
            _ => Err(format!(
                "Unknown format '{}'. Supported: json, text, dot",
                s
            )),
        }
    }
}

impl std::fmt::Display for AliasOutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AliasOutputFormat::Json => write!(f, "json"),
            AliasOutputFormat::Text => write!(f, "text"),
            AliasOutputFormat::Dot => write!(f, "dot"),
        }
    }
}

// =============================================================================
// AliasInfo Formatting Methods
// =============================================================================

impl AliasInfo {
    /// Format alias info as JSON string.
    ///
    /// Output format:
    /// ```json
    /// {
    ///   "function": "foo",
    ///   "may_alias": [["x", "y"], ["a", "b"]],
    ///   "must_alias": [["x", "y"]],
    ///   "points_to": {
    ///     "x": ["alloc_5", "param_p"],
    ///     "y": ["alloc_5"]
    ///   },
    ///   "allocation_sites": ["alloc_5", "param_p", "unknown_3"]
    /// }
    /// ```
    pub fn to_json(&self) -> String {
        // Convert may_alias to list of pairs (sorted, deduplicated)
        let may_alias_pairs = self.collect_alias_pairs(&self.may_alias);

        // Convert must_alias to list of pairs (sorted, deduplicated)
        let must_alias_pairs = self.collect_alias_pairs(&self.must_alias);

        // Sort points_to for deterministic output
        let sorted_points_to: BTreeMap<_, Vec<_>> = self
            .points_to
            .iter()
            .map(|(k, v)| {
                let mut sorted: Vec<_> = v.iter().cloned().collect();
                sorted.sort();
                (k.clone(), sorted)
            })
            .collect();

        // Collect all allocation sites as sorted list
        let allocation_sites: BTreeSet<_> = self.allocation_sites.values().cloned().collect();

        // Uncertain aliases
        let uncertain: Vec<_> = self
            .uncertain
            .iter()
            .map(|ua| {
                serde_json::json!({
                    "vars": ua.vars,
                    "line": ua.line,
                    "reason": ua.reason,
                })
            })
            .collect();

        let confidence_str = match self.confidence {
            super::types::Confidence::Low => "low",
            super::types::Confidence::Medium => "medium",
            super::types::Confidence::High => "high",
        };

        let mut json = serde_json::json!({
            "function": self.function_name,
            "may_alias": may_alias_pairs,
            "must_alias": must_alias_pairs,
            "points_to": sorted_points_to,
            "allocation_sites": allocation_sites,
            "uncertain": uncertain,
            "confidence": confidence_str,
        });

        // Add language_notes only if non-empty
        if !self.language_notes.is_empty() {
            json.as_object_mut().unwrap().insert(
                "language_notes".to_string(),
                serde_json::Value::String(self.language_notes.clone()),
            );
        }

        serde_json::to_string_pretty(&json).unwrap_or_else(|_| "{}".to_string())
    }

    /// Format alias info as human-readable text.
    ///
    /// Output format:
    /// ```text
    /// Alias Analysis: foo
    /// ====================
    /// May-Alias:
    ///   x <-> y (shared: alloc_5)
    ///   a <-> b (shared: param_p)
    ///
    /// Must-Alias:
    ///   x <-> y
    ///
    /// Points-To:
    ///   x -> {alloc_5, param_p}
    ///   y -> {alloc_5}
    /// ```
    pub fn to_text(&self) -> String {
        let mut output = String::new();

        // Header
        let header = format!("Alias Analysis: {}", self.function_name);
        output.push_str(&header);
        output.push('\n');
        output.push_str(&"=".repeat(header.len()));
        output.push('\n');

        // May-Alias section
        output.push_str("May-Alias:\n");
        let may_pairs = self.collect_alias_pairs(&self.may_alias);
        if may_pairs.is_empty() {
            output.push_str("  (none)\n");
        } else {
            for pair in &may_pairs {
                if pair.len() == 2 {
                    let shared = self.find_shared_locations(&pair[0], &pair[1]);
                    if shared.is_empty() {
                        output.push_str(&format!("  {} <-> {}\n", pair[0], pair[1]));
                    } else {
                        output.push_str(&format!(
                            "  {} <-> {} (shared: {})\n",
                            pair[0],
                            pair[1],
                            shared.join(", ")
                        ));
                    }
                }
            }
        }
        output.push('\n');

        // Must-Alias section
        output.push_str("Must-Alias:\n");
        let must_pairs = self.collect_alias_pairs(&self.must_alias);
        if must_pairs.is_empty() {
            output.push_str("  (none)\n");
        } else {
            for pair in &must_pairs {
                if pair.len() == 2 {
                    output.push_str(&format!("  {} <-> {}\n", pair[0], pair[1]));
                }
            }
        }
        output.push('\n');

        // Points-To section
        output.push_str("Points-To:\n");
        if self.points_to.is_empty() {
            output.push_str("  (none)\n");
        } else {
            // Sort variables for deterministic output
            let sorted_vars: BTreeMap<_, _> = self.points_to.iter().collect();
            for (var, locations) in sorted_vars {
                let mut sorted_locs: Vec<_> = locations.iter().cloned().collect();
                sorted_locs.sort();
                output.push_str(&format!("  {} -> {{{}}}\n", var, sorted_locs.join(", ")));
            }
        }

        output
    }

    /// Format alias info as Graphviz DOT format.
    ///
    /// Output format:
    /// ```dot
    /// digraph alias {
    ///   rankdir=LR;
    ///   x -> alloc_5;
    ///   x -> param_p;
    ///   y -> alloc_5;
    ///   x -> y [style=dashed, label="may"];
    /// }
    /// ```
    pub fn to_dot(&self) -> String {
        let mut output = String::new();

        output.push_str("digraph alias {\n");
        output.push_str("  rankdir=LR;\n");
        output.push_str("  node [shape=box];\n");
        output.push('\n');

        // Define variable nodes
        let mut all_vars: BTreeSet<_> = self.points_to.keys().cloned().collect();
        for vars in self.may_alias.keys() {
            all_vars.insert(vars.clone());
        }
        for vars in self.must_alias.keys() {
            all_vars.insert(vars.clone());
        }

        // Variable nodes (boxes)
        output.push_str("  // Variables\n");
        for var in &all_vars {
            output.push_str(&format!(
                "  \"{}\" [shape=box, style=filled, fillcolor=lightblue];\n",
                escape_dot_label(var)
            ));
        }
        output.push('\n');

        // Location nodes (ellipses)
        output.push_str("  // Abstract Locations\n");
        let mut all_locations: BTreeSet<String> = BTreeSet::new();
        for locs in self.points_to.values() {
            all_locations.extend(locs.iter().cloned());
        }
        for loc in &all_locations {
            output.push_str(&format!(
                "  \"{}\" [shape=ellipse, style=filled, fillcolor=lightyellow];\n",
                escape_dot_label(loc)
            ));
        }
        output.push('\n');

        // Points-to edges (solid arrows)
        output.push_str("  // Points-To Edges\n");
        let sorted_pts: BTreeMap<_, _> = self.points_to.iter().collect();
        for (var, locations) in sorted_pts {
            let mut sorted_locs: Vec<_> = locations.iter().collect();
            sorted_locs.sort();
            for loc in sorted_locs {
                output.push_str(&format!(
                    "  \"{}\" -> \"{}\";\n",
                    escape_dot_label(var),
                    escape_dot_label(loc)
                ));
            }
        }
        output.push('\n');

        // May-alias edges (dashed, bidirectional)
        output.push_str("  // May-Alias Edges\n");
        let may_pairs = self.collect_alias_pairs(&self.may_alias);
        for pair in &may_pairs {
            if pair.len() == 2 {
                output.push_str(&format!(
                    "  \"{}\" -> \"{}\" [style=dashed, label=\"may\", dir=both, color=orange];\n",
                    escape_dot_label(&pair[0]),
                    escape_dot_label(&pair[1])
                ));
            }
        }
        output.push('\n');

        // Must-alias edges (bold, bidirectional)
        output.push_str("  // Must-Alias Edges\n");
        let must_pairs = self.collect_alias_pairs(&self.must_alias);
        for pair in &must_pairs {
            if pair.len() == 2 {
                output.push_str(&format!(
                    "  \"{}\" -> \"{}\" [style=bold, label=\"must\", dir=both, color=green];\n",
                    escape_dot_label(&pair[0]),
                    escape_dot_label(&pair[1])
                ));
            }
        }

        output.push_str("}\n");
        output
    }

    /// Format using the specified output format.
    ///
    /// Dispatches to the appropriate formatter based on the format enum.
    pub fn format(&self, format: AliasOutputFormat) -> String {
        match format {
            AliasOutputFormat::Json => self.to_json(),
            AliasOutputFormat::Text => self.to_text(),
            AliasOutputFormat::Dot => self.to_dot(),
        }
    }

    // =========================================================================
    // Helper Methods
    // =========================================================================

    /// Collect alias relationships as deduplicated, sorted pairs.
    ///
    /// Converts the symmetric map representation to a list of [a, b] pairs
    /// where a < b (lexicographically) to avoid duplicates like [a, b] and [b, a].
    fn collect_alias_pairs(
        &self,
        alias_map: &std::collections::HashMap<String, std::collections::HashSet<String>>,
    ) -> Vec<Vec<String>> {
        let mut pairs: BTreeSet<(String, String)> = BTreeSet::new();

        for (var, aliases) in alias_map {
            for alias in aliases {
                // Normalize pair ordering to avoid duplicates
                let pair = if var < alias {
                    (var.clone(), alias.clone())
                } else {
                    (alias.clone(), var.clone())
                };
                pairs.insert(pair);
            }
        }

        pairs.into_iter().map(|(a, b)| vec![a, b]).collect()
    }

    /// Find shared locations between two variables (for text output annotation).
    fn find_shared_locations(&self, a: &str, b: &str) -> Vec<String> {
        let pts_a = self.points_to.get(a);
        let pts_b = self.points_to.get(b);

        match (pts_a, pts_b) {
            (Some(set_a), Some(set_b)) => {
                let mut shared: Vec<_> = set_a.intersection(set_b).cloned().collect();
                shared.sort();
                shared
            }
            _ => Vec::new(),
        }
    }
}

/// Escape special characters in DOT labels.
fn escape_dot_label(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

// =============================================================================
// Tests
// =============================================================================

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

    fn create_test_alias_info() -> AliasInfo {
        let mut info = AliasInfo::new("foo");

        // Add may-alias relationships
        info.add_may_alias("x", "y");
        info.add_may_alias("a", "b");

        // Add must-alias relationships
        info.add_must_alias("x", "y");

        // Add points-to relationships
        info.add_points_to("x", "alloc_5");
        info.add_points_to("x", "param_p");
        info.add_points_to("y", "alloc_5");

        // Add allocation sites
        info.add_allocation_site(5, "alloc_5");

        info
    }

    #[test]
    fn test_alias_output_format_parse() {
        assert_eq!(
            "json".parse::<AliasOutputFormat>().unwrap(),
            AliasOutputFormat::Json
        );
        assert_eq!(
            "text".parse::<AliasOutputFormat>().unwrap(),
            AliasOutputFormat::Text
        );
        assert_eq!(
            "txt".parse::<AliasOutputFormat>().unwrap(),
            AliasOutputFormat::Text
        );
        assert_eq!(
            "dot".parse::<AliasOutputFormat>().unwrap(),
            AliasOutputFormat::Dot
        );
        assert_eq!(
            "graphviz".parse::<AliasOutputFormat>().unwrap(),
            AliasOutputFormat::Dot
        );
        assert!("invalid".parse::<AliasOutputFormat>().is_err());
    }

    #[test]
    fn test_alias_output_format_display() {
        assert_eq!(AliasOutputFormat::Json.to_string(), "json");
        assert_eq!(AliasOutputFormat::Text.to_string(), "text");
        assert_eq!(AliasOutputFormat::Dot.to_string(), "dot");
    }

    #[test]
    fn test_to_json_structure() {
        let info = create_test_alias_info();
        let json_str = info.to_json();

        // Parse and verify structure
        let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        assert_eq!(json["function"], "foo");
        assert!(json["may_alias"].is_array());
        assert!(json["must_alias"].is_array());
        assert!(json["points_to"].is_object());
        assert!(json["allocation_sites"].is_array());
    }

    #[test]
    fn test_to_json_may_alias_pairs() {
        let info = create_test_alias_info();
        let json_str = info.to_json();
        let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        let may_alias = json["may_alias"].as_array().unwrap();
        // Should have 2 pairs: [a, b] and [x, y]
        assert_eq!(may_alias.len(), 2);

        // Pairs should be sorted
        let pair0 = may_alias[0].as_array().unwrap();
        assert_eq!(pair0[0], "a");
        assert_eq!(pair0[1], "b");
    }

    #[test]
    fn test_to_json_points_to() {
        let info = create_test_alias_info();
        let json_str = info.to_json();
        let json: serde_json::Value = serde_json::from_str(&json_str).unwrap();

        let points_to = &json["points_to"];
        assert!(points_to["x"].is_array());
        assert!(points_to["y"].is_array());

        let x_pts = points_to["x"].as_array().unwrap();
        assert_eq!(x_pts.len(), 2); // alloc_5, param_p

        let y_pts = points_to["y"].as_array().unwrap();
        assert_eq!(y_pts.len(), 1); // alloc_5
    }

    #[test]
    fn test_to_text_header() {
        let info = create_test_alias_info();
        let text = info.to_text();

        assert!(text.starts_with("Alias Analysis: foo"));
        // Underline length matches header length ("Alias Analysis: foo" = 19 chars)
        assert!(text.contains("=".repeat(19).as_str()));
    }

    #[test]
    fn test_to_text_may_alias() {
        let info = create_test_alias_info();
        let text = info.to_text();

        assert!(text.contains("May-Alias:"));
        assert!(text.contains("x <-> y"));
        assert!(text.contains("a <-> b"));
    }

    #[test]
    fn test_to_text_shared_locations() {
        let info = create_test_alias_info();
        let text = info.to_text();

        // x and y share alloc_5
        assert!(text.contains("(shared: alloc_5)"));
    }

    #[test]
    fn test_to_text_must_alias() {
        let info = create_test_alias_info();
        let text = info.to_text();

        assert!(text.contains("Must-Alias:"));
        assert!(text.contains("x <-> y"));
    }

    #[test]
    fn test_to_text_points_to() {
        let info = create_test_alias_info();
        let text = info.to_text();

        assert!(text.contains("Points-To:"));
        assert!(text.contains("x -> {alloc_5, param_p}"));
        assert!(text.contains("y -> {alloc_5}"));
    }

    #[test]
    fn test_to_text_empty() {
        let info = AliasInfo::new("empty");
        let text = info.to_text();

        assert!(text.contains("(none)"));
    }

    #[test]
    fn test_to_dot_structure() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        assert!(dot.starts_with("digraph alias {"));
        assert!(dot.ends_with("}\n"));
        assert!(dot.contains("rankdir=LR"));
    }

    #[test]
    fn test_to_dot_variable_nodes() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        // Variables should be box-shaped with lightblue fill
        assert!(dot.contains("\"x\" [shape=box"));
        assert!(dot.contains("\"y\" [shape=box"));
        assert!(dot.contains("fillcolor=lightblue"));
    }

    #[test]
    fn test_to_dot_location_nodes() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        // Locations should be ellipse-shaped with lightyellow fill
        assert!(dot.contains("\"alloc_5\" [shape=ellipse"));
        assert!(dot.contains("\"param_p\" [shape=ellipse"));
        assert!(dot.contains("fillcolor=lightyellow"));
    }

    #[test]
    fn test_to_dot_points_to_edges() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        // Points-to edges (solid)
        assert!(dot.contains("\"x\" -> \"alloc_5\""));
        assert!(dot.contains("\"x\" -> \"param_p\""));
        assert!(dot.contains("\"y\" -> \"alloc_5\""));
    }

    #[test]
    fn test_to_dot_may_alias_edges() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        // May-alias edges (dashed, orange)
        assert!(dot.contains("style=dashed"));
        assert!(dot.contains("label=\"may\""));
        assert!(dot.contains("color=orange"));
    }

    #[test]
    fn test_to_dot_must_alias_edges() {
        let info = create_test_alias_info();
        let dot = info.to_dot();

        // Must-alias edges (bold, green)
        assert!(dot.contains("style=bold"));
        assert!(dot.contains("label=\"must\""));
        assert!(dot.contains("color=green"));
    }

    #[test]
    fn test_format_dispatch() {
        let info = create_test_alias_info();

        assert!(info
            .format(AliasOutputFormat::Json)
            .contains("\"function\""));
        assert!(info
            .format(AliasOutputFormat::Text)
            .contains("Alias Analysis:"));
        assert!(info.format(AliasOutputFormat::Dot).contains("digraph"));
    }

    #[test]
    fn test_collect_alias_pairs_deduplication() {
        let mut info = AliasInfo::new("test");
        // Add symmetric relationship (both directions already added by add_may_alias)
        info.add_may_alias("x", "y");

        let pairs = info.collect_alias_pairs(&info.may_alias);
        // Should only have one pair, not two
        assert_eq!(pairs.len(), 1);
        assert_eq!(pairs[0], vec!["x", "y"]);
    }

    #[test]
    fn test_collect_alias_pairs_ordering() {
        let mut info = AliasInfo::new("test");
        info.add_may_alias("z", "a"); // z > a alphabetically

        let pairs = info.collect_alias_pairs(&info.may_alias);
        // Should be normalized to [a, z]
        assert_eq!(pairs[0], vec!["a", "z"]);
    }

    #[test]
    fn test_escape_dot_label() {
        assert_eq!(escape_dot_label("simple"), "simple");
        assert_eq!(escape_dot_label("with\"quote"), "with\\\"quote");
        assert_eq!(escape_dot_label("with\\backslash"), "with\\\\backslash");
        assert_eq!(escape_dot_label("with\nnewline"), "with\\nnewline");
    }

    #[test]
    fn test_find_shared_locations() {
        let info = create_test_alias_info();

        let shared = info.find_shared_locations("x", "y");
        assert_eq!(shared, vec!["alloc_5"]);

        // No shared locations
        let shared_none = info.find_shared_locations("a", "b");
        assert!(shared_none.is_empty());

        // Unknown variable
        let shared_unknown = info.find_shared_locations("x", "unknown");
        assert!(shared_unknown.is_empty());
    }
}