CLANG-TIDY(1)
β¨ NAME β¨
clang-tidy β bestie, this is the manual page for clang-tidy 16 π
π¦ DESCRIPTION π¦
Okay so here's the whole vibe β how you actually run it:
USAGE: clang-tidy [options] <source0> [... <sourceN>]
And now, the OPTIONS, because that's the main character of this page π
πΈ Generic Options πΈ
--helpβ shows you the available options (and if you're nosy,--help-hiddenshows even more π)--help-listβ prints out the list of available options (again,--help-list-hiddenfor the extra secret ones)--versionβ tells you what version of this program you've got, no cap
π clang-tidy options π
--checks=<string>β a comma-separated list of globs, and any glob can have a-prefix. The globs get processed in the order they show up in the list. A glob with no-prefix adds checks whose names match it to the set; a glob with the-prefix removes matching checks from the set of enabled checks. Whatever you put here gets appended onto the value of theChecksoption in the.clang-tidyfile, if there is one.--config=<string>β specifies a config in YAML/JSON format, like:
-config="{Checks: '*', CheckOptions: {x: y}}"
And when the value is empty, clang-tidy will go hunting for a file named .clang-tidy for each source file, looking in its parent directories.
--config-file=<string>β gives the path of a.clang-tidyfile or a custom config file, e.g.--config-file=/some/path/myTidyConfigFile. Internally this works exactly the same way as--configafter it reads the config file you specified. Pick either--config-fileor--config, bestie, not both π ββοΈ--dump-configβ dumps the configuration in YAML format to stdout. You can use this alongside a file name (and--if the file lives outside a project that has a configured compilation database) and it'll print the config that was used for that file. Run it with-checks=*to also include the configuration of all the checks.--enable-check-profileβ turns on per-check timing profiles and prints a report to stderr, so you can see where the time is going β±οΈ--explain-configβ for every enabled check, explains where it got enabled β in the clang-tidy binary, on the command line, or in a specific configuration file.--export-fixes=<filename>β a YAML file to store the suggested fixes in. You can then apply those stored fixes to the input source code withclang-apply-replacements.--extra-arg=<string>β an extra argument to append to the compiler command line--extra-arg-before=<string>β an extra argument to prepend to the compiler command line--fixβ applies the suggested fixes. Heads up: without-fix-errors,clang-tidywill bail out if any compilation errors were found π¨--fix-errorsβ applies suggested fixes even if compilation errors were found. And if the compiler errors have fix-its attached to them,clang-tidyapplies those too.--fix-notesβ if a warning has no fix, but a single fix can be found through an associated diagnostic note, apply that fix. Fair warning: specifying this flag implicitly enables the--fixflag.--format-style=<string>β the style used for formatting code around applied fixes:'none'(that's the default) turns formatting off entirely'file'(literally the wordfile, it is not a placeholder) uses the.clang-formatfile in the closest parent directory'{ <json> }'specifies options inline, e.g.-format-style='{BasedOnStyle: llvm, IndentWidth: 8}''llvm','google','webkit','mozilla'β the classic named styles. See theclang-formatdocumentation for the up-to-date info about formatting styles and options. This option overrides theFormatStyleoption in the.clang-tidyfile, if there is one.
--header-filter=<string>β a regular expression matching the names of the headers to output diagnostics from. Diagnostics from the main file of each translation unit are always displayed, periodt. Can be used together with-line-filter. This option overrides theHeaderFilterRegexoption in the.clang-tidyfile, if there is one.--line-filter=<string>β a list of files with line ranges to filter the warnings. Can be used together with-header-filter. The format of the list is a JSON array of objects, like:
[ {"name":"file1.cpp","lines":[[1,3],[5,7]]}, {"name":"file2.h"} ]
--list-checksβ lists all the enabled checks and exits. Use it with-checks=*to list every check that's available.--load=<pluginfilename>β loads the plugin you point at-p <string>β build path--quietβ runsclang-tidyin quiet mode. This suppresses printing statistics about ignored warnings, and warnings that got treated as errors, if the respective options for those are specified π€«--store-check-profile=<prefix>β by default the reports get printed in tabulated format to stderr. When you pass this option, those per-TU profiles are stored as JSON instead.--system-headersβ displays the errors coming from system headers--use-colorβ uses colors in diagnostics. If it's not set, colors get used when the terminal connected to standard output supports colors. This option overrides theUseColoroption in the.clang-tidyfile, if there is one π--verify-configβ checks the config files to make sure every check and every option is actually recognized--vfsoverlay=<filename>β overlays the virtual filesystem described by that file on top of the real file system--warnings-as-errors=<string>β upgrades warnings to errors. Same format as-checks. This option's value gets appended onto the value of theWarningsAsErrorsoption in the.clang-tidyfile, if there is one.
Okay so now the β¨importantβ¨ stuff:
-p <build-path> is what you use to read a compile command database.
For example, it can be a CMake build directory that has a file named compile_commands.json in it (use the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CMake option to get that output). When no build path is specified, clang-tidy will attempt a search for compile_commands.json through all the parent paths of the first input file. See: https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an example of setting up Clang Tooling on a source tree.
<source0> ... is where you specify the paths of your source files. These paths get looked up in the compile command database. Here's the tea: if the path of a file is absolute, it needs to point into CMake's source tree. If the path is relative, then the current working directory needs to be in the CMake source tree, and the file must be in a subdirectory of the current working directory. Any "./" prefixes on relative files get automatically removed, but the rest of a relative path has to be a suffix of a path in the compile command database. Fr fr, that's the rule π«Ά
π Configuration files π
clang-tidy attempts to read configuration for each source file from a .clang-tidy file located in the closest parent directory of the source file. If InheritParentConfig is true in a config file, then the configuration file in the parent directory (if one exists) gets taken, and the current config file is applied on top of the parent one. And if any configuration options have a corresponding command-line option, the command-line option takes precedence. You can inspect the effective configuration using -dump-config:
$ clang-tidy -dump-config
---
Checks: '-*,some-check'
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
InheritParentConfig: true
User: user
CheckOptions:
some-check.SomeOption: 'some value'
...
And that's the whole page, bestie!! π This was clang-tidy 16, from October 2023 β slay, go forth and tidy your code, you've absolutely got this πβ¨
π¦ where this page comes from
- debian bookworm: clang-tidy-16 1:16.0.6-15~deb12u1 Β· APACHE-2-LLVM-EXCEPTIONS
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms