Disable or enable warnings for cppcheck using a configuration file

Viewed 1066

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
...
1 Answers

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