Skip to content

Commit

Permalink
Improve checking code in external scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
PabstMirror committed Nov 24, 2024
1 parent 4a3a1cb commit b686b2a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
18 changes: 16 additions & 2 deletions libs/sqf/src/analyze/inspector/external_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl SciptScope {
}
}
}
"cba_fnc_addperframehandler" | "cba_fnc_waitandexecute" => {
"cba_fnc_addperframehandler" | "cba_fnc_waitandexecute" | "cba_fnc_execnextframe" => {
if !gv_array.is_empty() {
self.external_new_scope(&gv_array[0], &vec![], database);
}
Expand All @@ -113,6 +113,20 @@ impl SciptScope {
);
}
}
"cba_fnc_addeventhandlerargs" => {
if gv_array.len() > 1 {
self.external_new_scope(
&gv_array[1],
&vec![
("_thisType", GameValue::String(None)),
("_thisId", GameValue::Number(None)),
("_thisFnc", GameValue::Code(None)),
("_thisArgs", GameValue::Anything),
],
database,
);
}
}
_ => {}
},
_ => {}
Expand All @@ -135,7 +149,7 @@ impl SciptScope {
if self.code_used.contains(expression) {
return;
}
let mut ext_scope = Self::create(&self.ignored_vars, true);
let mut ext_scope = Self::create(&self.ignored_vars, false);

for (var, value) in vars {
ext_scope.var_assign(var, true, HashSet::from([value.clone()]), VarSource::Ignore);
Expand Down
1 change: 1 addition & 0 deletions libs/sqf/src/analyze/inspector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct SciptScope {
local: Vec<Stack>,
code_seen: HashSet<Expression>,
code_used: HashSet<Expression>,
/// Orphan scopes are code blocks that are created but don't appear to be called in a known way
is_orphan_scope: bool,
ignored_vars: HashSet<String>,
}
Expand Down
28 changes: 21 additions & 7 deletions libs/sqf/tests/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod tests {
pub fn test_1() {
let (_pro, sqf, _database) = get_statements("test_1.sqf");
let result = sqf.issues();
assert_eq!(result.len(), 13);
assert_eq!(result.len(), 15);
// Order not guarenteed
assert!(result.iter().any(|i| {
if let Issue::InvalidArgs(cmd, _) = i {
Expand All @@ -47,8 +47,8 @@ mod tests {
}
}));
assert!(result.iter().any(|i| {
if let Issue::Undefined(var, _, _) = i {
var == "_test2"
if let Issue::Undefined(var, _, orphan) = i {
var == "_test2" && !orphan
} else {
false
}
Expand Down Expand Up @@ -89,15 +89,15 @@ mod tests {
}
}));
assert!(result.iter().any(|i| {
if let Issue::Undefined(var, _, _) = i {
var == "_test8"
if let Issue::Undefined(var, _, orphan) = i {
var == "_test8" && !orphan
} else {
false
}
}));
assert!(result.iter().any(|i| {
if let Issue::Undefined(var, _, _) = i {
var == "_test9"
if let Issue::Undefined(var, _, orphan) = i {
var == "_test9" && !orphan
} else {
false
}
Expand Down Expand Up @@ -130,6 +130,20 @@ mod tests {
false
}
}));
assert!(result.iter().any(|i| {
if let Issue::Undefined(var, _, orphan) = i {
var == "_test12" && !orphan
} else {
false
}
}));
assert!(result.iter().any(|i| {
if let Issue::Undefined(var, _, orphan) = i {
var == "_test13" && *orphan
} else {
false
}
}));
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions libs/sqf/tests/inspector/test_1.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ call _someCode; // InvalidArgs for setGusts
params [["_varP", "", ["", []]]];
format _varP;


[{
[_test12] call some_func; // undef, not orphan because CBA_fnc_execNextFrame is a known clean scope
}, player] call CBA_fnc_execNextFrame;
[{
[_test13] call some_func; // undef, is orphan
}, player] call unknown_fnc_Usage;

0 comments on commit b686b2a

Please sign in to comment.