Debian Codegraph @

src/com/google/javascript/jscomp/CheckMissingReturn.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.DiGraph.DiGraphEdge;
23 import com.google.javascript.rhino.Node;
24 import com.google.javascript.rhino.jstype.FunctionType;
25 import com.google.javascript.rhino.jstype.JSType;
26 import com.google.javascript.rhino.jstype.JSTypeNative;
27 import com.google.javascript.rhino.jstype.TernaryValue;
28
29 /**
30 * Checks functions for missing return statements. Return statements are only
31 * expected for functions with return type information. Functions with empty
32 * bodies are ignored.
33 *
34 */
35 class CheckMissingReturn implements ScopedCallback {
36
37 static final DiagnosticType MISSING_RETURN_STATEMENT =
38 DiagnosticType.warning(
39 "JSC_MISSING_RETURN_STATEMENT",
40 "Missing return statement. Function expected to return {0}.");
41
42 private final AbstractCompiler compiler;
43 private final CheckLevel level;
44
45 private static final Predicate<Node> IS_RETURN = new Predicate<Node>() {
46 @Override
47 public boolean apply(Node input) {
48 // Check for null because the control flow graph's implicit return node is
49 // represented by null, so this value might be input.
50 return input != null && input.isReturn();
51 }
52 };
53
54 /* Skips all exception edges and impossible edges. */
55 private static final Predicate<DiGraphEdge<Node, ControlFlowGraph.Branch>>
56 GOES_THROUGH_TRUE_CONDITION_PREDICATE =
57 new Predicate<DiGraphEdge<Node, ControlFlowGraph.Branch>>() {
58 @Override
59 public boolean apply(DiGraphEdge<Node, ControlFlowGraph.Branch> input) {
60 // First skill all exceptions.
61 Branch branch = input.getValue();
62 if (branch == Branch.ON_EX) {
63 return false;
64 } else if (branch.isConditional()) {
65 Node condition = NodeUtil.getConditionExpression(
66 input.getSource().getValue());
67 // TODO(user): We CAN make this bit smarter just looking at
68 // constants. We DO have a full blown ReverseAbstractInterupter and
69 // type system that can evaluate some impressions' boolean value but
70 // for now we will keep this pass lightweight.
71 if (condition != null) {
72 TernaryValue val = NodeUtil.getImpureBooleanValue(condition);
73 if (val != TernaryValue.UNKNOWN) {
74 return val.toBoolean(true) == (Branch.ON_TRUE == branch);
75 }
76 }
77 }
78 return true;
79 }
80 };
81
82 /**
83 * @param level level of severity to report when a missing return statement
84 * is discovered
85 */
86 CheckMissingReturn(AbstractCompiler compiler, CheckLevel level) {
87 this.compiler = compiler;
88 this.level = level;
89 }
90
91 @Override
92 public void enterScope(NodeTraversal t) {
93 JSType returnType = explicitReturnExpected(t.getScopeRoot());
94 if (returnType == null) {
95 return;
96 }
97
98 if (fastAllPathsReturnCheck(t.getControlFlowGraph())) {
99 return;
100 }
101
102 CheckPathsBetweenNodes<Node, ControlFlowGraph.Branch> test =
103 new CheckPathsBetweenNodes<Node, ControlFlowGraph.Branch>(
104 t.getControlFlowGraph(),
105 t.getControlFlowGraph().getEntry(),
106 t.getControlFlowGraph().getImplicitReturn(),
107 IS_RETURN, GOES_THROUGH_TRUE_CONDITION_PREDICATE);
108
109 if (!test.allPathsSatisfyPredicate()) {
110 compiler.report(
111 t.makeError(t.getScopeRoot(), level,
112 MISSING_RETURN_STATEMENT, returnType.toString()));
113 }
114 }
115
116 /**
117 * Fast check to see if all execution paths contain a return statement.
118 * May spuriously report that a return statement is missing.
119 *
120 * @return true if all paths return, converse not necessarily true
121 */
122 private static boolean fastAllPathsReturnCheck(ControlFlowGraph<Node> cfg) {
123 for (DiGraphEdge<Node, Branch> s : cfg.getImplicitReturn().getInEdges()) {
124 if (!s.getSource().getValue().isReturn()) {
125 return false;
126 }
127 }
128 return true;
129 }
130
131 @Override
132 public void exitScope(NodeTraversal t) {
133 }
134
135 @Override
136 public boolean shouldTraverse(
137 NodeTraversal nodeTraversal, Node n, Node parent) {
138 return true;
139 }
140
141 @Override
142 public void visit(NodeTraversal t, Node n, Node parent) {
143 }
144
145 /**
146 * Determines if the given scope should explicitly return. All functions
147 * with non-void or non-unknown return types must have explicit returns.
148 * @return If a return type is expected, returns it. Otherwise, returns null.
149 */
150 private JSType explicitReturnExpected(Node scope) {
151 FunctionType scopeType = JSType.toMaybeFunctionType(scope.getJSType());
152
153 if (scopeType == null) {
154 return null;
155 }
156
157 if (isEmptyFunction(scope)) {
158 return null;
159 }
160
161 JSType returnType = scopeType.getReturnType();
162
163 if (returnType == null) {
164 return null;
165 }
166
167 if (!isVoidOrUnknown(returnType)) {
168 return returnType;
169 }
170
171 return null;
172 }
173
174
175 /**
176 * @return {@code true} if function represents a JavaScript function
177 * with an empty body
178 */
179 private static boolean isEmptyFunction(Node function) {
180 return function.getChildCount() == 3 &&
181 !function.getFirstChild().getNext().getNext().hasChildren();
182 }
183
184 /**
185 * @return {@code true} if returnType is void, unknown, or a union
186 * containing void or unknown
187 */
188 private boolean isVoidOrUnknown(JSType returnType) {
189 final JSType voidType =
190 compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE);
191 return voidType.isSubtype(returnType);
192 }
193 }