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-reporter

Analysis Reporter

6. Exercise 5: Analysis Reporter (18 tests)

Goal: Generate human-readable text reports, JSON output, summary lines, and formatted tables from analysis findings.

Time: ~25 minutes

File to edit: exercises/analysis-reporter/starter/reporter.ml

Also provided (do not edit):

  • finding_types.ml -- unified finding types with all helpers
  • sample_findings.ml -- pre-built findings for testing

Dependencies: None (self-contained)

Types provided (not a TODO)

type report = {
  program_name : string;
  total_findings : int;
  findings : Finding_types.finding list;
  severity_counts : (Finding_types.severity * int) list;
  category_counts : (Finding_types.category * int) list;
  pass_counts : (string * int) list;
}

What to implement (in order)

#FunctionHint
1build_report name findingsFill in all report fields. Count severities, categories, and pass names.
2format_text_report rHeader with "=== Analysis Report: name ===", total count, each formatted finding, severity breakdown
3format_json_finding fJSON object string with fields: id, category, severity, pass_name, location, message, suggestion (null if None)
4format_json_report rJSON object with fields: program, total, findings (array), severity_counts, category_counts
5format_summary rEmpty: "Analysis of 'name': No findings." Non-empty: "Analysis of 'name': N findings (X Critical, Y High, ...)" -- only include non-zero severity counts
6format_findings_table findingsAligned text table with columns: Severity, Category, Pass, Location, Message
7top_n_findings n findingsSort by severity (highest first), take first n
8findings_above_severity threshold findingsKeep findings where severity_to_int f.severity >= severity_to_int threshold

Run tests

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

Starter output (all 18 tests error):

EEEEEEEEEEEEEEEEEE

Hints:

  • For format_json_finding, use Printf.sprintf to build the JSON string. Escape quotes in string values if needed, but the test data does not contain embedded quotes.
  • For format_summary, iterate over severity_counts and build "X Critical, Y High" fragments, filtering out zero counts.
  • For format_findings_table, compute the max width of each column first, then pad with Printf.sprintf "%-*s".