Coccinelle
$ spatch 社区开源 低风险Coccinelle(spatch)使用语义补丁语言(SmPL)而非脆弱的文本正则来匹配与重构 C 代码,广泛应用于 Linux 内核。默认情况下 spatch 执行安全试运行并将统一 diff 打印至标准输出,仅在显式传入 --in-place 时才修改文件。
brew install coccinelle spatch 直接在本地解析大型 C 代码树并生成 diff,无需将 SmPL 执行封装进 MCP 服务。
Agent 友好度体检单
当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。
FCRS-1.0 六维评分拆解
支持非交互执行,默认输出试运行统一 diff,并提供 --very-quiet 与 --mode report,但缺少 --json 输出。
- Defaults to read-only diff stdout;
--very-quietsuppresses progress noise - Outputs unified diff or text reports, not JSON
默认安全:除非显式传入 --in-place,否则绝不会修改磁盘上的文件。
- Read-only dry-run is the default behavior
可通过 brew install coccinelle、Linux 发行版仓库及 opam 安装,但原生 Windows 需依赖 WSL/Cygwin 且 OCaml 构建较重。
- Primarily macOS and Linux; limited native Windows support
C 语言 AST 与控制流重构的黄金标准引擎,可完成正则无法胜任的语义级改写。
- Battle-tested across Linux kernel
coccicheckrules
由 Inria 研究团队与 Linux 内核贡献者共同遵循 GPL-2.0 协议长期维护。
- Long-term academic and kernel maintenance
提供详尽的 SmPL 语法手册、教程以及 spatch(1) 手册页。
- Official documentation and kernel
scripts/coccinelleexamples
安全边界、鉴权与放权指南
无需凭据;仅在本地 C 源码文件与 .cocci 脚本上运行。
100% 免费、离线、开源(GPL-2.0)。
# Coccinelle (spatch) Agent Rules - `spatch` defaults to read-only diff output; pass `--very-quiet` to suppress banner noise. - Only add `--in-place` after inspecting the unified diff output on target `.c`/`.h` files.
Agent 高频安全命令配方
只读查询(安全推荐)
spatch --very-quiet --sp-file rule.cocci --dir src/
对 src/ 目录执行 rule.cocci 并向标准输出打印统一 diff,不改动文件。
spatch --very-quiet -D report --sp-file check.cocci --dir src/
启用虚拟 report 规则输出 file:line:col 格式的诊断告警。
受控写入与预演
spatch --very-quiet --in-place --sp-file rule.cocci --dir src/
通过 --in-place 将已审核的 SmPL 转换直接写入匹配的 C 文件。
核心优势
- + 默认只读输出统一 diff,必须显式传入
--in-place才会修改文件。 - + 深入理解 C 语言控制流、类型与宏,远胜纯文本替换工具。
注意事项与踩坑点
- ! 仅支持 C 语言且需要编写
.cocciSmPL 语义补丁规则。 - ! 不支持
--json输出,且缺少原生 Windows 支持。