$npx -y skills add mohitmishra786/low-level-dev-skills --skill clangClang/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-
| 1 | # Clang |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "I want better compiler diagnostics/errors" |
| 10 | - "How do I use clang-tidy / clang-format?" |
| 11 | - "How do I see what the compiler optimised or didn't?" |
| 12 | - "I'm on macOS / FreeBSD using clang" |
| 13 | - "clang-cl for MSVC-compatible builds" — see `skills/compilers/msvc-cl` |
| 14 | - Sanitizer queries — see `skills/runtimes/sanitizers` |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Build mode flags (identical to GCC) |
| 19 | |
| 20 | Clang accepts most GCC flags. Key differences: |
| 21 | |
| 22 | | Feature | GCC | Clang | |
| 23 | |---------|-----|-------| |
| 24 | | Min size | `-Os` | `-Os` or `-Oz` (more aggressive) | |
| 25 | | Optimise only hot | — | `-fprofile-instr-use` (LLVM PGO) | |
| 26 | | Thin LTO | `-flto` | `-flto=thin` (faster) | |
| 27 | | Static analyser | `-fanalyzer` | `clang --analyze` or `clang-tidy` | |
| 28 | |
| 29 | ### 2. Clang-specific diagnostic flags |
| 30 | |
| 31 | ```bash |
| 32 | # Show fix-it hints inline |
| 33 | clang -Wall -Wextra --show-fixits src.c |
| 34 | |
| 35 | # Limit error count |
| 36 | clang -ferror-limit=5 src.c |
| 37 | |
| 38 | # Verbose template errors (disable elision) |
| 39 | clang -fno-elide-type src.cpp |
| 40 | |
| 41 | # Show tree diff for template mismatch |
| 42 | clang -fdiagnostics-show-template-tree src.cpp |
| 43 | ``` |
| 44 | |
| 45 | Clang's diagnostics include exact range highlighting and fix-it suggestions that GCC lacks. |
| 46 | |
| 47 | ### 3. Optimization remarks |
| 48 | |
| 49 | Optimization remarks let you see what Clang did or refused to do: |
| 50 | |
| 51 | ```bash |
| 52 | # Inliner decisions |
| 53 | clang -O2 -Rpass=inline src.c |
| 54 | |
| 55 | # Missed vectorisation |
| 56 | clang -O2 -Rpass-missed=loop-vectorize src.c |
| 57 | |
| 58 | # Why a loop was not vectorized |
| 59 | clang -O2 -Rpass-analysis=loop-vectorize src.c |
| 60 | |
| 61 | # Save all remarks to YAML for post-processing |
| 62 | clang -O2 -fsave-optimization-record src.c |
| 63 | # Produces src.opt.yaml |
| 64 | ``` |
| 65 | |
| 66 | Interpret remarks: |
| 67 | |
| 68 | - `remark: foo inlined into bar` — inlining happened; good for hot paths |
| 69 | - `remark: loop not vectorized: loop control flow is not understood` — restructure the loop |
| 70 | - `remark: not vectorized: cannot prove it is safe to reorder...` — add `__restrict__` or `#pragma clang loop vectorize(assume_safety)` |
| 71 | |
| 72 | ### 4. Static analysis |
| 73 | |
| 74 | ```bash |
| 75 | # Built-in analyser (CSA) |
| 76 | clang --analyze -Xanalyzer -analyzer-output=text src.c |
| 77 | |
| 78 | # clang-tidy (separate tool, richer checks) |
| 79 | clang-tidy src.c -- -std=c++17 -I/usr/include |
| 80 | |
| 81 | # Enable specific check families |
| 82 | clang-tidy -checks='clang-analyzer-*,modernize-*,bugprone-*' src.cpp -- |
| 83 | |
| 84 | # Apply fixits automatically |
| 85 | clang-tidy -fix src.cpp -- |
| 86 | ``` |
| 87 | |
| 88 | Common `clang-tidy` check families: |
| 89 | |
| 90 | - `bugprone-*`: real bugs (use-after-move, dangling, etc.) |
| 91 | - `clang-analyzer-*`: CSA checks (memory, null deref) |
| 92 | - `modernize-*`: C++11/14/17 modernisation |
| 93 | - `performance-*`: unnecessary copies, move candidates |
| 94 | - `readability-*`: naming, complexity |
| 95 | |
| 96 | ### 5. LTO with lld |
| 97 | |
| 98 | ```bash |
| 99 | # Full LTO |
| 100 | clang -O2 -flto -fuse-ld=lld src.c -o prog |
| 101 | |
| 102 | # Thin LTO (faster link, nearly same quality) |
| 103 | clang -O2 -flto=thin -fuse-ld=lld src.c -o prog |
| 104 | |
| 105 | # Check lld is available |
| 106 | clang -fuse-ld=lld -Wl,--version 2>&1 | head -1 |
| 107 | ``` |
| 108 | |
| 109 | For large projects, ThinLTO is preferred: link times 5-10x faster than full LTO with comparable code quality. |
| 110 | |
| 111 | ### 6. PGO (LLVM instrumentation) |
| 112 | |
| 113 | ```bash |
| 114 | # Step 1: instrument |
| 115 | clang -O2 -fprofile-instr-generate prog.c -o prog_inst |
| 116 | |
| 117 | # Step 2: run with representative input |
| 118 | ./prog_inst < workload.input |
| 119 | # Generates default.profraw |
| 120 | |
| 121 | # Step 3: merge profiles |
| 122 | llvm-profdata merge -output=prog.profdata default.profraw |
| 123 | |
| 124 | # Step 4: use profile |
| 125 | clang -O2 -fprofile-instr-use=prog.profdata prog.c -o prog |
| 126 | ``` |
| 127 | |
| 128 | AutoFDO (sampling-based, less intrusive): collect with `perf`, convert with `create_llvm_prof`, use with `-fprofile-sample-use`. See `skills/profilers/linux-perf`. |
| 129 | |
| 130 | ### 7. GCC compatibility |
| 131 | |
| 132 | Clang is intentionally GCC-compatible for driver flags. Key differences: |
| 133 | |
| 134 | - Clang does not support all GCC-specific attributes; check with `__has_attribute(foo)` |
| 135 | - `-Weverything` enables all Clang warnings (no GCC equivalent); too noisy for production, useful for one-off audits |
| 136 | - Some GCC intrinsics need `#include <x86intrin.h>` on Clang too |
| 137 | - `__int128` is supported; `__float128` requires `-lquadmath` on some targets |
| 138 | |
| 139 | ### 8. macOS specifics |
| 140 | |
| 141 | On macOS, `clang` is the system compiler (Apple LLVM). Key points: |
| 142 | |
| 143 | - `ld64` is the default linker; `lld` requires explicit `-fuse-ld=lld` and Homebrew LLVM |
| 144 | - Use `-mmacosx-version-min=X.Y` to set deployment target |
| 145 | - Sanitizers on macOS use `DYLD_INSERT_LIBRARIES`; do not strip the binary |
| 146 | - `xcrun clang` resolves to the Xcode toolchain c |