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

clang-tidy的readability-identifier-naming仅修复头文件,CPP文件无变更求助

问题描述

在Windows环境下,基于CMake 3.31.0-rc1、Ninja生成器构建C++20 MSVC项目,使用LLVM 18.1.8的clang-tidy工具,通过readability-identifier-naming规则批量修改符号命名风格。执行命令:

clang-tidy -checks="-*,readability-identifier-naming" --fix-errors -p dir\with\compile_commands .\src\SampleFile.*

后发现仅.hpp文件被正确修复,对应的.cpp文件无变更。CPP文件输出显示:

.\src\SampleFile.cpp 10 warnings generated. Suppressed 10 warnings (10 in non-user code).

而.hpp文件成功应用10项修复。此前已清理CMake生成的compile_commands.json中不存在的.modmap文件引用(项目未使用模块),但问题依旧。当前.clang-tidy配置内容如下:

---
Checks: >
  -*,
  -readability-identifier-naming

CheckOptions:
  - { key: readability-identifier-naming.NamespaceCase,            value: lower_case }
  - { key: readability-identifier-naming.ClassCase,                value: CamelCase  }
  - { key: readability-identifier-naming.StructCase,               value: CamelCase  }
  - { key: readability-identifier-naming.TemplateParameterCase,    value: CamelCase  }
  - { key: readability-identifier-naming.FunctionCase,             value: lower_case }
  - { key: readability-identifier-naming.VariableCase,             value: lower_case }
  - { key: readability-identifier-naming.MemberCase,               value: lower_case }
  - { key: readability-identifier-naming.MethodCase,               value: camelBack  }
  - { key: readability-identifier-naming.PrivateMemberPrefix,      value: 'm_'        }
  - { key: readability-identifier-naming.ProtectedMemberPrefix,    value: 'm_'        }
  - { key: readability-identifier-naming.MacroDefinitionCase,      value: UPPER_CASE }
  - { key: readability-identifier-naming.EnumConstantCase,         value: CamelCase  }
  - { key: readability-identifier-naming.ConstexprVariableCase,    value: CamelCase  }
  - { key: readability-identifier-naming.GlobalConstantCase,       value: UPPER_CASE }
  - { key: readability-identifier-naming.MemberConstantCase,       value: CamelCase  }
  - { key: readability-identifier-naming.StaticConstantCase,       value: CamelCase  }
可能的原因与解决步骤
  1. 修正.clang-tidy的Checks配置错误
    当前配置中Checks字段写的是-*, -readability-identifier-naming,这相当于禁用了readability-identifier-naming规则。.hpp文件生效是因为命令行显式指定了该规则,而.cpp文件的规则判定被配置覆盖。需要把Checks改成启用该规则:

    Checks: >
      -*,
      readability-identifier-naming
    

    注意去掉规则名称前的减号。

  2. 调整non-user code的判定逻辑
    输出显示CPP文件的警告被标记为非用户代码,clang-tidy默认不会修改这类代码。可以通过两种方式解决:

    • 在.clang-tidy中添加配置,指定用户代码路径:
      CheckOptions:
        # 原有配置...
        - { key: HeaderFilterRegex, value: "^src/" }  # 匹配你的源码目录
      
    • 在命令行添加--header-filter=^src/参数,强制clang-tidy处理src目录下的所有文件:
      clang-tidy -checks="-*,readability-identifier-naming" --fix-errors --header-filter=^src/ -p dir\with\compile_commands .\src\SampleFile.*
      
  3. 验证compile_commands.json的正确性
    即使清理了.modmap引用,仍需确认CPP文件的编译条目是否正确:

    • 检查compile_commands.json中对应.cpp文件的command字段,确保包含正确的/I(包含路径)、/std:c++20等参数,且无错误的模块相关编译选项。
    • 尝试手动指定编译参数测试:
      clang-tidy -checks="-*,readability-identifier-naming" --fix-errors --extra-arg="/std:c++20" --extra-arg="/Isrc" .\src\SampleFile.cpp
      
  4. 单独指定.cpp文件测试
    命令中使用.\src\SampleFile.*可能存在匹配问题,尝试单独指定.cpp文件:

    clang-tidy -checks="-*,readability-identifier-naming" --fix-errors -p dir\with\compile_commands .\src\SampleFile.cpp
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:56:16