23.03.2013 Views

Integrate an external code checker into Eclipse CDT - IBM

Integrate an external code checker into Eclipse CDT - IBM

Integrate an external code checker into Eclipse CDT - IBM

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong><br />

<strong>CDT</strong><br />

Execute C/C++ <strong>code</strong> <strong>an</strong>alysis tools with Cod<strong>an</strong> in <strong>Eclipse</strong><br />

Alex Ruiz (alruiz@google.com)<br />

Software engineer<br />

Google<br />

Skill Level: Intermediate<br />

Date: 22 Aug 2012<br />

Cod<strong>an</strong> is the <strong>Eclipse</strong> <strong>CDT</strong> (C/C++ Development Tooling) built-in <strong>code</strong> <strong>an</strong>alysis<br />

framework for C/C++ projects. Cod<strong>an</strong> provides infrastructure to perform static<br />

<strong>code</strong> <strong>an</strong>alysis <strong>an</strong>d comes with ready-to-use problem <strong>checker</strong>s. With the <strong>Eclipse</strong><br />

Juno release, Cod<strong>an</strong> was extended to enable developers to automatically execute<br />

<strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tools. Alex Ruiz explains why the update is good news<br />

for users of <strong>Eclipse</strong> <strong>CDT</strong>, then shows you how to use Java <strong>code</strong> <strong>an</strong>d a little bit<br />

of XML to quickly integrate your favorite <strong>code</strong> <strong>checker</strong> <strong>into</strong> your <strong>Eclipse</strong> C/C++<br />

development environment.<br />

Cod<strong>an</strong> is a <strong>code</strong> <strong>an</strong>alysis framework that performs <strong>code</strong> checks on C/C++ projects.<br />

Part of the <strong>Eclipse</strong> <strong>CDT</strong> (C/C++ Development Tooling) suite since 2011, Cod<strong>an</strong> not<br />

only provides all the infrastructure necessary to perform static <strong>code</strong> <strong>an</strong>alysis, but also<br />

some useful, ready-to-use problem <strong>checker</strong>s (see Resources).<br />

Cod<strong>an</strong> was updated with the <strong>Eclipse</strong> Juno release in June 2012 to enable developers<br />

to automatically execute <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tools within <strong>Eclipse</strong>. This is <strong>an</strong><br />

exciting adv<strong>an</strong>ce for <strong>Eclipse</strong> <strong>CDT</strong> <strong>an</strong>d for C/C++ developers. While the previously<br />

provided problem <strong>checker</strong>s were good, m<strong>an</strong>y more were needed to give Cod<strong>an</strong><br />

feature-parity with existing <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tools. Cod<strong>an</strong> now easily integrates<br />

with mature <strong>external</strong> tools such as Cppcheck <strong>an</strong>d cl<strong>an</strong>g_check.<br />

Related tutorials on developerWorks<br />

More about application development with <strong>Eclipse</strong> <strong>CDT</strong>:<br />

• "C/C++ development with the <strong>Eclipse</strong> platform"<br />

• "Developing applications using the <strong>Eclipse</strong> C/C++ Development<br />

Toolkit"<br />

Integrating <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tools with <strong>Eclipse</strong> <strong>CDT</strong> enables more <strong>an</strong>d better<br />

<strong>code</strong> checks th<strong>an</strong> developers were able to do with Cod<strong>an</strong> alone. It should also greatly<br />

© Copyright <strong>IBM</strong> Corporation 2012 Trademarks<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 1 of 17


developerWorks® ibm.com/developerWorks/<br />

improve overall development productivity. We c<strong>an</strong> now configure <strong>external</strong> <strong>code</strong><br />

<strong>an</strong>alysis tools from Cod<strong>an</strong>'s Preferences pages. Once integrated with Cod<strong>an</strong>, the<br />

tools are invoked automatically <strong>an</strong>d their outputs are displayed as editor markers.<br />

In this article, I'll show you how to use Java <strong>code</strong> <strong>an</strong>d a little bit of XML to integrate<br />

your favorite <strong>code</strong> <strong>an</strong>alysis tool <strong>into</strong> your <strong>Eclipse</strong> C/C++ development environment.<br />

My example will be based on integrating Cppcheck with Cod<strong>an</strong>, but the process<br />

