private static void parseIssues(InputFile file, SensorContext context, Map<Integer, TextPointer> startPositions, Map<Integer, TextPointer> endPositions) { int currentLine = 0; try { for (String lineStr : Files.readAllLines(file.path(), file.charset())) { currentLine++; Matcher m = START_ISSUE_PATTERN.matcher(lineStr); while (m.find()) { Integer issueId = Integer.parseInt(m.group(1)); TextPointer newPointer = file.newPointer(currentLine, m.end()); startPositions.put(issueId, newPointer); } m = END_ISSUE_PATTERN.matcher(lineStr); while (m.find()) { Integer issueId = Integer.parseInt(m.group(1)); TextPointer newPointer = file.newPointer(currentLine, m.start()); endPositions.put(issueId, newPointer); } } } catch (IOException e) { throw new IllegalStateException("Unable to read file", e); } }
Integer issueFlowId = Integer.parseInt(m.group(2)); Integer issueFlowNum = Integer.parseInt(m.group(3)); TextPointer newPointer = file.newPointer(currentLine, m.end()); if (!startFlowsPositions.containsKey(issueId)) { startFlowsPositions.put(issueId, HashBasedTable.create()); Integer issueFlowId = Integer.parseInt(m.group(2)); Integer issueFlowNum = Integer.parseInt(m.group(3)); TextPointer newPointer = file.newPointer(currentLine, m.start()); if (!endFlowsPositions.containsKey(issueId)) { endFlowsPositions.put(issueId, HashBasedTable.create());
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
private void processLine(String fileLine, InputFile inputFile, SensorContext context) { String[] textPointer = fileLine.split(","); if (textPointer.length != 3) { throw new IllegalStateException("Invalid format in error file"); } try { int line = Integer.parseInt(textPointer[0]); int lineOffset = Integer.parseInt(textPointer[1]); String msg = textPointer[2]; context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(line, lineOffset)) .message(msg) .save(); } catch (NumberFormatException e) { throw new IllegalStateException("Invalid format in error file", e); } }
@CheckForNull private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) { InputFile file = findFile(context, location.filePath); if (file != null) { newLocation .on(file); if (location.message != null) { newLocation.message(location.message); } if (location.textRange != null) { if (location.textRange.startColumn != null) { TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn); TextPointer end = file.newPointer(location.textRange.endLine, location.textRange.endColumn); newLocation.at(file.newRange(start, end)); } else { newLocation.at(file.selectLine(location.textRange.startLine)); } } return newLocation; } return null; }
/** * Get highlighting types at a given position in an inputfile * * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); if (!reader.hasSyntaxHighlighting(ref)) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<>(); try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) { while (it.hasNext()) { ScannerReport.SyntaxHighlightingRule rule = it.next(); TextRange ruleRange = toRange(file, rule.getRange()); if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { result.add(ScannerReportUtils.toBatchType(rule.getType())); } } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + file, e); } return result; }
private static void parseIssues(InputFile file, SensorContext context, Map<Integer, TextPointer> startPositions, Map<Integer, TextPointer> endPositions) { int currentLine = 0; try { for (String lineStr : Files.readAllLines(file.path(), file.charset())) { currentLine++; Matcher m = START_ISSUE_PATTERN.matcher(lineStr); while (m.find()) { Integer issueId = Integer.parseInt(m.group(1)); TextPointer newPointer = file.newPointer(currentLine, m.end()); startPositions.put(issueId, newPointer); } m = END_ISSUE_PATTERN.matcher(lineStr); while (m.find()) { Integer issueId = Integer.parseInt(m.group(1)); TextPointer newPointer = file.newPointer(currentLine, m.start()); endPositions.put(issueId, newPointer); } } } catch (IOException e) { throw new IllegalStateException("Unable to read file", e); } }
private static TextRange toRange(InputFile file, BatchReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
private static TextRange toRange(InputFile file, ScannerReport.TextRange reportRange) { return file.newRange(file.newPointer(reportRange.getStartLine(), reportRange.getStartOffset()), file.newPointer(reportRange.getEndLine(), reportRange.getEndOffset())); }
private void processLine(String fileLine, InputFile inputFile, SensorContext context) { String[] textPointer = fileLine.split(","); if (textPointer.length != 3) { throw new IllegalStateException("Invalid format in error file"); } try { int line = Integer.parseInt(textPointer[0]); int lineOffset = Integer.parseInt(textPointer[1]); String msg = textPointer[2]; context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(line, lineOffset)) .message(msg) .save(); } catch (NumberFormatException e) { throw new IllegalStateException("Invalid format in error file", e); } }
@CheckForNull private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) { InputFile file = findFile(context, location.filePath); if (file != null) { newLocation .on(file); if (location.message != null) { newLocation.message(location.message); } if (location.textRange != null) { if (location.textRange.startColumn != null) { TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn); TextPointer end = file.newPointer(location.textRange.endLine, location.textRange.endColumn); newLocation.at(file.newRange(start, end)); } else { newLocation.at(file.selectLine(location.textRange.startLine)); } } return newLocation; } return null; }
/** * Get highlighting types at a given position in an inputfile * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); if (!reader.hasSyntaxHighlighting(ref)) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<>(); try (CloseableIterator<BatchReport.SyntaxHighlighting> it = reader.readComponentSyntaxHighlighting(ref)) { while (it.hasNext()) { BatchReport.SyntaxHighlighting rule = it.next(); TextRange ruleRange = toRange(file, rule.getRange()); if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { result.add(BatchReportUtils.toBatchType(rule.getType())); } } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e); } return result; }
static void reportAnalysisErrors(SensorContext sensorContext, SensorContextUtils.AnalysisResponse response, InputFile inputFile) { for (Diagnostic diagnostic : response.diagnostics) { LOG.error("Compilation error at {}:{} \"{}\"", inputFile, diagnostic.line, diagnostic.message); LOG.error("File {} will be ignored", inputFile, diagnostic.message); sensorContext.newAnalysisError() .onFile(inputFile) .message(diagnostic.message) .at(inputFile.newPointer(diagnostic.line, diagnostic.col)) .save(); } }
private void processRecognitionException(SightlyCompilerException e, SensorContext sensorContext, InputFile inputFile) { if (parsingErrorRuleKey != null) { processRecognitionExceptionForCustomRule(e, sensorContext, inputFile); } int lineOffset = 0; sensorContext.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), lineOffset)) .message(e.getMessage()) .save(); }
private void processRecognitionException(RecognitionException e, SensorContext sensorContext, InputFile inputFile) { if (parsingErrorRuleKey != null) { NewIssue newIssue = sensorContext.newIssue(); NewIssueLocation primaryLocation = newIssue.newLocation() .message(ParsingErrorCheck.MESSAGE) .on(inputFile) .at(inputFile.selectLine(e.getLine())); newIssue .forRule(parsingErrorRuleKey) .at(primaryLocation) .save(); } sensorContext.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); }
private void processRecognitionException(RecognitionException e, SensorContext sensorContext, InputFile inputFile) { if (parsingErrorRuleKey != null) { NewIssue newIssue = sensorContext.newIssue(); NewIssueLocation primaryLocation = newIssue.newLocation() .message(ParsingErrorCheck.MESSAGE) .on(inputFile) .at(inputFile.selectLine(e.getLine())); newIssue .forRule(parsingErrorRuleKey) .at(primaryLocation) .save(); } sensorContext.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); }
private void scanFile(InputFile inputFile) { PerlFile pythonFile = SonarQubePerlFile.create(inputFile); PerlVisitorContext visitorContext; try { visitorContext = new PerlVisitorContext(parser.parse(pythonFile.content()), pythonFile); } catch (RecognitionException e) { visitorContext = new PerlVisitorContext(pythonFile, e); LOG.error("Unable to parse file: " + inputFile.toString()); LOG.error(e.getMessage()); context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); } new PerlHighlighter(context, inputFile).scanFile(visitorContext); }
/** * Creates and saves an issue for a parsing error. */ private void saveParsingIssue(SensorContext context, RecognitionException e, InputFile inputFile) { if (parsingErrorRuleKey != null) { NewIssue issue = context.newIssue(); NewIssueLocation location = issue.newLocation() .message(e.getMessage()) .on(inputFile) .at(inputFile.selectLine(e.getLine())); issue .forRule(parsingErrorRuleKey) .at(location) .save(); } context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); }
private void scanFile(InputFile inputFile) { PythonFile pythonFile = SonarQubePythonFile.create(inputFile); PythonVisitorContext visitorContext; try { visitorContext = new PythonVisitorContext(parser.parse(pythonFile.content()), pythonFile); saveMeasures(inputFile, visitorContext); } catch (RecognitionException e) { visitorContext = new PythonVisitorContext(pythonFile, e); LOG.error("Unable to parse file: " + inputFile.absolutePath()); LOG.error(e.getMessage()); context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); } for (PythonCheck check : checks.all()) { saveIssues(inputFile, check, check.scanFileForIssues(visitorContext)); } new PythonHighlighter(context, inputFile).scanFile(visitorContext); }
private void scanFile(InputFile inputFile) { PythonFile pythonFile = SonarQubePythonFile.create(inputFile); PythonVisitorContext visitorContext; try { visitorContext = new PythonVisitorContext(parser.parse(pythonFile.content()), pythonFile); saveMeasures(inputFile, visitorContext); } catch (RecognitionException e) { visitorContext = new PythonVisitorContext(pythonFile, e); LOG.error("Unable to parse file: " + inputFile.absolutePath()); LOG.error(e.getMessage()); context.newAnalysisError() .onFile(inputFile) .at(inputFile.newPointer(e.getLine(), 0)) .message(e.getMessage()) .save(); } for (PythonCheck check : checks.all()) { saveIssues(inputFile, check, check.scanFileForIssues(visitorContext)); } new PythonHighlighter(context, inputFile).scanFile(visitorContext); }