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
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
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
Usually I enable all the ‘Potential Programming Problems’:
Enabled Code Analysis Settings
The cool thing is: now it parses my sources in the background for problems:
Problem View with Code Analysis Report
Additionally it flags the problem place in the 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:
I can configure the behaviour, such as if things are worth an error or are just a warning:
Preferences for problem checking
For example I can specify inclusion or exclusion pattern/files for each message:
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?
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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)