clang
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseClang
Clang
Purpose
用途
Guide agents through Clang-specific features: superior diagnostics, sanitizer integration, optimization remarks, static analysis, and LLVM tooling. Covers divergences from GCC and Apple/FreeBSD specifics.
指导使用者了解Clang专属特性:更出色的诊断功能、Sanitizer集成、优化备注、静态分析以及LLVM工具链。内容涵盖与GCC的差异,以及Apple/FreeBSD平台的专属细节。
Triggers
触发场景
- "I want better compiler diagnostics/errors"
- "How do I use clang-tidy / clang-format?"
- "How do I see what the compiler optimised or didn't?"
- "I'm on macOS / FreeBSD using clang"
- "clang-cl for MSVC-compatible builds" — see
skills/compilers/msvc-cl - Sanitizer queries — see
skills/runtimes/sanitizers
- "我想要更友好的编译器诊断/错误提示"
- "如何使用clang-tidy / clang-format?"
- "如何查看编译器优化了哪些内容,或是未优化哪些内容?"
- "我在macOS / FreeBSD上使用clang"
- "用于兼容MSVC构建的clang-cl" — 请查看
skills/compilers/msvc-cl - Sanitizer相关查询 — 请查看
skills/runtimes/sanitizers
Workflow
操作流程
1. Build mode flags (identical to GCC)
1. 构建模式选项(与GCC一致)
Clang accepts most GCC flags. Key differences:
| Feature | GCC | Clang |
|---|---|---|
| Min size | | |
| Optimise only hot | — | |
| Thin LTO | | |
| Static analyser | | |
Clang支持大多数GCC编译选项。核心差异如下:
| 特性 | GCC | Clang |
|---|---|---|
| 最小体积 | | |
| 仅优化热点代码 | — | |
| Thin LTO | | |
| 静态分析器 | | |
2. Clang-specific diagnostic flags
2. Clang专属诊断选项
bash
undefinedbash
undefinedShow fix-it hints inline
内联显示修复提示
clang -Wall -Wextra --show-fixits src.c
clang -Wall -Wextra --show-fixits src.c
Limit error count
限制错误数量
clang -ferror-limit=5 src.c
clang -ferror-limit=5 src.c
Verbose template errors (disable elision)
详细模板错误(禁用省略)
clang -fno-elide-type src.cpp
clang -fno-elide-type src.cpp
Show tree diff for template mismatch
显示模板不匹配的树形差异
clang -fdiagnostics-show-template-tree src.cpp
Clang's diagnostics include exact range highlighting and fix-it suggestions that GCC lacks.clang -fdiagnostics-show-template-tree src.cpp
Clang的诊断功能包含精确的范围高亮和GCC所没有的修复建议。3. Optimization remarks
3. 优化备注
Optimization remarks let you see what Clang did or refused to do:
bash
undefined优化备注可让你查看Clang执行了哪些优化,或是拒绝了哪些优化:
bash
undefinedInliner decisions
内联器决策
clang -O2 -Rpass=inline src.c
clang -O2 -Rpass=inline src.c
Missed vectorisation
未完成向量化的情况
clang -O2 -Rpass-missed=loop-vectorize src.c
clang -O2 -Rpass-missed=loop-vectorize src.c
Why a loop was not vectorized
循环未被向量化的原因
clang -O2 -Rpass-analysis=loop-vectorize src.c
clang -O2 -Rpass-analysis=loop-vectorize src.c
Save all remarks to YAML for post-processing
将所有备注保存为YAML以便后续处理
clang -O2 -fsave-optimization-record src.c
clang -O2 -fsave-optimization-record src.c
Produces src.opt.yaml
生成src.opt.yaml文件
Interpret remarks:
- `remark: foo inlined into bar` — inlining happened; good for hot paths
- `remark: loop not vectorized: loop control flow is not understood` — restructure the loop
- `remark: not vectorized: cannot prove it is safe to reorder...` — add `__restrict__` or `#pragma clang loop vectorize(assume_safety)`
解读优化备注:
- `remark: foo inlined into bar` — 已执行内联;适合热点路径
- `remark: loop not vectorized: loop control flow is not understood` — 需要重构循环
- `remark: not vectorized: cannot prove it is safe to reorder...` — 添加`__restrict__`或`#pragma clang loop vectorize(assume_safety)`4. Static analysis
4. 静态分析
bash
undefinedbash
undefinedBuilt-in analyser (CSA)
内置分析器(CSA)
clang --analyze -Xanalyzer -analyzer-output=text src.c
clang --analyze -Xanalyzer -analyzer-output=text src.c
clang-tidy (separate tool, richer checks)
clang-tidy(独立工具,检查功能更丰富)
clang-tidy src.c -- -std=c++17 -I/usr/include
clang-tidy src.c -- -std=c++17 -I/usr/include
Enable specific check families
启用特定检查家族
clang-tidy -checks='clang-analyzer-,modernize-,bugprone-*' src.cpp --
clang-tidy -checks='clang-analyzer-,modernize-,bugprone-*' src.cpp --
Apply fixits automatically
自动应用修复建议
clang-tidy -fix src.cpp --
Common `clang-tidy` check families:
- `bugprone-*`: real bugs (use-after-move, dangling, etc.)
- `clang-analyzer-*`: CSA checks (memory, null deref)
- `modernize-*`: C++11/14/17 modernisation
- `performance-*`: unnecessary copies, move candidates
- `readability-*`: naming, complexityclang-tidy -fix src.cpp --
常见的`clang-tidy`检查家族:
- `bugprone-*`: 真实bug检查(比如use-after-move、悬空指针等)
- `clang-analyzer-*`: CSA检查(内存问题、空指针解引用等)
- `modernize-*`: C++11/14/17语法现代化检查
- `performance-*`: 不必要的拷贝、可移动候选检查
- `readability-*`: 命名规范、代码复杂度检查5. LTO with lld
5. 借助lld实现LTO
bash
undefinedbash
undefinedFull LTO
完整LTO
clang -O2 -flto -fuse-ld=lld src.c -o prog
clang -O2 -flto -fuse-ld=lld src.c -o prog
Thin LTO (faster link, nearly same quality)
Thin LTO(链接速度更快,质量几乎一致)
clang -O2 -flto=thin -fuse-ld=lld src.c -o prog
clang -O2 -flto=thin -fuse-ld=lld src.c -o prog
Check lld is available
检查lld是否可用
clang -fuse-ld=lld -Wl,--version 2>&1 | head -1
For large projects, ThinLTO is preferred: link times 5-10x faster than full LTO with comparable code quality.clang -fuse-ld=lld -Wl,--version 2>&1 | head -1
对于大型项目,推荐使用ThinLTO:链接速度比完整LTO快5-10倍,同时代码质量相当。6. PGO (LLVM instrumentation)
6. PGO(LLVM插桩式)
bash
undefinedbash
undefinedStep 1: instrument
步骤1:生成插桩程序
clang -O2 -fprofile-instr-generate prog.c -o prog_inst
clang -O2 -fprofile-instr-generate prog.c -o prog_inst
Step 2: run with representative input
步骤2:使用代表性输入运行程序
./prog_inst < workload.input
./prog_inst < workload.input
Generates default.profraw
生成default.profraw文件
Step 3: merge profiles
步骤3:合并性能数据
llvm-profdata merge -output=prog.profdata default.profraw
llvm-profdata merge -output=prog.profdata default.profraw
Step 4: use profile
步骤4:使用性能数据编译
clang -O2 -fprofile-instr-use=prog.profdata prog.c -o prog
AutoFDO (sampling-based, less intrusive): collect with `perf`, convert with `create_llvm_prof`, use with `-fprofile-sample-use`. See `skills/profilers/linux-perf`.clang -O2 -fprofile-instr-use=prog.profdata prog.c -o prog
AutoFDO(基于采样,侵入性更低):使用`perf`采集数据,通过`create_llvm_prof`转换,配合`-fprofile-sample-use`使用。详情请查看`skills/profilers/linux-perf`。7. GCC compatibility
7. GCC兼容性
Clang is intentionally GCC-compatible for driver flags. Key differences:
- Clang does not support all GCC-specific attributes; check with
__has_attribute(foo) - enables all Clang warnings (no GCC equivalent); too noisy for production, useful for one-off audits
-Weverything - Some GCC intrinsics need on Clang too
#include <x86intrin.h> - is supported;
__int128requires__float128on some targets-lquadmath
Clang在驱动程序选项上刻意保持与GCC兼容。核心差异如下:
- Clang不支持所有GCC专属属性;可通过检查
__has_attribute(foo) - 启用所有Clang警告(GCC无对应选项);对于生产环境过于嘈杂,适合一次性审计
-Weverything - 部分GCC内建函数在Clang上也需要
#include <x86intrin.h> - 支持;
__int128在部分目标平台上需要__float128-lquadmath
8. macOS specifics
8. macOS平台专属细节
On macOS, is the system compiler (Apple LLVM). Key points:
clang- is the default linker;
ld64requires explicitlldand Homebrew LLVM-fuse-ld=lld - Use to set deployment target
-mmacosx-version-min=X.Y - Sanitizers on macOS use ; do not strip the binary
DYLD_INSERT_LIBRARIES - resolves to the Xcode toolchain clang
xcrun clang
For flag reference, see references/flags.md.
For clang-tidy config examples, see references/clang-tidy.md.
在macOS上,是系统默认编译器(Apple LLVM)。核心要点:
clang- 默认链接器是;使用
ld64需要显式添加lld,且需通过Homebrew安装LLVM-fuse-ld=lld - 使用设置部署目标版本
-mmacosx-version-min=X.Y - macOS上的Sanitizers使用;请勿剥离二进制文件
DYLD_INSERT_LIBRARIES - 会解析为Xcode工具链中的clang
xcrun clang
编译选项参考请查看references/flags.md。
clang-tidy配置示例请查看references/clang-tidy.md。
Related skills
相关技能
- Use for GCC-equivalent flag mapping
skills/compilers/gcc - Use for
skills/runtimes/sanitizersworkflows-fsanitize=* - Use for IR-level work (
skills/compilers/llvm,opt,llc)llvm-dis - Use for
skills/compilers/msvc-clon Windowsclang-cl - Use for linker-level LTO details
skills/binaries/linkers-lto
- 如需GCC等效选项映射,请使用
skills/compilers/gcc - 如需相关操作流程,请使用
-fsanitize=*skills/runtimes/sanitizers - 如需IR层面操作(、
opt、llc),请使用llvm-disskills/compilers/llvm - 如需Windows平台上的相关内容,请使用
clang-clskills/compilers/msvc-cl - 如需链接器层面的LTO细节,请使用
skills/binaries/linkers-lto