should be applicable to the tool of your choice.<br />

Install <strong>Eclipse</strong> Juno <strong>an</strong>d <strong>CDT</strong><br />

You will need to have both <strong>Eclipse</strong> Juno <strong>an</strong>d <strong>CDT</strong> installed in order to follow the<br />

examples in this article. If you haven't installed <strong>Eclipse</strong> yet, you c<strong>an</strong> install a version<br />

that comes with <strong>CDT</strong> pre-installed. To do this, simply select <strong>Eclipse</strong> IDE for C/C++<br />

Developers from the <strong>Eclipse</strong> downloads page.<br />

If you already have <strong>an</strong> <strong>Eclipse</strong> installation that does not include <strong>CDT</strong>, follow these<br />

instructions to update your development environment:<br />

1. Inside <strong>Eclipse</strong>, select the menu Help > Install New Software....<br />

2. In the Install dialog, select Juno from the drop-down list.<br />

3. In the Programming L<strong>an</strong>guages category, select C/C++ Development Tools<br />

SDK.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 2 of 17


ibm.com/developerWorks/ developerWorks®<br />

Figure 1. Installing <strong>CDT</strong><br />

In addition to <strong>CDT</strong>, you will need the st<strong>an</strong>dard GNU C/C++ development tools for<br />

compiling, building, <strong>an</strong>d debugging your <strong>code</strong>. See Resources for instructions on how<br />

to install these tools.<br />

Start up Cod<strong>an</strong><br />

Most Cod<strong>an</strong> <strong>code</strong> checks are enabled by default. You c<strong>an</strong> individually configure<br />

Cod<strong>an</strong> <strong>code</strong> checks at the workspace or project levels using <strong>Eclipse</strong>'s Preferences or<br />

Project Property pages, respectively.<br />

From Cod<strong>an</strong>'s Preferences page, shown in Figure 2, you c<strong>an</strong> see all of the <strong>code</strong><br />

<strong>checker</strong>s available <strong>an</strong>d the <strong>code</strong> problems each one reports on.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 3 of 17


developerWorks® ibm.com/developerWorks/<br />

Figure 2. Code <strong>checker</strong>s on Cod<strong>an</strong>'s Preferences page<br />

From this page you c<strong>an</strong> enable, disable, or ch<strong>an</strong>ge the severity of a problem. If you<br />

w<strong>an</strong>t to configure other properties of individual problems you c<strong>an</strong> select a problem<br />

<strong>an</strong>d click the Customize Selected... button. Figure 3 shows the configuration options<br />

for the problem "Name convention for function."<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 4 of 17


ibm.com/developerWorks/ developerWorks®<br />

Figure 3. Configuring a problem<br />

The third tab in Figure 3 allows you to specify how a problem check should be<br />

launched:<br />

• Run as you type: When a user makes ch<strong>an</strong>ges to a file in a <strong>CDT</strong> editor.<br />

• Run on file open: When a file is opened in a <strong>CDT</strong> editor.<br />

• Run on file save: When unsaved ch<strong>an</strong>ges on a <strong>CDT</strong> editor are saved.<br />

• Run on incremental build: When <strong>an</strong> incremental build is issued (usually when<br />

a file is saved <strong>an</strong>d the project-level option "Build Automatically" is enabled). If<br />

you enable this option <strong>an</strong>d "Run on file save" together, then a <strong>code</strong> check will<br />

run twice.<br />

• Run on full build: When a full build is issued (e.g., when a project is cle<strong>an</strong>ed).<br />

• Run on dem<strong>an</strong>d: When a user m<strong>an</strong>ually triggers a <strong>code</strong> check from the context<br />

menu item "Run C/C++ Code Analysis."<br />

Code checks with Cod<strong>an</strong><br />

So that you c<strong>an</strong> see Cod<strong>an</strong> at work, I'll create a C++ project with a short C++ file. In<br />

this file, I'll assign a variable to itself. Cod<strong>an</strong> includes the <strong>code</strong> check "Assignment to<br />

itself," which is enabled by default with the severity level of "error." It is configured to<br />

run as you type, so the error will pop up immediately.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 5 of 17


developerWorks® ibm.com/developerWorks/<br />

Figure 4. Cod<strong>an</strong> executing a <strong>code</strong> check<br />

