embedded-coding-style
This Skill is designed for embedded C projects, offering MISRA-like coding style governance. It emphasizes strict GB2312 encoding safety, supports selectable scope execution, and provides verifiable rule checks for code style, ensuring project code quality and consistency.
npx skills add Z1R343L-D77/embedded-coding-style-codex-skills --skill embedded-coding-styleBefore / After Comparison
1 组Without automated tools, embedded C project teams spend significant time on manual code style reviews, checking encoding, brace placement, comment formats, and more. This process is time-consuming, prone to errors, leads to inconsistent codebases, and increases maintenance costs.
With SkillForge's embedded coding style Skill, teams can automate MISRA-like style checks and corrections, ensuring all code strictly adheres to GB2312 encoding and unified standards. This significantly reduces manual review time, enhancing code quality and development efficiency.
name: embedded-coding-style description: "Initialize embedded project context, then apply and verify MISRA-like C coding style rules with selectable scope, ANSI encoding policy, brace policy, header/doc comment policy, and verification checklist."
Embedded Coding Style Skill
Initialize project context first, then apply a strict, repeatable embedded C style policy to selected scope.
Initialization + Scope Selection
Step 0: Initialize Project Context (Mandatory)
Before any edit, initialize the project and gather context:
Get-Location
Get-ChildItem -Path <project_root>
If backup is provided, verify backup path exists before edits.
Step 1: Select Scope (Mandatory)
Choose one scope per run:
- Driver layer only:
<project_root>/<driver_dir> - BSP layer only:
<project_root>/<bsp_dir> - App layer only:
<project_root>/<app_dir> - Combined subset: any two scopes
- Full target: all selected core scopes
Default behavior: if user does not specify scope, use full target scope.
Do not edit outside selected scope.
Hard Rules
- Encoding:
GB2312(code page 936), mandatory. - Line ending:
CRLF. - File end: one blank line (
CRLF+CRLF). - Every
.c/.hbegins with:
/**
* @file ...
* @author ...
* @brief ...
* @version ...
* @date ...
*
* @copyright Copyright (c) 2026
*/
- Function doc comment uses
/** ... */and includes@brief @param @retval. - Inline/block explanation comments use
/* ... */. - Comment language: Chinese; avoid AI-template phrasing.
- Standard headers use
<>(e.g.<stdint.h>,<stdio.h>,<string.h>,<math.h>,<stddef.h>). - Project headers use
"...". - Header files keep include guard and must contain C++ compatibility block:
#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif
- Left brace must be on a new line.
- Brace indentation must align with control statement indentation.
- Always use braces for single-line bodies:
ifwhileelse ifelse- (also keep
for/switch/doas full blocks)
switchrequiresdefault.- Each
casemust end with explicitbreak/return(or controlled jump). - Prefer explicit comparisons; avoid implicit truthiness when unclear.
- Integer literals should use explicit suffixes where applicable (
U,UL). - Macro parameters/expressions require parentheses.
- Multi-statement macros require
do { ... } while (0). - Minimize global visibility: file-private symbols should be
static. - Pointer params should be null-checked before dereference for externally reachable paths.
- Avoid magic numbers; extract to macro/constant (prefer existing project style).
- Do not alter timing/ISR/state-machine behavior for formatting-only tasks.
- Text read/write encoding is mandatory
GB2312(code page 936), no auto-detect, no fallback encoding. - Read path must use strict
GB2312decoding (decoder exception fallback enabled); if decode fails, stop immediately. - Write path must use strict
GB2312encoding (encoder exception fallback enabled); if encode fails, stop immediately and rollback. - Remove obvious AI-style comments (for example:
/* global variable with static scope */); rewrite as natural engineering comments that describe current module intent/context, not generic coding slogans. - Struct-pointer function parameters must use unified name
handle(for example:static void gpio_control_work(GPIO_Control_t *handle)), avoid mixed aliases such asp_ctrl/p_xxx.
Execution Procedure
Step 2: Pre-check Files in Selected Scope
Enumerate selected files only:
Get-ChildItem -Path <selected_scope_paths> -Recurse -File
Step 3: Safe Edit Strategy
- Edit in small passes (headers first, then braces, then comments).
- Avoid broad regex that can touch function comments repeatedly.
- For file header replacement, replace only the first top-of-file block comment.
- During comment cleanup, rewrite AI-template comments into natural project comments; preserve technical meaning and avoid repetitive rule-recitation wording.
- During signature cleanup, rename struct-pointer parameters to
handleconsistently and update in-function references in the same scope. - Run strict encoding preflight before first write:
- Force
GB2312decode for source text (code page 936). - Decoder fallback must be exception-based; any decode error means immediate stop.
- Do not switch to UTF-8/UTF-16/other encodings as fallback.
- Force
- Required operation: explicit
GB2312read + explicitGB2312write for every touched.c/.hfile.
Step 4: Brace Normalization
- Convert control statements to newline brace style.
- Add missing braces to all single-line
if/while/else if/elsebodies. - Ensure opening brace indent equals control-statement indent.
Step 5: Header/C++ Guard Normalization
- Keep include guard.
- Ensure exactly one open C++ block and one close C++ block.
- Place close C++ block immediately before final
#endifof include guard.
Step 6: Include Normalization
- Convert standard C headers from
"x.h"to<x.h>. - Keep project headers in
"x.h".
Step 7: Final Newline + Encoding Pass
- Normalize newline to CRLF.
- Trim trailing blank noise.
- Force final
CRLF+CRLF. - Write using
GB2312(code page 936) with strict encoder fallback (exception on unmappable chars).
Verification Checklist
Run and confirm all are clean (within selected scope only):
if/while/else if/elsewithout brace:
rg -n "^\s*(if|while|else\s+if|else)\b" <selected_scope_paths>
- C++ guards exist in headers:
rg -n "#ifdef __cplusplus|extern \"C\"|#endif" <selected_scope_paths>
- File header presence:
rg -n "^/\*\*|@file|@author|@brief|@version|@date" <selected_scope_paths>
- Tail bytes check (
13,10,13,10expected):
Get-ChildItem <selected_scope_paths> -Recurse -File | ForEach-Object {
$b=[IO.File]::ReadAllBytes($_.FullName)
$tail=if($b.Length -ge 4){$b[($b.Length-4)..($b.Length-1)]}else{$b}
"{0}:{1}" -f $_.FullName,($tail -join ',')
}
- Spot-check random files for Chinese comment readability.
- Encoding safety check:
rg -n "\?\?\?|\uFFFD" <selected_scope_paths>
Result must be empty for newly changed content.
Recovery / Rollback
- If text becomes mojibake or strict
GB2312decode/encode throws:- Stop immediately.
- Restore from known-good backup.
- Re-apply rules with explicit strict
GB2312read/write configuration.
- Never perform non-GB2312 encode/decode cycles.
- Do not continue batch edits after detecting corruption or codec exception.
- If strict GB2312 preflight fails, only allow byte-level operations (copy/backup/line-ending check), no semantic rewrite.
Success Criteria
- All files in selected scope comply with Hard Rules.
- No mojibake/no
?replacement damage in Chinese comments. - No unintended behavior changes.
- Diff is style-focused and reviewable.
User Reviews (0)
Write a Review
No reviews yet
Statistics
User Rating
Rate this Skill