CLANG-TIDY(1)

πŸ“¦ version 12.0.1, in ubuntu jammy

πŸ’– clang-tidy πŸ’–

hey bestie 🫢 this is the manual page for clang-tidy 12 β€” section 1, User Commands, she's a whole icon πŸ‘‘

🌸 NAME 🌸

clang-tidy β€” the manual page for clang-tidy 12. that's it, that's the name, periodt ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's how you actually call her:

USAGE: clang-tidy [options] <source0> [... <sourceN>]

and then, the OPTIONS πŸ’…

⭐ Generic Options ⭐

the universal stuff, the basics, the starter pack:

  • --help β€” shows you all the available options. (and --help-hidden for even more πŸ‘€)
  • --help-list β€” shows you a list of the available options. (and --help-list-hidden for even more πŸ‘€)
  • --version β€” prints the version of this program. that's it, that's the whole job πŸ’

πŸŽ€ clang-tidy options πŸŽ€

the main character section, no cap:

  • --checks=<string> β€” a comma-separated list of globs, each with an optional - prefix. globs get processed in order of appearance in the list. globs without a - prefix add checks with matching names to the set, and globs with the - prefix remove checks with matching names from the set of enabled checks. this option's value gets appended to the value of the Checks option in the .clang-tidy file, if there is one. πŸ’–
  • --config=<string> β€” specifies a configuration in YAML/JSON format:
-config="{Checks: '*',
CheckOptions: [{key: x,

value: y}]}"

when the value is empty, clang-tidy will try to find a file named .clang-tidy for each source file in its parent directories. πŸ”

  • --config-file=<string> β€” specify the path of .clang-tidy or a custom config file, like:
e.g. --config-file=/some/path/myTidyConfigFile

internally this option works exactly the same way as the --config option after reading the specified config file. use either --config-file or --config, not both. 🚫

  • --dump-config β€” dumps the configuration in YAML format to stdout. you can use this option along with a file name (and -- if the file is outside of a project with a configured compilation database). the configuration used for this file will be printed. use it along with -checks=* to include the configuration of all checks. πŸ“‹
  • --enable-check-profile β€” enables per-check timing profiles, and prints a report to stderr. ⏱️
  • --explain-config β€” for each enabled check, explains where it's enabled, i.e. in the clang-tidy binary, the command line, or a specific configuration file. πŸ•΅οΈ
  • --export-fixes=<filename> β€” a YAML file to store suggested fixes in. the stored fixes can be applied to the input source code with clang-apply-replacements. πŸ”§
  • --extra-arg=<string> β€” an additional argument to append to the compiler command line. βž•
  • --extra-arg-before=<string> β€” an additional argument to prepend to the compiler command line. ⬆️
  • --fix β€” apply suggested fixes. without -fix-errors, clang-tidy will bail out if any compilation errors were found. 😭
  • --fix-errors β€” apply suggested fixes even if compilation errors were found. if compiler errors have attached fix-its, clang-tidy will apply them as well. πŸ’€
  • --format-style=<string> β€” style for formatting code around applied fixes:
    • 'none' (default) turns off formatting
    • 'file' (literally 'file', not a placeholder) uses the .clang-format file in the closest parent directory
    • '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}'
    • 'llvm', 'google', 'webkit', 'mozilla'

see the clang-format documentation for the up-to-date information about formatting styles and options. this option overrides the FormatStyle`` option in the .clang-tidy` file, if any. (yes that stray backtick is canon, i don't make the rules πŸ™ˆ)

  • --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. can be used together with -line-filter. this option overrides the HeaderFilterRegex option in the .clang-tidy file, if any. 🌈
  • --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:
[      {"name":"file1.cpp","lines":[[1,3],[5,7]]}, {"name":"file2.h"}

]
  • --list-checks β€” list all enabled checks and exit. use with -checks=* to list all available checks. πŸ“
  • -p=<string> β€” Build path. πŸ“
  • --quiet β€” run clang-tidy in quiet mode. this suppresses printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. 🀫
  • --store-check-profile=<prefix> β€” by default reports are printed in tabulated format to stderr. when this option is passed, these per-TU profiles are instead stored as JSON. πŸ“Š
  • --system-headers β€” display the errors from system headers. πŸ‘€
  • --use-color β€” use colors in diagnostics. if not set, colors will be used if the terminal connected to standard output supports colors. this option overrides the UseColor option in the .clang-tidy file, if any. 🎨
  • --vfsoverlay=<filename> β€” overlay the virtual filesystem described by file over the real file system. πŸͺž
  • --warnings-as-errors=<string> β€” upgrades warnings to errors. same format as -checks. this option's value is appended to the value of the WarningsAsErrors option in the .clang-tidy file, if any. 🚨

okay now the plot twist, bestie β€” -p <build-path> is used to read a compile command database. πŸ—‚οΈ

for example, it can be a CMake build directory in which a file named compile_commands.json exists (use the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON CMake option to get this output). when no build path is specified, a search for compile_commands.json will be attempted through all 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> ... specify the paths of source files. these paths are looked up in the compile command database. if the path of a file is absolute, it needs to point into CMake's source tree. if the path is relative, 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. "./" prefixes in the relative files will be automatically removed, but the rest of a relative path must be a suffix of a path in the compile command database. 🧚


πŸ’œ Configuration files: πŸ’œ

clang-tidy tries 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, the configuration file in the parent directory (if any exists) will be taken and the current config file will be applied on top of the parent one. if any configuration options have a corresponding command-line option, the command-line option takes precedence. the effective configuration can be inspected using -dump-config:

$ clang-tidy -dump-config
---

Checks:
       '-*,some-check'

WarningsAsErrors:
       ''

HeaderFilterRegex:
       ''

FormatStyle:
       none

       InheritParentConfig: true User:                user CheckOptions:

- key: some-check.SomeOption

value: 'some value'

       ...

and that's the whole page, bestie, you ate it πŸ’…βœ¨

this was clang-tidy 12, dated February 2022 β€” love her, respect her, run her on your codebase 🫢

πŸ“¦ where this page comes from