Package: BlockCleanupTrans

BlockCleanupTrans

nameinstructionbranchcomplexitylinemethod
BlockCleanupTrans()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseABlockStmIR(ABlockStmIR)
M: 0 C: 50
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 11
100%
M: 0 C: 1
100%
isEmpty(SStmIR)
M: 3 C: 37
93%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 2 C: 10
83%
M: 0 C: 1
100%
singleBlockWrapsBlock(ABlockStmIR)
M: 0 C: 19
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import java.util.LinkedList;
4:
5: import org.overture.codegen.ir.SStmIR;
6: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
7: import org.overture.codegen.ir.statements.ABlockStmIR;
8:
9: public class BlockCleanupTrans extends DepthFirstAnalysisAdaptor
10: {
11:         @Override
12:         public void caseABlockStmIR(ABlockStmIR node)
13:                         throws org.overture.codegen.ir.analysis.AnalysisException
14:         {
15:•                for (SStmIR s : new LinkedList<>(node.getStatements()))
16:                 {
17:                         s.apply(this);
18:                 }
19:
20:•                if (node.parent() instanceof ABlockStmIR && isEmpty(node))
21:                 {
22:                         /**
23:                          * Only remove empty blocks from blocks, otherwise we can get into a situation where we destroy the tree.
24:                          * For example, 'if exp then () else ()'
25:                          */
26:                         node.parent().removeChild(node);
27:                         return;
28:                 }
29:
30:•                if (singleBlockWrapsBlock(node))
31:                 {
32:                         SStmIR enclosedStm = node.getStatements().get(0);
33:
34:•                        if (node.parent() != null)
35:                         {
36:                                 node.parent().replaceChild(node, enclosedStm);
37:                         }
38:                 }
39:         }
40:
41:         private boolean singleBlockWrapsBlock(ABlockStmIR node)
42:         {
43:•                return node.getLocalDefs().isEmpty() && node.getStatements().size() == 1
44:•                                && node.getStatements().get(0) instanceof ABlockStmIR;
45:         }
46:
47:         public boolean isEmpty(SStmIR target)
48:         {
49:•                if (!(target instanceof ABlockStmIR))
50:                 {
51:                         return false;
52:                 }
53:
54:                 ABlockStmIR block = (ABlockStmIR) target;
55:
56:•                if (!block.getLocalDefs().isEmpty())
57:                 {
58:                         return false;
59:                 }
60:
61:•                if (block.getStatements().isEmpty())
62:                 {
63:                         return true;
64:                 } else
65:                 {
66:•                        for (SStmIR s : block.getStatements())
67:                         {
68:•                                if (!isEmpty(s))
69:                                 {
70:                                         return false;
71:                                 }
72:                         }
73:
74:                         return true;
75:                 }
76:         }
77: }