-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RenameLabels.java
301 lines (265 loc) · 9.01 KB
/
RenameLabels.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2008 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Supplier;
import com.google.javascript.jscomp.NodeTraversal.ScopedCallback;
import com.google.javascript.rhino.Node;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
/**
* RenameLabels renames all the labels so that they have short names, to reduce
* code size and also to obfuscate the code.
*
* Label names have a unique namespace, so variable or function names clashes
* are not a concern, but keywords clashes are.
*
* Additionally, labels names are only within the statements include in the
* label and do not cross function boundaries. This means that it is possible to
* create one label name that is used for labels at any given depth of label
* nesting. Typically, the name "a" will be used for all top-level labels, "b"
* for the next nested label, and so on. For example:
*
* <code>
* function bar() {
* a: {
* b: {
* foo();
* }
* }
*
* a: {
* b: break a;
* }
* }
* </code>
*
* The general processes is as follows: process() is the entry point for the
* CompilerPass, and from there a standard "ScopedCallback" traversal is done,
* where "shouldTraverse" is called when descending the tree, and the "visit" is
* called in a depth first manner. The name for the label is selected during the
* decent in "shouldTraverse", and the references to the label name are renamed
* as they are encountered during the "visit". This means that if the label is
* unreferenced, it is known when the label node is visited, and, if so, can be
* safely removed.
*
* @author johnlenz@google.com (John Lenz)
*/
final class RenameLabels implements CompilerPass {
private final AbstractCompiler compiler;
private final Supplier<String> nameSupplier;
private final boolean removeUnused;
private final boolean markChanges;
RenameLabels(final AbstractCompiler compiler) {
this(compiler, new DefaultNameSupplier(), true, true);
}
RenameLabels(
AbstractCompiler compiler,
Supplier<String> supplier,
boolean removeUnused,
boolean markChanges) {
this.compiler = compiler;
this.nameSupplier = supplier;
this.removeUnused = removeUnused;
this.markChanges = markChanges;
}
static class DefaultNameSupplier implements Supplier<String> {
// DefaultNameGenerator is used to create safe label names.
private final NameGenerator nameGenerator;
private DefaultNameSupplier(final NameGenerator nameGen) {
this.nameGenerator = nameGen;
}
private DefaultNameSupplier() {
this.nameGenerator = new DefaultNameGenerator(
new HashSet<String>(), "", null);
}
@Override
public String get() {
return nameGenerator.generateNextName();
}
}
/**
* Iterate through the nodes, renaming all the labels.
*/
class ProcessLabels implements ScopedCallback {
private final boolean markChanges;
ProcessLabels(boolean markChanges) {
this.markChanges = markChanges;
// Create a entry for global scope.
namespaceStack.push(new LabelNamespace());
}
// A stack of labels namespaces. Labels in an outer scope aren't part of an
// inner scope, so a new namespace is created each time a scope is entered.
final Deque<LabelNamespace> namespaceStack = new ArrayDeque<>();
// The list of generated names. Typically, the first name will be "a",
// the second "b", etc.
final ArrayList<String> names = new ArrayList<>();
@Override
public void enterScope(NodeTraversal nodeTraversal) {
// Start a new namespace for label names.
if (nodeTraversal.getScopeRoot().isFunction()) {
namespaceStack.push(new LabelNamespace());
}
}
@Override
public void exitScope(NodeTraversal nodeTraversal) {
if (nodeTraversal.getScopeRoot().isFunction()) {
namespaceStack.pop();
}
}
/**
* shouldTraverse is call when descending into the Node tree, so it is used
* here to build the context for label renames.
*
* {@inheritDoc}
*/
@Override
public boolean shouldTraverse(NodeTraversal nodeTraversal, Node node,
Node parent) {
if (node.isLabel()) {
// Determine the new name for this label.
LabelNamespace current = namespaceStack.peek();
int currentDepth = current.renameMap.size() + 1;
String name = node.getFirstChild().getString();
// Store the context for this label name.
LabelInfo li = new LabelInfo(currentDepth);
checkState(!current.renameMap.containsKey(name));
current.renameMap.put(name, li);
// Create a new name, if needed, for this depth.
if (names.size() < currentDepth) {
names.add(nameSupplier.get());
}
String newName = getNameForId(currentDepth);
compiler.addToDebugLog("label renamed: ", name, " => ", newName);
}
return true;
}
/**
* Delegate the actual processing of the node to visitLabel and
* visitBreakOrContinue.
*
* {@inheritDoc}
*/
@Override
public void visit(NodeTraversal t, Node node, Node parent) {
switch (node.getToken()) {
case LABEL:
visitLabel(t, node, parent);
break;
case BREAK:
case CONTINUE:
visitBreakOrContinue(t, node);
break;
default:
break;
}
}
/**
* Rename label references in breaks and continues.
* @param node The break or continue node.
*/
private void visitBreakOrContinue(NodeTraversal t, Node node) {
Node nameNode = node.getFirstChild();
if (nameNode != null) {
// This is a named break or continue;
String name = nameNode.getString();
checkState(!name.isEmpty());
LabelInfo li = getLabelInfo(name);
if (li != null) {
String newName = getNameForId(li.id);
// Mark the label as referenced so it isn't removed.
li.referenced = true;
if (!name.equals(newName)) {
// Give it the short name.
nameNode.setString(newName);
if (markChanges) {
t.reportCodeChange();
}
}
}
}
}
/**
* Rename or remove labels.
* @param node The label node.
* @param parent The parent of the label node.
*/
private void visitLabel(NodeTraversal t, Node node, Node parent) {
Node nameNode = node.getFirstChild();
checkState(nameNode != null);
String name = nameNode.getString();
LabelInfo li = getLabelInfo(name);
// This is a label...
if (li.referenced || !removeUnused) {
String newName = getNameForId(li.id);
if (!name.equals(newName)) {
// ... and it is used, give it the short name.
nameNode.setString(newName);
if (markChanges) {
t.reportCodeChange();
}
}
} else {
// ... and it is not referenced, just remove it.
Node newChild = node.getLastChild();
node.removeChild(newChild);
parent.replaceChild(node, newChild);
if (newChild.isNormalBlock()) {
NodeUtil.tryMergeBlock(newChild, false);
}
if (markChanges) {
t.reportCodeChange();
}
}
// Remove the label from the current stack of labels.
namespaceStack.peek().renameMap.remove(name);
}
/**
* @param id The id, which is the depth of the label in the current context,
* for which to get a short name.
* @return The short name of the identified label.
*/
String getNameForId(int id) {
return names.get(id - 1);
}
/**
* @param name The name to retrieve information about.
* @return The structure representing the name in the current context.
*/
LabelInfo getLabelInfo(String name) {
return namespaceStack.peek().renameMap.get(name);
}
}
@Override
public void process(Node externs, Node root) {
// Do variable reference counting.
NodeTraversal.traverseEs6(compiler, root, new ProcessLabels(markChanges));
}
private static class LabelInfo {
boolean referenced = false;
final int id;
LabelInfo(int id) {
this.id = id;
}
}
private static class LabelNamespace {
final Map<String, LabelInfo> renameMap = new HashMap<>();
}
}