In Figure 4, you c<strong>an</strong> see that Cod<strong>an</strong> found the self-assignment error <strong>an</strong>d reported it<br />

before I had a ch<strong>an</strong>ce to save the file. Perfect!<br />

To learn more about using Cod<strong>an</strong>, visit the project's homepage (see Resources).<br />

<strong>Integrate</strong> Cppcheck <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong><br />

To integrate <strong>an</strong> <strong>external</strong> <strong>code</strong>-<strong>an</strong>alysis tool with Cod<strong>an</strong>, we need to write a special<br />

kind of <strong>checker</strong> that knows how to invoke that tool. A <strong>checker</strong> is <strong>an</strong> implementation<br />

of Cod<strong>an</strong>'s IChecker interface that performs some kind of <strong>code</strong> check on a given<br />

IResource (typically <strong>an</strong> IFile).<br />

To demonstrate how easy it is to create <strong>an</strong> <strong>external</strong>-tool-based <strong>checker</strong>, we'll create<br />

a <strong>checker</strong> that invokes the popular tool Cppcheck (see Resources). Here's what we'll<br />

do:<br />

• Create <strong>an</strong> <strong>Eclipse</strong> plug-in project <strong>an</strong>d add Cod<strong>an</strong> as a dependency.<br />

• Create <strong>an</strong> error parser to parse the output of Cppcheck <strong>an</strong>d create editor<br />

markers if necessary.<br />

• Create the <strong>code</strong> <strong>checker</strong>, which is the class responsible for invoking Cppcheck.<br />

Step 1. Create <strong>an</strong> <strong>Eclipse</strong> plug-in project<br />

To create a Cod<strong>an</strong> <strong>checker</strong>, we start by creating a new <strong>Eclipse</strong> plug-in project:<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 6 of 17


ibm.com/developerWorks/ developerWorks®<br />

1. Select the menu File > New > Project....<br />

2. In the category Plug-in Development, select Plug-in Project.<br />

3. Enter a name for the project (mine is "CppcheckChecker") <strong>an</strong>d click Next.<br />

4. Accept defaults <strong>an</strong>d click Finish.<br />

Figure 5. Creating a plug-in project<br />

Right after creating the new plug-in project, <strong>Eclipse</strong> will automatically open the<br />

MANIFEST.MF file. This is the file where we'll add Cod<strong>an</strong>'s dependencies.<br />

In the editor, select the Dependencies tab <strong>an</strong>d add the following to the Required<br />

Plug-ins list:<br />

org.eclipse.cdt.cod<strong>an</strong>.core<br />

org.eclipse.cdt.cod<strong>an</strong>.core.cxx<br />

org.eclipse.cdt.cod<strong>an</strong>.ui<br />

org.eclipse.cdt.cod<strong>an</strong>.ui.cxx<br />

org.eclipse.cdt.core<br />

org.eclipse.cdt.ui<br />

org.eclipse.core.resources<br />

org.eclipse.core.runtime<br />

org.eclipse.ui<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 7 of 17


developerWorks® ibm.com/developerWorks/<br />

<strong>Eclipse</strong> plug-ins<br />

Step 2. Create <strong>an</strong> error parser<br />

I won't cover all the details of writing <strong>an</strong> <strong>Eclipse</strong> plug-in in this article<br />

because <strong>Eclipse</strong> isn't the focus of my discussion. See Resources for <strong>an</strong><br />

introduction to writing <strong>Eclipse</strong> plug-ins.<br />

We'll need <strong>an</strong> error parser to create editor markers from Cppcheck's output, so our<br />

next step is to extend <strong>Eclipse</strong>'s C/C++ tooling with a plug in. We'll use Java <strong>code</strong> for<br />

this purpose because <strong>Eclipse</strong> itself is a Java application.<br />

First, we'll create a class, CppcheckErrorParser, that implements<br />

org.eclipse.cdt.core.IErrorParser. We start by finding the pattern that Cppcheck<br />

uses when reporting <strong>code</strong> problems. The error parser will use this pattern to identify<br />

a line of output that represents a problem report, then extract from that output the<br />

information it needs to create <strong>an</strong> editor marker.<br />

Listing 1. A pattern that matches Cppcheck's output<br />

// sample line to parse:<br />

//<br />

