Skip to content

Commit 7004f8f

Browse files
committed
Обход NPE
1 parent ae7c865 commit 7004f8f

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

src/main/java/com/github/_1c_syntax/bsl/languageserver/cfg/CfgBuildingParseTreeVisitor.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public ParseTree visitElsifBranch(BSLParser.ElsifBranchContext ctx) {
195195
public ParseTree visitCodeBlock(BSLParser.CodeBlockContext ctx) {
196196
var currentBlock = blocks.getCurrentBlock();
197197
graph.addVertex(currentBlock.begin());
198-
return super.visitCodeBlock(ctx);
198+
return ctx == null ? null : super.visitCodeBlock(ctx);
199199
}
200200

201201
@Override
@@ -537,11 +537,7 @@ private void buildLoopSubgraph(@Nullable BSLParser.CodeBlockContext ctx, LoopVer
537537
jumpState.loopBreak = blocks.getCurrentBlock().end();
538538

539539
blocks.enterBlock(jumpState);
540-
541-
if (ctx != null) {
542-
ctx.accept(this);
543-
}
544-
540+
visitCodeBlock(ctx);
545541
var body = blocks.leaveBlock();
546542

547543
graph.addEdge(loopStart, body.begin(), CfgEdgeType.TRUE_BRANCH);

src/test/java/com/github/_1c_syntax/bsl/languageserver/cfg/ControlFlowGraphBuilderTest.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,40 @@ void testInnerLoops() {
288288
assertThat(walker.getCurrentNode()).isInstanceOf(ExitVertex.class);
289289
}
290290

291+
@Test
292+
void testLoopWithNullCodeBlock() {
293+
var code = """
294+
Для х = 1 По 10 Цикл
295+
КонецЦикла;""";
296+
297+
var parseTree = parse(code);
298+
299+
// Force null code block by removing the CodeBlock child
300+
var forStatement = parseTree.statement(0).compoundStatement().forStatement();
301+
var children = forStatement.children;
302+
303+
// Find and remove the CodeBlockContext child
304+
for (int i = 0; i < children.size(); i++) {
305+
if (children.get(i) instanceof BSLParser.CodeBlockContext) {
306+
children.remove(i);
307+
break;
308+
}
309+
}
310+
311+
var builder = new CfgBuildingParseTreeVisitor();
312+
var graph = builder.buildGraph(parseTree);
313+
314+
// Check if graph was built successfully
315+
assertThat(graph).isNotNull();
316+
assertThat(graph.vertexSet()).isNotEmpty();
317+
318+
// Verify basic structure - should at least have loop vertex and exit
319+
var vertices = traverseToOrderedList(graph);
320+
assertThat(vertices)
321+
.hasAtLeastOneElementOfType(ForLoopVertex.class)
322+
.hasAtLeastOneElementOfType(ExitVertex.class);
323+
}
324+
291325
@Test
292326
void tryHandlerFlowTest() {
293327
var code = """
@@ -451,41 +485,6 @@ void preprocessorIfWithElseBranching() {
451485
assertThat(walker.getCurrentNode()).isSameAs(lastStatement);
452486
}
453487

454-
@Test
455-
void testLoopWithNullCodeBlock() {
456-
var code = """
457-
Для х = 1 По 10 Цикл
458-
А = 1;
459-
КонецЦикла;""";
460-
461-
var parseTree = parse(code);
462-
463-
// Force null code block by removing the CodeBlock child
464-
var forStatement = parseTree.statement(0).compoundStatement().forStatement();
465-
var children = forStatement.children;
466-
467-
// Find and remove the CodeBlockContext child
468-
for (int i = 0; i < children.size(); i++) {
469-
if (children.get(i) instanceof BSLParser.CodeBlockContext) {
470-
children.remove(i);
471-
break;
472-
}
473-
}
474-
475-
var builder = new CfgBuildingParseTreeVisitor();
476-
var graph = builder.buildGraph(parseTree);
477-
478-
// Check if graph was built successfully
479-
assertThat(graph).isNotNull();
480-
assertThat(graph.vertexSet()).isNotEmpty();
481-
482-
// Verify basic structure - should at least have loop vertex and exit
483-
var vertices = traverseToOrderedList(graph);
484-
assertThat(vertices)
485-
.hasAtLeastOneElementOfType(ForLoopVertex.class)
486-
.hasAtLeastOneElementOfType(ExitVertex.class);
487-
}
488-
489488
@Test
490489
void preprocessorIfWithElseIfBranching() {
491490
var code = """

0 commit comments

Comments
 (0)