Debian Codegraph @

src/com/google/javascript/jscomp/CheckUnreachableCode.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.base.Predicate;
20 import com.google.javascript.jscomp.ControlFlowGraph.Branch;
21 import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
22 import com.google.javascript.jscomp.graph.GraphNode;
23 import com.google.javascript.jscomp.graph.GraphReachability;
24 import com.google.javascript.jscomp.graph.GraphReachability.EdgeTuple;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.jstype.TernaryValue;
27
28 /**
29 * Use {@link ControlFlowGraph} and {@link GraphReachability} to inform user
30 * about unreachable code.
31 *
32 */
33 class CheckUnreachableCode implements ScopedCallback {
34
35 static final DiagnosticType UNREACHABLE_CODE = DiagnosticType.error(
36 "JSC_UNREACHABLE_CODE", "unreachable code");
37
38 private final AbstractCompiler compiler;
39 private final CheckLevel level;
40
41 CheckUnreachableCode(AbstractCompiler compiler, CheckLevel level) {
42 this.compiler = compiler;
43 this.level = level;
44 }
45
46 @Override
47 public void enterScope(NodeTraversal t) {
48 initScope(t.getControlFlowGraph());
49 }
50
51 @Override
52 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
53 GraphNode<Node, Branch> gNode = t.getControlFlowGraph().getNode(n);
54 if (gNode != null && gNode.getAnnotation() != GraphReachability.REACHABLE) {
55
56 // Only report error when there are some line number informations.
57 // There are synthetic nodes with no line number informations, nodes
58 // introduce by other passes (although not likely since this pass should
59 // be executed early) or some rhino bug.
60 if (n.getLineno() != -1 &&
61 // Allow spurious semi-colons and spurious breaks.
62 !n.isEmpty() && !n.isBreak()) {
63 compiler.report(t.makeError(n, level, UNREACHABLE_CODE));
64 // From now on, we are going to assume the user fixed the error and not
65 // give more warning related to code section reachable from this node.
66 new GraphReachability<Node, ControlFlowGraph.Branch>(
67 t.getControlFlowGraph()).recompute(n);
68
69 // Saves time by not traversing children.
70 return false;
71 }
72 }
73 return true;
74 }
75
76 private void initScope(ControlFlowGraph<Node> controlFlowGraph) {
77 new GraphReachability<Node, ControlFlowGraph.Branch>(
78 controlFlowGraph, new ReachablePredicate()).compute(
79 controlFlowGraph.getEntry().getValue());
80 }
81
82 @Override
83 public void exitScope(NodeTraversal t) {
84 }
85
86 @Override
87 public void visit(NodeTraversal t, Node n, Node parent) {
88 }
89
90 private final class ReachablePredicate implements
91 Predicate<EdgeTuple<Node, ControlFlowGraph.Branch>> {
92
93 @Override
94 public boolean apply(EdgeTuple<Node, Branch> input) {
95 Branch branch = input.edge;
96 if (!branch.isConditional()) {
97 return true;
98 }
99 Node predecessor = input.sourceNode;
100 Node condition = NodeUtil.getConditionExpression(predecessor);
101
102 // TODO(user): Handle more complicated expression like true == true,
103 // etc....
104 if (condition != null) {
105 TernaryValue val = NodeUtil.getImpureBooleanValue(condition);
106 if (val != TernaryValue.UNKNOWN) {
107 return val.toBoolean(true) == (branch == Branch.ON_TRUE);
108 }
109 }
110 return true;
111 }
112 }
113 }