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.

1Clone the repo: git clone https://github.com/weihaoqu/program-analysis-bootcamp-student
2Edit the starter file in your editor (VS Code, Vim, etc.) — replace failwith "TODO" with your implementation.
3Run the tests: dune runtest modules/module6-tools-integration/exercises/analysis-finding

Analysis Finding

2. Exercise 1: Analysis Finding (20 tests)

Goal: Define utility functions on the unified finding type -- conversion, comparison, filtering, deduplication, formatting, and counting.

Time: ~20 minutes

File to edit: exercises/analysis-finding/starter/analysis_finding.ml

Dependencies: None (self-contained)

Types provided (not a TODO)

The severity, category, and finding types are already defined at the top of the file. You implement the functions that operate on them.

What to implement (in order)

#FunctionHint
1severity_to_string sPattern match: Critical -> "Critical", etc.
2category_to_string cPattern match: Security -> "Security", CodeQuality -> "CodeQuality", etc.
3severity_to_int sCritical=4, High=3, Medium=2, Low=1, Info=0
4compare_by_severity a bHigher severity first: compare severity_to_int b - severity_to_int a (negative means a is more severe)
5compare_by_location a bString.compare a.location b.location
6filter_by_severity threshold findingsKeep findings where severity_to_int f.severity >= severity_to_int threshold
7filter_by_category cat findingsKeep findings where f.category = cat
8deduplicate findingsRemove duplicates with same message AND same location; preserve first occurrence
9format_finding fFormat: "[Severity] Category - message in location". If suggestion is Some s, append "\n Suggestion: s"
10format_findings_list findingsReturn "No findings." for empty list; otherwise one formatted finding per line
11count_by_severity findingsReturn (severity * int) list ordered Critical..Info, excluding zero-count entries
12count_by_category findingsReturn (category * int) list ordered Security..Performance, excluding zero-count entries

Run tests

dune runtest modules/module6-tools-integration/exercises/analysis-finding/

Starter output (all 20 tests error):

EEEEEEEEEEEEEEEEEEEE

Hints:

  • For deduplicate, fold through the list keeping a set of (message, location) pairs you have already seen. List.rev at the end to preserve order.
  • For count_by_severity, iterate over [Critical; High; Medium; Low; Info], count how many findings match each, and drop entries with count 0.