// [/src/HelloWorld.cpp:19]: (style) The scope of the variable 'i' c<strong>an</strong> be reduced<br />

// ----------1--------- -2 --3-- ------------------4-------------------------<br />

//<br />

// groups:<br />

// 1: file path <strong>an</strong>d name<br />

// 2: line where problem was found<br />

// 3: problem severity<br />

// 4: problem description<br />

private static Pattern pattern =<br />

Pattern.compile("\\[(.*):(\\d+)\\]:\\s*\\((.*)\\)\\s*(.*)");<br />

Listing 2 shows how the error parser uses the pattern to extract the path <strong>an</strong>d name<br />

of the file being checked, as well as the location, description, <strong>an</strong>d severity of the<br />

found error. With this information, the error parser creates a new ProblemMarkerInfo<br />

<strong>an</strong>d passes it to the given ErrorParserM<strong>an</strong>ager. ErrorParserM<strong>an</strong>ager is the class<br />

responsible for creating editor markers.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 8 of 17


ibm.com/developerWorks/ developerWorks®<br />

Listing 2. Processing Cppcheck's output<br />

}<br />

@Override<br />

public boole<strong>an</strong> processLine(String line, ErrorParserM<strong>an</strong>ager parserM<strong>an</strong>ager) {<br />

Matcher matcher = pattern.matcher(line);<br />

if (!matcher.matches()) {<br />

return false;<br />

}<br />

IFile fileName = parserM<strong>an</strong>ager.findFileName(matcher.group(1));<br />

if (fileName != null) {<br />

int lineNumber = Integer.parseInt(matcher.group(2));<br />

String description = matcher.group(4);<br />

int severity = findSeverityCode(matcher.group(3));<br />

ProblemMarkerInfo info =<br />

new ProblemMarkerInfo(fileName, lineNumber, description, severity, null);<br />

parserM<strong>an</strong>ager.addProblemMarker(info);<br />

return true;<br />

}<br />

return false;<br />

}<br />

Mapping problem severities<br />

Cppcheck defines its own problem severities, which are not the same as the ones<br />

used by editor markers. For example, the Cppcheck severity "style" does not have<br />

a counterpart in the <strong>Eclipse</strong> world. To overcome this issue, we need to create a<br />

mapping between the two kinds of problem severities. The method findSeverityCode<br />

(shown in Listing 3) demonstrates a straightforward way to implement this mapping:<br />

Listing 3. Mapping problem severities<br />

private static Map SEVERITY_MAPPING = new HashMap();<br />

static {<br />

SEVERITY_MAPPING.put("error", IMarkerGenerator.SEVERITY_ERROR_RESOURCE);<br />

SEVERITY_MAPPING.put("warning", IMarkerGenerator.SEVERITY_WARNING);<br />

SEVERITY_MAPPING.put("style", IMarkerGenerator.SEVERITY_INFO);<br />

}<br />

private int findSeverityCode(String text) {<br />

Integer <strong>code</strong> = SEVERITY_MAPPING.get(text);<br />

if (<strong>code</strong> != null) {<br />

return <strong>code</strong>;<br />

}<br />

return IMarkerGenerator.SEVERITY_INFO;<br />

}<br />

After we've created the mapping, <strong>an</strong>y problem reported by Cppcheck with severity<br />

"style" will be displayed in <strong>Eclipse</strong> using severity SEVERITY_INFO. This mapping<br />

defines only the default values for problem severities. As you will see later, it is<br />

possible to configure this mapping from the Cod<strong>an</strong> Preferences page.<br />

For CppcheckErrorParser to be recognized by Cod<strong>an</strong>, it needs to be registered in the<br />

plugin.xml file, using the extension point org.eclipse.cdt.core.ErrorParser:<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 9 of 17


developerWorks® ibm.com/developerWorks/<br />

Listing 4. Registering the error parser<br />

<br />

<br />

<br />

<br />

<br />

Note in Listing 4 that the ErrorParser extension point was originally created for<br />

registering parsers for <strong>CDT</strong>'s build tools. It is not specific to Cod<strong>an</strong>. To indicate that<br />

CppcheckErrorParser should be used with Cod<strong>an</strong> only, we add the context "cod<strong>an</strong>."<br />

Step 3. Create the <strong>code</strong> <strong>checker</strong><br />

