Skip to content

Add cppcheck action#491

Draft
Neverlord wants to merge 4 commits intomasterfrom
topic/neverlord/gh-249-cppcheck
Draft

Add cppcheck action#491
Neverlord wants to merge 4 commits intomasterfrom
topic/neverlord/gh-249-cppcheck

Conversation

@Neverlord
Copy link
Member

Relates #249.

@Neverlord Neverlord force-pushed the topic/neverlord/gh-249-cppcheck branch 2 times, most recently from 95b321a to 1e463d5 Compare November 30, 2025 16:07
@Neverlord Neverlord force-pushed the topic/neverlord/gh-249-cppcheck branch from 1e463d5 to 2c0754b Compare November 30, 2025 16:17
@Neverlord Neverlord force-pushed the topic/neverlord/gh-249-cppcheck branch from 3d56433 to ec44ade Compare November 30, 2025 19:47
@Neverlord Neverlord force-pushed the topic/neverlord/gh-249-cppcheck branch from ec44ade to 0a23736 Compare November 30, 2025 21:11
@@ -0,0 +1,75 @@
name: cppcheck
Copy link
Contributor

@awelzel awelzel Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neverlord - have you considered doing this via the pre-commit setup?

There's the following, somewhat stale repo, but we could always fork if we need to:
https://github.com/pocc/pre-commit-hooks

@bbannier - any thoughts?

(Mostly because maybe that'd allow to just use it for zeek/zeek, too)

Copy link
Member

@bbannier bbannier Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO pre-commit is preferable as it makes running these checks locally much easier. As for the hook, since some version of cppcheck is available on pypi the following setup also works:

repos:
- repo: local
  hooks:
  - id: cppcheck
    name: cppcheck
    language: python
    entry: cppcheck
    types_or:
      - "c"
      - "c++"
    additional_dependencies:
      - cppcheck

Note that this currently causes no diagnostics to be emitted.

That said, in my experience cppcheck provides not much on top of clang-tidy, but is much harder to set up, e.g., the change to an explicit ctr in this PR could just as well be implemented with clang-tidy's google-explicit-constructor, and I'd just not implement cppcheck here at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to hardcoding flags for CI would be to run cppcheck as part of CMake.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the early feedback here!

have you considered doing this via the pre-commit setup?

Didn't cross my mind. To me, a pre-commit hook is for quick checks like formatting, spell checking, etc. that take a couple seconds to run. Using a pre-commit hook to run a static analyzer doesn't fall into that category in my mental model. I can see the argument for convenience, but cppcheck is basically available anywhere. We already have ci/analyze.sh that I can run locally to run clang-tidy if I really want to. So maybe we could just offer a similar thing for cppcheck or extend the existing script with a CLI switch?

At least for me, running a static analyzer isn't part of the regular work flow. I see it as a quality gate on a PR and if the pipeline complains, I'll look at the output/artifact fix the issue and push again. Usually it's not something I'd need to reproduce locally.

But that's just me saying I wouldn't use it and thus didn't consider it. If there are other work flows on the team that integrate tools like this - just tell me what I need to do. 🙂

An alternative to hardcoding flags ...

The usual way to run cppcheck is the same as for clang-tidy: using the compile database. I didn't get that to work, because unfortunately cppcheck (at least in the versions I've tried for CI) doesn't understand include paths via -isystem (that's what CMake will pass to the compiler for includes tagged as SYSTEM). I don't know if CMake would do something different when setting CXX_CPPCHECK, but at least from the documentation I don't see a way to generate the HTML report that we generate as artifact for CI? Am I just missing something obvious?

FWIW, cppcheck and clang-tidy definitely do have overlap but I still think running both has value since cppcheck has it's own independent implementation. Of course, if the consensus is that we won't integrate both than please just close the PR and I'll maybe cherry-pick some of the changes later.

Copy link
Contributor

@awelzel awelzel Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a pre-commit hook to run a static analyzer doesn't fall into that category in my mental model.

This is fair and if cppcheck isn't super fast, pre-commit isn't the right place. Thanks.

if I really want to. So maybe we could just offer a similar thing for cppcheck or extend the existing script with a CLI switch?

Yeah, that sound good. I primarily didn't like the cppcheck invocation with all the flags in a file somewhere within .github - ci/cppcheck.sh that is then invoked from the GitHub job works nicely IMO. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants