Skip to content

Commit

Permalink
Catch PC-Lint MISRA C++ messages
Browse files Browse the repository at this point in the history
Added check for default PC-Lint MISRA C++ 2008 message identifier.  Rules already exist.

Added additional check I missed before

Added tests for MISRA C++ PC-Lint sensor
  • Loading branch information
dkt01 committed Jul 30, 2016
1 parent 600fc4e commit 3fc6c1b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Expand Up @@ -94,8 +94,8 @@ public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
String msg = errorCursor.getAttrValue("desc");

if (isInputValid(file, line, id, msg)) {
//remap MISRA IDs. Only Unique rules for MISRA 2004 and 2008 has been created in the rule repository
if (msg.contains("MISRA 2004") || msg.contains("MISRA 2008")) {
//remap MISRA IDs. Only Unique rules for MISRA C 2004 and MISRA C/C++ 2008 have been created in the rule repository
if (msg.contains("MISRA 2004") || msg.contains("MISRA 2008") || msg.contains("MISRA C++ 2008") || msg.contains("MISRA C++ Rule")) {
id = mapMisraRulesToUniqueSonarRules(msg);
}
saveUniqueViolation(context, CxxPCLintRuleRepository.KEY,
Expand Down
Expand Up @@ -19,6 +19,8 @@
*/
package org.sonar.plugins.cxx.pclint;

import java.util.ArrayList;

import static org.fest.assertions.Assertions.assertThat;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.config.Settings;
Expand All @@ -29,6 +31,7 @@

import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;

public class CxxPCLintSensorTest {
private DefaultFileSystem fs;
Expand Down Expand Up @@ -74,6 +77,20 @@ public void shouldReportCorrectMisra2004PcLint9Violations() {
assertThat(context.allIssues()).hasSize(1);
}

@Test
public void shouldReportCorrectMisraCppViolations() {
SensorContextTester context = SensorContextTester.create(fs.baseDir());
Settings settings = new Settings();
settings.setProperty(CxxPCLintSensor.REPORT_PATH_KEY, "pclint-reports/pclint-result-MISRACPP.xml");
context.fileSystem().add(new DefaultInputFile("myProjectKey", "test.c").setLanguage("cpp").initMetadata(new String("asd\nasdas\nasda\n")));
CxxPCLintSensor sensor = new CxxPCLintSensor(settings);
sensor.execute(context);
assertThat(context.allIssues()).hasSize(2);
ArrayList<Issue> issuesList = new ArrayList<Issue>(context.allIssues());
assertThat(issuesList.get(0).ruleKey().rule()).isEqualTo("M5-0-19");
assertThat(issuesList.get(1).ruleKey().rule()).isEqualTo("M18-4-1");
}

@Test
public void shouldNotSaveIssuesWhenMisra2004DescIsWrong() {
SensorContextTester context = SensorContextTester.create(fs.baseDir());
Expand Down
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<issue file =".\test.c" line = "4" number = "1960" desc = "Violates MISRA C++ 2008 Required Rule 5-0-19, More than two pointer indirection levels used for type: 'struct _wireSAFEARRAY ***"/>
<issue file =".\test.c" line = "10" number = "586" desc = "operator 'new' is deprecated. [MISRA C++ Rule 18-4-1]"/>
</result>

0 comments on commit 3fc6c1b

Please sign in to comment.