AbstractExternalToolBasedChecker is the superclass for <strong>an</strong>y <strong>external</strong>-tool-based<br />

Cod<strong>an</strong> <strong>code</strong> <strong>checker</strong>. It provides most of the infrastructure necessary to invoke <strong>an</strong><br />

<strong>external</strong> <strong>code</strong>-<strong>an</strong>alysis tool. Because we're integrating Cppcheck, we'll call this class<br />

CppcheckChecker.<br />

The first thing we need to do is specify the default values for the information, related<br />

to the <strong>external</strong> tool, which will be displayed on the Cod<strong>an</strong> Preferences page.<br />

This information, which should be passed to the <strong>checker</strong>'s constructor, includes the<br />

following:<br />

• The name of the <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tool, in this case Cppcheck.<br />

• The name of the tool's executable, which is cppcheck. We don't need to specify<br />

a path for the executable because we're assuming it is in the system's PATH.<br />

• The arguments to pass to the executable, which are contained in a single<br />

String. We specify "--enable=all" in order to enable all of Cppcheck's checks.<br />

Listing 5. Default Cppcheck information<br />

public CppCheckChecker() {<br />

super(new ConfigurationSettings("Cppcheck", new File("cppcheck"), "--enable=all"));<br />

}<br />

Note that Cod<strong>an</strong>'s Preferences page lets us modify both the executable path <strong>an</strong>d the<br />

arguments to pass.<br />

Mapping problem severities to problem IDs<br />

Next we w<strong>an</strong>t to specify the IDs of the error parsers we'll be using, shown in Listing 6.<br />

The IDs must be the same as the ones used in the plugin.xml file.<br />

Listing 6. Specifying the IDs of the error parser to use<br />

@Override<br />

protected String[] getParserIDs() {<br />

return new String[] { "com.dw.cdt.<strong>checker</strong>s.CppcheckErrorParser" };<br />

}<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 10 of 17


ibm.com/developerWorks/ developerWorks®<br />

Back in Listing 2, we created a ProblemMarkerInfo <strong>an</strong>d passed it to the given<br />

ErrorParserM<strong>an</strong>ager in order to create editor markers. ErrorParserM<strong>an</strong>ager will<br />

delegate the creation of editor markers to our newly created <strong>checker</strong>.<br />

To have the <strong>checker</strong> create <strong>an</strong> editor marker, we need to override the method<br />

addMarker(ProblemMarkerInfo) (whose job it is to find <strong>an</strong>other type of mismatch).<br />

Cod<strong>an</strong> <strong>checker</strong>s c<strong>an</strong>not create editor markers directly from ProblemMarkerInfos.<br />

They have their own mech<strong>an</strong>ism that uses problem IDs to figure out the appropriate<br />

severity for the created editor marker.<br />

A problem ID is a unique ID that Cod<strong>an</strong> uses to identify a <strong>code</strong> problem that is<br />

reported by a <strong>code</strong> <strong>checker</strong>. All <strong>code</strong> problems are displayed on Cod<strong>an</strong>'s Preferences<br />

page (see Figure 2).<br />

Listing 7. Creating error markers<br />

@Override<br />

public void addMarker(ProblemMarkerInfo info) {<br />

String problemId = PROBLEM_IDS.get(info.severity);<br />

String description = String.format("[cppcheck] %s", info.description);<br />

reportProblem(problemId, createProblemLocation(info), description);<br />

}<br />

To find the problem ID that corresponds to a ProblemMarkerInfo's severity, we need<br />

to create a mapping between severities <strong>an</strong>d problem IDs. Listing 8 shows how the<br />

mapping is implemented:<br />

Listing 8. Mapping problem severities to problem IDs<br />

private static final String ERROR_PROBLEM_ID =<br />

"com.dw.cdt.<strong>checker</strong>s.cppcheck.error";<br />

private static final Map PROBLEM_IDS =<br />

new HashMap();<br />

static {<br />

PROBLEM_IDS.put(<br />

IMarkerGenerator.SEVERITY_ERROR_RESOURCE, ERROR_PROBLEM_ID);<br />

PROBLEM_IDS.put(<br />

IMarkerGenerator.SEVERITY_WARNING, "com.dw.cdt.<strong>checker</strong>s.cppcheck.warning");<br />

PROBLEM_IDS.put(<br />

IMarkerGenerator.SEVERITY_INFO, "com.dw.cdt.<strong>checker</strong>s.cppcheck.style");<br />

}<br />

