Search in sources :

Example 1 with InvalidReportException

use of org.sonar.cxx.sensors.utils.InvalidReportException in project sonar-cxx by SonarOpenCommunity.

the class InferParser method parse.

public void parse(File report) {
    InferIssue[] inferIssues;
    try {
        try (var reader = new JsonReader(new FileReader(report))) {
            inferIssues = new Gson().fromJson(reader, InferIssue[].class);
            if (inferIssues == null) {
                throw new EmptyReportException("The 'Infer JSON' report is empty");
            }
        }
    } catch (IOException e) {
        throw new InvalidReportException("The 'Infer JSON' report is invalid", e);
    }
    for (var issue : inferIssues) {
        if (issue.getFile() != null) {
            var cxxReportIssue = new CxxReportIssue(issue.getBugType(), issue.getFile(), String.valueOf(issue.getLine()), null, issue.getQualifier());
            sensor.saveUniqueViolation(cxxReportIssue);
        } else {
            LOG.debug("Invalid infer issue '{}', skipping", issue.toString());
        }
    }
}
Also used : EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) JsonReader(com.google.gson.stream.JsonReader) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) IOException(java.io.IOException) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Example 2 with InvalidReportException

use of org.sonar.cxx.sensors.utils.InvalidReportException in project sonar-cxx by SonarOpenCommunity.

the class CxxOtherSensor method processReport.

@Override
public void processReport(File report) {
    try {
        var parser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            rootCursor.advance();
            SMInputCursor errorCursor = rootCursor.childElementCursor("error");
            while (errorCursor.getNext() != null) {
                String file = errorCursor.getAttrValue("file");
                String line = errorCursor.getAttrValue("line");
                String column = errorCursor.getAttrValue("column");
                String id = errorCursor.getAttrValue("id");
                String msg = errorCursor.getAttrValue("msg");
                var issue = new CxxReportIssue(id, file, line, column, msg);
                saveUniqueViolation(issue);
            }
        });
        parser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("The 'other' report is invalid", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Example 3 with InvalidReportException

use of org.sonar.cxx.sensors.utils.InvalidReportException in project sonar-cxx by SonarOpenCommunity.

the class CoberturaParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, CoverageMeasures> parse(File report) {
    var coverageData = new HashMap<String, CoverageMeasures>();
    try {
        baseDir = Paths.get(".");
        var sourceParser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            try {
                rootCursor.advance();
            } catch (com.ctc.wstx.exc.WstxEOFException e) {
                throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + this + ")", e);
            }
            readBaseDir(rootCursor.descendantElementCursor("source"));
        });
        sourceParser.parse(report);
        var packageParser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            rootCursor.advance();
            collectPackageMeasures(rootCursor.descendantElementCursor("package"), coverageData);
        });
        packageParser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("Cobertura coverage report '" + report + "' cannot be parsed.", e);
    }
    return coverageData;
}
Also used : SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException)

Example 4 with InvalidReportException

use of org.sonar.cxx.sensors.utils.InvalidReportException in project sonar-cxx by SonarOpenCommunity.

the class TestwellCtcTxtParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, CoverageMeasures> parse(File report) {
    var coverageData = new HashMap<String, CoverageMeasures>();
    try (var scanner = new TextScanner(report, StandardCharsets.UTF_8.name())) {
        scanner.useDelimiter(SECTION_SEP);
        var headerMatcher = FILE_HEADER.matcher(scanner.next());
        while (parseUnit(scanner, coverageData, headerMatcher)) {
            headerMatcher.reset(scanner.next());
        }
    } catch (IOException | NoSuchElementException e) {
        throw new InvalidReportException("Testwell CTC++ coverage report '" + report + "' cannot be parsed.", e);
    }
    return coverageData;
}
Also used : TextScanner(org.sonar.cxx.sensors.utils.TextScanner) HashMap(java.util.HashMap) IOException(java.io.IOException) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with InvalidReportException

use of org.sonar.cxx.sensors.utils.InvalidReportException in project sonar-cxx by SonarOpenCommunity.

the class CxxClangSASensor method processReport.

@Override
protected void processReport(File report) {
    try {
        var f = new File(report.getPath());
        var rootDict = (NSDictionary) PropertyListParser.parse(f);
        NSObject[] diagnostics = ((NSArray) require(rootDict.objectForKey("diagnostics"), "Missing mandatory entry 'diagnostics'")).getArray();
        NSObject[] sourceFiles = ((NSArray) require(rootDict.objectForKey("files"), "Missing mandatory entry 'files'")).getArray();
        for (var diagnostic : diagnostics) {
            var diag = (NSDictionary) diagnostic;
            String description = ((NSString) require(diag.get("description"), "Missing mandatory entry 'diagnostics/description'")).getContent();
            String checkerName = ((NSString) require(diag.get("check_name"), "Missing mandatory entry 'diagnostics/check_name'")).getContent();
            var location = (NSDictionary) require(diag.get("location"), "Missing mandatory entry 'diagnostics/location'");
            var line = ((NSNumber) require(location.get("line"), "Missing mandatory entry 'diagnostics/location/line'")).intValue();
            var column = ((NSNumber) require(location.get("col"), "Missing mandatory entry 'diagnostics/location/col'")).intValue();
            var fileIndex = ((NSNumber) require(location.get("file"), "Missing mandatory entry 'diagnostics/location/file'")).intValue();
            if (fileIndex < 0 || fileIndex >= sourceFiles.length) {
                throw new IllegalArgumentException("Invalid file index");
            }
            String filePath = ((NSString) sourceFiles[fileIndex]).getContent();
            var issue = new CxxReportIssue(checkerName, filePath, Integer.toString(line), Integer.toString(column), description);
            addFlowToIssue(diag, sourceFiles, issue);
            saveUniqueViolation(issue);
        }
    } catch (Exception e) {
        throw new InvalidReportException("The 'Clang Static Analyzer' report is invalid", e);
    }
}
Also used : NSObject(com.dd.plist.NSObject) NSArray(com.dd.plist.NSArray) NSDictionary(com.dd.plist.NSDictionary) NSString(com.dd.plist.NSString) NSString(com.dd.plist.NSString) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) NSNumber(com.dd.plist.NSNumber) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) File(java.io.File) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Aggregations

InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)12 XMLStreamException (javax.xml.stream.XMLStreamException)6 CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)6 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)5 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)5 StaxParser (org.sonar.cxx.sensors.utils.StaxParser)5 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)2 TextScanner (org.sonar.cxx.sensors.utils.TextScanner)2 NSArray (com.dd.plist.NSArray)1 NSDictionary (com.dd.plist.NSDictionary)1 NSNumber (com.dd.plist.NSNumber)1 NSObject (com.dd.plist.NSObject)1 NSString (com.dd.plist.NSString)1 Gson (com.google.gson.Gson)1 JsonReader (com.google.gson.stream.JsonReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 NoSuchElementException (java.util.NoSuchElementException)1