-
Notifications
You must be signed in to change notification settings - Fork 19
/
VariableInitializationCheck.java
825 lines (706 loc) · 29.6 KB
/
VariableInitializationCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
* Sonar Delphi Plugin
* Copyright (C) 2019 Integrated Application Development
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.checks;
import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import org.sonar.check.Rule;
import org.sonar.plugins.communitydelphi.api.ast.ArgumentListNode;
import org.sonar.plugins.communitydelphi.api.ast.ArgumentNode;
import org.sonar.plugins.communitydelphi.api.ast.AssignmentStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.CommonDelphiNode;
import org.sonar.plugins.communitydelphi.api.ast.CompoundStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.ConstStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
import org.sonar.plugins.communitydelphi.api.ast.ExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.ForLoopVarNode;
import org.sonar.plugins.communitydelphi.api.ast.ForLoopVarReferenceNode;
import org.sonar.plugins.communitydelphi.api.ast.ForStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.FormalParameterNode.FormalParameterData;
import org.sonar.plugins.communitydelphi.api.ast.LocalDeclarationSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.NameDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.NameReferenceNode;
import org.sonar.plugins.communitydelphi.api.ast.Node;
import org.sonar.plugins.communitydelphi.api.ast.PrimaryExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.RepeatStatementNode;
import org.sonar.plugins.communitydelphi.api.ast.RoutineImplementationNode;
import org.sonar.plugins.communitydelphi.api.ast.StatementNode;
import org.sonar.plugins.communitydelphi.api.ast.UnaryExpressionNode;
import org.sonar.plugins.communitydelphi.api.ast.VarDeclarationNode;
import org.sonar.plugins.communitydelphi.api.ast.VarSectionNode;
import org.sonar.plugins.communitydelphi.api.ast.VarStatementNode;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
import org.sonar.plugins.communitydelphi.api.operator.UnaryOperator;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.NameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.PropertyNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.TypeNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.TypedDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.VariableNameDeclaration;
import org.sonar.plugins.communitydelphi.api.token.DelphiTokenType;
import org.sonar.plugins.communitydelphi.api.type.Parameter;
import org.sonar.plugins.communitydelphi.api.type.Type;
import org.sonar.plugins.communitydelphi.api.type.Type.ProceduralType;
import org.sonar.plugins.communitydelphi.api.type.Type.StructType;
import org.sonar.plugins.communitydelphi.api.type.Typed;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
@DeprecatedRuleKey(ruleKey = "VariableInitializationRule", repositoryKey = "delph")
@Rule(key = "VariableInitialization")
public class VariableInitializationCheck extends DelphiCheck {
private static final String MESSAGE = "Initialize this variable before using it.";
private final IdentityHashMap<NameDeclaration, InitializationState> initializationStateMap =
new IdentityHashMap<>();
private final IdentityHashMap<NameDeclaration, RoutineImplementationNode> nestedRoutines =
new IdentityHashMap<>();
@Override
public DelphiCheckContext visit(RoutineImplementationNode routine, DelphiCheckContext context) {
initializationStateMap.clear();
nestedRoutines.clear();
visitRoutine(routine, context);
return context;
}
private void visitRoutine(RoutineImplementationNode routine, DelphiCheckContext context) {
CompoundStatementNode block = routine.getStatementBlock();
if (block != null) {
collectNestedRoutines(routine);
collectDeclarationsFromParameterList(routine);
collectDeclarationsFromVarSections(routine);
visitStatements(block, context);
}
}
private void collectNestedRoutines(RoutineImplementationNode routine) {
LocalDeclarationSectionNode declarationSection = routine.getDeclarationSection();
if (declarationSection == null) {
return;
}
declarationSection
.findChildrenOfType(RoutineImplementationNode.class)
.forEach(nested -> nestedRoutines.put(nested.getRoutineNameDeclaration(), nested));
}
private void collectDeclarationsFromParameterList(RoutineImplementationNode routine) {
routine.getParameters().stream()
.filter(FormalParameterData::isOut)
.map(FormalParameterData::getNode)
.map(NameDeclarationNode::getNameDeclaration)
.filter(VariableNameDeclaration.class::isInstance)
.map(VariableNameDeclaration.class::cast)
.forEach(
declaration -> {
InitializationState state = InitializationState.from(declaration.getType());
initializationStateMap.put(declaration, state);
});
}
private void collectDeclarationsFromVarSections(RoutineImplementationNode routine) {
LocalDeclarationSectionNode declarationSection = routine.getDeclarationSection();
if (declarationSection == null) {
return;
}
for (VarSectionNode section : declarationSection.findChildrenOfType(VarSectionNode.class)) {
for (VarDeclarationNode variable : section.getDeclarations()) {
variable.getNameDeclarationList().getDeclarations().stream()
.map(NameDeclarationNode::getNameDeclaration)
.filter(VariableNameDeclaration.class::isInstance)
.map(VariableNameDeclaration.class::cast)
.filter(VariableInitializationCheck::isUnmanagedVariable)
.forEach(
declaration -> {
InitializationState state = InitializationState.from(declaration.getType());
if (declaration.isUnion()) {
state.assignedTo();
}
initializationStateMap.put(declaration, state);
});
}
}
}
private void visitStatements(DelphiNode node, DelphiCheckContext context) {
boolean visitChildrenFirst = node instanceof RepeatStatementNode;
if (!visitChildrenFirst) {
visitStatement(node, context);
}
for (DelphiNode child : node.getChildren()) {
visitStatements(child, context);
}
if (visitChildrenFirst) {
visitStatement(node, context);
}
}
private void visitStatement(Node node, DelphiCheckContext context) {
if (node instanceof StatementNode) {
StatementNode statement = (StatementNode) node;
searchForInitializationsByOutArgument(statement);
searchForInitializationsByAddressOfHeuristic(statement);
searchForInitializationsByRecordInvocationHeuristic(statement);
handleNestedRoutineInvocations(statement, context);
handleAssignment(statement);
handleVarStatement(statement);
handleConstStatement(statement);
handleForStatement(statement);
searchForViolations(statement, context);
}
}
private void handleNestedRoutineInvocations(StatementNode statement, DelphiCheckContext context) {
for (NameReferenceNode name : findNameReferences(statement)) {
for (NameReferenceNode namePart : name.flatten()) {
NameDeclaration declaration = namePart.getNameDeclaration();
if (!nestedRoutines.containsKey(declaration)) {
continue;
}
RoutineImplementationNode nestedRoutine = nestedRoutines.get(declaration);
nestedRoutines.remove(declaration);
visitRoutine(nestedRoutine, context);
nestedRoutines.put(declaration, nestedRoutine);
}
}
}
private void searchForInitializationsByOutArgument(StatementNode statement) {
findNameReferences(statement).stream()
.filter(VariableInitializationCheck::isOutArgument)
.map(this::getReferredInitializationState)
.filter(Objects::nonNull)
.forEach(InitializationState::assignedTo);
}
private void searchForInitializationsByAddressOfHeuristic(StatementNode statement) {
findNameReferences(statement).stream()
.filter(VariableInitializationCheck::isAddressOfReference)
.map(this::getReferredInitializationState)
.filter(Objects::nonNull)
.forEach(InitializationState::assignedTo);
}
private void searchForInitializationsByRecordInvocationHeuristic(StatementNode statement) {
findNameReferences(statement)
.forEach(this::searchForInitializationsByRecordInvocationHeuristic);
}
private void searchForInitializationsByRecordInvocationHeuristic(NameReferenceNode name) {
NameDeclaration declaration = name.getNameDeclaration();
InitializationState initializationState = this.initializationStateMap.get(declaration);
while (initializationState != null) {
if (isRecordInvocation(name)) {
initializationState.assignedTo();
}
name = name.nextName();
if (name == null) {
break;
}
declaration = name.getNameDeclaration();
initializationState = initializationState.getInitializationState(declaration);
}
}
private static boolean isRecordInvocation(NameReferenceNode name) {
NameDeclaration declaration = name.getNameDeclaration();
if (!(declaration instanceof TypedDeclaration
&& ((TypedDeclaration) declaration).getType().isRecord())) {
return false;
}
NameReferenceNode next = name.nextName();
if (next == null) {
return false;
}
NameDeclaration nextDeclaration = next.getNameDeclaration();
return nextDeclaration instanceof RoutineNameDeclaration
|| nextDeclaration instanceof PropertyNameDeclaration;
}
private void handleAssignment(StatementNode statement) {
if (!(statement instanceof AssignmentStatementNode)) {
return;
}
var assignmentStatement = (AssignmentStatementNode) statement;
ExpressionNode assignee = skipHardCasts(assignmentStatement.getAssignee());
NameReferenceNode assigneeName = getLastNameReference(assignee);
if (assigneeName == null) {
return;
}
NameDeclaration declaration = assigneeName.getNameDeclaration();
InitializationState previousInitializationState = null;
InitializationState initializationState = this.initializationStateMap.get(declaration);
while (initializationState != null) {
NameReferenceNode next = assigneeName.nextName();
if (next == null) {
break;
}
assigneeName = next;
declaration = assigneeName.getNameDeclaration();
previousInitializationState = initializationState;
initializationState = initializationState.getInitializationState(declaration);
}
for (NameReferenceNode name : findNameReferences(assignmentStatement.getValue())) {
if (name.getNameDeclaration() == declaration && !isOutArgument(name)) {
return;
}
}
InitializationState assignedState =
getReferredInitializationState(assignmentStatement.getValue());
if (assignedState != null) {
if (previousInitializationState != null) {
previousInitializationState.setInitializationState(declaration, assignedState.copy());
} else {
this.initializationStateMap.put(declaration, assignedState.copy());
}
} else if (initializationState != null) {
initializationState.assignedTo();
}
}
private static boolean isOutArgument(NameReferenceNode name) {
ArgumentNode argument = getArgument(name);
if (argument == null) {
return false;
}
ArgumentListNode argumentList = (ArgumentListNode) argument.getParent();
ProceduralType procedural = getInvokedProcedural(argumentList);
if (procedural == null) {
return false;
}
int argumentIndex = argumentList.getArgumentNodes().indexOf(argument);
Parameter parameter = procedural.getParameter(argumentIndex);
return (parameter.isOut() || parameter.isVar()) && !isOutArgumentExclusion(argumentList);
}
private static boolean isSizeOfArgument(NameReferenceNode name) {
ArgumentNode argument = getArgument(name);
if (argument == null) {
return false;
}
ArgumentListNode argumentList = (ArgumentListNode) argument.getParent();
RoutineNameDeclaration routine = getRoutineDeclaration(argumentList);
if (routine == null) {
return false;
}
return routine.fullyQualifiedName().equals("System.SizeOf");
}
private static ArgumentNode getArgument(NameReferenceNode name) {
if (!(name.getParent() instanceof PrimaryExpressionNode)) {
return null;
}
if (!Iterables.getLast(name.getParent().getChildren()).equals(name)) {
return null;
}
ArgumentNode argument = null;
ArgumentListNode argumentList;
Node current = name.getParent();
while (current instanceof PrimaryExpressionNode) {
ExpressionNode expression = ((ExpressionNode) current).skipParentheses();
if (!(expression.getParent() instanceof ArgumentNode)) {
return null;
}
argument = (ArgumentNode) expression.getParent();
argumentList = (ArgumentListNode) expression.getNthParent(2);
current = findHardCast(argumentList);
}
return argument;
}
private static Node findHardCast(ArgumentListNode argumentList) {
if (isHardCast(argumentList)) {
return argumentList.getParent();
}
return null;
}
private static ExpressionNode skipHardCasts(ExpressionNode expression) {
while (true) {
ArgumentListNode argumentList = expression.getFirstChildOfType(ArgumentListNode.class);
if (argumentList == null || !isHardCast(argumentList)) {
return expression;
}
expression = argumentList.getArgumentNodes().get(0).getExpression();
}
}
private static boolean isHardCast(ArgumentListNode argumentList) {
if (!argumentList.isEmpty()) {
Node previous = argumentList.getParent().getChild(argumentList.getChildIndex() - 1);
if (previous instanceof NameReferenceNode) {
NameReferenceNode nameReference = ((NameReferenceNode) previous);
NameDeclaration declaration = nameReference.getLastName().getNameDeclaration();
return declaration instanceof TypeNameDeclaration;
} else if (previous instanceof CommonDelphiNode) {
DelphiTokenType tokenType = previous.getTokenType();
return tokenType == DelphiTokenType.STRING || tokenType == DelphiTokenType.FILE;
}
}
return false;
}
private static ProceduralType getInvokedProcedural(ArgumentListNode argumentList) {
Node previous = argumentList.getParent().getChild(argumentList.getChildIndex() - 1);
if (!(previous instanceof Typed)) {
return null;
}
Type type = ((Typed) previous).getType();
if (!type.isProcedural()) {
return null;
}
return (ProceduralType) type;
}
private static RoutineNameDeclaration getRoutineDeclaration(ArgumentListNode argumentList) {
Node previous = argumentList.getParent().getChild(argumentList.getChildIndex() - 1);
if (!(previous instanceof NameReferenceNode)) {
return null;
}
NameReferenceNode proceduralReference = ((NameReferenceNode) previous).getLastName();
NameDeclaration declaration = proceduralReference.getNameDeclaration();
if (!(declaration instanceof RoutineNameDeclaration)) {
return null;
}
return (RoutineNameDeclaration) declaration;
}
private static boolean isOutArgumentExclusion(ArgumentListNode argumentList) {
RoutineNameDeclaration routine = getRoutineDeclaration(argumentList);
if (routine == null) {
return false;
}
return routine.fullyQualifiedName().equals("System.SysUtils.FreeAndNil")
|| routine.fullyQualifiedName().equals("System.Assigned");
}
private void searchForViolations(StatementNode statement, DelphiCheckContext context) {
if (statement instanceof AssignmentStatementNode) {
searchForViolationsInAssignment((AssignmentStatementNode) statement, context);
} else {
searchForViolationsInStatement(statement, context);
}
}
private void searchForViolationsInAssignment(
AssignmentStatementNode assignment, DelphiCheckContext context) {
searchForViolationsInAssignmentAssignee(assignment.getAssignee(), context);
searchForViolationsInAssignmentValue(assignment.getValue(), context);
}
private void searchForViolationsInAssignmentAssignee(
ExpressionNode assignee, DelphiCheckContext context) {
NameReferenceNode lastName = getLastNameReference(assignee);
searchForViolationsInNameReference(
lastName, context, VariableInitializationCheck::isAssignmentAssigneeViolation);
findNameReferences(assignee).stream()
.filter(reference -> reference != lastName)
.forEach(
reference ->
searchForViolationsInNameReference(
reference, context, VariableInitializationCheck::isRegularViolation));
}
private void searchForViolationsInAssignmentValue(
ExpressionNode value, DelphiCheckContext context) {
NameReferenceNode lastName = getLastNameReference(value);
searchForViolationsInNameReference(
lastName, context, VariableInitializationCheck::isAssignmentValueViolation);
findNameReferences(value).stream()
.filter(reference -> reference != lastName)
.forEach(
reference ->
searchForViolationsInNameReference(
reference, context, VariableInitializationCheck::isRegularViolation));
}
private static boolean isRegularViolation(NameReferenceNode name, InitializationState state) {
if (name.nextName() == null) {
return !state.isInitialized() && !isSizeOfArgument(name);
}
return !state.canBeReferencedInQualifiedName();
}
private static boolean isAssignmentAssigneeViolation(
NameReferenceNode name, InitializationState state) {
return name.nextName() != null && !state.canBeReferencedInQualifiedName();
}
private static boolean isAssignmentValueViolation(
NameReferenceNode name, InitializationState state) {
if (name.nextName() == null) {
return !state.canBeAssignedFrom();
}
return !state.canBeReferencedInQualifiedName();
}
private static boolean isAddressOfReference(NameReferenceNode name) {
if (name.nextName() != null) {
return false;
}
DelphiNode node = name;
do {
node = node.getParent();
} while (node instanceof NameReferenceNode);
if (node instanceof PrimaryExpressionNode) {
node = ((PrimaryExpressionNode) node).findParentheses().getParent();
if (node instanceof UnaryExpressionNode) {
UnaryExpressionNode unary = (UnaryExpressionNode) node;
return unary.getOperator() == UnaryOperator.ADDRESS;
}
}
return false;
}
private static NameReferenceNode getLastNameReference(ExpressionNode expression) {
expression = expression.skipParentheses();
if (expression instanceof PrimaryExpressionNode) {
Node lastChild = Iterables.getLast(expression.getChildren());
if (lastChild instanceof NameReferenceNode) {
return ((NameReferenceNode) lastChild);
}
}
return null;
}
private void searchForViolationsInStatement(StatementNode statement, DelphiCheckContext context) {
for (NameReferenceNode reference : findNameReferences(statement)) {
searchForViolationsInNameReference(
reference, context, VariableInitializationCheck::isRegularViolation);
}
}
private void searchForViolationsInNameReference(
NameReferenceNode name,
DelphiCheckContext context,
BiPredicate<NameReferenceNode, InitializationState> isViolation) {
if (name == null) {
return;
}
NameDeclaration declaration = name.getNameDeclaration();
InitializationState initializationState = this.initializationStateMap.get(declaration);
while (initializationState != null) {
if (!initializationState.isViolation() && isViolation.test(name, initializationState)) {
initializationState.flagViolation();
reportIssue(context, name.getIdentifier(), MESSAGE);
}
name = name.nextName();
if (name == null) {
break;
}
declaration = name.getNameDeclaration();
initializationState = initializationState.getInitializationState(declaration);
}
}
private static List<NameReferenceNode> findNameReferences(DelphiNode node) {
List<NameReferenceNode> references = new ArrayList<>();
findNameReferences(node, references);
return references;
}
private static void findNameReferences(DelphiNode node, List<NameReferenceNode> references) {
if (node instanceof NameReferenceNode) {
references.add((NameReferenceNode) node);
} else {
for (DelphiNode child : node.getChildren()) {
if (!(child instanceof StatementNode)) {
findNameReferences(child, references);
}
}
}
}
private void handleVarStatement(StatementNode statement) {
if (statement instanceof VarStatementNode) {
var varStatement = (VarStatementNode) statement;
boolean assigned = varStatement.getExpression() != null;
ExpressionNode expression = varStatement.getExpression();
InitializationState assignedState;
if (expression != null) {
assignedState = getReferredInitializationState(expression);
} else {
assignedState = null;
}
varStatement.getNameDeclarationList().getDeclarations().stream()
.map(NameDeclarationNode::getNameDeclaration)
.filter(VariableInitializationCheck::isUnmanagedVariable)
.map(VariableNameDeclaration.class::cast)
.forEach(
declaration -> {
InitializationState initializationState;
if (assignedState != null) {
initializationState = assignedState.copy();
} else {
initializationState = InitializationState.from(declaration.getType());
if (assigned) {
initializationState.assignedTo();
}
}
initializationStateMap.put(declaration, initializationState);
});
}
}
private void handleConstStatement(StatementNode statement) {
if (statement instanceof ConstStatementNode) {
var constStatement = (ConstStatementNode) statement;
NameDeclaration declaration = constStatement.getNameDeclarationNode().getNameDeclaration();
if (isUnmanagedVariable(declaration)) {
var varDeclaration = (VariableNameDeclaration) declaration;
InitializationState assignedState =
getReferredInitializationState(constStatement.getExpression());
InitializationState initializationState;
if (assignedState != null) {
initializationState = assignedState.copy();
} else {
initializationState = InitializationState.from(varDeclaration.getType());
initializationState.assignedTo();
}
initializationStateMap.put(declaration, initializationState);
}
}
}
private void handleForStatement(StatementNode statement) {
if (statement instanceof ForStatementNode) {
var forStatement = (ForStatementNode) statement;
ForLoopVarNode forLoopVar = forStatement.getVariable();
if (forLoopVar instanceof ForLoopVarReferenceNode) {
NameReferenceNode name = ((ForLoopVarReferenceNode) forLoopVar).getNameReference();
NameDeclaration declaration = name.getNameDeclaration();
InitializationState initializationState = initializationStateMap.get(declaration);
if (initializationState != null) {
initializationState.assignedTo();
}
}
}
}
private InitializationState getReferredInitializationState(ExpressionNode expression) {
expression = expression.skipParentheses();
if (expression instanceof PrimaryExpressionNode) {
Node lastChild = Iterables.getLast(expression.getChildren());
if (lastChild instanceof NameReferenceNode) {
return this.getReferredInitializationState((NameReferenceNode) lastChild);
}
}
return null;
}
private InitializationState getReferredInitializationState(NameReferenceNode name) {
NameDeclaration declaration = name.getNameDeclaration();
InitializationState initializationState = this.initializationStateMap.get(declaration);
while (initializationState != null) {
name = name.nextName();
if (name == null) {
break;
}
declaration = name.getNameDeclaration();
initializationState = initializationState.getInitializationState(declaration);
}
return initializationState;
}
private static boolean isUnmanagedVariable(NameDeclaration declaration) {
return declaration instanceof VariableNameDeclaration
&& isUnmanagedType(((VariableNameDeclaration) declaration).getType());
}
private static boolean isUnmanagedType(Type type) {
return !type.isString() && !type.isArray() && !type.isVariant() && !type.isInterface();
}
private interface InitializationState {
boolean isInitialized();
boolean canBeReferencedInQualifiedName();
boolean canBeAssignedFrom();
boolean isViolation();
void flagViolation();
void assignedTo();
InitializationState getInitializationState(NameDeclaration declaration);
void setInitializationState(NameDeclaration declaration, InitializationState state);
InitializationState copy();
static InitializationState from(Type type) {
if (type.isRecord()) {
return new RecordInitializationState((StructType) type);
} else {
return new DefaultInitializationState();
}
}
}
private abstract static class AbstractInitializationState implements InitializationState {
private boolean violation;
@Override
public void flagViolation() {
violation = true;
}
@Override
public boolean isViolation() {
return violation;
}
}
private static class DefaultInitializationState extends AbstractInitializationState {
private boolean initialized;
public DefaultInitializationState() {
initialized = false;
}
private DefaultInitializationState(DefaultInitializationState initializationState) {
initialized = initializationState.initialized;
}
@Override
public boolean isInitialized() {
return initialized;
}
@Override
public boolean canBeReferencedInQualifiedName() {
return isInitialized();
}
@Override
public boolean canBeAssignedFrom() {
return isInitialized();
}
@Override
public void assignedTo() {
initialized = true;
}
@Nullable
@Override
public InitializationState getInitializationState(NameDeclaration declaration) {
return null;
}
@Override
public void setInitializationState(NameDeclaration declaration, InitializationState state) {
// Do nothing
}
@Override
public InitializationState copy() {
return new DefaultInitializationState(this);
}
}
private static class RecordInitializationState extends AbstractInitializationState {
private final IdentityHashMap<NameDeclaration, InitializationState> fields =
new IdentityHashMap<>();
public RecordInitializationState(StructType type) {
type.typeScope().getVariableDeclarations().stream()
.filter(VariableNameDeclaration::isField)
.filter(Predicate.not(VariableNameDeclaration::isClassVar))
.filter(VariableInitializationCheck::isUnmanagedVariable)
.forEach(
declaration -> {
InitializationState state = InitializationState.from(declaration.getType());
if (declaration.isUnion()) {
state.assignedTo();
}
fields.put(declaration, state);
});
}
private RecordInitializationState(RecordInitializationState initializationState) {
initializationState.fields.forEach((key, value) -> fields.put(key, value.copy()));
}
@Override
public boolean isInitialized() {
return fields.values().stream().allMatch(InitializationState::isInitialized);
}
@Override
public boolean canBeReferencedInQualifiedName() {
return true;
}
@Override
public boolean canBeAssignedFrom() {
return true;
}
@Override
public void assignedTo() {
fields.values().forEach(InitializationState::assignedTo);
}
@Nullable
@Override
public InitializationState getInitializationState(NameDeclaration declaration) {
return fields.get(declaration);
}
@Override
public void setInitializationState(NameDeclaration declaration, InitializationState state) {
fields.computeIfPresent(declaration, (key, value) -> state);
}
@Override
public InitializationState copy() {
return new RecordInitializationState(this);
}
}
}