Code <strong>checker</strong>s that use <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tool need to indicate which of<br />

their problem IDs is considered the "reference." The reference problem ID is used to<br />

obtain the preference values of the <strong>checker</strong> (for example, the name of the <strong>external</strong><br />

tool, as shown in Listing 5). It doesn't matter which problem ID is the reference<br />

because all the problems will share preferences.<br />

Listing 9. Specifying the reference problem ID<br />

@Override<br />

protected String getReferenceProblemId() {<br />

return ERROR_PROBLEM_ID;<br />

}<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 11 of 17


developerWorks® ibm.com/developerWorks/<br />

The const<strong>an</strong>t ERROR_PROBLEM_ID is defined in Listing 8.<br />

Registering the <strong>checker</strong><br />

So that the <strong>code</strong> <strong>checker</strong> <strong>an</strong>d all the problems it reports will show up on Cod<strong>an</strong>'s<br />

Preferences page (<strong>an</strong>d thus be accessible to users), we need to register the <strong>checker</strong><br />

with Cod<strong>an</strong>'s plugin.xml file.<br />

Because we don't know all the problems that Cppcheck c<strong>an</strong> report, <strong>an</strong>d because<br />

we c<strong>an</strong>'t prevent Cppcheck from adding or removing <strong>code</strong> checks in future versions,<br />

we won't be able to register each individual problem. Instead, we'll group problems<br />

by severity <strong>an</strong>d treat each group as <strong>an</strong> individual problem. In Listing 10 we register<br />

errors, warnings, <strong>an</strong>d style violations as three individual problems:<br />

Listing 10. Registering the <strong>checker</strong> <strong>an</strong>d problem reports<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

We specify the name of the <strong>checker</strong> that will be displayed to users in the category<br />

element. The IDs of the problems should be the same as the ones used in the<br />

<strong>checker</strong> (see Listing 8).<br />

In the plugin.xml file, we specify the following about all three problems:<br />

• They are enabled by default.<br />

• They have the default severities "Error," "Warning," <strong>an</strong>d "Info," respectively.<br />

• They have the message pattern: "{0},," which forces Cod<strong>an</strong> to use the problem<br />

description as it was reported by Cppcheck.<br />

Using Cppcheck in Cod<strong>an</strong><br />

We c<strong>an</strong> now see CppcheckChecker on the Cod<strong>an</strong> Preferences page, as shown in<br />

Figure 6:<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 12 of 17


ibm.com/developerWorks/ developerWorks®<br />

Figure 6. Cppcheck displayed on Cod<strong>an</strong>'s Preferences page<br />

Figure 7 shows options for configuring how Cppcheck should report <strong>code</strong> errors:<br />

Figure 7. Configure Cppcheck's error reporting<br />

Figure 8 shows how Cppcheck reports <strong>code</strong> problems. Note that Cppcheck was<br />

automatically invoked after the file was saved.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 13 of 17


developerWorks® ibm.com/developerWorks/<br />

Figure 8. A Cppcheck report<br />

A downside to integration<br />

One limitation of integrating Cod<strong>an</strong> with <strong>external</strong> <strong>code</strong> <strong>an</strong>alysis tools is that <strong>external</strong>tool-based<br />

<strong>checker</strong>s c<strong>an</strong>not run while the user types. This is simply because <strong>an</strong><br />

<strong>external</strong> tool c<strong>an</strong>not see a file's unsaved ch<strong>an</strong>ges. So <strong>an</strong> <strong>external</strong> <strong>checker</strong> needs to<br />

be run when a file is open <strong>an</strong>d when a file is saved.<br />

This limitation is outweighed by the benefits of using a mature <strong>code</strong>-<strong>an</strong>alysis tool,<br />

however. Integrating <strong>an</strong> <strong>external</strong> tool with Cod<strong>an</strong> is also a lot easier <strong>an</strong>d simpler<br />

th<strong>an</strong> creating a regular <strong>checker</strong>, which requires deep knowledge of the C or C+<br />

+ l<strong>an</strong>guages <strong>an</strong>d <strong>CDT</strong>'s AST implementation. By contrast, we were able to write<br />

