Debian Codegraph @

src/com/google/javascript/jscomp/RecordFunctionInformation.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 package com.google.javascript.jscomp;
17
18 import com.google.common.collect.Iterables;
19 import com.google.common.collect.Sets;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.Node;
22 import java.util.Comparator;
23 import java.util.TreeSet;
24
25 /**
26 * Records information about functions and modules.
27 *
28 */
29 class RecordFunctionInformation extends AbstractPostOrderCallback
30 implements CompilerPass {
31 private final Compiler compiler;
32 private final FunctionNames functionNames;
33 private final JSModuleGraph moduleGraph;
34
35 /**
36 * Protocol buffer builder.
37 */
38 private final FunctionInformationMap.Builder mapBuilder;
39
40 /**
41 * Creates a record function information compiler pass.
42 *
43 * @param compiler The JSCompiler
44 * @param functionNames Assigned function identifiers.
45 */
46 RecordFunctionInformation(Compiler compiler,
47 FunctionNames functionNames) {
48 this.compiler = compiler;
49 this.moduleGraph = compiler.getModuleGraph();
50 this.functionNames = functionNames;
51 this.mapBuilder = FunctionInformationMap.newBuilder();
52 }
53
54 /**
55 * Returns the built-out map.
56 */
57 FunctionInformationMap getMap() {
58 return mapBuilder.build();
59 }
60
61 @Override
62 public void process(Node externs, Node root) {
63 NodeTraversal.traverse(compiler, root, this);
64
65 if (moduleGraph == null) {
66 addModuleInformation(null);
67 } else {
68 // The test expects a consistent module order.
69 TreeSet<JSModule> modules = Sets.newTreeSet(new Comparator<JSModule>() {
70 @Override
71 public int compare(JSModule o1, JSModule o2) {
72 return o1.getName().compareTo(o2.getName());
73 }
74 });
75 Iterables.addAll(modules, moduleGraph.getAllModules());
76 for (JSModule m : modules) {
77 addModuleInformation(m);
78 }
79 }
80 }
81
82 @Override
83 public void visit(NodeTraversal t, Node n, Node parent) {
84 if (!n.isFunction()) {
85 return;
86 }
87
88 int id = functionNames.getFunctionId(n);
89 if (id < 0) {
90 // Function node was added during compilation; don't instrument.
91 return;
92 }
93
94 String compiledSource = compiler.toSource(n);
95 JSModule module = t.getModule();
96 mapBuilder.addEntry(FunctionInformationMap.Entry.newBuilder()
97 .setId(id)
98 .setSourceName(NodeUtil.getSourceName(n))
99 .setLineNumber(n.getLineno())
100 .setModuleName(moduleGraph == null ? "" : module.getName())
101 .setSize(compiledSource.length())
102 .setName(functionNames.getFunctionName(n))
103 .setCompiledSource(compiledSource).build());
104 }
105
106 /**
107 * Record a module's compiled source. The view of the source we get
108 * from function sources alone is not complete; it doesn't contain
109 * assignments and function calls in the global scope which are
110 * crucial to understanding how the application works.
111 *
112 * This version of the source is also written out to js_output_file,
113 * module_output_path_prefix or other places. Duplicating it here
114 * simplifies the process of writing tools that combine and present
115 * module and function for debugging purposes.
116 */
117 private void addModuleInformation(JSModule module) {
118 String name;
119 String source;
120 if (module != null) {
121 name = module.getName();
122 source = compiler.toSource(module);
123 } else {
124 name = "";
125 source = compiler.toSource();
126 }
127
128 mapBuilder.addModule(FunctionInformationMap.Module.newBuilder()
129 .setName(name)
130 .setCompiledSource(source).build());
131 }
132 }