-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.rs
More file actions
269 lines (241 loc) · 9.56 KB
/
Copy pathparser.rs
File metadata and controls
269 lines (241 loc) · 9.56 KB
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//
// Source parser for affinescriptiser — Scans source files (Rust, C, Zig) to identify call sites
// where tracked resources are allocated or deallocated. Produces a list of ResourceSite records
// that the affine_gen module uses to generate AffineScript type wrappers.
use crate::manifest::{Manifest, ResourceEntry, SourceLanguage};
/// A detected resource usage site in the source code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResourceSite {
/// Name of the resource type (matches ResourceEntry.name from the manifest).
pub resource_name: String,
/// The function call detected at this site (allocator or deallocator name).
pub function: String,
/// Whether this site is an allocation or a deallocation.
pub kind: SiteKind,
/// Source file path where this call was found.
pub file: String,
/// Line number (1-based) within the source file.
pub line: usize,
/// The full line of source code at this location, trimmed.
pub context: String,
}
/// Whether a resource site is an allocation (creation) or deallocation (destruction).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SiteKind {
/// The resource is being created/allocated at this site.
Allocation,
/// The resource is being destroyed/deallocated at this site.
Deallocation,
}
impl std::fmt::Display for SiteKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SiteKind::Allocation => write!(f, "alloc"),
SiteKind::Deallocation => write!(f, "dealloc"),
}
}
}
/// Parse all source files declared in the manifest, scanning for allocation and deallocation
/// call sites matching the declared resources. Returns a list of all detected sites across
/// all source files.
///
/// This performs a line-by-line text scan (not a full AST parse) looking for function call
/// patterns. For each declared resource, we search for its allocator and deallocator function
/// names followed by an opening parenthesis. This heuristic works well for common C, Rust,
/// and Zig calling conventions.
pub fn parse_sources(manifest: &Manifest) -> Vec<ResourceSite> {
let mut sites = Vec::new();
for source in &manifest.sources {
let lang = SourceLanguage::from_str(&source.language);
if lang.is_none() {
// Unknown language — skip (validation should have caught this).
continue;
}
// Read the source file contents; if unavailable, skip gracefully.
let content = match std::fs::read_to_string(&source.path) {
Ok(c) => c,
Err(_) => continue,
};
for (line_idx, line) in content.lines().enumerate() {
let line_number = line_idx + 1;
let trimmed = line.trim();
// Skip comments (basic heuristic per language).
if is_comment(trimmed, lang.expect("TODO: handle error")) {
continue;
}
// Check each declared resource's allocator and deallocator.
for resource in &manifest.resources {
if contains_call(trimmed, &resource.allocator) {
sites.push(ResourceSite {
resource_name: resource.name.clone(),
function: resource.allocator.clone(),
kind: SiteKind::Allocation,
file: source.path.clone(),
line: line_number,
context: trimmed.to_string(),
});
}
if contains_call(trimmed, &resource.deallocator) {
sites.push(ResourceSite {
resource_name: resource.name.clone(),
function: resource.deallocator.clone(),
kind: SiteKind::Deallocation,
file: source.path.clone(),
line: line_number,
context: trimmed.to_string(),
});
}
}
}
}
sites
}
/// Parse resource sites from a string of source code directly (useful for testing without
/// needing to create files on disk). Scans the given content against the provided resource
/// entries using the same heuristic as parse_sources.
pub fn parse_source_string(
content: &str,
file_name: &str,
resources: &[ResourceEntry],
) -> Vec<ResourceSite> {
let mut sites = Vec::new();
for (line_idx, line) in content.lines().enumerate() {
let line_number = line_idx + 1;
let trimmed = line.trim();
for resource in resources {
if contains_call(trimmed, &resource.allocator) {
sites.push(ResourceSite {
resource_name: resource.name.clone(),
function: resource.allocator.clone(),
kind: SiteKind::Allocation,
file: file_name.to_string(),
line: line_number,
context: trimmed.to_string(),
});
}
if contains_call(trimmed, &resource.deallocator) {
sites.push(ResourceSite {
resource_name: resource.name.clone(),
function: resource.deallocator.clone(),
kind: SiteKind::Deallocation,
file: file_name.to_string(),
line: line_number,
context: trimmed.to_string(),
});
}
}
}
sites
}
/// Check if a source line contains a function call matching the given function name.
/// Looks for the pattern `function_name(` which covers standard call syntax in
/// Rust, C, and Zig. Also matches method-call syntax like `.function_name(` and
/// namespace-qualified calls like `module::function_name(`.
fn contains_call(line: &str, function_name: &str) -> bool {
// Find all occurrences of the function name in the line.
let mut search_from = 0;
while let Some(pos) = line[search_from..].find(function_name) {
let abs_pos = search_from + pos;
let after = abs_pos + function_name.len();
// The character immediately after the function name should be '(' or whitespace
// followed by '(' to count as a call.
if after < line.len() {
let rest = &line[after..];
let rest_trimmed = rest.trim_start();
if rest_trimmed.starts_with('(') {
// Verify the character before the function name is not alphanumeric
// (to avoid matching substrings like "xcreate_buffer").
if abs_pos == 0 {
return true;
}
let before = line.as_bytes()[abs_pos - 1];
if !before.is_ascii_alphanumeric() && before != b'_' {
return true;
}
}
}
search_from = abs_pos + 1;
}
false
}
/// Basic comment detection heuristic per language. Returns true if the line is a
/// single-line comment.
fn is_comment(line: &str, lang: SourceLanguage) -> bool {
match lang {
SourceLanguage::Rust => line.starts_with("//"),
SourceLanguage::C => line.starts_with("//") || line.starts_with("/*"),
SourceLanguage::Zig => line.starts_with("//"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::abi::AffinityKind;
fn gpu_resource() -> ResourceEntry {
ResourceEntry {
name: "GpuBuffer".into(),
allocator: "create_buffer".into(),
deallocator: "release_buffer".into(),
affinity: AffinityKind::Affine,
}
}
#[test]
fn test_contains_call_basic() {
assert!(contains_call(
"let buf = create_buffer(size);",
"create_buffer"
));
assert!(contains_call(" release_buffer(buf);", "release_buffer"));
}
#[test]
fn test_contains_call_method_syntax() {
assert!(contains_call("gpu.create_buffer(1024)", "create_buffer"));
}
#[test]
fn test_contains_call_rejects_substring() {
// "xcreate_buffer" should NOT match "create_buffer".
assert!(!contains_call("xcreate_buffer(x)", "create_buffer"));
}
#[test]
fn test_contains_call_namespace_qualified() {
assert!(contains_call("gpu::create_buffer(size)", "create_buffer"));
}
#[test]
fn test_parse_source_string_finds_sites() {
let code = r#"
fn main() {
let buf = create_buffer(1024);
process(buf);
release_buffer(buf);
}
"#;
let sites = parse_source_string(code, "test.rs", &[gpu_resource()]);
assert_eq!(sites.len(), 2);
assert_eq!(sites[0].kind, SiteKind::Allocation);
assert_eq!(sites[0].line, 3);
assert_eq!(sites[1].kind, SiteKind::Deallocation);
assert_eq!(sites[1].line, 5);
}
#[test]
fn test_parse_source_string_double_free() {
let code = r#"
let buf = create_buffer(64);
release_buffer(buf);
release_buffer(buf);
"#;
let sites = parse_source_string(code, "test.rs", &[gpu_resource()]);
let deallocs: Vec<_> = sites
.iter()
.filter(|s| s.kind == SiteKind::Deallocation)
.collect();
assert_eq!(deallocs.len(), 2, "Should detect both deallocation sites");
}
#[test]
fn test_is_comment() {
assert!(is_comment("// this is a comment", SourceLanguage::Rust));
assert!(!is_comment("let x = 1;", SourceLanguage::Rust));
assert!(is_comment("/* block comment */", SourceLanguage::C));
}
}