Skip to content

Commit

Permalink
parse Clang Static Analyzer plist reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyorgy Orban committed Jun 13, 2017
1 parent c262545 commit 80b464d
Show file tree
Hide file tree
Showing 13 changed files with 2,101 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cxx-sensors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.plist/dd-plist -->
<dependency>
<groupId>com.googlecode.plist</groupId>
<artifactId>dd-plist</artifactId>
<version>1.19</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.sensors.clangsa;

import org.sonar.api.platform.ServerFileSystem;
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.sensors.utils.CxxAbstractRuleRepository;

/**
* {@inheritDoc}
*/
public class CxxClangSARuleRepository extends CxxAbstractRuleRepository {

public static final String KEY = "ClangSA";
public static final String CUSTOM_RULES_KEY = "clangsa.customRules";
private static final String NAME = "Clang-SA";

/**
* {@inheritDoc}
*/
public CxxClangSARuleRepository(ServerFileSystem fileSystem, RulesDefinitionXmlLoader xmlRuleLoader, CxxLanguage language) {
super(fileSystem, xmlRuleLoader, KEY, NAME, CUSTOM_RULES_KEY, language);
}

@Override
protected String fileName() {
return "/clangsa.xml";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.sensors.clangsa;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import javax.xml.parsers.ParserConfigurationException;

import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.CxxLanguage;
import org.sonar.cxx.sensors.utils.CxxReportSensor;

import org.xml.sax.SAXException;

import com.dd.plist.PropertyListParser;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSArray;
import com.dd.plist.NSObject;
import com.dd.plist.NSString;
import com.dd.plist.PropertyListFormatException;
import com.dd.plist.NSNumber;


/**
* Sensor for Clang Static Analyzer.
*
*/
public class CxxClangSASensor extends CxxReportSensor {
public static final Logger LOG = Loggers.get(CxxClangSASensor.class);
public static final String REPORT_PATH_KEY = "clangsa.reportPath";
public static String KEY = "ClangSA";

/**
* {@inheritDoc}
*/
public CxxClangSASensor(CxxLanguage language) {
super(language);
}

@Override
protected String reportPathKey() {
return REPORT_PATH_KEY;
}

@Override
public void describe(SensorDescriptor descriptor) {
descriptor.onlyOnLanguage(this.language.getKey()).name(language.getName() + " ClangSASensor");
}

@Override
protected void processReport(final SensorContext context, File report)
throws javax.xml.stream.XMLStreamException {

LOG.debug("Processing clangsa report '{}''", report.getName());

try {
File f = new File(report.getPath());

NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(f);

// Array of file paths where an issue was detected.
NSObject[] sourceFiles = ((NSArray)rootDict.objectForKey("files")).getArray();

NSObject[] diagnostics = ((NSArray)rootDict.objectForKey("diagnostics")).getArray();

for(NSObject diagnostic:diagnostics){
NSDictionary diag = (NSDictionary)diagnostic;

NSString desc = (NSString)diag.get("description");
String description = desc.getContent();

String checkerName = ((NSString)diag.get("check_name")).getContent();

NSDictionary location = (NSDictionary)diag.get("location");

Integer line = ((NSNumber)location.get("line")).intValue();

NSNumber fileIndex = (NSNumber)location.get("file");

NSObject filePath = sourceFiles[fileIndex.intValue()];

saveUniqueViolation(context,
CxxClangSARuleRepository.KEY,
((NSString)filePath).getContent(),
line.toString(),
checkerName,
description);

}
} catch (final java.io.IOException
| java.text.ParseException
| javax.xml.parsers.ParserConfigurationException
| org.xml.sax.SAXException
| com.dd.plist.PropertyListFormatException e){

LOG.error("Failed to parse clangsa report: {}", e);

}
}

@Override
protected String getSensorKey() {
return KEY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2017 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/**
* Package with sensor to evaluate Cppcheck specific report files.
*/
@ParametersAreNonnullByDefault
package org.sonar.cxx.sensors.clangsa;

import javax.annotation.ParametersAreNonnullByDefault;
Loading

0 comments on commit 80b464d

Please sign in to comment.