use of org.codehaus.staxmate.in.SMInputCursor in project sonar-cxx by SonarOpenCommunity.
the class VisualStudioParser method collectRangeMeasures.
private static void collectRangeMeasures(SMInputCursor function, Map<String, CoverageMeasures> coverageData) throws XMLStreamException {
SMInputCursor range = function.childElementCursor("ranges").advance().childElementCursor("range");
CoverageMeasures builder = null;
var lastSourceId = "";
while (range.getNext() != null) {
String sourceId = range.getAttrValue("source_id");
var startLine = Integer.parseInt(range.getAttrValue("start_line"));
var endLine = Integer.parseInt(range.getAttrValue("end_line"));
// value: yes/no/partial
int covered = !"no".equalsIgnoreCase(range.getAttrValue("covered")) ? 1 : 0;
if (!sourceId.equals(lastSourceId) || builder == null) {
builder = coverageData.get(sourceId);
if (builder == null) {
builder = CoverageMeasures.create();
coverageData.put(sourceId, builder);
}
lastSourceId = sourceId;
}
while (startLine <= endLine) {
builder.setHits(startLine, covered);
startLine++;
}
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonar-cxx by SonarOpenCommunity.
the class CppcheckParser method parse.
public void parse(File report) throws javax.xml.stream.XMLStreamException {
var parser = new StaxParser(new StaxParser.XmlStreamHandler() {
/**
* {@inheritDoc}
*/
@Override
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
var parsed = false;
try {
rootCursor.advance();
} catch (com.ctc.wstx.exc.WstxEOFException e) {
throw new EmptyReportException("The 'Cppcheck V2' report is empty", e);
}
try {
String version = rootCursor.getAttrValue("version");
if ("2".equals(version)) {
SMInputCursor errorsCursor = rootCursor.childElementCursor("errors");
if (errorsCursor.getNext() != null) {
parsed = true;
SMInputCursor errorCursor = errorsCursor.childElementCursor("error");
while (errorCursor.getNext() != null) {
processErrorTag(errorCursor);
}
}
}
} catch (RuntimeException e) {
throw new XMLStreamException("parsing failed", e);
}
if (!parsed) {
throw new XMLStreamException();
}
}
private void processErrorTag(SMInputCursor errorCursor) throws XMLStreamException {
String id = requireAttributeSet(errorCursor.getAttrValue("id"), "Missing mandatory attribute /results/errors/error[@id]");
String msg = requireAttributeSet(errorCursor.getAttrValue("msg"), "Missing mandatory attribute /results/errors/error[@msg]");
boolean isInconclusive = "true".equals(errorCursor.getAttrValue("inconclusive"));
String issueText = createIssueText(msg, isInconclusive);
CxxReportIssue issue = null;
SMInputCursor locationCursor = errorCursor.childElementCursor("location");
while (locationCursor.getNext() != null) {
String file = locationCursor.getAttrValue("file");
String line = locationCursor.getAttrValue("line");
String info = locationCursor.getAttrValue("info");
if (file != null) {
file = file.replace('\\', '/');
}
if ("*".equals(file)) {
// findings on project level
file = null;
line = null;
info = null;
}
final boolean isLocationInProject = isLocationInProject(file);
if (issue == null) {
// the current module) we are not interested in this <error>
if (!isLocationInProject) {
LOG.debug("Cppcheck issue outside of project, skipping issue: {}:{} {}:{}", file, line, id, msg);
return;
}
issue = new CxxReportIssue(id, file, line, null, issueText);
// information about the flow/analysis
if (info != null && !msg.equals(info)) {
issue.addLocation(file, line, null, info);
}
} else if (info != null) {
// info
if (isLocationInProject) {
issue.addLocation(file, line, null, info);
} else {
var primaryLocation = issue.getLocations().get(0);
String primaryFile = primaryLocation.getFile();
String primaryLine = primaryLocation.getLine();
var extendedInfo = new StringBuilder(512);
extendedInfo.append(makeRelativePath(file, primaryFile)).append(":").append(line).append(" ").append(info);
issue.addLocation(primaryFile, primaryLine, null, extendedInfo.toString());
}
}
}
// no <location> tags: issue raised on the whole module/project
if (issue == null) {
issue = new CxxReportIssue(id, null, null, null, issueText);
}
sensor.saveUniqueViolation(issue);
}
private String makeRelativePath(String path, String basePath) {
try {
return Paths.get(basePath).relativize(Paths.get(path)).toString();
} catch (IllegalArgumentException e) {
LOG.warn("Can't create relative path: basePath='{}', path='{}'", basePath, path);
return path;
}
}
private boolean isLocationInProject(@Nullable String file) {
// project/module
return (file == null) || (sensor.getInputFileIfInProject(file) != null);
}
});
parser.parse(report);
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonar-cxx by SonarOpenCommunity.
the class BullseyeParser method recTreeWalk.
private void recTreeWalk(String refPath, SMInputCursor folder, List<String> path, final Map<String, CoverageMeasures> coverageData) throws XMLStreamException {
String correctPath = ensureRefPathIsCorrect(refPath);
SMInputCursor child = folder.childElementCursor();
while (child.getNext() != null) {
String folderChildName = child.getLocalName();
String name = child.getAttrValue("name");
path.add(name);
if ("src".equalsIgnoreCase(folderChildName)) {
String filePath = buildPath(path, correctPath);
var fileMeasuresBuilderIn = CoverageMeasures.create();
fileWalk(child, fileMeasuresBuilderIn);
coverageData.put(filePath, fileMeasuresBuilderIn);
} else {
recTreeWalk(correctPath, child, path, coverageData);
}
path.remove(path.size() - 1);
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonar-cxx by SonarOpenCommunity.
the class BullseyeParser method recTreeTopWalk.
private void recTreeTopWalk(File fileName, SMInputCursor folder, final Map<String, CoverageMeasures> coverageData) throws XMLStreamException {
SMInputCursor child = folder.childElementCursor();
while (child.getNext() != null) {
var fileMeasuresBuilderIn = CoverageMeasures.create();
funcWalk(child, fileMeasuresBuilderIn);
coverageData.put(fileName.getPath(), fileMeasuresBuilderIn);
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonar-cxx by SonarOpenCommunity.
the class BullseyeParser method funcWalk.
private void funcWalk(SMInputCursor func, CoverageMeasures fileMeasuresBuilderIn) throws XMLStreamException {
SMInputCursor prob = func.childElementCursor("probe");
while (prob.getNext() != null) {
probWalk(prob, fileMeasuresBuilderIn);
}
saveConditions(fileMeasuresBuilderIn);
}
Aggregations