Analyzing MISRA C Compliance
Given a C source file, run the four-stage pipeline:
- Ingest the C code (preserve line numbers exactly — they are load-bearing for all downstream steps).
- Detect violations → emit JSON array matching the schema below.
- Remediate each violation → emit a minimal compliant patch.
- Verify each patch → confirm it (a) resolves the original violation, (b) introduces no new MISRA violations, and (c) preserves the surrounding code's style/idioms.
Never skip straight to patching without first producing the Step 2 JSON — it is the audit trail every later step depends on.
Progress:
- [ ] Step 1: Ingest & line-index the C source
- [ ] Step 2: Detect violations, emit JSON report
- [ ] Step 3: Generate compliant patch per violation
- [ ] Step 4: Verify patch (compliance + style preservation)
Step 1 — Ingest
- Keep the source verbatim; do not reformat before analysis (reformatting shifts line numbers and invalidates
start_line_of_error/end_line_of_error). - If the file has no trailing newline or unusual tabs, note it internally — this affects line counting.
Step 2 — Violation Detection
For each violation found, emit one JSON object with exactly these fields:
| Field | Type | Notes |
|---|---|---|
rank | int 1–5 | Severity-ish ordering; 1 = highest priority to fix first |
rule_id | string | "MISRA C:2025 Rule X.Y" |
start_line_of_error | int | First line of the relevant span |
end_line_of_error | int | Last line of the relevant span |
identified_bug | string | Exact offending code text (copy verbatim, don't paraphrase) |
identified_line | int | Single representative line within the span |
suspicious_syntax | string | The exact token: variable name, cast expression, function name, or operator |
reasoning | string | 1–2 sentences, written for a downstream rule-checking tool, not a human narrative |
confidence | float 0.0–1.0 | Calibrated — do not default to 0.9 for everything |
Output as a JSON array, one object per violation, sorted by start_line_of_error ascending. If zero violations, output [] — never omit the array.
Confidence calibration rule of thumb:
- 0.9–1.0: unambiguous syntactic violation (e.g., implicit int-to-pointer cast)
- 0.6–0.89: pattern-matches a rule but context-dependent (e.g., recursion, dynamic memory)
- 0.3–0.59: plausible but requires knowledge the snippet doesn't fully provide (e.g., cross-TU linkage)
Step 3 — Patch Generation
For each violation (process by rank, then by start_line_of_error):
- Quote the minimal surrounding block needed for a valid patch (prefer function-scope, not whole-file).
- Produce a diff-style or before/after snippet that fixes only that violation — don't bundle unrelated cleanup.
- State which
rule_idthe patch addresses. - If a single fix would resolve multiple reported violations, note the overlap explicitly rather than duplicating the patch.
Step 4 — Verification (Research Point)
This is the step most pipelines skimp on — do not skip it.
For every patch produced in Step 3, check:
- Compliance regression check: re-scan the patched snippet (mentally re-run Step 2's logic) against the full rule set, not just the original
rule_id. A fix for Rule 10.3 (implicit conversion) can easily introduce a Rule 17.7 (ignored return value) violation. - Original violation resolved: confirm the
identified_bugtext no longer appears in the offending form. - Style preservation: compare against surrounding code for:
- Naming convention (snake_case vs camelCase, Hungarian prefixes)
- Brace style and indentation width
- Preferred idioms already used in the file (e.g., if the file always uses
(void)casts to discard return values, the patch should too) - Comment density/style
- Semantic equivalence: the patch must not change observable behavior unless the violation is the behavior (e.g., undefined behavior fixes legitimately change semantics — note this explicitly).
Emit a verification verdict per patch:
JSON{ "rule_id": "MISRA C:2025 Rule 10.3", "patch_accepted": true, "new_violations_introduced": [], "style_preserved": true, "notes": "Explicit cast added matches existing style of explicit casts elsewhere in file (line 42)." }
If patch_accepted is false, loop back to Step 3 with the reason appended as a constraint.
Example 1:
Input (excerpt, line 14):
Cint *p = 0x1000;
Step 2 output:
JSON{ "rank": 1, "rule_id": "MISRA C:2025 Rule 11.4", "start_line_of_error": 14, "end_line_of_error": 14, "identified_bug": "int *p = 0x1000;", "identified_line": 14, "suspicious_syntax": "0x1000", "reasoning": "An integer constant is converted to a pointer type without an explicit cast, which Rule 11.4 flags as a conversion requiring scrutiny.", "confidence": 0.95 }
Step 3 patch:
Cint *p = (int *)0x1000; /* explicit cast per MISRA Rule 11.4 */
Step 4 verdict:
JSON{ "rule_id": "MISRA C:2025 Rule 11.4", "patch_accepted": true, "new_violations_introduced": [], "style_preserved": true, "notes": "Comment style matches existing inline MISRA annotations in the file." }
Example 2 (overlap case):
Input has two reported violations on the same line — Rule 10.1 (operand type) and Rule 10.4 (essential type mismatch in arithmetic) both pointing at result = flag + count; where flag is bool and count is int.
Step 3 output notes: "Single patch resolves both Rule 10.1 and Rule 10.4 — casting flag to int explicitly makes the essential types compatible for the arithmetic." Patch: result = (int)flag + count;
- Always copy
identified_bugtext verbatim from source — never paraphrase or reformat it, downstream tools may string-match it. - Keep patches minimal and single-purpose; reviewers trust small diffs more than large rewrites.
- When uncertain between two rule IDs for the same symptom, report both as separate JSON objects rather than guessing one.
- Preserve the original author's variable names and structure unless the violation itself is the naming (e.g., Rule 5.x identifier rules).
- Treat Step 4 as a second, independent pass — don't assume a patch is correct just because it was generated to look correct.
- Don't shift or recompute line numbers after reformatting — always index against the original input.
- Don't emit
confidence: 1.0as a default; reserve it for truly mechanical, unambiguous cases. - Don't bundle multiple unrelated fixes into one patch "for efficiency" — this breaks traceability to
rule_id. - Don't skip Step 4 or treat it as a rubber stamp — the most common pipeline failure is a patch that fixes the reported rule but silently violates another.
- Don't invent a rule ID not in the allowed set of seven; if no allowed rule fits, lower confidence and say so in
reasoningrather than forcing a match. - Don't change code style wholesale (e.g., reformatting braces) as a side effect of a MISRA fix — this fails the style-preservation check in Step 4.