LinuxCommandLibrary

clang-tidy

Analyze C/C++ code for style and errors

TLDR

Run default checks on a source file

$ clang-tidy [path/to/file.cpp]
copy

Don't run any checks other than the cppcoreguidelines checks on a file
$ clang-tidy [path/to/file.cpp] -checks=[-*,cppcoreguidelines-*]
copy

List all available checks
$ clang-tidy -checks=[*] -list-checks
copy

Specify defines and includes as compilation options (after --)
$ clang-tidy [path/to/file.cpp] -- -I[my_project/include] -D[definitions]
copy

SYNOPSIS

clang-tidy [options...] <source0> [<sourceN>...] -- [<compile-args>]

PARAMETERS

-checks=glob
    Comma-separated list of globs for checks to run (e.g., modernize-*)

--config=string
    Configuration override as key:value pairs

--config-file=path
    Path to YAML/JSON config file

--dump-config
    Dump effective configuration to stdout

--explain-config
    Explain how config was derived

--export-fixes=file
    Export fixes to YAML file for clang-apply-replacements

--fix
    Apply suggested fixes to source code

--fix-errors
    Apply fixes even after compiler errors

--header-filter=regex
    Regular expression for headers to analyze (default: ignore)

--line-filter=file
    JSON file specifying line ranges to check

--list-checks[=category]
    List all or category-specific available checks

-p build-path or --
    Path to compilation database directory

--extra-arg=arg
    Additional argument to pass to compiler after inputs

--extra-arg-before=arg
    Additional argument to pass to compiler before inputs

--format-style=style
    Style for clang-format during fixes (e.g., file, LLVM)

--quiet
    Suppress non-check warnings

-v, --verbose
    Enable verbose output

--help, -h
    Print help message

--version
    Print version information

--system-headers
    Analyze system headers too

DESCRIPTION

Clang-tidy is a Clang-based tool for analyzing C and C++ code, providing extensive static analysis checks, linting, and automatic fixes. It integrates checks from the Clang Static Analyzer, LLVM, Google C++ style guide enforcer, Facebook Infer, and community contributions.

Designed for integration into build systems, it uses a compilation database (compile_commands.json) generated by tools like CMake to understand compile flags and apply context-aware checks. Clang-tidy detects issues in categories like bugs, performance, readability, modern C++ usage, and security.

With the -fix option, it applies transformations directly to source files. Fixes can be exported in YAML format for IDE integration. It supports custom configurations via YAML/JSON files, enabling project-specific check selections and settings. Commonly used in CI/CD pipelines alongside clang-format for code quality enforcement.

Clang-tidy enhances code reliability without runtime overhead, supporting C++11 to C++23 standards.

CAVEATS

Requires compilation database for accurate analysis; many checks are experimental (alpha); resource-intensive on large codebases; --fix may introduce regressions without review.

USAGE TIP

Generate compile_commands.json with CMake: cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON; run clang-tidy -p build/ src/*.cpp.

CHECK CATEGORIES

Core: bugprone-*, clang-analyzer-*, cppcoreguidelines-*, modernize-*, performance-*, readability-*.

HISTORY

Introduced in LLVM/Clang 3.5 (2014) as a modular extension to clang; rapidly expanded with contributions from Google, Facebook, and LLVM community; now includes 500+ checks, integral to modern C++ workflows.

SEE ALSO

clang(1), clang-format(1), clang-apply-replacements(1), cppcheck(1)

Copied to clipboard