繁体   English   中英

防止 clang tidy 报告关于 Boost 测试标头的警告

[英]Prevent clang tidy to report warnings on Boost Test headers

注意 2022 年 12 月:这个问题似乎在 clang-tidy 14 中得到了解决。仅通过实验,我可以用clang-tidy 11, 12 and 13重现该问题,但不能用14重现。


我有一个使用 Boost.UnitTest 进行测试的 Cmake 项目。 当我使用 clang-tidy 进行静态分析时,它会报告一些来自 Boost.UnitTest 标头的警告。 我想过滤那些。

举个例子(忽略细节)

/usr/include/boost/test/tools/old/interface.hpp:84:45: note: expanded from macro 'BOOST_REQUIRE'
#define BOOST_REQUIRE( P )                  BOOST_TEST_TOOL_IMPL( 2, \
                                            ^
/usr/include/boost/test/tools/old/interface.hpp:65:5: note: expanded from macro 'BOOST_TEST_TOOL_IMPL'
    BOOST_TEST_PASSPOINT();                                                     \
    ^
/usr/include/boost/test/unit_test_log.hpp:261:5: note: expanded from macro 'BOOST_TEST_PASSPOINT'
    ::boost::unit_test::unit_test_log.set_checkpoint(           \
    ^
/usr/include/boost/test/unit_test_log.hpp:209:82: note: default parameter was declared here
    void                set_checkpoint( const_string file, std::size_t line_num, const_string msg = const_string() );
                                                                                 ^
/home/user/prj/alf/boost/multi/test/zero_dimensionality.cpp:23:3: error: calling a function that uses a default argument is disallowed [fuchsia-default-arguments-calls,-warnings-as-errors]
                BOOST_REQUIRE( num_elements(m1) == 3 );

到目前为止,我使用这些行添加了对 Boost.UnitTest 的依赖

    target_link_libraries(${TEST_EXE} PRIVATE Boost::unit_test_framework Boost::serialization)

我试过这个,使 Boost.UnitTest 成为一个“系统”库,但我仍然收到相同的警告

    target_include_directories(${TEST_EXE} SYSTEM PRIVATE ${Boost_INCLUDE_DIRS})
    target_link_libraries(${TEST_EXE} PRIVATE ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
    target_link_libraries(${TEST_EXE} PRIVATE ${Boost_SERIALIZATION_LIBRARY})

但我仍然得到相同的结果。

如何防止 clang-tidy 检查或报告 Boost 标头中的错误?

(我接受更改clang-tidy本身配置的答案(我使用了.clang-tidy配置文件);尽管改为更改 CMakeLists.txt 似乎更优雅。)


补充说明:

按照评论中的一项建议,我禁用了这些与 Boost.Test“不兼容”的警告。 我仍然希望保留它们并以某种方式使 clang-tidy 过滤器成为 Boost.Test 的标头:

#  -altera-unroll-loops,                                  // BOOST_REQUIRE macro requires this
#  -cert-err58-cpp,                                       // BOOST_AUTO_TEST_CASE macro requires this
#  -cppcoreguidelines-avoid-non-const-global-variables,   // BOOST_AUTO_TEST_CASE macros require this
#  -cppcoreguidelines-macro-usage,                        // BOOST_TEST_MODULE macro requires this
#  -cppcoreguidelines-pro-type-vararg,                    // BOOST_REQUIRE macros require this
#  -fuchsia-default-arguments-declarations                // BOOST_AUTO_TEST_CASE_TEMPLATE
#  -fuchsia-default-arguments-calls,                      // BOOST_REQUIRE macros require this
#  -fuchsia-statically-constructed-objects,               // BOOST_AUTO_TEST_CASE creates these
#  -hicpp-vararg,                                         // all BOOST_TEST_REQUIRE macros require this

好的,我(对我而言)对此并不完全清楚:

我做 CXX=clang++ cmake.. -DCMAKE_CXX_CLANG_TIDY="clang-tidy" (加上 clang-tidy 的配置文件)

实际调用 clang-tidy 的方式或时间。 显然,它使用了一些我不熟悉的 CMake 魔法。 我确实记得曾经看到过这样的东西,但我最终没有使用它是有原因的。

相反,我使用CMAKE_EXPORT_COMPILE_COMMANDS=On (对于所有基于 libclang 的工具非常方便,然后是一些,如 IDE 和 LSP 服务器)。 有了compile_commands.json ,您就可以调用run-clang-tidy工具并使用-p指向它。

以我的经验,它确实过滤系统包括它应该包含的内容(并且它给出了在详细模式下被抑制的诊断计数)。

如果您使用库中的宏会引发 clang-tidy 警告,我认为今天没有避免它的好方法。 即使您将该库包含为系统库(例如,通过-isystem ),我的理解是警告仍会触发,因为您的代码中使用了宏,即使它是在库中声明的。

这是修复它的提议,不幸的是,它似乎自 2021 年以来就停滞不前了。

解决方案:使用 clang-tidy 14 或更新版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM