Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/dead code improve #1770

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ControlFlowGraph extends DefaultDirectedGraph<CfgVertex, CfgEdge> {
@Getter
private ExitVertex exitPoint;

ControlFlowGraph() {
public ControlFlowGraph() {
super(CfgEdge.class);
exitPoint = new ExitVertex();
addVertex(exitPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package com.github._1c_syntax.bsl.languageserver.diagnostics;

import com.github._1c_syntax.bsl.languageserver.cfg.CfgBuildingParseTreeVisitor;
import com.github._1c_syntax.bsl.languageserver.cfg.ExitVertex;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
Expand All @@ -37,6 +39,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -110,6 +113,59 @@ private void savePreprocessorRange(Deque<ParseTree> nodes, BSLParser.Preprocesso
}
}

@Override
public ParseTree visitSubCodeBlock(BSLParser.SubCodeBlockContext ctx) {
errorRanges.clear();
super.visitSubCodeBlock(ctx);
appendUnreachableCode(ctx.codeBlock());
return ctx;
}

@Override
public ParseTree visitFileCodeBlock(BSLParser.FileCodeBlockContext ctx) {
errorRanges.clear();
super.visitFileCodeBlock(ctx);
appendUnreachableCode(ctx.codeBlock());
return ctx;
}

private void appendUnreachableCode(BSLParser.CodeBlockContext ctx) {
var builder = new CfgBuildingParseTreeVisitor();
builder.producePreprocessorConditions(true);
builder.produceLoopIterations(false);
builder.determineAdjacentDeadCode(false);

var graph = builder.buildGraph(ctx);
var deadCode = graph.vertexSet().stream()
.filter(vertex -> vertex != graph.getEntryPoint() && vertex.getClass() != ExitVertex.class)
.filter(vertex -> graph.inDegreeOf(vertex) == 0)
.flatMap(vertex -> vertex.getAst().stream())
.sorted(Comparator.comparingInt(bslParserRuleContext -> bslParserRuleContext.getStart().getLine()))
.map(Ranges::create)
.collect(Collectors.toList());


var newRanges = new ArrayList<Range>();
for (var range : deadCode) {
var alreadyDetected = false;
for (Range detectedRange : errorRanges) {
var pos = new Position(range.getStart().getLine(), range.getStart().getCharacter());
if (Ranges.containsPosition(detectedRange, pos)) {
alreadyDetected = true;
break;
}
}
if(!alreadyDetected) {
newRanges.add(range);
}
}

for (var range : newRanges) {
diagnosticStorage.addDiagnostic(range);
}

}

@Override
public ParseTree visitContinueStatement(BSLParser.ContinueStatementContext ctx) {
findAndAddDiagnostic(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UnreachableCodeDiagnosticTest extends AbstractDiagnosticTest<UnreachableCo
void test() {
List<Diagnostic> diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(15);
assertThat(diagnostics).hasSize(16);
assertThat(diagnostics, true)
.hasRange(12, 12, 20)
.hasRange(21, 12, 20)
Expand All @@ -52,9 +52,10 @@ void test() {
.hasRange(102, 8, 17)
.hasRange(108, 16, 111, 29)
.hasRange(138, 4, 16)
.hasRange(161, 4, 13)
.hasRange(166, 4, 168, 13)
.hasRange(172, 0, 9);
.hasRange(163, 4, 22)
.hasRange(171, 4, 13)
.hasRange(176, 4, 178, 13)
.hasRange(182, 0, 9);
}

@Test
Expand Down
12 changes: 11 additions & 1 deletion src/test/resources/diagnostics/UnreachableCodeDiagnostic.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@
КонецФункции
#КонецОбласти

Функция ДосрочныйВыход()
Если А Тогда
Возврат 1;
Иначе
Возврат 2;
КонецЕсли;

ТутОшибка = Истина; // <- недостижимый код
КонецФункции

#Если Сервер Тогда
Возврат;
#Иначе
Expand All @@ -170,4 +180,4 @@
КонецЕсли;

Возврат;
Метод2(); // Ошибка: После Возврат
Метод2(); // Ошибка: После Возврат