Creating C/C++ KAST checkers

Creating C/C++ KAST checkers

When you have a code analysis issue that isn't detected by the Klocwork checkers, you can create a custom checker to find the problem in your code. This article explains the workflow for creating a custom checker.

Workflow: Creating a custom checker

The basic steps involved in creating a KAST checker are:

1. Run kwcreatechecker to create the files for your checker:

kwcreatechecker --language cxx --type kast --code <checker_name> 

where<checker_name> is the name you want to call your checker

The command creates the stub files you need for creating your checker and places them in a <checker_name> directory. At this stage, the files contain template material, which you will replace with your own data as the process continues:

  •, which will contain your testcase
  • checkers.xml, which will contain your XPath-like KAST expression defining the checker
  • help.xml, which will contain help for your checker
  • Makefile, which is your C/C++ build file

2. Create a simple test case that contains the issue you want the checker to find, and place it in the file.

3. Use Checker Studio to:

  • create the KAST expression that defines your checker
  • test the expression
  • add more complexity to your test case

Checker Studio takes your test case as source code, presenting a visual representation of its hierarchical structure in an Abstract Syntax Tree (AST). As the studio builds the tree structure, the built-in compiler creates a 'node' for each item of source code. Connected through parent-child relationships, nodes can be the whole code sample, through a number of intermediate levels like statements or declarations, to simple constructs like an operator sign.

Using the tree, you can identify nodes of interest and select qualifiers, conditions, variables, and iterative sequences to create the checker expression. The studio also provides a method of testing your expression, so that you finish up with a KAST expression that navigates through the tree and successfully pinpoints the defect in the source code.

4. Edit the template stub files.

Place the KAST expression in the checkers.xml file by replacing the sample KAST expression in the <pattern> tag with yours. Create help documentation for your checker (at least a description), and place it in the help.xml file.

5. Build the checker.

In this step, you generate the build specification for your test case and import it into your local project:

  • On Unix:make install buildspec
  • On Windows: nmake install buildspec from the Visual Studio prompt

The build process generates a checker deployment package with the following files:

  • a build specification file you can use in kwcheck or kwbuildproject to test your checker
  • a zip file containing your checker files

6. Test the checker.

Install the checker on your desktop and test it until you're sure of the results. Do this by unzipping the checker deployment package into your <username>/.klocwork./plugins directory.

7. Deploy the checker.

When you're ready to deploy the checker in your server project, use kwdeploy for the deployment. See Deploying custom checkers.