Free Static Code Analysis with Eclipse

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 🙂

9 thoughts on “Free Static Code Analysis with Eclipse

  1. Pingback: Tutorial: IAR + FreeRTOS + Freedom Board | MCU on Eclipse

  2. Pingback: Top 10 Customization of Eclipse Settings | MCU on Eclipse

  3. Pingback: Eclipse CODAN (Static Code Analysis) for C/C++ | MCU on Eclipse

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.