首页 > 解决方案 > 使用配置文件禁用或启用 cppcheck 警告

问题描述

使用 clang-tidy 静态分析器,我可以在项目的根目录中保留一个文件 ( .clang-tidy),其中包含我想要激活或停用的警告。 clang-tidy将寻找这个文件(据我所知)并使用那里定义的选项。这使我免于在 CMake 或 Makefiles 中对长命令行进行硬编码。

cppcheck静态分析器可以做同样的事情吗?

目前我有这个很长的命令行硬编码:

cppcheck --max-ctu-depth=3 --enable=all --inline-suppr --suppress=*:*thrust/complex* --suppress=missingInclude --suppress=syntaxError --suppress=unmatchedSuppression --suppress=preprocessorErrorDirective --language=c++ --std=c++14 --error-exitcode=666

.clang-tidy这是我保留在项目根目录的配置文件示例:

---
Checks: '
    *,
    -readability-magic-numbers,
    -modernize-use-nodiscard,
    -altera-struct-pack-align,
    -cert-err58-cpp,
    -cppcoreguidelines-avoid-non-const-global-variables,
    -cppcoreguidelines-macro-usage,
    -cppcoreguidelines-pro-bounds-array-to-pointer-decay,
    -cppcoreguidelines-pro-type-vararg,
    -cppcoreguidelines-avoid-magic-numbers,
    -fuchsia-default-arguments-calls,
    -fuchsia-trailing-return,
    -fuchsia-statically-constructed-objects,
    -fuchsia-overloaded-operator,
    -hicpp-vararg,
    -hicpp-no-array-decay,
    -llvm-header-guard,
    -llvmlibc-restrict-system-libc-headers,
    -llvmlibc-implementation-in-namespace,
    -llvmlibc-callee-namespace
'
WarningsAsErrors: '*'
HeaderFilterRegex: '.'
AnalyzeTemporaryDtors: false
FormatStyle: file
...

标签: c++configuration-filescppcheckclang-tidy

解决方案


You can store the configuration in a *.cppcheck file and then use the --project command line option to run the check. See the manual - Cppcheck GUI project section.

cppcheck files are normally generated by CppCheckGUI via File -> New project file. The exact syntax is undocumented but it's basically just an XML file and looks to be fairly straightforward if you want to create the file directly without using the GUI.

Sample test.cppcheck file:

<?xml version="1.0" encoding="UTF-8"?>
<project version="1">
    <builddir>test2-cppcheck-build-dir</builddir>
    <platform>Unspecified</platform>
    <analyze-all-vs-configs>false</analyze-all-vs-configs>
    <check-headers>true</check-headers>
    <check-unused-templates>false</check-unused-templates>
    <max-ctu-depth>10</max-ctu-depth>
    <exclude>
        <path name="WINDOWS/"/>
    </exclude>
    <suppressions>
        <suppression>IOWithoutPositioning</suppression>
    </suppressions>
</project>

推荐阅读