Disable or enable warnings for cppcheck using a configuration file
Asked Answered
K

2

7

With clang-tidy static analyzer I can keep a file (.clang-tidy) in the root of the project with the warnings I want to activate or deactivate. clang-tidy will look for this file (as far I know) and use the options defined there. This saves me from hard coding long command lines in CMake or Makefiles.

Is it possible to do the same with cppcheck static analyzer?

Currently I have this very long command line hardcoded:

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

This is an example of .clang-tidy configuration file that I keep at the root of a project:

---
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
...
Kuban answered 21/9, 2021 at 16:0 Comment(0)
L
1

I think the easiest way is via --suppressions-list=<file> option.

For example, write the following .suppress.cppcheck file:

IOWithoutPositioning
MissingInclude
...

And add --suppressions-list=./.suppress.cppcheck to your cppcheck cmdline.

Lissie answered 24/3, 2023 at 5:24 Comment(1)
I wonder why I didn't see this option before. --suppressions-list should have a default value.Kuban
J
3

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>
Jene answered 24/9, 2021 at 7:56 Comment(4)
I tried to generate the xml from the GUI but I couldn’t even select the exceptions in the GUI yo store it in the xml. Do you have an example xml file?Kuban
Added a sample. You can add edit the .cppcheck file via gui by going to File - Edit project file.Jene
The format is defined here cppcheck.sourceforge.io/manual.pdf. XML is only one of two possible formats.Kell
I am using these options cppcheck --enable=all --suppress=missingIncludeSystem --inline-suppr --std=c++17 --check-config --error-exitcode=1. It would be nice to know how to put these in an XML or text configuration file. So far I wasn't able to put any of these options in a configuration file, they are ignored.Kuban
L
1

I think the easiest way is via --suppressions-list=<file> option.

For example, write the following .suppress.cppcheck file:

IOWithoutPositioning
MissingInclude
...

And add --suppressions-list=./.suppress.cppcheck to your cppcheck cmdline.

Lissie answered 24/3, 2023 at 5:24 Comment(1)
I wonder why I didn't see this option before. --suppressions-list should have a default value.Kuban

© 2022 - 2024 — McMap. All rights reserved.