clang-tidy: Exclude specific dir from analysis
Asked Answered
T

2

9

I'm using clang-tidy in a medium-size project with the following three folders:

srcA
srcB
external

I'm trying to exclude external folder from the analysis, but with no luck. The command I'm using is:

clang-tidy $SRC -p build/ --extra-arg=-ferror-limit=0'

with

SRC=srcA/file.cpp srcA/fileN.cpp srcB/file.cpp srcB/fileN.cpp ...

and a compilation database under build/ generated by cmake. Note that SRC doesn't contain any external file, only from srcA and srcB (both .cpp and .hpp). Also, and obviusly, some files under srcA and srcB are using libraries under external.

The 80% of the errors from clang-tidy comes from external/ files, which I can't fix because there're third party libraries.

Below, the .clang-tidy file I'm using:

Checks: '-*,readability-identifier-naming'
WarningsAsErrors: "*"
CheckOptions:
  - { key: readability-identifier-naming.ClassCase, value: CamelCase }
  - { key: readability-identifier-naming.ClassMethodCase, value: camelBack }
  - { key: readability-identifier-naming.VariableCase, value: camelBack }
  - { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ }
  - { key: readability-identifier-naming.PrivateMemberCase, value: camelBack }
  - { key: readability-identifier-naming.FunctionCase, value: camelBack }
  - { key: readability-identifier-naming.MethodCase, value: camelBack }
  - { key: readability-identifier-naming.ParameterCase, value: camelBack }
  - { key: readability-identifier-naming.MemberCase, value: camelBack }
  - { key: readability-identifier-naming.EnumCase, value: CamelCase }
  - { key: readability-identifier-naming.StructCase, value: CamelCase }
  - { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
  - { key: readability-identifier-naming.TypeAliasCase, value: CamelCase }
  - { key: readability-identifier-naming.TypedefCase, value: CamelCase }
  - { key: readability-identifier-naming.ConstexprVariableCase, value: UPPER_CASE }
  - { key: readability-identifier-naming.ConstantCase, value: UPPER_CASE }
FormatStyle: 'file'

I know this question is already posted here, but I've tried the proposed solutions and none of them worked. For example, I've tried using HeaderFilterRegex, matching only the desired files, and didn't work.

Am I missing something? Is this even possible to achieve (I've read in some page that this is a known bug from clang-tidy)?

Tonguelash answered 7/11, 2022 at 15:59 Comment(3)
Have you try to put a "nul" (i.e Checks: '-*') .clang-tidy in external directory (to take precedence over the root one)?Wheatley
Didn't work @Jarod42, thanks anyway.Tonguelash
Hi all. I am facing a similar issue. I need to exclude my conan package folder which is /root/.conan/data folder. I can't implement the workaround mentioned there. I tried HeaderFilterRegex and --header-filter, so far none of the negation regex worked for me. Any ideas would be greatly appreciated.Noisome
S
8

I found the following workaround.

Project structure:

- project/.clang-tidy
- project/srcA/
- project/srcB/
- project/external/
- project/external/.clang-tidy

Top-level project/.clang-tidy:

# File: project/.clang-tidy

Checks: '-*,readability-identifier-naming'
WarningsAsErrors: "*"
#
# ...more configs here...
#

# IMPORTANT: Set HeaderFilterRegex as shown below.
# Do not set it to '.*', for example.
HeaderFilterRegex: ''

Then create a separate .clang-tidy file in the folder you want to ignore/exclude, with the following contents.

project/external/.clang-tidy:

# File: project/external/.clang-tidy

# Disable all checks in this folder.
Checks: '-*'

I then run clang-tidy with the command:

$ fd '\.(c|h)$' -X clang-tidy {} -p build/ --quiet
Settle answered 27/2, 2023 at 22:17 Comment(0)
E
1

I am afraid it's impossible at the moment. It seems the only possible solution: list all the allowed paths except external/ in HeaderFilterRegex in .clang-tidy.

See the discussion thread "clang-tidy Negative Lookahead Support".

Evertor answered 7/11, 2022 at 16:29 Comment(1)
I've tried that as well but didn't work either.Tonguelash

© 2022 - 2024 — McMap. All rights reserved.