【问题标题】:clang-tidy - ignore third party headers codeclang-tidy - 忽略第三方标头代码
【发布时间】:2020-07-20 21:16:33
【问题描述】:

我在我的项目中使用 CMake,我想在项目中引入 clang-tidy 检查。

我为此目的使用CMAKE_CXX_CLANG_TIDY.clang-tidy 文件进行检查设置。

我想在 CI 中使用 warnings-as-errors 以可靠的方式检查提交是否引入了一些新的违规行为。不幸的是,由于 3rd-party 库,我在启用检查时遇到了一些问题。 例如,我使用Eigen,它是仅标头库。由于这个事实,我在代码中收到了一些警告,例如。 “a_file.cpp”

/snap/cmake/301/bin/cmake -E __run_co_compile --tidy="clang-tidy;--extra-arg-before=--driver-mode=g++" --source=../../a_file.cpp -- /usr/bin/clang++ -DEIGEN_MPL2_ONLY -DEIGEN_STACK_ALLOCATION_LIMIT=16384 -I../../SomePath -I../../SomePath2 -isystem ../../path_to/include/Eigen -m64 -stdlib=libc++ -g -fPIC -std=c++11 -MD -MT a_file.cpp.o -MF a_file.cpp.o.d -o a_file.cpp.o -c a_file.cpp                                                                            
../../path_to/include/Eigen/Eigen/src/Core/Swap.h:89:36: error: Assigned value is garbage or undefined [clang-analyzer-core.uninitialized.Assign,-warnings-as-errors] 
a_file.cpp:279:5: note: Loop condition is true.  Entering loop body                            
    for( unsigned int i = 0; i < 100; ++i )                                                                                                                                                                                                                         
    ^                                                                                                                                                                                                                                                                 
a_file.cpp:282:13: note: Calling move assignment operator for 'Matrix<float, 3, 1, 0, 3, 1>'   
            some_name = Vector3f( GetRandom( fDummy ),GetRandom( fDummy ), GetRandom( fDummy ) );  

我有点不知道如何忽略此类问题,因为header-filter 似乎无法解决此问题 - 对于其他检查 [bugprone-xxx] 我有类似的问题。除了到处添加//NO-LINT,我还有什么选择?

编辑:为错误添加了一些上下文。

【问题讨论】:

  • 为什么 header-filter 或 HeaderFilterRegex 不能解决问题? --header-filter='Eigen/src/Core/.*' 会起作用吗?
  • 其实-header-filter是白名单而不是黑名单。我试过了 - 它不能解决问题。与 -isystem 类似,这使得 include 被视为系统标头,默认情况下会忽略所有警告(如果没有此选项,我会收到更多警告)。我的理解是这部分代码被内联在a_file.cpp 中,并且clang-tidy 将此警告视为.cpp 文件本身的问题,而不是.hpp 文件的问题。

标签: c++ cmake clang clang-tidy


【解决方案1】:

.clang-tidy

...
HeaderFilterRegex: '.*'
...

clang-tidy ... --header-filter='.*' ...

分析大量代码时的clang-tidy输出:

Suppressed ... warnings (... in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

【讨论】:

    【解决方案2】:

    您发布的错误看起来并不虚假。问题可能不在于 第三方库,而在于 您对它的使用。您没有提供足够的代码来给出完全正确的答案,因为不清楚 fDummyGetRandom 是什么。如果fDummy移动GetRandom 中,那么这里确实存在错误。

    cmets已经列出了忽略头文件中包含的错误的方法:使用-isystem(在CMake中,导入的目标默认使用此,否则您可以手动将SYSTEM应用于target_include_directories)。

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 2018-10-25
      • 2018-03-20
      • 2022-07-12
      • 2020-04-08
      • 2020-12-30
      • 2019-10-01
      • 2018-07-02
      • 2016-05-17
      相关资源
      最近更新 更多