Tutorial 1 - Create a simple C/C++ Path checker

From Insight-9.5

Revision as of 16:46, 3 February 2012 by Kwelsh (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Custom checkers > Custom C/C++ checkers > Custom C/C++ Path checkers > Tutorial 1 - Create a simple C/C++ Path checker


To get started with creating C/C++ Path checkers, you can build and deploy the sample C/C++ Path checker, called MallocDelete.

Run kwcreatechecker

In the directory where you want the checker files to be created, run kwcreatechecker with the following options:

kwcreatechecker --language cxx --type common --code MallocDelete

Note: Multibyte characters are not supported in error IDs (issue codes) and there's a 255-character limit.

The checker source files are created in the MallocDelete subdirectory.

Build the checker

Now we'll compile the checker and create a build specification file we can use to test it.


  1. Open the Visual Studio prompt.
  2. Go to your MallocDelete checker directory.
  3. Run:
    nmake install
  4. To generate the build specification you’ll need to test your deployed checker, run:
    nmake buildspec


  1. With GNU make in your path, run:
    make install
  2. To generate the build specification you’ll need to test your deployed checker, run:
    make buildspec

Deploy the checker

Now we’ll deploy the checker to our desktop so that it can be used by Klocwork Truepath to detect the new issue type.

If you do not see a plugins directory in the following location, you must create it manually. The location varies depending on your operating system:

  • Unix: ~<username>/.klocwork/plugins
  • Windows 7 and Vista: C:\users\<username>\.klocwork\plugins
  • Windows XP: C:\Documents and Settings\<username>\.klocwork\plugins

Unzip the ZIP file into the plugins folder you created.

Test the checker

We’re going to use kwcheck, the command-line analysis tool, to make sure the new issue type is being detected in the test case.

  1. Set up the project to test. In your plugins folder, set up a local project:
    kwcheck create -b <build-specification>
    • the create subcommand sets up local project directories you need to run an analysis
    • -b imports the build specification you created when you compiled the checker
  2. Run kwcheck to see if the issue is detected in your test case:
    kwcheck run
    Your checker will detect the issue from your test case.

If all is well, you should see something like this:

1 C:\MallocDelete\testcase.cc:5 FMM.MUST Analyze
Freeing mismatched memory from 'p'. Memory referenced by 'p' allocated through function 'malloc' at line 4 is illegally freed by passing argument 1 to function
'delete' at line 5

2 C:\MallocDelete\testcase.cc:5 MallocDelete Analyze
Your message goes here

The second detected issue was reported by the custom checker.

If you want to put your own message in the checker, edit the checkers.xml file and re-build the checker. For more information, see checkers.xml file: C/C++ checker configuration.

What’s next?

Try the more advanced tutorials:

Documentation for custom C/C++ checkers