clang
Original:🇺🇸 English
Translated
Clang/LLVM compiler skill for C/C++ projects. Use when working with clang or clang++ for diagnostics, sanitizer instrumentation, optimization remarks, static analysis with clang-tidy, LTO via lld, or when migrating from GCC to Clang. Activates on queries about clang flags, clang-tidy, clang-format, better error messages, Apple/FreeBSD toolchains, or LLVM-specific optimizations. Covers flag selection, diagnostic tuning, and integration with LLVM tooling.
3installs
Added on
NPX Install
npx skill4agent add mohitmishra786/low-level-dev-skills clangTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →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.
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
Workflow
1. Build mode flags (identical to GCC)
Clang accepts most GCC flags. Key differences:
| Feature | GCC | Clang |
|---|---|---|
| Min size | | |
| Optimise only hot | — | |
| Thin LTO | | |
| Static analyser | | |
2. Clang-specific diagnostic flags
bash
# Show fix-it hints inline
clang -Wall -Wextra --show-fixits src.c
# Limit error count
clang -ferror-limit=5 src.c
# Verbose template errors (disable elision)
clang -fno-elide-type src.cpp
# Show tree diff for template mismatch
clang -fdiagnostics-show-template-tree src.cppClang's diagnostics include exact range highlighting and fix-it suggestions that GCC lacks.
3. Optimization remarks
Optimization remarks let you see what Clang did or refused to do:
bash
# Inliner decisions
clang -O2 -Rpass=inline src.c
# Missed vectorisation
clang -O2 -Rpass-missed=loop-vectorize src.c
# Why a loop was not vectorized
clang -O2 -Rpass-analysis=loop-vectorize src.c
# Save all remarks to YAML for post-processing
clang -O2 -fsave-optimization-record src.c
# Produces src.opt.yamlInterpret remarks:
- — inlining happened; good for hot paths
remark: foo inlined into bar - — restructure the loop
remark: loop not vectorized: loop control flow is not understood - — add
remark: not vectorized: cannot prove it is safe to reorder...or__restrict__#pragma clang loop vectorize(assume_safety)
4. Static analysis
bash
# Built-in analyser (CSA)
clang --analyze -Xanalyzer -analyzer-output=text src.c
# clang-tidy (separate tool, richer checks)
clang-tidy src.c -- -std=c++17 -I/usr/include
# Enable specific check families
clang-tidy -checks='clang-analyzer-*,modernize-*,bugprone-*' src.cpp --
# Apply fixits automatically
clang-tidy -fix src.cpp --Common check families:
clang-tidy- : real bugs (use-after-move, dangling, etc.)
bugprone-* - : CSA checks (memory, null deref)
clang-analyzer-* - : C++11/14/17 modernisation
modernize-* - : unnecessary copies, move candidates
performance-* - : naming, complexity
readability-*
5. LTO with lld
bash
# Full LTO
clang -O2 -flto -fuse-ld=lld src.c -o prog
# Thin LTO (faster link, nearly same quality)
clang -O2 -flto=thin -fuse-ld=lld src.c -o prog
# Check lld is available
clang -fuse-ld=lld -Wl,--version 2>&1 | head -1For large projects, ThinLTO is preferred: link times 5-10x faster than full LTO with comparable code quality.
6. PGO (LLVM instrumentation)
bash
# Step 1: instrument
clang -O2 -fprofile-instr-generate prog.c -o prog_inst
# Step 2: run with representative input
./prog_inst < workload.input
# Generates default.profraw
# Step 3: merge profiles
llvm-profdata merge -output=prog.profdata default.profraw
# Step 4: use profile
clang -O2 -fprofile-instr-use=prog.profdata prog.c -o progAutoFDO (sampling-based, less intrusive): collect with , convert with , use with . See .
perfcreate_llvm_prof-fprofile-sample-useskills/profilers/linux-perf7. GCC compatibility
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
8. macOS specifics
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.
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