The Beehive, City Place, Gatwick, RH6 0PA, United Kingdom
+44 (0)20 801 74646

Writing Custom Code Checkers in Codesonar

Besides the many out-of-the-box checkers that Codesonar presents for detecting a wide range of coding errors, there are several mechanisms provided for defining your own checkers.

The most powerful is the Codesonar Plugin API, which is available in either  Scheme or C.

When active, your checkers behave just like any other Codesonar checker: they execute during Codesonar’s normal analysis phase, detected cases are listed along with the built-in checkers, and they produce warning reports using the same source code annotations. In summary, they become indistinguishable from Codesonar’s own built-in checks.

A custom checker comprises boiler-plate code and user-defined sections. For brevity, below is the user-defined part of a checker that simply counts the number of return statements and generates a warning if they exceed a predefined limit.

Having compiled and copied the resulting library to the Codesonar installation directory, here’s how an instance of this warning would be listed in the set of detected warnings:

And when clicking into the above “Too many Return statements” warning, here is how Codesonar would present the annotated source code:

Note, how and what annotations are included is dictated by how the custom checker is authored; in this case the above was deemed sufficient.

Did you enjoy this post?

Subscribe to our newsletter and to keep up to date on blog posts, product updates and events.

Related Posts