You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置clang-tidy跳过Abseil等第三方依赖的检查?

问题

在C++项目中使用clang-tidy时,第三方依赖Abseil(absl)的头文件被检查并报错,希望跳过对这类依赖的检查,只分析自己的代码。

执行pre-commit run后的报错信息:

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); }

当前配置

CMakeLists.txt

FetchContent_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'

版本信息

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

尽管已在FetchContent_Declare中使用SYSTEM标志,且设置HeaderFilterRegex为src/.*,clang-tidy仍会检查Abseil头文件并报错,需调整配置实现跳过第三方依赖检查。


解决方案

可以通过以下步骤调整配置,让clang-tidy跳过第三方依赖:

  • 完善.clang-tidy的排除规则
    仅设置HeaderFilterRegex可能无法完全拦截依赖头文件的检查,添加ExcludeHeaderFilterRegex明确排除Abseil路径:

    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/.*'
    ExcludeHeaderFilterRegex: 'cmake-build-debug/_deps/absl-src/.*'
    FormatStyle: 'file'
    
  • 在pre-commit钩子中添加排除参数
    修改.pre-commit-config.yaml中的clang-tidy钩子,通过--exclude参数直接跳过指定目录:

    - id: clang-tidy
      args: [-p=./cmake-build-debug, --fix-errors, --exclude='cmake-build-debug/_deps/absl-src/.*']
    
  • 确保CMake的SYSTEM标志生效
    确认Abseil的头文件被标记为系统头文件,clang-tidy默认会跳过系统头文件检查:

    FetchContent_MakeAvailable(absl)
    # 针对你的项目目标,将Abseil头文件设为系统包含目录
    target_include_directories(your_project_target SYSTEM PRIVATE ${absl_SOURCE_DIR})
    
  • 临时禁用触发错误的特定规则(备选)
    如果上述方法仍有遗漏,可以临时禁用Abseil触发的错误规则,但此方法仅针对单个规则,不如路径排除彻底:

    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,-clang-analyzer-optin.core.EnumCastOutOfRange'
    

内容的提问来源于stack exchange,提问作者Jiawei Lu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 00:43:17