0

I'm using clang-tidy in my C++ project, and I'm encountering errors related to third-party dependencies, specifically Abseil (absl). I want to skip checking these dependencies.

jiawei@DESKTOP-AEQS7B5:~/Development/OSM2GMNS/OSM2GMNS$ pre-commit run
trim trailing whitespace.................................................Passed
check for added large files..............................................Passed
check yaml...........................................(no files to check)Skipped
clang-format.............................................................Passed
clang-tidy...............................................................Failed
- hook id: clang-tidy
- exit code: 1

b'as error\n'
/home/jiawei/Development/OSM2GMNS/OSM2GMNS/cmake-build-debug/_deps/absl-src/absl/container/internal/raw_hash_set.h:520:44: error: The value '0' provided to the cast expression is not in the valid range of values for 'ctrl_t' [clang-analyzer-optin.core.EnumCastOutOfRange,-warnings-as-errors]
  520 | inline bool IsFull(ctrl_t c) { return c >= static_cast<ctrl_t>(0); }

Here's my current setup:

CMakeLists.txt

cmakeCopyFetchContent_Declare(
    absl
    GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
    GIT_TAG 20230125.3
    SYSTEM
)

.pre-commit-config.yaml:

fail_fast: false
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.5.0
    hooks:
      - id: trailing-whitespace
      - id: check-added-large-files
      - id: check-yaml
        exclude: ".clang-*"
  - repo: https://github.com/jiawlu/pre-commit-hooks
    rev: 49cddc161d4fe41ee9f16922f2c0e634b0a115ff
    hooks:
      - id: clang-format
        args: [--style=file, -i]
      - id: clang-tidy
        args: [-p=./cmake-build-debug, --fix-errors]

.clang-tidy

Checks: '*,-=,-fuchsia-*,-zircon-*,-modernize-use-trailing-return-type,-hicpp-use-emplace,-modernize-use-emplace,-llvm-*,-llvmlibc-*,-readability-function-cognitive-complexity,-altera-*,-openmp-exception-escape,-cppcoreguidelines-owning-memory,-bugprone-easily-swappable-parameters,-bugprone-branch-clone'
WarningsAsErrors: '*'
HeaderFilterRegex: 'src/.*'
FormatStyle: 'file'

versions

clang --version
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang-tidy --version
Ubuntu LLVM version 18.1.3
  Optimized build
pre-commit --version
pre-commit 3.6.2

Despite using the SYSTEM flag in FetchContent_Declare and setting HeaderFilterRegex to 'src/.*', clang-tidy is still checking the Abseil headers and reporting errors. How can I configure clang-tidy to skip checking third-party dependencies like Abseil while still analyzing my own code?

1 Answer 1

-1

pre-commit works by passing checked-in files to hooks

as you've configured clang-tidy:

      - id: clang-tidy
        args: [-p=./cmake-build-debug, --fix-errors]

you are using the default filtering options:

    types_or: [c, c++, c#, objective-c]

this means that clang-tidy will be invoked (as per the docs)

clang-tidy -p=./cmake-build-debug --fix-errors filename filename filename

where each filename is one which matches types_or: [...] from above

in order to not pass your vendored files you can use one of the file filtering options (probably exclude in your case) such as:

      - id: clang-tidy
        args: [-p=./cmake-build-debug, --fix-errors]
        exclude: ^cmake-build-debug/_deps/

though it's strange that you've checked in your _deps in the first place -- usually that's .gitignored. you could also use a top-level exclude:

exclude: ^cmake-build-debug/_deps/

disclaimer: I wrote pre-commit

Sign up to request clarification or add additional context in comments.

4 Comments

the error still exists, no matter I add exclude: ^cmake-build-debug/_deps/ under - id: clang-tidy or at the top level. the code I shared in my original post actually works fine on my mac machine with clang/clang-tidy 17.0.6 and pre-commit 3.7.0 installed
what's the file path relative to your repository root?
.pre-commit-config.yaml and .clang-tidy are in my repo root
It works fine after I downgrade clang/clang-tidy to version 17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.