Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.Preconditions;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.NameReferenceGraph.Name;
22 import com.google.javascript.jscomp.NameReferenceGraph.Reference;
23 import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge;
24 import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.jstype.JSType;
27
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.List;
31
32 /**
33 * Generate a nice HTML file describing the name reference graph.
34 * For each declaration, list the sites where the declaration's name
35 * is referenced, and list all the names that the declaration references.
36 * For each, name exactly where use occurs in the source code.
37 *
38 * <p>This report should be useful both for internal compiler
39 * developers and for engineers trying to understand running behavior
40 * of their code or who want to understand why the compiler won't
41 * move their code into a new module.
42 *
43 * @author [email protected] (Robert Bowdidge)
44 */
45
46 final class NameReferenceGraphReport {
47 private NameReferenceGraph graph = null;
48
49 /**
50 * Create a NameReferenceGraphReport object.
51 *
52 * @param g name reference graph to describe in report.
53 */
54 NameReferenceGraphReport(NameReferenceGraph g) {
55 this.graph = g;
56 }
57
58 /**
59 * Generate a nice HTML file describing the name reference graph.
60 * For each declaration, list the sites where the declaration's name
61 * is referenced, and list all the names that the declaration references.
62 * For each, name exactly where use occurs in the source code.
63 *
64 * <p>This report should be useful both for internal compiler
65 * developers and for engineers trying to understand running
66 * behavior of their code or who want to understand why
67 * AbstractCompiler won't move their code into a new module.
68 *
69 * @return String containing the entire HTML for the report.
70 */
71 public String getHtmlReport() {
72 StringBuilder builder = new StringBuilder();
73 List<DiGraphNode<Name, Reference>> nodes = Lists.newArrayList(
74 graph.getDirectedGraphNodes());
75
76 generateHtmlReportHeader(builder);
77
78 builder.append("<h1>Name Reference Graph Dump</h1>\n");
79 builder.append("OVERALL STATS\n");
80 builder.append("<ul>\n");
81 builder.append("<li>Total names: " + nodes.size());
82 builder.append("</ul>\n");
83
84 builder.append("ALL NAMES\n");
85 builder.append("<UL>\n");
86
87 // Sort declarations in alphabetical order.
88 Collections.sort(nodes, new DiGraphNodeComparator());
89
90 for (DiGraphNode<Name, Reference> n : nodes) {
91 // Generate the HTML describing the declaration itself.
92 generateDeclarationReport(builder, n);
93
94 // Next, list the places where this name is used (REFERS TO), and the
95 // names that this declaration refers to (REFERENCED BY).
96 List<DiGraphEdge<Name, Reference>> outEdges =
97 graph.getOutEdges(n.getValue());
98 List<DiGraphEdge<Name, Reference>> inEdges =
99 graph.getInEdges(n.getValue());
100
101 // Don't bother to create the dotted list if we don't have anything to
102 // put in it.
103 if (!outEdges.isEmpty() || !inEdges.isEmpty()) {
104 builder.append("<ul>");
105
106 if (outEdges.size() > 0) {
107 builder.append("<li>REFERS TO:<br>\n");
108 builder.append("<ul>");
109 for (DiGraphEdge<Name, Reference> edge : outEdges) {
110 generateEdgeReport(builder, edge.getDestination().getValue(),
111 edge);
112 }
113 builder.append("</ul>\n");
114 }
115
116 if (inEdges.size() > 0) {
117 builder.append("<li>REFERENCED BY:<br>\n");
118 builder.append("<ul>");
119 for (DiGraphEdge<Name, Reference> edge : inEdges) {
120 generateEdgeReport(builder, edge.getSource().getValue(), edge);
121 }
122 builder.append("</ul>");
123 }
124 builder.append("</ul>\n");
125 }
126 }
127 builder.append("</ul>\n");
128 generateHtmlReportFooter(builder);
129 return builder.toString();
130 }
131
132 /**
133 * Given a node, find the name of the containing source file.
134 *
135 * @param node Parse tree node whose filename is requested
136 * @return String containing name of source file, or empty string if name
137 * cannot be identified.
138 */
139 private String getSourceFile(Node node) {
140 String filename = node.getSourceFileName();
141 if (filename == null) {
142 return "";
143 }
144 return filename;
145 }
146
147 /**
148 * Generate the HTML for describing a specific declaration.
149 * @param builder contents of report to be generated
150 * @param declarationNode declaration to describe
151 */
152 private void generateDeclarationReport(StringBuilder builder,
153 DiGraphNode<Name, Reference> declarationNode) {
154 // Provide the name and location of declaration,
155 // with an anchor to allow navigation to this declaration.
156 String declName = declarationNode.getValue().getQualifiedName();
157 JSType declType = declarationNode.getValue().getType();
158
159 builder.append("<LI> ");
160 builder.append("<A NAME=\"" + declName + "\">");
161 builder.append(declName);
162 builder.append("\n");
163
164 // Provide the type of the declaration.
165 // This is helpful for debugging.
166 generateType(builder, declType);
167
168 // List all the definitions of this name that were found in the code.
169 // For each, list
170 List<DefinitionsRemover.Definition> defs =
171 declarationNode.getValue().getDeclarations();
172
173 if (defs.size() == 0) {
174 builder.append("<br>No definitions found<br>");
175 } else {
176 // Otherwise, provide a list of definitions in a dotted list.
177 // For each definition, print the location where that definition is
178 // found.
179 builder.append("<ul>");
180 for (DefinitionsRemover.Definition def : defs) {
181 Node fnDef = def.getRValue();
182 String sourceFileName = getSourceFile(fnDef);
183 builder.append("<li> Defined: ");
184 generateSourceReferenceLink(builder,
185 sourceFileName, fnDef.getLineno(), fnDef.getCharno());
186 }
187 builder.append("</ul>");
188 }
189 }
190
191 /**
192 * Generate the HTML header for the report style.
193 * Borrowed straight from NameAnalyzer's report style.
194 *
195 * @param builder contents of the report to be generated
196 */
197 private void generateHtmlReportHeader(StringBuilder builder) {
198 builder.append("<!DOCTYPE html>\n" +
199 "<html>" +
200 "<head>" +
201 "<meta http-equiv=\"Content-Type\" " +
202 "content=\"text/html;charset=utf-8\" >" +
203 "<title>Name Reference Graph Dump</title>" +
204 "<style type=\"text/css\">body, td, ");
205 builder.append("p {font-family: Arial; font-size: 83%} ");
206 builder.append("ul {margin-top:2px; margin-left:0px; padding-left:1em;}");
207 builder.append("li {margin-top:3px; margin-left:24px;" +
208 "padding-left:0px;padding-bottom: 4px}");
209 builder.append("</style></head><body>\n");
210 }
211
212 /**
213 * Generate the HTML footer for the report style.
214 */
215 private void generateHtmlReportFooter(StringBuilder builder) {
216 builder.append("</body></html>");
217 }
218
219 /**
220 * Generate a description of a specific edge between two nodes.
221 * For each edge, name the element being linked, the location of the
222 * reference in the source file, and the type of the reference.
223 *
224 * @param builder contents of the report to be generated
225 * @param referencedDecl name of the declaration being referenced
226 * @param edge the graph edge being described
227 */
228 private void generateEdgeReport(StringBuilder builder,
229 Name referencedDecl, DiGraphEdge<Name, Reference> edge) {
230 String srcDeclName = referencedDecl.getQualifiedName();
231 builder.append("<li><A HREF=\"#" + srcDeclName + "\">");
232 builder.append(srcDeclName);
233 builder.append("</a> ");
234
235 Node def = edge.getValue().getSite();
236 int lineNumber = def.getLineno();
237 int columnNumber = def.getCharno();
238 String sourceFile = getSourceFile(def);
239
240 generateSourceReferenceLink(builder, sourceFile, lineNumber, columnNumber);
241
242 JSType defType = edge.getValue().getSite().getJSType();
243 generateType(builder, defType);
244 }
245
246 /**
247 * Generate a link and text for a reference to a particular location
248 * in a source file. Selecting the link should take the programmer
249 * to a browsable version of the file.
250 *
251 * @param builder contents of the report to be generated
252 * @param sourceFile Path to the file
253 * @param lineNumber line where the object to view is located
254 * @param columnNumber column where the object to highlight is located.
255 */
256 private void generateSourceReferenceLink(StringBuilder builder,
257 String sourceFile, int lineNumber, int columnNumber) {
258 assert(sourceFile != null);
259
260 builder.append("(");
261
262
263 // Print out the text path so the user knows where things come from.
264 builder.append(sourceFile + ":" +
265 lineNumber + "," + columnNumber);
266
267
268 builder.append(")");
269 }
270
271 /**
272 * Dump a type in a nice, readable way.
273 *
274 * @param builder contents of the report to be generated.
275 * @param defType type to describe
276 */
277 private void generateType(StringBuilder builder, JSType defType) {
278 if (defType == null) {
279 builder.append(" (type: null) ");
280 } else if (defType.isUnknownType()) {
281 builder.append(" (type: unknown) ");
282 } else {
283 builder.append(" (type: " +
284 defType.toString() + ") ");
285 }
286 }
287
288 /**
289 * DiGraphNodeComparator gives us a way to generate sorted lists
290 * of DiGraphNodes. It provides a compare function used by the
291 * String class's sort method.
292 */
293 class DiGraphNodeComparator implements
294 Comparator<DiGraphNode<Name, Reference>> {
295 @Override
296 public int compare(DiGraphNode<Name, Reference> node1,
297 DiGraphNode<Name, Reference> node2) {
298 Preconditions.checkNotNull(node1.getValue());
299 Preconditions.checkNotNull(node2.getValue());
300
301 if ((node1.getValue().getQualifiedName() == null) &&
302 (node2.getValue().getQualifiedName() == null)) {
303 return 0;
304 }
305
306 // Node 1, if null, comes before node 2.
307 if (node1.getValue().getQualifiedName() == null) {
308 return -1;
309 }
310
311 // Node 2, if null, comes before node 1.
312 if (node2.getValue().getQualifiedName() == null) {
313 return 1;
314 }
315
316 return node1.getValue().getQualifiedName().compareTo(
317 node2.getValue().getQualifiedName());
318 }
319 }
320
321 }