plugins/languages/c/skills/core/SKILL.md
C language core conventions covering C11/C17/C23 standard features, coding style, build systems (CMake 3.30+, Meson), and static analysis (clang-tidy, cppcheck, scan-build). Use when writing, reviewing, refactoring, or debugging any C source. Also triggers on "C 标准", "C11", "C17", "C23 特性", "CMake C", "constexpr C", "nullptr C", "_BitInt", "#embed", "[[nodiscard]]", "clang-tidy 配置", "C 编译警告".
npx skillsauth add lazygophers/ccplugin c-coreInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
应作为所有 C 任务(开发 / 测试 / 调试 / 优化)的标准基线。其它 C skill 在本文之上做领域细化。
| 主题 | 跳转 |
|------|------|
| 内存分配 / 泄漏 / 对齐 | c-memory |
| 错误处理 / errno / 安全字符串 | c-error |
| 并发 / 原子 / 线程 | c-concurrency |
| POSIX 系统调用 / 网络 | c-posix |
| 嵌入式 / MISRA / volatile | c-embedded |
-std=c17;使用 C23 特性时通过 __STDC_VERSION__ >= 202311L 条件编译保护。-Wall -Wextra -Werror -pedantic -Wshadow -Wconversion -Wdouble-promotion -Wformat=2。malloc/calloc/realloc 以及系统调用 / 库函数返回值。const;显式 cast 必须有注释解释原因。goto cleanup 统一释放(参考 c-error)。strcpy / sprintf / strcat / gets;使用 snprintf / strncpy + 手动 NUL / memcpy。.c 和 .h 单文件 ≤ 600 行(推荐 200–400)。| 特性 | 关键字 / 语法 | 典型用途 |
|------|-------------|----------|
| 泛型选择 | _Generic | 类型安全宏 |
| 静态断言 | _Static_assert / static_assert | 编译期不变式 |
| 匿名结构 / 联合 | 无名 struct/union 成员 | 简化嵌套访问 |
| 对齐控制 | _Alignas / _Alignof | 缓存行 / SIMD |
| 无返回 | _Noreturn | abort 类函数 |
| 线程局部 | _Thread_local | 线程私有状态 |
| 原子操作 | _Atomic / <stdatomic.h> | 无锁并发(见 c-concurrency) |
-std=c23)| 特性 | 语法 | 编译器(最早稳定) |
|------|------|-------------------|
| nullptr / nullptr_t | if (p == nullptr) | GCC 13 / Clang 16 |
| constexpr 对象与函数 | constexpr int sq(int x){return x*x;} | GCC 14 / Clang 17 |
| typeof / typeof_unqual | typeof(expr) tmp; | GCC 13 / Clang 16 |
| auto 类型推导(语义变更) | auto x = 1;(旧 K&R 风格须写显式类型) | GCC 15 / Clang 18 |
| _BitInt(N) | _BitInt(7) val; | GCC 14 / Clang 16 |
| 二进制字面量 + 数字分隔符 | 0b1010'0011 | GCC 14 / Clang 16 |
| #embed | #embed "data.bin" | GCC 15 / Clang 19 |
| 标准属性 | [[nodiscard]] [[maybe_unused]] [[deprecated]] [[noreturn]] [[fallthrough]] | GCC 14 / Clang 16 |
| VLA {} 零初始化 | int arr[n] = {}; | GCC 14 / Clang 16 |
C2y(下一版)追踪:参见 open-std.org WG14。生产代码不依赖 C2y 草案特性。
// _Generic 类型分发
#define abs_val(x) _Generic((x), \
int: abs, long: labs, float: fabsf, double: fabs)(x)
// 编译期不变式
static_assert(sizeof(int) >= 4, "need >=32-bit int");
static_assert(_Alignof(double) == 8, "double alignment");
// C23 属性带条件保护
#if __STDC_VERSION__ >= 202311L
[[nodiscard]] int compute(int x);
[[maybe_unused]] static void helper(void) { }
#else
int compute(int x);
static void helper(void) { }
#endif
cmake_minimum_required(VERSION 3.30)
project(myproject C)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_compile_options(
-Wall -Wextra -Werror -pedantic
-Wshadow -Wconversion -Wdouble-promotion -Wformat=2
)
option(ENABLE_SANITIZERS "Enable ASan+UBSan" OFF)
if(ENABLE_SANITIZERS)
add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer -g -O1)
add_link_options(-fsanitize=address,undefined)
endif()
# clang-tidy(项目根放 .clang-tidy 控制规则集)
clang-tidy src/*.c -- -std=c17
# cppcheck
cppcheck --enable=all --inline-suppr --std=c17 --error-exitcode=1 src/
# Clang scan-build(路径敏感)
scan-build -enable-checker security,unix make
.clang-tidy 起步规则集:bugprone-*, cert-*, clang-analyzer-*, misc-*, performance-*, portability-*, readability-*。
-D_FORTIFY_SOURCE=3 -fstack-protector-strong -fstack-clash-protection
-fcf-protection=full -fPIE -pie
-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack
-std=c17,C23 特性有条件保护-Werror 通过-Wconversion 无警告)strcpy / sprintf / gets / strcat)clang-tidy 与 cppcheck 零警告compile_commands.json 已导出c-status)development
Go 数据库规范——GORM Model 命名 ModelXxx、表名单数、枚举 uint8 + 常量、索引 idx_ 前缀 + deleted_at leading column、禁 time.Time 统一 int64 unix、禁指针/nullable 字段、TEXT/BLOB/JSON 禁 default、AutoMigrate 禁改主键。设计 DB model、写 GORM tag、建索引、做 migration 审查时触发。
development
Go HTTP API 规范——响应始终 200 + body code 字段、路由 /api/* 全 POST 单段 <Action><Model>、中间件逐路由注册禁 Group(prefix,mw...)、handler 仅返回 (rsp,error)、认证走 header。设计 HTTP API、写路由/handler/中间件时触发。
development
Go 项目结构规范——三层架构(API → Impl → State)、全局状态模式、internal/ 私有包、cmd/ 仅 main.go、go.work 多模块、禁止 Repository 接口和 DI 容器、struct 公共字段开头全 omitempty、handler var rsp 顶声明、禁 legacy migration。设计项目骨架、新建目录、组织包、做架构评审时触发。
development
Go 命名规范——Id/Uid 字段(非 ID)、IsActive/HasMFA 布尔前缀、CreatedAt 时间字段、接收者统一用 p、包名全小写无下划线、泛型类型参数描述性命名、集合字段 xxx_list 禁 xxxs 复数、Enum 0 值 XxxNil 禁 Unknown、禁 Status 统一 State、Set/Update 语义区分。定义结构体字段、函数、变量、包、接收者名、泛型、枚举时触发。