Thursday, April 9, 2015

Integrating Cppcheck into Atmel Studio 6.2.

Introduction.

Static analysis tools are very useful. And it's a good idea to use them frequently.
Since I am mostly using Atmel Stuio as my IDE, it seems natural to try and integrate all possible static analysis tools right into it than use them in standalone manner.

Tools I've tried so far are Cppcheck and PC-Lint. The former is freeware and has GUI application where you can test the whole project folder.
The latter is not free, but it is a nightmare to use with GCC compiler - there's no any nice instructions on how to set it up for GCC. I did all the steps from readme files of PC-Lint, but in my case there were a lot of  errors after invoking GNU Make and I didn't really want to dig into it to make it work.

Instead, I downloaded Visual Lint, which has integration of PC-Lint into Atmel Studio 6.1 and 6.2, although even this way needs some manual Windows registry editing to make things work. More to say, Visual Lint still uses all PC-Lint configuration and option files, so even if it seems to work now, I'm still not sure it works how it should work. I'll take a more closer look on working with Visual Lint and post about it later. One more thing - Visual Lint is not freeware, it has Trial version which works for 30 days. Seems enough to decide whether you need it or not.

Now, let's get to integrating free Cppcheck into Atmel Studio.

Integration of Cppcheck into Atmel Studio.

This article is where I got the method and description.

First, get Cppcheck from their site and install it.

Then, you may see that there're integration plugins for popular IDEs, including Visual Studio.
And since Atmel Studio is based on VS, it seems appropriate to use VS plugin.
Unfortunately, it didn't work for me.

I also found somewhere VSIX plugin for Atmel Studio 6.0, but that didn't work either.

The solution is to use Cppcheck as External Tool. The procedure of integrating is usual for Visual Studio and the same with Atmel Studio.

In Atmel Studio, open Tools-->External Tools.
Click Add and fill in forms as seen on this screenshot:


You can change arguments as you wish to get different level of testing and reporting.

After that, Cppcheck appears in External Tools menu:


Using Cppcheck under Atmel Studio.

It can be used in following way. When source file opened in Atmel Studio, just click Cppcheck in External Tools and it will perform static analisys of this file. Any messages will be shown in Output window.
Here's an example of Error message from Cppcheck:

The example of arguments shown previously works for testing only current source file. To test all files in project folder, just change $(ItemPath) to $(ItemDir).

Then, you can experiment with different flags. Try and find what combination works better for you: maybe it's preferred to check only for critical errors in code, or maybe it's nice to have warnings about style or performance issues.

This is flags I found useful for me:
--force - forces checking of all possible #IFDEFs; could be slow, but is a must for big projects
--includes-file=$(EXTRA_INCLUDE_PATHS_ARM)" - in my case EXTRA_INCLUDE_PATHS_ARM is a system path variable showing path for text file with paths of all header files from GCC-ARM toolchain.
-q or --quiet - quiet mode, suppresses service messages from Cppcheck
-j 4 - to use all four cores of CPU for faster testing
--enable=warning,style,performance - enable more tests
--inconclusive - shows warnings even if Cppcheck is not sure if it's really and error. There may be false positives, each such warning should be closely investigated whether it really needs any correction.

Full list of checks performed by Cppcheck is here.

No comments:

Post a Comment