CppcheckChecker with about 100 lines of simple Java <strong>code</strong> (contained in two classes)<br />

<strong>an</strong>d 30 lines of XML.<br />

In conclusion<br />

Prior to the <strong>Eclipse</strong> Juno release, creating custom <strong>code</strong> checks for Cod<strong>an</strong> required<br />

a good underst<strong>an</strong>ding of the C/C++ l<strong>an</strong>guages <strong>an</strong>d <strong>CDT</strong>'s own AST implementation.<br />

The Juno release of <strong>Eclipse</strong> <strong>CDT</strong> resolves that issue by letting developers create<br />

Cod<strong>an</strong> <strong>code</strong> <strong>checker</strong>s, which c<strong>an</strong> then delegate the heavy lifting to <strong>an</strong> <strong>external</strong> <strong>code</strong><strong>an</strong>alysis<br />

tool.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 14 of 17


ibm.com/developerWorks/ developerWorks®<br />

In this article, we used just a little bit of Java <strong>code</strong> <strong>an</strong>d XML to integrate Cppcheck<br />

with Cod<strong>an</strong>, bringing together this popular C/C++ <strong>code</strong> <strong>an</strong>alysis tool with the built-in<br />

<strong>code</strong> <strong>an</strong>alysis framework for C/C++ programs in <strong>Eclipse</strong>. As I previously mentioned,<br />

you should be able to apply the process demonstrated here to your favorite <strong>code</strong><br />

<strong>an</strong>alysis tool. See Resources to learn more.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 15 of 17


developerWorks® ibm.com/developerWorks/<br />

Resources<br />

Learn<br />

• <strong>Eclipse</strong> C/C++ Development Tooling (<strong>CDT</strong>): Visit <strong>Eclipse</strong> <strong>CDT</strong>'s homepage.<br />

• New <strong>an</strong>d noteworthy in <strong>CDT</strong> 8.1: Find out about new features in <strong>CDT</strong> 8.1, part<br />

of the <strong>Eclipse</strong> Juno release.<br />

• "Plug-in development 101, Part 1: The fundamentals" (Chris Aniszczyk,<br />

developerWorks, February 2008): A step-by-step guide to creating <strong>Eclipse</strong> plugins.<br />

• "Developing applications using the <strong>Eclipse</strong> C/C++ Development<br />

Toolkit" (Michael Galpin, developerWorks, July 2007): Explains how to get<br />

started with <strong>CDT</strong> <strong>an</strong>d the GNU C/C++ Development Tools.<br />

• "My contribution to <strong>Eclipse</strong> <strong>CDT</strong>: <strong>external</strong>-tool-based <strong>code</strong> <strong>checker</strong>s" (Alex Ruiz,<br />

February 2012): The author introduces Google's contribution to <strong>CDT</strong>.<br />

Get products <strong>an</strong>d technologies<br />

• Download Cppcheck: Visit Cppcheck's home page.<br />

• <strong>Eclipse</strong> downloads page: See a listing of available downloads for Juno,<br />

<strong>Eclipse</strong>'s 2012 release.<br />

Discuss<br />

• Get involved in the developerWorks community. Connect with other<br />

developerWorks users while exploring dW's developer-driven blogs, forums,<br />

groups, <strong>an</strong>d wikis.<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 16 of 17


ibm.com/developerWorks/ developerWorks®<br />

About the author<br />

Alex Ruiz<br />

Alex Ruiz works in the Engineering Tools org<strong>an</strong>ization at Google Inc.<br />

He spends his spare time working on open source projects, blogging,<br />

writing technical articles, <strong>an</strong>d speaking at international conferences.<br />

Check out Alex's blog. The opinions expressed in this article represent<br />

his own <strong>an</strong>d not those of his employer.<br />

© Copyright <strong>IBM</strong> Corporation 2012<br />

(www.ibm.com/legal/copytrade.shtml)<br />

Trademarks<br />

(www.ibm.com/developerworks/ibm/trademarks/)<br />

<strong>Integrate</strong> <strong>an</strong> <strong>external</strong> <strong>code</strong> <strong>checker</strong> <strong>into</strong> <strong>Eclipse</strong> <strong>CDT</strong> Page 17 of 17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!