Eclipse Zone is brought to you in partnership with:

Erich is Professor at Lucerne University of Applied Sciences and Arts and Distinguished Member of Technical Staff at Freescale Semiconductor. Erich has a MsCS degree and 18+ years of experience in the embedded software and tools world. He created many embedded cross C/C++ compilers and debuggers. Additionally he is researching in the domain of programming languages, real time and mechatronic systems. Erich is a DZone MVB and is not an employee of DZone and has posted 130 posts at DZone. You can read more from them at their website. View Full User Profile

Free Static Code Analysis with Eclipse

01.10.2013
| 6930 views |
  • submit to reddit


I know for myself: the earlier I’m able to fix a bug, the better. So I’m always grateful for things which help me to find issues in my sources as early in the development process as possible. Eclipse and CodeWarrior already help me to find syntax errors in my code while I’m typing:

Eclipse highlighting syntax error

Eclipse highlighting syntax error

With the built-in syntax checker of Eclipse, this helps me many times to get things right without the need to build my code with the compiler. But when I’m able to compile successfully my code, this does not mean it is without bugs. It would be good to catch as many errors *before* downloading and running it on the target.

I have presented a while ago PC-lint: but this does not come free of charge. The good news is that the new Eclipse version in CodeWarroir for MCU10.3 has static code analyser built-in: Code name “Codan”: not as powerful as PC-lint, but good enough as a little helper.

C/C++ Code Analysis

The static C/C++ Code Analysis is available if I select the project and use the context menu:

Run C/C++ Code Analysis

Run C/C++ Code Analysis

But when I run this, nothing seems to happen?

Enabling Code Analysis

The reason is that CodeWarrior has it disabled by default in a new workspace. It is enabled/disabled for each project. What I recommend is to enable it on the workspace level. Using the menu Window > Preferences and filter for ‘code’ shows the setting:

Code Analysis Options in Preferences

Code Analysis Options in Preferences

Usually I enable all the ‘Potential Programming Problems’:

Enabled Code Analysis Settings

Enabled Code Analysis Settings

The cool thing is: now it parses my sources in the background for problems:

Problem View with Code Analysis Report

Problem View with Code Analysis Report

Additionally it flags the problem place in the source view:

Problem in Source View

Problem in Source View

At any time, Code Analysis is run with the context menu on the project folder.

Customizing Code Analysis

The analysis is customized using the ‘Customize Selected..‘ Button:

Customize Selected

Customize Selected

I can configure the behaviour, such as if things are worth an error or are just a warning:

Preferences for problem checking

Preferences for problem checking

For example I can specify inclusion or exclusion pattern/files for each message:

Inclusion and Exclusion Patterns

Inclusion and Exclusion Patterns

:?: I have not found a way to configure/disable messages directly in the source files. That’s something I really like with PC-lint, but I’m not able to do with the Eclipse C/C++ Code Analysis?

Summary

I expect the features and functionality of the C/C++ Code Analysis to grow and extend over time in Eclipse. Right now it is able to catch not that many problems, and some are rather basic. But still: it helps to get my code right the first time. There are sometimes bogus messages, but then I can disable the messages or add that file to the ‘exclusion’ list. And even a small little helper adds to the benefits of Eclipse.

Happy analysing :)

 

Published at DZone with permission of Erich Styger, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)