Work on this exercise locally
This web app is a reference guide — you can read instructions, browse starter code, and view tests here. To actually complete the exercise, you need to work in your local development environment.
git clone https://github.com/weihaoqu/program-analysis-bootcamp-studentfailwith "TODO" with your implementation.dune runtest modules/module6-tools-integration/exercises/dead-code-detectorDead Code Detector
3. Exercise 2: Dead Code Detector (20 tests)
Goal: Detect dead code patterns using purely AST-level analysis: unreachable
code after Return, unused variables, and unused function parameters.
Time: ~25 minutes
File to edit: exercises/dead-code-detector/starter/dead_code.ml
Also provided (do not edit): exercises/dead-code-detector/starter/finding_types.ml
Dependencies: shared_ast (for AST types)
What to implement (in order)
| # | Function | Hint |
|---|---|---|
| 1 | has_return stmts | Check if any top-level stmt in the list is a Return |
| 2 | stmts_after_return stmts | Walk the list; once you hit a Return, return everything after it |
| 3 | collect_used_vars_expr e | Recursively collect Var x names into a StringSet. IntLit/BoolLit contribute nothing. BinOp unions both sides. Call unions all args. |
| 4 | collect_used_vars_stmts stmts | Union used vars from each statement. For Assign(_, e) collect from e. For If/While collect from condition + bodies. For Return (Some e) collect from e. |
| 5 | collect_assigned_vars stmts | Collect all x from Assign(x, _) statements, recursing into If/While/Block bodies |
| 6 | find_unreachable_code func | If stmts_after_return func.body is non-empty, emit a CodeQuality/Medium finding |
| 7 | find_unused_variables func | Assigned but never read variables get CodeQuality/Low findings. Variables starting with _ are exempt. |
| 8 | find_unused_parameters func | Parameters never read in the body get CodeQuality/Info findings. Parameters starting with _ are exempt. |
| 9 | analyze_function func | Concatenate results of all three detectors for one function |
| 10 | analyze_program prog | List.concat_map analyze_function prog |
Run tests
dune runtest modules/module6-tools-integration/exercises/dead-code-detector/
Starter output (all 20 tests error):
EEEEEEEEEEEEEEEEEEEE
Hints:
- Use
StringSet.mem,StringSet.union,StringSet.singleton, andStringSet.difffor variable tracking. - The
_-prefix exemption check:String.length name > 0 && name.[0] = '_'. - Use
fresh_id ()to generate unique IDs for each finding. - Finding
locationshould be the function name (func.name).
Starter Files
Test Files
Work on this exercise locally
This web app is a reference guide — you can read instructions, browse starter code, and view tests here. To actually complete the exercise, you need to work in your local development environment.
git clone https://github.com/weihaoqu/program-analysis-bootcamp-studentfailwith "TODO" with your implementation.dune runtest modules/module6-tools-integration/exercises/dead-code-detectorDead Code Detector
3. Exercise 2: Dead Code Detector (20 tests)
Goal: Detect dead code patterns using purely AST-level analysis: unreachable
code after Return, unused variables, and unused function parameters.
Time: ~25 minutes
File to edit: exercises/dead-code-detector/starter/dead_code.ml
Also provided (do not edit): exercises/dead-code-detector/starter/finding_types.ml
Dependencies: shared_ast (for AST types)
What to implement (in order)
| # | Function | Hint |
|---|---|---|
| 1 | has_return stmts | Check if any top-level stmt in the list is a Return |
| 2 | stmts_after_return stmts | Walk the list; once you hit a Return, return everything after it |
| 3 | collect_used_vars_expr e | Recursively collect Var x names into a StringSet. IntLit/BoolLit contribute nothing. BinOp unions both sides. Call unions all args. |
| 4 | collect_used_vars_stmts stmts | Union used vars from each statement. For Assign(_, e) collect from e. For If/While collect from condition + bodies. For Return (Some e) collect from e. |
| 5 | collect_assigned_vars stmts | Collect all x from Assign(x, _) statements, recursing into If/While/Block bodies |
| 6 | find_unreachable_code func | If stmts_after_return func.body is non-empty, emit a CodeQuality/Medium finding |
| 7 | find_unused_variables func | Assigned but never read variables get CodeQuality/Low findings. Variables starting with _ are exempt. |
| 8 | find_unused_parameters func | Parameters never read in the body get CodeQuality/Info findings. Parameters starting with _ are exempt. |
| 9 | analyze_function func | Concatenate results of all three detectors for one function |
| 10 | analyze_program prog | List.concat_map analyze_function prog |
Run tests
dune runtest modules/module6-tools-integration/exercises/dead-code-detector/
Starter output (all 20 tests error):
EEEEEEEEEEEEEEEEEEEE
Hints:
- Use
StringSet.mem,StringSet.union,StringSet.singleton, andStringSet.difffor variable tracking. - The
_-prefix exemption check:String.length name > 0 && name.[0] = '_'. - Use
fresh_id ()to generate unique IDs for each finding. - Finding
locationshould be the function name (func.name).