Search code examples
c++clang-tidy

Clang Tidy config format


At the moment I am using the Clang Format utility in my project. In order to share its settings in my team, I put the .clang-format configuration file in the root of the project folder, and now IDE automatically loads it when working with the project. In the same way, I want to use the Clang Tidy utility. However, unlike Clang Format, I cannot find a description of the configuration file format or a utility to create it. I need IDE to also automatically load these settings and take them into account in autoformatting, so it's not possible for me to run the utility using a script that will pass it the necessary parameters. Is there a way to achieve what I need?


Solution

  • .clang-tidy file format is actually specified in the command-line help, see the documentation.

    --config=<string>              -
                                       Specifies a configuration in YAML/JSON format:
                                         -config="{Checks: '*',
                                                   CheckOptions: [{key: x,
                                                                   value: y}]}"
                                       When the value is empty, clang-tidy will
                                       attempt 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 custom config file:
                                        e.g. --config-file=/some/path/myTidyConfigFile
                                      This option internally works exactly the same way as
                                        --config option after reading specified config file.
                                      Use either --config-file or --config, not both.
    

    All you need to do is to put the config string in a file and you're good to go. If you don't specify the --config-file option it will automatically search for a .clang-tidy file in the directory where the checked code is.

    An example .clang-tidy file:

    Checks: '-*,bugprone-*'
    CheckOptions:
        - key: bugprone-argument-comment.StrictMode
          value: 1
        - key: bugprone-exception-escape.FunctionsThatShouldNotThrow
          value: WinMain,SDL_main
    FormatStyle: 'file'
    

    This would run all bugprone checks and set options for two of them.