|
| 1 | +use super::{SafetyClassification, UnsafeReason}; |
| 2 | + |
| 3 | +/// Represents an unsafe block within Rust code. |
| 4 | +/// |
| 5 | +/// It includes the start and end line numbers, reasons for using unsafe, |
| 6 | +/// an optional explanation, and the name of the containing function (if any). |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +pub struct UnsafeBlock { |
| 9 | + /// The starting line number of the unsafe block. |
| 10 | + pub start_line: usize, |
| 11 | + /// The ending line number of the unsafe block. |
| 12 | + pub end_line: usize, |
| 13 | + /// The list of reasons why this block is marked as unsafe. |
| 14 | + pub reasons: Vec<UnsafeReason>, |
| 15 | + /// Optional documentation explaining why unsafe is needed. |
| 16 | + pub explanation: Option<String>, |
| 17 | + /// Optional name of the function that contains this unsafe block. |
| 18 | + pub containing_function: Option<String>, |
| 19 | +} |
| 20 | + |
| 21 | +impl UnsafeBlock { |
| 22 | + /// Creates a new `UnsafeBlock` with the specified start and end line numbers. |
| 23 | + /// |
| 24 | + /// # Arguments |
| 25 | + /// |
| 26 | + /// * `start_line` - The starting line number. |
| 27 | + /// * `end_line` - The ending line number. |
| 28 | + pub fn new(start_line: usize, end_line: usize) -> Self { |
| 29 | + Self { |
| 30 | + start_line, |
| 31 | + end_line, |
| 32 | + reasons: Vec::new(), |
| 33 | + explanation: None, |
| 34 | + containing_function: None, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Analyzes and tracks safety-related information in Rust code. |
| 40 | +/// |
| 41 | +/// This structure aggregates various pieces of data related to unsafe operations, |
| 42 | +/// such as unsafe blocks, function calls, raw pointer usages, FFI interactions, and more. |
| 43 | +#[derive(Debug, Clone)] |
| 44 | +pub struct SafetyAnalysis { |
| 45 | + /// The overall safety classification. |
| 46 | + pub classification: SafetyClassification, |
| 47 | + /// A list of unsafe blocks encountered in the code. |
| 48 | + pub unsafe_blocks: Vec<UnsafeBlock>, |
| 49 | + /// A list of unsafe function calls detected. |
| 50 | + pub unsafe_fn_calls: Vec<String>, |
| 51 | + /// Indicates if raw pointer usage was found. |
| 52 | + pub raw_pointer_usage: bool, |
| 53 | + /// Indicates if FFI interactions were found. |
| 54 | + pub ffi_interactions: bool, |
| 55 | + /// A list of unsafe traits used. |
| 56 | + pub unsafe_traits_used: Vec<String>, |
| 57 | + /// A list of mutable static variables. |
| 58 | + pub mutable_statics: Vec<String>, |
| 59 | + /// Optional safety comments. |
| 60 | + pub safety_comments: Option<String>, |
| 61 | +} |
| 62 | + |
| 63 | +impl SafetyAnalysis { |
| 64 | + /// Creates a new `SafetyAnalysis` with the given classification. |
| 65 | + /// |
| 66 | + /// All other fields are initialized to their default values. |
| 67 | + /// |
| 68 | + /// # Arguments |
| 69 | + /// |
| 70 | + /// * `classification` - The overall safety classification. |
| 71 | + pub fn new(classification: SafetyClassification) -> Self { |
| 72 | + Self { |
| 73 | + classification, |
| 74 | + unsafe_blocks: Vec::new(), |
| 75 | + unsafe_fn_calls: Vec::new(), |
| 76 | + raw_pointer_usage: false, |
| 77 | + ffi_interactions: false, |
| 78 | + unsafe_traits_used: Vec::new(), |
| 79 | + mutable_statics: Vec::new(), |
| 80 | + safety_comments: None, |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments