Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

wurstscript / WurstScript / 645

3 Aug 2018 - 7:21 coverage increased (+0.06%) to 60.856%
645

Pull #701

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
add Player() to useless function calls

Doesn't allocate handles
Pull Request #701: Get rid of empty init functions

13459 of 22116 relevant lines covered (60.86%)

0.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.63
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imoptimizer/GlobalsInliner.java
1
package de.peeeq.wurstscript.translation.imoptimizer;
2

3
import com.google.common.collect.Sets;
4
import de.peeeq.wurstscript.jassIm.*;
5
import de.peeeq.wurstscript.translation.imtranslation.ImHelper;
6
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
7
import de.peeeq.wurstscript.utils.Utils;
8
import org.jetbrains.annotations.Nullable;
9

10
import java.util.List;
11
import java.util.Set;
12
import java.util.stream.Collectors;
13

14
public class GlobalsInliner implements OptimizerPass {
1×
15

16
    public int optimize(ImTranslator trans) {
17
        int obsoleteCount = 0;
1×
18
        ImProg prog = trans.getImProg();
1×
19
        prog.clearAttributes(); // TODO only clear read/write attributes
1×
20

21
        Set<ImVar> obsoleteVars = Sets.newLinkedHashSet();
1×
22
        for (final ImVar v : prog.getGlobals()) {
1×
23
            if (trans.isUnitTestMode() && v.getName().equals("MagicFunctions_compiletime")) {
1×
24
                // in unit test mode we run tests and compiletime functions with optimizations,
25
                // so it is important, that we do not optimize away the compiletime constant
26
                continue;
1×
27
            }
28
            if (v.attrWrites().size() == 1) {
1×
29
                ImExpr right = null;
1×
30
                ImVarWrite obs = null;
1×
31
                for (ImVarWrite write : v.attrWrites()) {
1×
32
                    ImFunction func = write.getNearestFunc();
1×
33
                    if (isInInit(func)) {
1×
34
                        right = write.getRight();
1×
35
                        obs = write;
1×
36
                        break;
1×
37
                    }
38
                }
1×
39
                if (obs == null) {
1×
40
                    continue;
1×
41
                }
42

43
                ImExpr replacement = findReplacement(right, obs);
1×
44
                if (replacement != null) {
1×
45
                    for (ImVarRead v3 : v.attrReads()) {
1×
46
                        v3.replaceBy(replacement.copy());
1×
47
                    }
1×
48
                }
49
                if (replacement != null || v.attrReads().size() == 0) {
1×
50
                    obsoleteVars.add(v);
1×
51
                }
52
            } else if (v.attrWrites().size() > 1 && !(v.getType() instanceof ImArrayType || v.getType() instanceof ImTupleArrayType
1×
53
                    || v.getType() instanceof ImTupleType || v.getType() instanceof ImTupleArrayType)) {
1×
54
                List<ImVarWrite> initWrites = v.attrWrites().stream().filter(write -> {
1×
55
                    ImFunction nearestFunc = write.getNearestFunc();
1×
56
                    return isInInit(nearestFunc);
1×
57
                }).collect(Collectors.toList());
1×
58
                if (initWrites.size() == 1) {
1×
59
                    boolean isDefault = ImHelper.defaultValueForType((ImSimpleType) v.getType()).structuralEquals(v.attrWrites().iterator().next().getRight());
1×
60
                    if (isDefault) {
1×
61
                        // Assignment is default value and can be removed
62
                        v.attrWrites().iterator().next().replaceBy(JassIm.ImNull());
1×
63
                    }
64
                }
65
            }
66

67
        }
1×
68
        obsoleteCount += obsoleteVars.size();
1×
69
        for (ImVar i : obsoleteVars) {
1×
70
            // remove the write
71
            if (i.attrWrites().size() > 0) {
1×
72
                ImVarWrite write = Utils.getFirstAndOnly(i.attrWrites());
1×
73
                if (write.getParent() != null) {
1×
UNCOV
74
                    write.replaceBy(write.getRight().copy());
!
75
                }
76
            }
77
        }
1×
78
        prog.getGlobals().removeAll(obsoleteVars);
1×
79
        return obsoleteCount;
1×
80
    }
81

82
    @Nullable
83
    private ImExpr findReplacement(ImExpr right, ImVarWrite obs) {
84
        ImExpr replacement;
85
        if (right instanceof ImIntVal) {
1×
86
            ImIntVal val = (ImIntVal) right;
1×
87
            replacement = (JassIm.ImIntVal(val.getValI()));
1×
88
            if (obs.getParent() != null)
1×
89
                obs.replaceBy(JassIm.ImNull());
1×
90
        } else if (right instanceof ImRealVal) {
1×
91
            ImRealVal val = (ImRealVal) right;
1×
92
            replacement = (JassIm.ImRealVal(val.getValR()));
1×
93
            if (obs.getParent() != null)
1×
94
                obs.replaceBy(JassIm.ImNull());
1×
95
        } else if (right instanceof ImStringVal) {
1×
96
            ImStringVal val = (ImStringVal) right;
1×
97
            replacement = (JassIm.ImStringVal(val.getValS()));
1×
98
            if (obs.getParent() != null)
1×
99
                obs.replaceBy(JassIm.ImNull());
1×
100
        } else if (right instanceof ImBoolVal) {
1×
101
            ImBoolVal val = (ImBoolVal) right;
1×
102
            replacement = (JassIm.ImBoolVal(val.getValB()));
1×
103
            if (obs.getParent() != null)
1×
104
                obs.replaceBy(JassIm.ImNull());
1×
105
        } else {
1×
106
            replacement = null;
1×
107
        }
108
        return replacement;
1×
109
    }
110

111
    @Override
112
    public String getName() {
113
        return "Globals Inlined";
1×
114
    }
115

116

117
    private static boolean isInInit(ImFunction func) {
118
        return func != null && (func.getName().startsWith("init_") || func.getName().equals("main") || func.getName().startsWith("InitTrig_")
1×
119
                || func.getName().equals("initGlobals"));
1×
120
    }
121

122
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2022 Coveralls, Inc