CLI 目录 横向对比 FCRS-1.0 标准 /llms.txt
← 返回 CLI 目录

clang-format

$ clang-format 基金会 低风险
LLVM Project · 代码托管与研发协同 · Apache-2.0 WITH LLVM-exception · macos / linux / windows

LLVM 官方代码格式化命令行工具,依据 .clang-format 规则格式化 C、C++、Objective-C、Java、JavaScript 与 Protocol Buffers。支持无损 --dry-run 与 -Werror 预检,再通过 -i 原地写入。

FCRS-1.0 综合评分
87 /100
良好 (Good)
中置信度 (源码/文档)
零摩擦安装命令
brew install clang-format
CLI vs MCP 选型建议 仅 CLI 可用

毫秒级完成本地 C/C++/Protobuf 语法树格式化,无需引入 MCP 服务。

Agent 友好度体检单

— 原生 JSON 输出
— 字段裁剪
✓ 无头非交互
✓ 支持 Dry-Run
— 只读模式开关
— 支持 NO_COLOR
✓ 自省/Schema 发现
— 官方 SKILL.md

当前为静态评估(本机尚未安装或尚未运行探针)。可在本地运行 npm run data:probe 并提交 data/probes/ 升级为实测证据。

FCRS-1.0 六维评分拆解

Agent 友好度 19 / 25

支持 --dry-run、-Werror 与 --output-replacements-xml,但缺少原生 --json 输出。

  • --dry-run -Werror gives deterministic pass/fail exit codes
  • --output-replacements-xml emits machine-parseable XML hunks instead of JSON
安全与权限边界 23 / 25

默认只读输出到 stdout;仅在显式传入 -i 时才会写入磁盘。

  • Non-destructive default stdout mode and explicit --dry-run flag
安装与运行摩擦 14 / 15

原生 C++ 独立二进制,可通过 Homebrew、Linux 包管理器、PyPI 与 LLVM Releases 跨平台安装。

  • Cross-platform native C++ binary on macOS, Linux, and Windows
能力覆盖与边际价值 13 / 15

C/C++/Protobuf 生态事实标准格式化器,.clang-format 配置解析稳定确定。

  • Supports multiple C-family and IDL languages from one binary
维护与来源背书 10 / 10

由 LLVM 基金会在 llvm/llvm-project 中持续官方维护。

  • Part of official llvm/llvm-project toolchain
文档与可验证性 8 / 10

clang.llvm.org 提供详尽的选项与 .clang-format 样式参考。

  • Official documentation at clang.llvm.org/docs/ClangFormat.html

安全边界、鉴权与放权指南

鉴权方式
无需鉴权
凭证存储位置

纯本地源码格式化,不存储也不需要任何凭证。

潜在副作用
只读查询 本地写入
费用与配额提示

基于 Apache-2.0 with LLVM Exception 开源协议,完全免费。

可复制到 CLAUDE.md / AGENTS.md 的安全护栏
# clang-format Agent Rules
- Always verify with `clang-format --dry-run -Werror` before running `clang-format -i`.
- Scope `-i` to edited files rather than globbing the entire repo unless requested.

Agent 高频安全命令配方

只读查询(安全推荐)

以非零退出码执行格式化预检
clang-format --dry-run -Werror src/*.cpp include/*.h

向 stderr 报告未格式化位置并以非零码退出,不修改任何文件。

输出 XML 格式的替换片段
clang-format --output-replacements-xml src/main.cpp

输出结构化 XML 字节偏移替换块,不改写磁盘文件。

受控写入与预演

原地格式化指定文件
clang-format -i src/main.cpp

依据仓库 .clang-format 配置仅原地格式化目标文件。

核心优势

  • + --dry-run -Werror 可在不修改文件的前提下可靠检测格式偏差。
  • + 跨 C、C++、Objective-C、Java 与 Protobuf 的 .clang-format 配置继承高度一致。

注意事项与踩坑点

  • ! 不支持 --json 输出,程序化解析需依赖 --output-replacements-xml 或 diff。
  • ! 不同 LLVM 大版本的格式化细节可能存在微小差异。

已验证兼容的 Agent 宿主

Claude Code Codex CLI Gemini CLI