Does Zig have a static code analyzer?

I’m basically looking for an analyzing tools for zig, which can do memory and resource checks, path analysis… stuff like that, rely on written rules.

2 Likes

Checkout Tracy and KCacheGrind

3 Likes

You mean something similar to clang-tidy?

I might wanna give a look into zlint

There are some predefined rules and you decide which one you want to enable to a zlint.json

I didn’t use it a lot, so I’m not sure about the correctness about the rules, but I think it’s the only project of this kind being maintained

2 Likes

I’ve been working on one for a while, does control flow graph analysis. Not very advanced yet but catches real issues: GitHub - forketyfork/zwanzig: A static analyzer and linter for Zig · GitHub

3 Likes

These are runtime tools though, I guess @continue2breakpoint is looking for something similar to https://clang-analyzer.llvm.org/.

This basically does complex static control flow analysis to find all sorts of issues upfront (out-of-bounds-access, use-after-free, etc…) - e.g. when it finds a problem it reports a whole chain of things that have to be true to trigger the problem (like: when this specific function is called with argument x being less than 0, then in this other function several steps down the callstack this specific pointer access may be an out-of-bounds access… complete with a list of substeps how that result was reached.

E.g. a proper static analyzer goes way beyond of what’s commonly known as ‘linting’. The downside of static analyzers is that they are usually not ‘waterproof’, e.g. most errors it finds are ‘potential problems’ which a human needs to verify (the other downside is of course that static analysis is slow, even slower than Rust compile times). In C/C++ code bases it also requires a strict assert hygiene to hint the static analyzer to cut down on false positives (e.g. when running clang in ‘static analysis’ mode, a C/C++ assert will be resolved into __builtin_assume() which the analyser understands as a hint - e.g. an assert(ptr) will tell the analyzer that ptr cannot be null in this specific place, which might silence a ‘potential null pointer access’ false positive.

4 Likes