MISRA Automotive C — Review Skill
Quick Reference
| File | Coverage |
|---|
| INLINECODE0 | Mandatory rules — never violate, no deviation allowed |
| INLINECODE1 |
Required rules — must comply or raise formal deviation |
|
types-and-casting.md | Essential types, fixed-width integers, casts, essential type model |
|
memory-embedded.md | No dynamic alloc, volatile, ISR constraints, stack discipline |
|
control-flow.md | goto, loops, switch, if-else chains, single exit |
|
preprocessor.md | Macros, include guards, #undef, ## operator |
|
iso26262-mapping.md | ASIL A–D relevance for key MISRA C:2012 rules |
When to Activate
Activate this skill when the user:
- - Pastes C code and asks for a MISRA review, compliance check, or audit
- Uses trigger words: "misra", "misra check", "misra review", "automotive c", "embedded c review", "iso 26262", "asil"
- Asks "is this MISRA compliant?" or "what rules does this violate?"
Review Workflow — Follow These Steps in Order
Step 1 — Parse the Code
- - Read every line of the submitted C code carefully.
- Identify the context: function body, header file, ISR, macro definition, type declaration.
- Note all variable types, control flow paths, preprocessor directives.
Step 2 — Check Mandatory Rules First (load misra-mandatory.md)
Mandatory rules have zero tolerance — flag every violation immediately.
Priority mandatory checks:
- - Rule 1.3 — No undefined behaviour
- Rule 2.1 — No unreachable code
- Rule 13.2 — No side effects in expressions where order is unsequenced
- Rule 14.3 — Controlling expressions shall not be invariant
- Rule 15.1 — No INLINECODE8
- Rule 17.1 — No
<stdarg.h> features - Rule 17.3 — No implicit function declarations
- Rule 17.4 — All exit paths of a non-void function shall have an explicit return
- Rule 21.3 — No
malloc, calloc, realloc, INLINECODE13
Step 3 — Check Required Rules (load misra-required.md, types-and-casting.md, control-flow.md, preprocessor.md)
Required rules must be complied with unless a formal deviation exists. Check:
- - Typedef usage for fixed-width types (Rule 4.6 → D.4.6)
- Essential type model violations (Rules 10.1–10.8)
- All switch statements have a
default clause (Rule 16.4) - All
if-else if chains end with else (Rule 15.7) - Single-entry single-exit preferred (Rule 15.5)
- All struct/union members initialised (Rule 9.1)
- All macros parenthesised (Rule 20.7)
- No function-like macros unless unavoidable (Rule 20.10)
Step 4 — Check Embedded / Memory Rules (load memory-embedded.md)
- -
volatile used on all hardware-mapped registers and shared ISR variables - No dynamic memory allocation (heap) anywhere in safety-critical paths
- No recursion (Rule 17.2)
- Fixed-width integer types used exclusively for hardware registers and protocol fields
- ISR functions: no heavy computation, no blocking calls
Step 5 — Map to ISO 26262 ASIL (load iso26262-mapping.md)
- - For every rule violation found, report its ASIL classification
- ASIL D violations are highest severity — highlight prominently
- Report ASIL A violations but mark as lower urgency
Step 6 — Generate the Violation Report
Output the report in EXACTLY this format for every violation found:
CODEBLOCK0
After all individual violations, output a summary table:
CODEBLOCK1
Code Generation Workflow
When asked to generate new automotive/embedded C code (not review existing code):
Always apply these rules unconditionally — no exceptions:
Types:
- - Use
uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t — never int, unsigned int, long, char for numeric data - Use
bool (from <stdbool.h>) for boolean values — never integer flags - Always cast explicitly when converting between types — no implicit narrowing
Memory:
- - Never use
malloc, calloc, realloc, or INLINECODE41 - All variables initialised at declaration point
- Arrays are fixed-size, statically allocated
- Hardware register pointers declared INLINECODE42
Control flow:
- - No INLINECODE43
- All
if / else if chains end with INLINECODE46 - All
switch statements have a INLINECODE48 - All loops have a defined maximum iteration count
- Every non-void function has a single explicit
return at the end when possible
Functions:
- - All parameters and return types use fixed-width typedefs
- All return values checked by callers
- No recursion
- Maximum one level of pointer indirection unless justified
Macros:
- - Function-like macros: all parameters and the whole expression parenthesised
- Prefer
static inline functions over function-like macros - Include guards on every header (
#ifndef FILENAME_H / #define FILENAME_H / ... / #endif)
Escalation Rules
- - If the code contains Rule 21.3 violations (dynamic allocation) in any path reachable from a safety function, output a ⚠️ SAFETY CRITICAL banner at the top of the report before any violations.
- If Rule 15.1 (goto) is found, flag it as ASIL D regardless of context.
- If Rule 17.2 (recursion) is found in any function, trace the full call chain if visible and report it.
- If the code appears to be an ISR (function name contains
ISR, _IRQ, _Handler, _isr, or has a __attribute__((interrupt)) annotation), apply memory-embedded.md ISR rules with heightened strictness.
MISRA Automotive C — 审查技能
快速参考
| 文件 | 覆盖范围 |
|---|
| misra-mandatory.md | 强制规则 — 绝不可违反,不允许任何偏离 |
| misra-required.md |
必需规则 — 必须遵守或提出正式偏离申请 |
| types-and-casting.md | 基本类型、固定宽度整数、类型转换、基本类型模型 |
| memory-embedded.md | 无动态分配、volatile、ISR约束、栈纪律 |
| control-flow.md | goto、循环、switch、if-else链、单出口 |
| preprocessor.md | 宏、包含守卫、#undef、##运算符 |
| iso26262-mapping.md | 关键MISRA C:2012规则的ASIL A–D相关性 |
何时激活
当用户出现以下情况时激活此技能:
- - 粘贴C代码并要求进行MISRA审查、合规性检查或审计
- 使用触发词:misra、misra检查、misra审查、汽车级C、嵌入式C审查、iso 26262、asil
- 询问这段代码符合MISRA标准吗?或它违反了哪些规则?
审查工作流程 — 按顺序执行以下步骤
步骤1 — 解析代码
- - 仔细阅读提交的C代码的每一行。
- 识别上下文:函数体、头文件、ISR、宏定义、类型声明。
- 记录所有变量类型、控制流路径、预处理指令。
步骤2 — 首先检查强制规则(加载misra-mandatory.md)
强制规则零容忍 — 立即标记每一项违规。
优先强制检查:
- - 规则1.3 — 无未定义行为
- 规则2.1 — 无不可达代码
- 规则13.2 — 表达式中顺序未确定的副作用
- 规则14.3 — 控制表达式不得为不变式
- 规则15.1 — 无goto
- 规则17.1 — 无特性
- 规则17.3 — 无隐式函数声明
- 规则17.4 — 非void函数的所有退出路径必须有显式return
- 规则21.3 — 无malloc、calloc、realloc、free
步骤3 — 检查必需规则(加载misra-required.md、types-and-casting.md、control-flow.md、preprocessor.md)
除非存在正式偏离申请,否则必须遵守必需规则。检查:
- - 固定宽度类型的typedef使用(规则4.6 → D.4.6)
- 基本类型模型违规(规则10.1–10.8)
- 所有switch语句都有default子句(规则16.4)
- 所有if-else if链以else结尾(规则15.7)
- 优先单入口单出口(规则15.5)
- 所有结构体/联合体成员已初始化(规则9.1)
- 所有宏已加括号(规则20.7)
- 除非不可避免,否则无类函数宏(规则20.10)
步骤4 — 检查嵌入式/内存规则(加载memory-embedded.md)
- - 所有硬件映射寄存器和共享ISR变量使用volatile
- 安全关键路径中任何位置无动态内存分配(堆)
- 无递归(规则17.2)
- 硬件寄存器和协议字段专门使用固定宽度整数类型
- ISR函数:无繁重计算,无阻塞调用
步骤5 — 映射到ISO 26262 ASIL(加载iso26262-mapping.md)
- - 对于发现的每条规则违规,报告其ASIL分类
- ASIL D违规严重性最高 — 突出显示
- 报告ASIL A违规但标记为较低优先级
步骤6 — 生成违规报告
对于发现的每条违规,以完全相同的格式输出报告:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
违规 #
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
规则 : MISRA C:2012 规则
类别 : 强制 | 必需 | 建议
ASIL : A | B | C | D | 未映射
严重性 : 致命 | 高 | 中 | 低
位置 : 第行 — <该行功能的简要描述>
不合规代码:
<确切的违规行>
违反规则 的原因:
<1–3句通俗易懂的规则解释及代码为何违反>
符合MISRA标准的替代代码:
<修正后的代码,完整且可直接使用>
修正说明:
<1–2句解释更改内容及为何现在合规>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
在所有单个违规之后,输出一个摘要表:
审查摘要
──────────────────────────────────────────────────
违规总数 :
强制 : ← 在任何安全认证前必须修复
必需 : ← 必须修复或提出正式偏离申请
建议 : ← 建议修复
ASIL细分:
ASIL D : ← 安全关键,立即修复
ASIL C :
ASIL B :
ASIL A :
未映射 :
总体合规状态:失败 | 有条件通过 | 通过
──────────────────────────────────────────────────
代码生成工作流程
当要求生成新的汽车级/嵌入式C代码(而非审查现有代码)时:
始终无条件应用以下规则 — 无例外:
类型:
- - 使用uint8t、uint16t、uint32t、uint64t、int8t、int16t、int32t、int64t — 绝不使用int、unsigned int、long、char表示数值数据
- 使用bool(来自)表示布尔值 — 绝不使用整数标志
- 类型转换时始终显式强制转换 — 无隐式窄化
内存:
- - 绝不使用malloc、calloc、realloc或free
- 所有变量在声明点初始化
- 数组为固定大小,静态分配
- 硬件寄存器指针声明为volatile
控制流:
- - 无goto
- 所有if/else if链以else结尾
- 所有switch语句有default
- 所有循环有定义的最大迭代次数
- 每个非void函数在可能的情况下在末尾有一个显式return
函数:
- - 所有参数和返回类型使用固定宽度typedef
- 所有返回值由调用者检查
- 无递归
- 最多一级指针间接引用,除非有正当理由
宏:
- - 类函数宏:所有参数和整个表达式加括号
- 优先使用static inline函数而非类函数宏
- 每个头文件有包含守卫(#ifndef FILENAMEH / #define FILENAMEH / ... / #endif)
升级规则
- - 如果代码在安全功能可达的任何路径中包含规则21.3违规(动态分配),在报告顶部任何违规之前输出一个⚠️ 安全致命横幅。
- 如果发现规则15.1(goto),无论上下文如何,将其标记为ASIL D。
- 如果在任何函数中发现规则17.2(递归),如果可见则追踪完整调用链并报告。
- 如果代码看起来是ISR(函数名包含ISR、IRQ、Handler、_isr,或有attribute((interrupt))注解),以更高严格度应用memory-embedded.md的ISR规则。