Search in sources :

Example 36 with SMInputCursor

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++;
        }
    }
}
Also used : CoverageMeasures(org.sonar.cxx.sensors.coverage.CoverageMeasures) SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 37 with SMInputCursor

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);
}
Also used : EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Example 38 with SMInputCursor

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);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 39 with SMInputCursor

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);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 40 with SMInputCursor

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);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Aggregations

SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)42 XMLStreamException (javax.xml.stream.XMLStreamException)9 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)8 ArrayList (java.util.ArrayList)7 SMInputFactory (org.codehaus.staxmate.SMInputFactory)5 InputFile (org.sonar.api.batch.fs.InputFile)5 File (java.io.File)3 ElementFilter (org.codehaus.staxmate.in.ElementFilter)3 SonarException (org.sonar.api.utils.SonarException)3 StaxParser (org.sonar.cxx.sensors.utils.StaxParser)3 CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)3 HashMap (java.util.HashMap)2 RuleKey (org.sonar.api.rule.RuleKey)2 CoverageMeasures (org.sonar.cxx.sensors.coverage.CoverageMeasures)2 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)2 InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1