-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1161 from gyorb/clangsa
parse Clang Static Analyzer plist reports
- Loading branch information
Showing
13 changed files
with
2,101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangsa/CxxClangSARuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangsa/CxxClangSASensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
cxx-sensors/src/main/java/org/sonar/cxx/sensors/clangsa/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.