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] ... --

PARAMETERS

-checks=
    Comma-separated list of checks to enable or disable. Use '-' as prefix to disable.
For example: '-*,some-check' enables only 'some-check'.

-config=
    Specifies a configuration in YAML format.

-config-file=
    Path to the configuration file.

-export-fixes=
    Create a YAML file containing suggested fixes.

-header-filter=
    Regular expression matching the names of the headers to output diagnostics from. Diagnostics from the main file of each translation unit are always displayed.

-j
    Run specified number of clang-tidy instances in parallel.

-list-checks
    List all available checks.

-p
    Path used to read a compile command database.

-quiet
    Do not emit any output into standard error.

-fix
    Apply suggested fixes.

-use-color
    Use colors in diagnostics.

-version
    Display the version of clang-tidy.

DESCRIPTION

clang-tidy is a static analysis tool based on Clang that helps you automatically find and fix coding style issues, bugs, and potential performance problems in your C, C++, Objective-C, and Objective-C++ code. It uses a modular and extensible architecture, allowing for the creation of custom checks and integration with various build systems. It is designed to be easily integrated into existing development workflows. clang-tidy can detect a wide range of issues, from simple coding style violations to more complex problems like memory leaks, null pointer dereferences, and undefined behavior. Its primary goal is to improve code quality and maintainability by providing automated feedback and suggesting fixes. The checks performed by clang-tidy are configurable through a YAML file, allowing users to tailor the analysis to their specific needs and preferences. clang-tidy is particularly useful for large projects where manual code review is impractical or impossible for every change. It's a valuable tool for enforcing coding standards, identifying potential bugs early in the development cycle, and improving overall code quality.

CAVEATS

clang-tidy relies on a compile command database to correctly analyze code. Incorrect or incomplete compilation flags can lead to inaccurate results or false positives. Using '-fix' directly on large codebases without careful review can introduce unintended changes. Some checks may be computationally expensive, increasing analysis time.

CONFIGURATION

The behavior of clang-tidy can be customized using a YAML configuration file. This file allows you to enable or disable specific checks, configure their severity levels, and adjust their parameters. The file can be specified using the -config-file option, and its format is described in the Clang documentation. Checks are organized into groups, and wildcard patterns can be used to enable or disable multiple checks at once.

INTEGRATION WITH BUILD SYSTEMS

clang-tidy can be easily integrated into various build systems, such as CMake, Make, and Ninja. The compile command database, typically named compile_commands.json, is generated by the build system and provides clang-tidy with the necessary information to analyze the code. Some build systems have native support for running clang-tidy as part of the build process, allowing for continuous integration and automated code quality checks.

HISTORY

clang-tidy was developed as part of the LLVM project to provide a static analysis tool for C/C++ code. It evolved from earlier static analysis tools and integrated with the Clang compiler infrastructure. Its development has been driven by the need for automated code quality checks and style enforcement. Usage has increased with the growing adoption of coding standards and the need for improved code maintainability in large software projects.

SEE ALSO

Copied to clipboard