Creating and testing C/C++ KAST custom functions

A KAST custom function is written using a simple C API. A custom function defines one or more extensions to the KAST language in the form of either custom predicates or getter methods. In either case, the custom functions perform specific queries or interactions with the AST that represents your source module.

You'll typically want to create a KAST custom function if you're attempting to write a KAST checker that requires conditions that aren't available through Klocwork's standard built-in functions. For a list of built-in functions, launch Checker Studio, and go to Help > Help topics > KAST reference > C/C++ KAST built-in functions reference . Always make sure you consult the latest list of built-in functions to avoid recreating something that's already available.

Once a custom function is coded, you simply call it in your KAST expression and specify in your checker definition the name of the library that contains the function. The KAST API is documented in Klocwork C/C++ AST API Reference.

The first part of this article explains the standard workflow used when writing and testing a custom checker, and describes a method of customizing that template workflow for greater efficiency. The rest of the article explains examples of techniques used in custom functions. The examples start with a very simple custom function, and progress through more complex functions:

  • "Hello world"
  • modifying error messages
  • listing classes and members
  • accessing inheritance information
  • traversing the AST tree
  • applying traversal to the KAST custom function
  • designing a 'descendant::' search

Note: Make sure that any custom functions are built on the same type of machine (OS distribution and version) as the target machines on which the checker will be deployed. For Unix systems, the machine on which the checker is compiled must use a compatible version of GLibC as the machine on which the checker is to be deployed.