Clang tools Format, lint, analyze C/C++
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
clang-format -i <file>
clang-format --style=llvm --dump-config >
.clang-format
git clang-format
clang --analyze <file>
clang-tidy -p build <file>
clang-tidy --fix -p build <file>
clang -Weverything <file>
clang -ftime-trace -c <file>
Concepts
- CMake, Meson
- compile_commands.json
- clang-tidy, clangd
- warnings + fixes
clang-format
clang-format <file>
clang-format -i <a.cpp> <b.h>
clang-format --style=google -i <file>
clang-format --dry-run --Werror <file>
clang-format --lines=<start>:<end> -i
<file>
Style files
clang-format --style=llvm --dump-config
clang-format --dump-config <file>
BasedOnStyle: LLVMIndentWidth: 4ColumnLimit: 100---Language: ProtoDisableFormat: true
// clang-format off
.clang-format-ignore
Format only changes
git clang-format
git clang-format <branch>
git clang-format --diff
git clang-format --staged
clang-tidy
clang-tidy -p <build> <file>
clang-tidy <file> -- <compiler flags>
clang-tidy --checks='-*,bugprone-*'
<file>
clang-tidy --fix -p <build> <file>
clang-tidy --list-checks --checks='*'
clang-tidy --dump-config
clang-tidy --header-filter='<regex>'
<file>
clang-tidy --warnings-as-errors='*'
<file>
run-clang-tidy -p <build> -j 8
Check groups
| Prefix | Looks for |
|---|---|
bugprone- |
Likely bugs |
modernize- |
Newer idioms |
performance- |
Wasted copies |
readability- |
Clarity |
cppcoreguidelines- |
Guidelines |
clang-analyzer- |
Analyzer |
Configure and silence tidy
Checks: '-*,bugprone-*,performance-*'WarningsAsErrors: 'bugprone-*'HeaderFilterRegex: 'src/.*'CheckOptions: <check>.<Option>: <value>
// NOLINT// NOLINT(<check>)
// NOLINTNEXTLINE(<check>)
// NOLINTBEGIN(<check>)
clangd
CompileFlags: CompilationDatabase: build Add: [-Wall] Remove: [-fno-*]Diagnostics: UnusedIncludes: Strict ClangTidy: Add: [bugprone-*]
clangd --check=<file>
Clang-only warnings
clang -Weverything <file>
clang -fsyntax-only <file>
clang -fcolor-diagnostics <file>
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wunused-parameter"/* ... */#pragma clang diagnostic pop
Sanitizers and coverage
clang -fsanitize=memory -g <file>
clang -fprofile-instr-generate -fcoverage-mapping
<file>
llvm-cov report <app>
-instr-profile=<p.profdata>
Static analyzer
clang --analyze <file>
clang --analyze --analyzer-output text <file>
scan-build makescan-build -o <dir> -V make
Compile-time insight
clang -ftime-trace -c <file>
clang -O2 -Rpass=inline <file>
clang -O2 -Rpass-missed=loop-vectorize <file>
clang -### <file>
clang -Xclang -ast-dump -fsyntax-only <file>
clang -S -emit-llvm <file>
Toolchain choices
clang -fuse-ld=lld <file>
clang++ -stdlib=libc++ <file>
clang --target=<triple> <file>
CC=clang CXX=clang++ cmake -B build
Recipes
git ls-files '*.c' '*.h' '*.cpp' '*.hpp' \ | xargs clang-format --dry-run --Werror
export CMAKE_EXPORT_COMPILE_COMMANDS=1cmake -B buildrun-clang-tidy -p build -j 8 -quiet# apply fixesrun-clang-tidy -p build -fix -checks='-*,modernize-*'
CXX=clang++ CXXFLAGS=-ftime-trace cmake -B buildcmake --build build# open build/**/*.json in ui.perfetto.dev



