You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SymbolsLineReader.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.source.linereader;
  21. import com.google.common.collect.HashMultimap;
  22. import com.google.common.collect.Lists;
  23. import com.google.common.collect.SetMultimap;
  24. import java.util.Comparator;
  25. import java.util.IdentityHashMap;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Optional;
  30. import org.sonar.api.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. import org.sonar.ce.task.projectanalysis.component.Component;
  33. import org.sonar.db.protobuf.DbFileSources;
  34. import org.sonar.scanner.protocol.output.ScannerReport;
  35. import static java.lang.String.format;
  36. import static org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter.OFFSET_SEPARATOR;
  37. import static org.sonar.ce.task.projectanalysis.source.linereader.RangeOffsetConverter.SYMBOLS_SEPARATOR;
  38. public class SymbolsLineReader implements LineReader {
  39. private static final Logger LOG = Loggers.get(SymbolsLineReader.class);
  40. private final Component file;
  41. private final RangeOffsetConverter rangeOffsetConverter;
  42. private final Map<ScannerReport.Symbol, Integer> idsBySymbol;
  43. private final SetMultimap<Integer, ScannerReport.Symbol> symbolsPerLine;
  44. private ReadError readError = null;
  45. public SymbolsLineReader(Component file, Iterator<ScannerReport.Symbol> symbolIterator, RangeOffsetConverter rangeOffsetConverter) {
  46. this.file = file;
  47. this.rangeOffsetConverter = rangeOffsetConverter;
  48. List<ScannerReport.Symbol> symbols = Lists.newArrayList(symbolIterator);
  49. // Sort symbols to have deterministic id generation
  50. symbols.sort(SymbolsComparator.INSTANCE);
  51. this.idsBySymbol = createIdsBySymbolMap(symbols);
  52. this.symbolsPerLine = buildSymbolsPerLine(symbols);
  53. }
  54. /**
  55. * Stops reading at first encountered error, which implies the same
  56. * {@link org.sonar.ce.task.projectanalysis.source.linereader.LineReader.ReadError} will be returned once an error
  57. * has been encountered and for any then provided {@link DbFileSources.Line.Builder lineBuilder}.
  58. */
  59. @Override
  60. public Optional<ReadError> read(DbFileSources.Line.Builder lineBuilder) {
  61. if (readError == null) {
  62. try {
  63. processSymbols(lineBuilder);
  64. } catch (RangeOffsetConverter.RangeOffsetConverterException e) {
  65. readError = new ReadError(Data.SYMBOLS, lineBuilder.getLine());
  66. LOG.warn(format("Inconsistency detected in Symbols data. Symbols will be ignored for file '%s'", file.getDbKey()), e);
  67. }
  68. }
  69. return Optional.ofNullable(readError);
  70. }
  71. private void processSymbols(DbFileSources.Line.Builder lineBuilder) {
  72. int line = lineBuilder.getLine();
  73. // Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files
  74. // data
  75. StringBuilder symbolString = new StringBuilder();
  76. symbolsPerLine.get(line).stream().sorted(SymbolsComparator.INSTANCE).forEach(lineSymbol -> {
  77. int symbolId = idsBySymbol.get(lineSymbol);
  78. appendSymbol(symbolString, lineSymbol.getDeclaration(), line, symbolId, lineBuilder.getSource());
  79. for (ScannerReport.TextRange range : lineSymbol.getReferenceList()) {
  80. appendSymbol(symbolString, range, line, symbolId, lineBuilder.getSource());
  81. }
  82. });
  83. if (symbolString.length() > 0) {
  84. lineBuilder.setSymbols(symbolString.toString());
  85. }
  86. }
  87. private void appendSymbol(StringBuilder lineSymbol, ScannerReport.TextRange range, int line, int symbolId, String sourceLine) {
  88. if (matchLine(range, line)) {
  89. String offsets = rangeOffsetConverter.offsetToString(range, line, sourceLine.length());
  90. if (!offsets.isEmpty()) {
  91. if (lineSymbol.length() > 0) {
  92. lineSymbol.append(SYMBOLS_SEPARATOR);
  93. }
  94. lineSymbol.append(offsets)
  95. .append(OFFSET_SEPARATOR)
  96. .append(symbolId);
  97. }
  98. }
  99. }
  100. private static boolean matchLine(ScannerReport.TextRange range, int line) {
  101. return range.getStartLine() <= line && range.getEndLine() >= line;
  102. }
  103. private static Map<ScannerReport.Symbol, Integer> createIdsBySymbolMap(List<ScannerReport.Symbol> symbols) {
  104. Map<ScannerReport.Symbol, Integer> map = new IdentityHashMap<>(symbols.size());
  105. int symbolId = 1;
  106. for (ScannerReport.Symbol symbol : symbols) {
  107. map.put(symbol, symbolId);
  108. symbolId++;
  109. }
  110. return map;
  111. }
  112. private static SetMultimap<Integer, ScannerReport.Symbol> buildSymbolsPerLine(List<ScannerReport.Symbol> symbols) {
  113. SetMultimap<Integer, ScannerReport.Symbol> res = HashMultimap.create();
  114. for (ScannerReport.Symbol symbol : symbols) {
  115. putForTextRange(res, symbol, symbol.getDeclaration());
  116. for (ScannerReport.TextRange textRange : symbol.getReferenceList()) {
  117. putForTextRange(res, symbol, textRange);
  118. }
  119. }
  120. return res;
  121. }
  122. private static void putForTextRange(SetMultimap<Integer, ScannerReport.Symbol> res, ScannerReport.Symbol symbol, ScannerReport.TextRange declaration) {
  123. for (int i = declaration.getStartLine(); i <= declaration.getEndLine(); i++) {
  124. res.put(i, symbol);
  125. }
  126. }
  127. private enum SymbolsComparator implements Comparator<ScannerReport.Symbol> {
  128. INSTANCE;
  129. @Override
  130. public int compare(ScannerReport.Symbol o1, ScannerReport.Symbol o2) {
  131. if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) {
  132. return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset());
  133. } else {
  134. return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine());
  135. }
  136. }
  137. }
  138. }