Search in sources :

Example 1 with CxxReportIssue

use of org.sonar.cxx.utils.CxxReportIssue 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 CxxReportIssue

use of org.sonar.cxx.utils.CxxReportIssue 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 CxxReportIssue

use of org.sonar.cxx.utils.CxxReportIssue 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)

Example 4 with CxxReportIssue

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

the class ClangTidyParser method parse.

public void parse(File report, String defaultEncoding) throws IOException {
    try (var scanner = new TextScanner(report, defaultEncoding)) {
        LOG.debug("Encoding='{}'", scanner.encoding());
        CxxReportIssue currentIssue = null;
        while (scanner.hasNextLine()) {
            if (!parseLine(scanner.nextLine())) {
                continue;
            }
            if ("note".equals(issue.level)) {
                if (currentIssue != null) {
                    currentIssue.addFlowElement(issue.path, issue.line, issue.column, issue.info);
                }
            } else {
                if (currentIssue != null) {
                    sensor.saveUniqueViolation(currentIssue);
                }
                currentIssue = new CxxReportIssue(issue.ruleId, issue.path, issue.line, issue.column, issue.info);
                for (var aliasRuleId : issue.aliasRuleIds) {
                    currentIssue.addAliasRuleId(aliasRuleId);
                }
            }
        }
        if (currentIssue != null) {
            sensor.saveUniqueViolation(currentIssue);
        }
    }
}
Also used : TextScanner(org.sonar.cxx.sensors.utils.TextScanner) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Example 5 with CxxReportIssue

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

the class CxxCompilerSensor method processReport.

@Override
protected void processReport(File report) {
    final String reportEncoding = getEncoding();
    final String reportRegEx = getRegex();
    if (reportRegEx.isEmpty()) {
        LOG.error("processReport terminated because of empty custom regular expression");
        return;
    }
    if (!reportRegEx.contains("(?<")) {
        LOG.error("processReport terminated because regular expression contains no named-capturing group");
        return;
    }
    try (var scanner = new TextScanner(report, reportEncoding)) {
        var pattern = Pattern.compile(reportRegEx);
        LOG.debug("Processing '{}' report '{}', Encoding='{}', Pattern='{}'", getCompilerKey(), report, scanner.encoding(), pattern);
        while (scanner.hasNextLine()) {
            var matcher = pattern.matcher(scanner.nextLine());
            if (matcher.find()) {
                String filename = alignFilename(getSubSequence(matcher, "file"));
                String line = alignLine(getSubSequence(matcher, "line"));
                String column = alignColumn(getSubSequence(matcher, "column"));
                String id = alignId(getSubSequence(matcher, "id"));
                String msg = alignMessage(getSubSequence(matcher, "message"));
                if (isInputValid(filename, line, column, id, msg)) {
                    var issue = new CxxReportIssue(id, filename, line, column, msg);
                    saveUniqueViolation(issue);
                } else {
                    LOG.debug("Invalid compiler warning: '{}''{}', skipping", id, msg);
                }
            }
        }
    } catch (java.io.IOException | java.lang.IllegalArgumentException | java.lang.IllegalStateException e) {
        throw new InvalidReportException("The compiler report is invalid", e);
    }
}
Also used : TextScanner(org.sonar.cxx.sensors.utils.TextScanner) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Aggregations

CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)15 InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)6 SourceFile (org.sonar.cxx.squidbridge.api.SourceFile)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 SoftAssertions (org.assertj.core.api.SoftAssertions)3 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)3 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)3 Test (org.junit.Test)3 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)3 StaxParser (org.sonar.cxx.sensors.utils.StaxParser)3 CxxReportLocation (org.sonar.cxx.utils.CxxReportLocation)3 IOException (java.io.IOException)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