(Adapted from my EclipseCon Europe 2011 session)
Eclipse CDT 8.0, part of the Indigo release, includes a nice code checking framework: Codan. Out of the box, Codan provides both the infrastructure needed to perform code checks and some useful, ready-to-use checkers.
Even though the provided checkers are a great addition to a developer’s toolbox, many more are needed for Codan to have feature parity with existing tools like Cppcheck. In addition, Codan’s infrastructure provides good support for writing AST-based checkers, but support for integrating external tools was missing.
The main reason for integrating Codan with external tools is to enjoy all the code checks from mature tools without leaving our beloved Eclipse. With the new infrastructure:
- External tools can be configured using Codan’s preference page
- External tools are invoked automatically when a C/C++ file is saved
- The output of these tools can be displayed as editor markers
My contribution includes a Cppchecker-based checker.
This checker is disabled by default but it can be easily enabled and configured in the Codan preference page. Before Juno release, we decided to move
CppcheckChecker to the “examples” project.
A more detailed configuration dialog can be found by pressing the “Customize Selected…” button:
As you can see from the screenshot above, you can specify the path of the Cppcheck executable, the arguments to pass and whether Cppcheck’s output should be displayed in the console.
Now, let’s see it in action!
Writing your own external-tool-based checker
The new infrastructure makes it very easy to write your own external-tool-based checker. In the simplest case, you will need to:
- Extend the abstract class
- Provide the name of your tool (e.g. “Cppcheck”.)
- Provide default values for the path of the executable, arguments to pass and whether the output of the tool should be displayed in the console.
- Provide one or more implementations of
AbstractOutputParser. They will parse the output of the external tool, line by line. It’s up to you to decide what to do with the output (e.g. create error markers.)
You can take a look at the
CppcheckChecker class here.
In the case of tools that are complex to set up, the new infrastructure is extremely flexible and configurable. It allows you to pretty much to configure every single aspect of the checker, from the files that the tool can check to the way to feed arguments to the tool.
Many, many thanks to my teammate and mentor, Sergey Prigogin, for his guidance and for reviewing and improving this work.
Sergey is one of the best engineers I have ever met in my career (in fact, he is in my top-3 list,) a nice gentleman, and the toughest code reviewer…ever. His attention to detail and his high standards for quality are just out of this world.