114
114
import spoon .reflect .visitor .CtAbstractVisitor ;
115
115
116
116
import java .lang .annotation .Annotation ;
117
+ import java .util .ArrayList ;
117
118
import java .util .HashMap ;
118
119
import java .util .List ;
119
120
import java .util .Stack ;
@@ -390,7 +391,12 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
390
391
391
392
}
392
393
393
- private <R > void travelStatementList (List <CtStatement > statements ) {
394
+ /**
395
+ * Add a list of statements as a block to the current CFG.
396
+ * @param statements The list of statements
397
+ * @return The start node of the block
398
+ */
399
+ private ControlFlowNode travelStatementList (List <CtStatement > statements ) {
394
400
ControlFlowNode begin = new ControlFlowNode (null , result , BranchKind .BLOCK_BEGIN );
395
401
tryAddEdge (lastNode , begin );
396
402
lastNode = begin ;
@@ -402,6 +408,7 @@ private <R> void travelStatementList(List<CtStatement> statements) {
402
408
ControlFlowNode end = new ControlFlowNode (null , result , BranchKind .BLOCK_END );
403
409
tryAddEdge (lastNode , end );
404
410
lastNode = end ;
411
+ return begin ;
405
412
}
406
413
407
414
@ Override
@@ -774,14 +781,34 @@ public <S> void visitCtSwitch(CtSwitch<S> switchStatement) {
774
781
775
782
//Visit Case
776
783
registerStatementLabel (caseStatement );
777
- ControlFlowNode cn = new ControlFlowNode (caseStatement .getCaseExpression (), result , BranchKind .STATEMENT );
778
- hasDefaultCase |= caseStatement .getCaseExpressions ().isEmpty ();
779
- tryAddEdge (lastNode , cn );
780
- if (lastNode != switchNode ) {
781
- tryAddEdge (switchNode , cn );
784
+ var caseExpressions = caseStatement .getCaseExpressions ();
785
+ ArrayList <ControlFlowNode > caseExpressionNodes = new ArrayList <>();
786
+ for (CtExpression <?> expression : caseExpressions ) {
787
+ ControlFlowNode caseNode = new ControlFlowNode (expression , result , BranchKind .STATEMENT );
788
+ caseExpressionNodes .add (caseNode );
789
+ tryAddEdge (switchNode , caseNode );
790
+ }
791
+
792
+ if (caseExpressionNodes .isEmpty ()) {
793
+ hasDefaultCase = true ;
794
+ ControlFlowNode defaultNode = new ControlFlowNode (null , result , BranchKind .STATEMENT );
795
+ caseExpressionNodes .add (defaultNode );
796
+ tryAddEdge (switchNode , defaultNode );
797
+ }
798
+
799
+ ControlFlowNode fallThroughEnd = null ;
800
+ if (lastNode != switchNode ) {
801
+ fallThroughEnd = lastNode ;
782
802
}
783
- lastNode = cn ;
784
- travelStatementList (caseStatement .getStatements ());
803
+ lastNode = null ;
804
+
805
+ ControlFlowNode blockStart = travelStatementList (caseStatement .getStatements ());
806
+ tryAddEdge (fallThroughEnd , blockStart );
807
+
808
+ for (ControlFlowNode expressionNode : caseExpressionNodes ) {
809
+ tryAddEdge (expressionNode , blockStart );
810
+ }
811
+
785
812
if (lastNode .getStatement () instanceof CtBreak ) {
786
813
lastNode = switchNode ;
787
814
}
0 commit comments