使用配置文件禁用或启用 cppcheck 警告

问题描述 投票:0回答:2

使用 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-files cppcheck clang-tidy
2个回答
3
投票

您可以将配置存储在

*.cppcheck
文件中,然后使用
--project
命令行选项运行检查。请参阅手册 - Cppcheck GUI 项目部分。

cppcheck
文件通常由 CppCheckGUI 通过文件 -> 新项目文件生成。确切的语法没有记录,但它基本上只是一个 XML 文件,如果您想在不使用 GUI 的情况下直接创建文件,它看起来相当简单。

样本

test.cppcheck
文件:

<?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>

1
投票

我认为最简单的方法是通过

--suppressions-list=<file>
选项。

例如编写如下

.suppress.cppcheck
文件:

IOWithoutPositioning
MissingInclude
...

并将

--suppressions-list=./.suppress.cppcheck
添加到您的cppcheck cmdline.

© www.soinside.com 2019 - 2024. All rights reserved.