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
/// Analyze Lua files with module export awareness
///
/// Handles Lua module patterns:
/// - `local function name()` — local, dead if uncalled
/// - `function name()` — global, lower confidence (may be called externally)
/// - `function M.name()` / `M.name = function()` — exported if `return M` present
fn analyze_lua_files(files: &[std::path::PathBuf]) -> Result<(Vec<FunctionInfo>, HashSet<String>)> {
let mut defined_functions = Vec::new();
let mut called_functions = HashSet::new();
// Lua keywords and builtins to exclude from call tracking
let lua_keywords: HashSet<&str> = [
"if",
"then",
"else",
"elseif",
"end",
"do",
"while",
"for",
"repeat",
"until",
"function",
"local",
"return",
"break",
"goto",
"in",
"and",
"or",
"not",
"nil",
"true",
"false",
"require",
"print",
"pairs",
"ipairs",
"type",
"error",
"pcall",
"xpcall",
"select",
"rawget",
"rawset",
"rawlen",
"tostring",
"tonumber",
"setmetatable",
"getmetatable",
"table",
"string",
"math",
"coroutine",
"unpack",
"assert",
"next",
"io",
"os",
"debug",
"dofile",
"loadfile",
"loadstring",
]
.iter()
.copied()
.collect();
for file in files {
let content = std::fs::read_to_string(file)
.with_context(|| format!("Failed to read Lua file: {:?}", file))?;
// Skip test files
let file_str = file.display().to_string();
if file_str.contains("/tests/")
|| file_str.contains("/test/")
|| file_str.contains("/spec/")
|| file_str.ends_with("_test.lua")
|| file_str.ends_with("_spec.lua")
{
// Still collect calls from test files (they exercise production code)
collect_lua_calls(&content, &lua_keywords, &mut called_functions);
continue;
}
// Detect module return pattern: last non-empty, non-comment line is `return X`
let returned_module = detect_lua_module_return(&content);
// Extract function definitions
for (line_idx, line) in content.lines().enumerate() {
// Module functions: function M.name(...) or function M:name(...)
if let Some(cap) = LUA_MODULE_FUNC_NAME_REGEX.captures(line) {
let module_name = cap.get(1).map(|m| m.as_str()).unwrap_or("");
let func_name = cap.get(2).map(|m| m.as_str()).unwrap_or("");
// If the module table is returned, this function is exported
let is_exported = returned_module
.as_ref()
.map(|m| m == module_name)
.unwrap_or(false);
if is_exported {
// Mark exported functions as "called" so they're not flagged dead
called_functions.insert(func_name.to_string());
}
defined_functions.push(FunctionInfo {
name: func_name.to_string(),
file: file_str.clone(),
line: line_idx + 1,
});
continue;
}
// Table field functions: M.name = function(...)
if let Some(cap) = LUA_TABLE_FUNC_REGEX.captures(line) {
let module_name = cap.get(1).map(|m| m.as_str()).unwrap_or("");
let func_name = cap.get(2).map(|m| m.as_str()).unwrap_or("");
let is_exported = returned_module
.as_ref()
.map(|m| m == module_name)
.unwrap_or(false);
if is_exported {
called_functions.insert(func_name.to_string());
}
defined_functions.push(FunctionInfo {
name: func_name.to_string(),
file: file_str.clone(),
line: line_idx + 1,
});
continue;
}
// Local functions: local function name(...)
if let Some(cap) = LUA_LOCAL_FUNC_REGEX.captures(line) {
if let Some(name_match) = cap.get(1) {
let func_name = name_match.as_str();
if func_name != "main" {
defined_functions.push(FunctionInfo {
name: func_name.to_string(),
file: file_str.clone(),
line: line_idx + 1,
});
}
}
continue;
}
// Global functions: function name(...) — but not module funcs (handled above)
if let Some(cap) = LUA_GLOBAL_FUNC_REGEX.captures(line) {
if let Some(name_match) = cap.get(1) {
let func_name = name_match.as_str();
if func_name != "main" && !lua_keywords.contains(func_name) {
// Global functions may be called from other files
defined_functions.push(FunctionInfo {
name: func_name.to_string(),
file: file_str.clone(),
line: line_idx + 1,
});
}
}
}
}
// Collect function calls
collect_lua_calls(&content, &lua_keywords, &mut called_functions);
}
debug!(
"Found {} defined Lua functions, {} unique calls",
defined_functions.len(),
called_functions.len()
);
Ok((defined_functions, called_functions))
}
/// Detect if a Lua file returns a module table (e.g., `return M`)
fn detect_lua_module_return(content: &str) -> Option<String> {
// Find last non-empty, non-comment line
for line in content.lines().rev() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with("--") {
continue;
}
if let Some(cap) = LUA_RETURN_MODULE_REGEX.captures(trimmed) {
return cap.get(1).map(|m| m.as_str().to_string());
}
// Last meaningful line is not a module return
return None;
}
None
}
/// Collect function calls from Lua source
fn collect_lua_calls(content: &str, keywords: &HashSet<&str>, calls: &mut HashSet<String>) {
for line in content.lines() {
let trimmed = line.trim();
// Skip comments and function definitions
if trimmed.starts_with("--")
|| trimmed.starts_with("local function ")
|| trimmed.starts_with("function ")
{
continue;
}
for cap in LUA_CALL_REGEX.captures_iter(line) {
if let Some(name_match) = cap.get(1) {
let func_name = name_match.as_str();
if !keywords.contains(func_name) {
calls.insert(func_name.to_string());
}
}
}
// Also track method-style calls: obj:method() and obj.method()
// These appear as the part after : or . before (
// We already capture the identifier before (, which gets the method name
}
}