author | jjg |
Tue, 16 Sep 2008 18:35:18 -0700 | |
changeset 1260 | a772ba9ba43d |
parent 1109 | 853d8c191eac |
child 1471 | 57506cdfb7b4 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
735 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.HashSet; |
|
30 |
import java.util.Map; |
|
31 |
import java.util.Set; |
|
942 | 32 |
import java.util.Locale; |
10 | 33 |
import javax.tools.DiagnosticListener; |
34 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
35 |
|
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
167
diff
changeset
|
36 |
import com.sun.tools.javac.file.JavacFileManager; |
10 | 37 |
import com.sun.tools.javac.tree.JCTree; |
942 | 38 |
import com.sun.tools.javac.api.DiagnosticFormatter; |
10 | 39 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
40 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
|
41 |
||
42 |
/** A class for error logs. Reports errors and warnings, and |
|
43 |
* keeps track of error numbers and positions. |
|
44 |
* |
|
45 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
46 |
* you write code that depends on this, you do so at your own risk. |
|
47 |
* This code and its internal interfaces are subject to change or |
|
48 |
* deletion without notice.</b> |
|
49 |
*/ |
|
867 | 50 |
public class Log extends AbstractLog { |
10 | 51 |
/** The context key for the log. */ |
52 |
public static final Context.Key<Log> logKey |
|
53 |
= new Context.Key<Log>(); |
|
54 |
||
55 |
/** The context key for the output PrintWriter. */ |
|
56 |
public static final Context.Key<PrintWriter> outKey = |
|
57 |
new Context.Key<PrintWriter>(); |
|
58 |
||
59 |
//@Deprecated |
|
60 |
public final PrintWriter errWriter; |
|
61 |
||
62 |
//@Deprecated |
|
63 |
public final PrintWriter warnWriter; |
|
64 |
||
65 |
//@Deprecated |
|
66 |
public final PrintWriter noticeWriter; |
|
67 |
||
68 |
/** The maximum number of errors/warnings that are reported. |
|
69 |
*/ |
|
70 |
public final int MaxErrors; |
|
71 |
public final int MaxWarnings; |
|
72 |
||
73 |
/** Switch: prompt user on each error. |
|
74 |
*/ |
|
75 |
public boolean promptOnError; |
|
76 |
||
77 |
/** Switch: emit warning messages. |
|
78 |
*/ |
|
79 |
public boolean emitWarnings; |
|
80 |
||
81 |
/** Print stack trace on errors? |
|
82 |
*/ |
|
83 |
public boolean dumpOnError; |
|
84 |
||
85 |
/** Print multiple errors for same source locations. |
|
86 |
*/ |
|
87 |
public boolean multipleErrors; |
|
88 |
||
89 |
/** |
|
90 |
* Diagnostic listener, if provided through programmatic |
|
91 |
* interface to javac (JSR 199). |
|
92 |
*/ |
|
93 |
protected DiagnosticListener<? super JavaFileObject> diagListener; |
|
867 | 94 |
|
10 | 95 |
/** |
96 |
* Formatter for diagnostics |
|
97 |
*/ |
|
942 | 98 |
private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
10 | 99 |
|
100 |
/** Construct a log with given I/O redirections. |
|
101 |
*/ |
|
102 |
@Deprecated |
|
103 |
protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
|
867 | 104 |
super(JCDiagnostic.Factory.instance(context)); |
10 | 105 |
context.put(logKey, this); |
106 |
this.errWriter = errWriter; |
|
107 |
this.warnWriter = warnWriter; |
|
108 |
this.noticeWriter = noticeWriter; |
|
109 |
||
110 |
Options options = Options.instance(context); |
|
111 |
this.dumpOnError = options.get("-doe") != null; |
|
112 |
this.promptOnError = options.get("-prompt") != null; |
|
113 |
this.emitWarnings = options.get("-Xlint:none") == null; |
|
114 |
this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100); |
|
115 |
this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100); |
|
116 |
||
942 | 117 |
boolean rawDiagnostics = options.get("rawDiagnostics") != null; |
118 |
Messages msgs = Messages.instance(context); |
|
119 |
this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(msgs) : |
|
120 |
new BasicDiagnosticFormatter(options, msgs); |
|
10 | 121 |
@SuppressWarnings("unchecked") // FIXME |
122 |
DiagnosticListener<? super JavaFileObject> diagListener = |
|
123 |
context.get(DiagnosticListener.class); |
|
124 |
this.diagListener = diagListener; |
|
125 |
} |
|
126 |
// where |
|
127 |
private int getIntOption(Options options, String optionName, int defaultValue) { |
|
128 |
String s = options.get(optionName); |
|
129 |
try { |
|
130 |
if (s != null) return Integer.parseInt(s); |
|
131 |
} catch (NumberFormatException e) { |
|
132 |
// silently ignore ill-formed numbers |
|
133 |
} |
|
134 |
return defaultValue; |
|
135 |
} |
|
136 |
||
137 |
/** The default writer for diagnostics |
|
138 |
*/ |
|
139 |
static final PrintWriter defaultWriter(Context context) { |
|
140 |
PrintWriter result = context.get(outKey); |
|
141 |
if (result == null) |
|
142 |
context.put(outKey, result = new PrintWriter(System.err)); |
|
143 |
return result; |
|
144 |
} |
|
145 |
||
146 |
/** Construct a log with default settings. |
|
147 |
*/ |
|
148 |
protected Log(Context context) { |
|
149 |
this(context, defaultWriter(context)); |
|
150 |
} |
|
151 |
||
152 |
/** Construct a log with all output redirected. |
|
153 |
*/ |
|
154 |
protected Log(Context context, PrintWriter defaultWriter) { |
|
155 |
this(context, defaultWriter, defaultWriter, defaultWriter); |
|
156 |
} |
|
157 |
||
158 |
/** Get the Log instance for this context. */ |
|
159 |
public static Log instance(Context context) { |
|
160 |
Log instance = context.get(logKey); |
|
161 |
if (instance == null) |
|
162 |
instance = new Log(context); |
|
163 |
return instance; |
|
164 |
} |
|
165 |
||
166 |
/** The number of errors encountered so far. |
|
167 |
*/ |
|
168 |
public int nerrors = 0; |
|
169 |
||
170 |
/** The number of warnings encountered so far. |
|
171 |
*/ |
|
172 |
public int nwarnings = 0; |
|
173 |
||
174 |
/** A set of all errors generated so far. This is used to avoid printing an |
|
175 |
* error message more than once. For each error, a pair consisting of the |
|
176 |
* source file name and source code position of the error is added to the set. |
|
177 |
*/ |
|
178 |
private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
|
179 |
||
180 |
public boolean hasDiagnosticListener() { |
|
181 |
return diagListener != null; |
|
182 |
} |
|
183 |
||
184 |
public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { |
|
867 | 185 |
name.getClass(); // null check |
186 |
getSource(name).setEndPosTable(table); |
|
10 | 187 |
} |
188 |
||
189 |
/** Return current source name. |
|
190 |
*/ |
|
191 |
public JavaFileObject currentSource() { |
|
192 |
return source == null ? null : source.getFile(); |
|
193 |
} |
|
194 |
||
195 |
/** Flush the logs |
|
196 |
*/ |
|
197 |
public void flush() { |
|
198 |
errWriter.flush(); |
|
199 |
warnWriter.flush(); |
|
200 |
noticeWriter.flush(); |
|
201 |
} |
|
202 |
||
203 |
/** Returns true if an error needs to be reported for a given |
|
204 |
* source name and pos. |
|
205 |
*/ |
|
206 |
protected boolean shouldReport(JavaFileObject file, int pos) { |
|
207 |
if (multipleErrors || file == null) |
|
208 |
return true; |
|
209 |
||
210 |
Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
|
211 |
boolean shouldReport = !recorded.contains(coords); |
|
212 |
if (shouldReport) |
|
213 |
recorded.add(coords); |
|
214 |
return shouldReport; |
|
215 |
} |
|
216 |
||
217 |
/** Prompt user after an error. |
|
218 |
*/ |
|
219 |
public void prompt() { |
|
220 |
if (promptOnError) { |
|
221 |
System.err.println(getLocalizedString("resume.abort")); |
|
222 |
char ch; |
|
223 |
try { |
|
224 |
while (true) { |
|
225 |
switch (System.in.read()) { |
|
226 |
case 'a': case 'A': |
|
227 |
System.exit(-1); |
|
228 |
return; |
|
229 |
case 'r': case 'R': |
|
230 |
return; |
|
231 |
case 'x': case 'X': |
|
232 |
throw new AssertionError("user abort"); |
|
233 |
default: |
|
234 |
} |
|
235 |
} |
|
236 |
} catch (IOException e) {} |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** Print the faulty source code line and point to the error. |
|
241 |
* @param pos Buffer index of the error position, must be on current line |
|
242 |
*/ |
|
243 |
private void printErrLine(int pos, PrintWriter writer) { |
|
867 | 244 |
String line = (source == null ? null : source.getLine(pos)); |
245 |
if (line == null) |
|
10 | 246 |
return; |
1109
853d8c191eac
6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents:
942
diff
changeset
|
247 |
int col = source.getColumnNumber(pos, false); |
10 | 248 |
|
867 | 249 |
printLines(writer, line); |
250 |
for (int i = 0; i < col - 1; i++) { |
|
251 |
writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
|
10 | 252 |
} |
253 |
writer.println("^"); |
|
254 |
writer.flush(); |
|
255 |
} |
|
256 |
||
257 |
/** Print the text of a message, translating newlines appropriately |
|
258 |
* for the platform. |
|
259 |
*/ |
|
260 |
public static void printLines(PrintWriter writer, String msg) { |
|
261 |
int nl; |
|
262 |
while ((nl = msg.indexOf('\n')) != -1) { |
|
263 |
writer.println(msg.substring(0, nl)); |
|
264 |
msg = msg.substring(nl+1); |
|
265 |
} |
|
266 |
if (msg.length() != 0) writer.println(msg); |
|
267 |
} |
|
268 |
||
867 | 269 |
protected void directError(String key, Object... args) { |
270 |
printLines(errWriter, getLocalizedString(key, args)); |
|
271 |
errWriter.flush(); |
|
10 | 272 |
} |
273 |
||
274 |
/** Report a warning that cannot be suppressed. |
|
275 |
* @param pos The source position at which to report the warning. |
|
276 |
* @param key The key for the localized warning message. |
|
277 |
* @param args Fields of the warning message. |
|
278 |
*/ |
|
279 |
public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
|
280 |
writeDiagnostic(diags.warning(source, pos, key, args)); |
|
281 |
nwarnings++; |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Common diagnostic handling. |
|
286 |
* The diagnostic is counted, and depending on the options and how many diagnostics have been |
|
287 |
* reported so far, the diagnostic may be handed off to writeDiagnostic. |
|
288 |
*/ |
|
289 |
public void report(JCDiagnostic diagnostic) { |
|
290 |
switch (diagnostic.getType()) { |
|
291 |
case FRAGMENT: |
|
292 |
throw new IllegalArgumentException(); |
|
293 |
||
294 |
case NOTE: |
|
295 |
// Print out notes only when we are permitted to report warnings |
|
296 |
// Notes are only generated at the end of a compilation, so should be small |
|
297 |
// in number. |
|
298 |
if (emitWarnings || diagnostic.isMandatory()) { |
|
299 |
writeDiagnostic(diagnostic); |
|
300 |
} |
|
301 |
break; |
|
302 |
||
303 |
case WARNING: |
|
304 |
if (emitWarnings || diagnostic.isMandatory()) { |
|
305 |
if (nwarnings < MaxWarnings) { |
|
306 |
writeDiagnostic(diagnostic); |
|
307 |
nwarnings++; |
|
308 |
} |
|
309 |
} |
|
310 |
break; |
|
311 |
||
312 |
case ERROR: |
|
313 |
if (nerrors < MaxErrors |
|
314 |
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
|
315 |
writeDiagnostic(diagnostic); |
|
316 |
nerrors++; |
|
317 |
} |
|
318 |
break; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Write out a diagnostic. |
|
324 |
*/ |
|
325 |
protected void writeDiagnostic(JCDiagnostic diag) { |
|
326 |
if (diagListener != null) { |
|
327 |
try { |
|
328 |
diagListener.report(diag); |
|
329 |
return; |
|
330 |
} |
|
331 |
catch (Throwable t) { |
|
332 |
throw new ClientCodeException(t); |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
|
337 |
||
942 | 338 |
printLines(writer, diagFormatter.format(diag, Locale.getDefault())); |
339 |
if (diagFormatter.displaySource(diag)) { |
|
10 | 340 |
int pos = diag.getIntPosition(); |
341 |
if (pos != Position.NOPOS) { |
|
342 |
JavaFileObject prev = useSource(diag.getSource()); |
|
343 |
printErrLine(pos, writer); |
|
344 |
useSource(prev); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
if (promptOnError) { |
|
349 |
switch (diag.getType()) { |
|
350 |
case ERROR: |
|
351 |
case WARNING: |
|
352 |
prompt(); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
if (dumpOnError) |
|
357 |
new RuntimeException().printStackTrace(writer); |
|
358 |
||
359 |
writer.flush(); |
|
360 |
} |
|
361 |
||
362 |
@Deprecated |
|
363 |
protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
|
364 |
switch (dt) { |
|
365 |
case FRAGMENT: |
|
366 |
throw new IllegalArgumentException(); |
|
367 |
||
368 |
case NOTE: |
|
369 |
return noticeWriter; |
|
370 |
||
371 |
case WARNING: |
|
372 |
return warnWriter; |
|
373 |
||
374 |
case ERROR: |
|
375 |
return errWriter; |
|
376 |
||
377 |
default: |
|
378 |
throw new Error(); |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
/** Find a localized string in the resource bundle. |
|
383 |
* @param key The key for the localized string. |
|
384 |
* @param args Fields to substitute into the string. |
|
385 |
*/ |
|
386 |
public static String getLocalizedString(String key, Object ... args) { |
|
387 |
return Messages.getDefaultLocalizedString("compiler.misc." + key, args); |
|
388 |
} |
|
389 |
||
390 |
/*************************************************************************** |
|
391 |
* raw error messages without internationalization; used for experimentation |
|
392 |
* and quick prototyping |
|
393 |
***************************************************************************/ |
|
394 |
||
867 | 395 |
/** print an error or warning message: |
396 |
*/ |
|
10 | 397 |
private void printRawError(int pos, String msg) { |
867 | 398 |
if (source == null || pos == Position.NOPOS) { |
10 | 399 |
printLines(errWriter, "error: " + msg); |
400 |
} else { |
|
867 | 401 |
int line = source.getLineNumber(pos); |
10 | 402 |
JavaFileObject file = currentSource(); |
403 |
if (file != null) |
|
404 |
printLines(errWriter, |
|
405 |
JavacFileManager.getJavacFileName(file) + ":" + |
|
406 |
line + ": " + msg); |
|
407 |
printErrLine(pos, errWriter); |
|
408 |
} |
|
409 |
errWriter.flush(); |
|
410 |
} |
|
411 |
||
867 | 412 |
/** report an error: |
413 |
*/ |
|
10 | 414 |
public void rawError(int pos, String msg) { |
415 |
if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) { |
|
416 |
printRawError(pos, msg); |
|
417 |
prompt(); |
|
418 |
nerrors++; |
|
419 |
} |
|
420 |
errWriter.flush(); |
|
421 |
} |
|
422 |
||
867 | 423 |
/** report a warning: |
424 |
*/ |
|
10 | 425 |
public void rawWarning(int pos, String msg) { |
426 |
if (nwarnings < MaxWarnings && emitWarnings) { |
|
427 |
printRawError(pos, "warning: " + msg); |
|
428 |
} |
|
429 |
prompt(); |
|
430 |
nwarnings++; |
|
431 |
errWriter.flush(); |
|
432 |
} |
|
433 |
||
434 |
public static String format(String fmt, Object... args) { |
|
435 |
return String.format((java.util.Locale)null, fmt, args); |
|
436 |
} |
|
437 |
||
438 |
} |