Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.*; |
aoqi@0 | 29 | import java.util.Arrays; |
aoqi@0 | 30 | import java.util.EnumSet; |
aoqi@0 | 31 | import java.util.HashSet; |
aoqi@0 | 32 | import java.util.Queue; |
aoqi@0 | 33 | import java.util.Set; |
aoqi@0 | 34 | import javax.tools.DiagnosticListener; |
aoqi@0 | 35 | import javax.tools.JavaFileObject; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import com.sun.tools.javac.api.DiagnosticFormatter; |
aoqi@0 | 38 | import com.sun.tools.javac.main.Main; |
aoqi@0 | 39 | import com.sun.tools.javac.main.Option; |
aoqi@0 | 40 | import com.sun.tools.javac.tree.EndPosTable; |
aoqi@0 | 41 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
aoqi@0 | 42 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
aoqi@0 | 43 | |
aoqi@0 | 44 | import static com.sun.tools.javac.main.Option.*; |
aoqi@0 | 45 | |
aoqi@0 | 46 | /** A class for error logs. Reports errors and warnings, and |
aoqi@0 | 47 | * keeps track of error numbers and positions. |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 50 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 51 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 52 | * deletion without notice.</b> |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public class Log extends AbstractLog { |
aoqi@0 | 55 | /** The context key for the log. */ |
aoqi@0 | 56 | public static final Context.Key<Log> logKey |
aoqi@0 | 57 | = new Context.Key<Log>(); |
aoqi@0 | 58 | |
aoqi@0 | 59 | /** The context key for the output PrintWriter. */ |
aoqi@0 | 60 | public static final Context.Key<PrintWriter> outKey = |
aoqi@0 | 61 | new Context.Key<PrintWriter>(); |
aoqi@0 | 62 | |
aoqi@0 | 63 | /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */ |
aoqi@0 | 64 | public enum PrefixKind { |
aoqi@0 | 65 | JAVAC("javac."), |
aoqi@0 | 66 | COMPILER_MISC("compiler.misc."); |
aoqi@0 | 67 | PrefixKind(String v) { |
aoqi@0 | 68 | value = v; |
aoqi@0 | 69 | } |
aoqi@0 | 70 | public String key(String k) { |
aoqi@0 | 71 | return value + k; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | final String value; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | |
aoqi@0 | 76 | /** |
aoqi@0 | 77 | * DiagnosticHandler's provide the initial handling for diagnostics. |
aoqi@0 | 78 | * When a diagnostic handler is created and has been initialized, it |
aoqi@0 | 79 | * should install itself as the current diagnostic handler. When a |
aoqi@0 | 80 | * client has finished using a handler, the client should call |
aoqi@0 | 81 | * {@code log.removeDiagnosticHandler();} |
aoqi@0 | 82 | * |
aoqi@0 | 83 | * Note that javax.tools.DiagnosticListener (if set) is called later in the |
aoqi@0 | 84 | * diagnostic pipeline. |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | public static abstract class DiagnosticHandler { |
aoqi@0 | 87 | /** |
aoqi@0 | 88 | * The previously installed diagnostic handler. |
aoqi@0 | 89 | */ |
aoqi@0 | 90 | protected DiagnosticHandler prev; |
aoqi@0 | 91 | |
aoqi@0 | 92 | /** |
aoqi@0 | 93 | * Install this diagnostic handler as the current one, |
aoqi@0 | 94 | * recording the previous one. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | protected void install(Log log) { |
aoqi@0 | 97 | prev = log.diagnosticHandler; |
aoqi@0 | 98 | log.diagnosticHandler = this; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * Handle a diagnostic. |
aoqi@0 | 103 | */ |
aoqi@0 | 104 | public abstract void report(JCDiagnostic diag); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | /** |
aoqi@0 | 108 | * A DiagnosticHandler that discards all diagnostics. |
aoqi@0 | 109 | */ |
aoqi@0 | 110 | public static class DiscardDiagnosticHandler extends DiagnosticHandler { |
aoqi@0 | 111 | public DiscardDiagnosticHandler(Log log) { |
aoqi@0 | 112 | install(log); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | public void report(JCDiagnostic diag) { } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | /** |
aoqi@0 | 119 | * A DiagnosticHandler that can defer some or all diagnostics, |
aoqi@0 | 120 | * by buffering them for later examination and/or reporting. |
aoqi@0 | 121 | * If a diagnostic is not deferred, or is subsequently reported |
aoqi@0 | 122 | * with reportAllDiagnostics(), it will be reported to the previously |
aoqi@0 | 123 | * active diagnostic handler. |
aoqi@0 | 124 | */ |
aoqi@0 | 125 | public static class DeferredDiagnosticHandler extends DiagnosticHandler { |
aoqi@0 | 126 | private Queue<JCDiagnostic> deferred = new ListBuffer<>(); |
aoqi@0 | 127 | private final Filter<JCDiagnostic> filter; |
aoqi@0 | 128 | |
aoqi@0 | 129 | public DeferredDiagnosticHandler(Log log) { |
aoqi@0 | 130 | this(log, null); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) { |
aoqi@0 | 134 | this.filter = filter; |
aoqi@0 | 135 | install(log); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | public void report(JCDiagnostic diag) { |
aoqi@0 | 139 | if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) && |
aoqi@0 | 140 | (filter == null || filter.accepts(diag))) { |
aoqi@0 | 141 | deferred.add(diag); |
aoqi@0 | 142 | } else { |
aoqi@0 | 143 | prev.report(diag); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | public Queue<JCDiagnostic> getDiagnostics() { |
aoqi@0 | 148 | return deferred; |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | /** Report all deferred diagnostics. */ |
aoqi@0 | 152 | public void reportDeferredDiagnostics() { |
aoqi@0 | 153 | reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | /** Report selected deferred diagnostics. */ |
aoqi@0 | 157 | public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { |
aoqi@0 | 158 | JCDiagnostic d; |
aoqi@0 | 159 | while ((d = deferred.poll()) != null) { |
aoqi@0 | 160 | if (kinds.contains(d.getKind())) |
aoqi@0 | 161 | prev.report(d); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | deferred = null; // prevent accidental ongoing use |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | public enum WriterKind { NOTICE, WARNING, ERROR }; |
aoqi@0 | 168 | |
aoqi@0 | 169 | protected PrintWriter errWriter; |
aoqi@0 | 170 | |
aoqi@0 | 171 | protected PrintWriter warnWriter; |
aoqi@0 | 172 | |
aoqi@0 | 173 | protected PrintWriter noticeWriter; |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** The maximum number of errors/warnings that are reported. |
aoqi@0 | 176 | */ |
aoqi@0 | 177 | protected int MaxErrors; |
aoqi@0 | 178 | protected int MaxWarnings; |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** Switch: prompt user on each error. |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | public boolean promptOnError; |
aoqi@0 | 183 | |
aoqi@0 | 184 | /** Switch: emit warning messages. |
aoqi@0 | 185 | */ |
aoqi@0 | 186 | public boolean emitWarnings; |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** Switch: suppress note messages. |
aoqi@0 | 189 | */ |
aoqi@0 | 190 | public boolean suppressNotes; |
aoqi@0 | 191 | |
aoqi@0 | 192 | /** Print stack trace on errors? |
aoqi@0 | 193 | */ |
aoqi@0 | 194 | public boolean dumpOnError; |
aoqi@0 | 195 | |
aoqi@0 | 196 | /** Print multiple errors for same source locations. |
aoqi@0 | 197 | */ |
aoqi@0 | 198 | public boolean multipleErrors; |
aoqi@0 | 199 | |
aoqi@0 | 200 | /** |
aoqi@0 | 201 | * Diagnostic listener, if provided through programmatic |
aoqi@0 | 202 | * interface to javac (JSR 199). |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | protected DiagnosticListener<? super JavaFileObject> diagListener; |
aoqi@0 | 205 | |
aoqi@0 | 206 | /** |
aoqi@0 | 207 | * Formatter for diagnostics. |
aoqi@0 | 208 | */ |
aoqi@0 | 209 | private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
aoqi@0 | 210 | |
aoqi@0 | 211 | /** |
aoqi@0 | 212 | * Keys for expected diagnostics. |
aoqi@0 | 213 | */ |
aoqi@0 | 214 | public Set<String> expectDiagKeys; |
aoqi@0 | 215 | |
aoqi@0 | 216 | /** |
aoqi@0 | 217 | * Set to true if a compressed diagnostic is reported |
aoqi@0 | 218 | */ |
aoqi@0 | 219 | public boolean compressedOutput; |
aoqi@0 | 220 | |
aoqi@0 | 221 | /** |
aoqi@0 | 222 | * JavacMessages object used for localization. |
aoqi@0 | 223 | */ |
aoqi@0 | 224 | private JavacMessages messages; |
aoqi@0 | 225 | |
aoqi@0 | 226 | /** |
aoqi@0 | 227 | * Handler for initial dispatch of diagnostics. |
aoqi@0 | 228 | */ |
aoqi@0 | 229 | private DiagnosticHandler diagnosticHandler; |
aoqi@0 | 230 | |
aoqi@0 | 231 | /** Construct a log with given I/O redirections. |
aoqi@0 | 232 | */ |
aoqi@0 | 233 | protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
aoqi@0 | 234 | super(JCDiagnostic.Factory.instance(context)); |
aoqi@0 | 235 | context.put(logKey, this); |
aoqi@0 | 236 | this.errWriter = errWriter; |
aoqi@0 | 237 | this.warnWriter = warnWriter; |
aoqi@0 | 238 | this.noticeWriter = noticeWriter; |
aoqi@0 | 239 | |
aoqi@0 | 240 | @SuppressWarnings("unchecked") // FIXME |
aoqi@0 | 241 | DiagnosticListener<? super JavaFileObject> dl = |
aoqi@0 | 242 | context.get(DiagnosticListener.class); |
aoqi@0 | 243 | this.diagListener = dl; |
aoqi@0 | 244 | |
aoqi@0 | 245 | diagnosticHandler = new DefaultDiagnosticHandler(); |
aoqi@0 | 246 | |
aoqi@0 | 247 | messages = JavacMessages.instance(context); |
aoqi@0 | 248 | messages.add(Main.javacBundleName); |
aoqi@0 | 249 | |
aoqi@0 | 250 | final Options options = Options.instance(context); |
aoqi@0 | 251 | initOptions(options); |
aoqi@0 | 252 | options.addListener(new Runnable() { |
aoqi@0 | 253 | public void run() { |
aoqi@0 | 254 | initOptions(options); |
aoqi@0 | 255 | } |
aoqi@0 | 256 | }); |
aoqi@0 | 257 | } |
aoqi@0 | 258 | // where |
aoqi@0 | 259 | private void initOptions(Options options) { |
aoqi@0 | 260 | this.dumpOnError = options.isSet(DOE); |
aoqi@0 | 261 | this.promptOnError = options.isSet(PROMPT); |
aoqi@0 | 262 | this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); |
aoqi@0 | 263 | this.suppressNotes = options.isSet("suppressNotes"); |
aoqi@0 | 264 | this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); |
aoqi@0 | 265 | this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); |
aoqi@0 | 266 | |
aoqi@0 | 267 | boolean rawDiagnostics = options.isSet("rawDiagnostics"); |
aoqi@0 | 268 | this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
aoqi@0 | 269 | new BasicDiagnosticFormatter(options, messages); |
aoqi@0 | 270 | |
aoqi@0 | 271 | String ek = options.get("expectKeys"); |
aoqi@0 | 272 | if (ek != null) |
aoqi@0 | 273 | expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | private int getIntOption(Options options, Option option, int defaultValue) { |
aoqi@0 | 277 | String s = options.get(option); |
aoqi@0 | 278 | try { |
aoqi@0 | 279 | if (s != null) { |
aoqi@0 | 280 | int n = Integer.parseInt(s); |
aoqi@0 | 281 | return (n <= 0 ? Integer.MAX_VALUE : n); |
aoqi@0 | 282 | } |
aoqi@0 | 283 | } catch (NumberFormatException e) { |
aoqi@0 | 284 | // silently ignore ill-formed numbers |
aoqi@0 | 285 | } |
aoqi@0 | 286 | return defaultValue; |
aoqi@0 | 287 | } |
aoqi@0 | 288 | |
aoqi@0 | 289 | /** Default value for -Xmaxerrs. |
aoqi@0 | 290 | */ |
aoqi@0 | 291 | protected int getDefaultMaxErrors() { |
aoqi@0 | 292 | return 100; |
aoqi@0 | 293 | } |
aoqi@0 | 294 | |
aoqi@0 | 295 | /** Default value for -Xmaxwarns. |
aoqi@0 | 296 | */ |
aoqi@0 | 297 | protected int getDefaultMaxWarnings() { |
aoqi@0 | 298 | return 100; |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | /** The default writer for diagnostics |
aoqi@0 | 302 | */ |
aoqi@0 | 303 | static PrintWriter defaultWriter(Context context) { |
aoqi@0 | 304 | PrintWriter result = context.get(outKey); |
aoqi@0 | 305 | if (result == null) |
aoqi@0 | 306 | context.put(outKey, result = new PrintWriter(System.err)); |
aoqi@0 | 307 | return result; |
aoqi@0 | 308 | } |
aoqi@0 | 309 | |
aoqi@0 | 310 | /** Construct a log with default settings. |
aoqi@0 | 311 | */ |
aoqi@0 | 312 | protected Log(Context context) { |
aoqi@0 | 313 | this(context, defaultWriter(context)); |
aoqi@0 | 314 | } |
aoqi@0 | 315 | |
aoqi@0 | 316 | /** Construct a log with all output redirected. |
aoqi@0 | 317 | */ |
aoqi@0 | 318 | protected Log(Context context, PrintWriter defaultWriter) { |
aoqi@0 | 319 | this(context, defaultWriter, defaultWriter, defaultWriter); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | /** Get the Log instance for this context. */ |
aoqi@0 | 323 | public static Log instance(Context context) { |
aoqi@0 | 324 | Log instance = context.get(logKey); |
aoqi@0 | 325 | if (instance == null) |
aoqi@0 | 326 | instance = new Log(context); |
aoqi@0 | 327 | return instance; |
aoqi@0 | 328 | } |
aoqi@0 | 329 | |
aoqi@0 | 330 | /** The number of errors encountered so far. |
aoqi@0 | 331 | */ |
aoqi@0 | 332 | public int nerrors = 0; |
aoqi@0 | 333 | |
aoqi@0 | 334 | /** The number of warnings encountered so far. |
aoqi@0 | 335 | */ |
aoqi@0 | 336 | public int nwarnings = 0; |
aoqi@0 | 337 | |
aoqi@0 | 338 | /** A set of all errors generated so far. This is used to avoid printing an |
aoqi@0 | 339 | * error message more than once. For each error, a pair consisting of the |
aoqi@0 | 340 | * source file name and source code position of the error is added to the set. |
aoqi@0 | 341 | */ |
aoqi@0 | 342 | private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
aoqi@0 | 343 | |
aoqi@0 | 344 | public boolean hasDiagnosticListener() { |
aoqi@0 | 345 | return diagListener != null; |
aoqi@0 | 346 | } |
aoqi@0 | 347 | |
aoqi@0 | 348 | public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { |
aoqi@0 | 349 | name.getClass(); // null check |
aoqi@0 | 350 | getSource(name).setEndPosTable(endPosTable); |
aoqi@0 | 351 | } |
aoqi@0 | 352 | |
aoqi@0 | 353 | /** Return current sourcefile. |
aoqi@0 | 354 | */ |
aoqi@0 | 355 | public JavaFileObject currentSourceFile() { |
aoqi@0 | 356 | return source == null ? null : source.getFile(); |
aoqi@0 | 357 | } |
aoqi@0 | 358 | |
aoqi@0 | 359 | /** Get the current diagnostic formatter. |
aoqi@0 | 360 | */ |
aoqi@0 | 361 | public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
aoqi@0 | 362 | return diagFormatter; |
aoqi@0 | 363 | } |
aoqi@0 | 364 | |
aoqi@0 | 365 | /** Set the current diagnostic formatter. |
aoqi@0 | 366 | */ |
aoqi@0 | 367 | public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
aoqi@0 | 368 | this.diagFormatter = diagFormatter; |
aoqi@0 | 369 | } |
aoqi@0 | 370 | |
aoqi@0 | 371 | public PrintWriter getWriter(WriterKind kind) { |
aoqi@0 | 372 | switch (kind) { |
aoqi@0 | 373 | case NOTICE: return noticeWriter; |
aoqi@0 | 374 | case WARNING: return warnWriter; |
aoqi@0 | 375 | case ERROR: return errWriter; |
aoqi@0 | 376 | default: throw new IllegalArgumentException(); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | } |
aoqi@0 | 379 | |
aoqi@0 | 380 | public void setWriter(WriterKind kind, PrintWriter pw) { |
aoqi@0 | 381 | pw.getClass(); |
aoqi@0 | 382 | switch (kind) { |
aoqi@0 | 383 | case NOTICE: noticeWriter = pw; break; |
aoqi@0 | 384 | case WARNING: warnWriter = pw; break; |
aoqi@0 | 385 | case ERROR: errWriter = pw; break; |
aoqi@0 | 386 | default: throw new IllegalArgumentException(); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | } |
aoqi@0 | 389 | |
aoqi@0 | 390 | public void setWriters(PrintWriter pw) { |
aoqi@0 | 391 | pw.getClass(); |
aoqi@0 | 392 | noticeWriter = warnWriter = errWriter = pw; |
aoqi@0 | 393 | } |
aoqi@0 | 394 | |
aoqi@0 | 395 | /** |
aoqi@0 | 396 | * Propagate the previous log's information. |
aoqi@0 | 397 | */ |
aoqi@0 | 398 | public void initRound(Log other) { |
aoqi@0 | 399 | this.noticeWriter = other.noticeWriter; |
aoqi@0 | 400 | this.warnWriter = other.warnWriter; |
aoqi@0 | 401 | this.errWriter = other.errWriter; |
aoqi@0 | 402 | this.sourceMap = other.sourceMap; |
aoqi@0 | 403 | this.recorded = other.recorded; |
aoqi@0 | 404 | this.nerrors = other.nerrors; |
aoqi@0 | 405 | this.nwarnings = other.nwarnings; |
aoqi@0 | 406 | } |
aoqi@0 | 407 | |
aoqi@0 | 408 | /** |
aoqi@0 | 409 | * Replace the specified diagnostic handler with the |
aoqi@0 | 410 | * handler that was current at the time this handler was created. |
aoqi@0 | 411 | * The given handler must be the currently installed handler; |
aoqi@0 | 412 | * it must be specified explicitly for clarity and consistency checking. |
aoqi@0 | 413 | */ |
aoqi@0 | 414 | public void popDiagnosticHandler(DiagnosticHandler h) { |
aoqi@0 | 415 | Assert.check(diagnosticHandler == h); |
aoqi@0 | 416 | diagnosticHandler = h.prev; |
aoqi@0 | 417 | } |
aoqi@0 | 418 | |
aoqi@0 | 419 | /** Flush the logs |
aoqi@0 | 420 | */ |
aoqi@0 | 421 | public void flush() { |
aoqi@0 | 422 | errWriter.flush(); |
aoqi@0 | 423 | warnWriter.flush(); |
aoqi@0 | 424 | noticeWriter.flush(); |
aoqi@0 | 425 | } |
aoqi@0 | 426 | |
aoqi@0 | 427 | public void flush(WriterKind kind) { |
aoqi@0 | 428 | getWriter(kind).flush(); |
aoqi@0 | 429 | } |
aoqi@0 | 430 | |
aoqi@0 | 431 | /** Returns true if an error needs to be reported for a given |
aoqi@0 | 432 | * source name and pos. |
aoqi@0 | 433 | */ |
aoqi@0 | 434 | protected boolean shouldReport(JavaFileObject file, int pos) { |
aoqi@0 | 435 | if (multipleErrors || file == null) |
aoqi@0 | 436 | return true; |
aoqi@0 | 437 | |
aoqi@0 | 438 | Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
aoqi@0 | 439 | boolean shouldReport = !recorded.contains(coords); |
aoqi@0 | 440 | if (shouldReport) |
aoqi@0 | 441 | recorded.add(coords); |
aoqi@0 | 442 | return shouldReport; |
aoqi@0 | 443 | } |
aoqi@0 | 444 | |
aoqi@0 | 445 | /** Prompt user after an error. |
aoqi@0 | 446 | */ |
aoqi@0 | 447 | public void prompt() { |
aoqi@0 | 448 | if (promptOnError) { |
aoqi@0 | 449 | System.err.println(localize("resume.abort")); |
aoqi@0 | 450 | try { |
aoqi@0 | 451 | while (true) { |
aoqi@0 | 452 | switch (System.in.read()) { |
aoqi@0 | 453 | case 'a': case 'A': |
aoqi@0 | 454 | System.exit(-1); |
aoqi@0 | 455 | return; |
aoqi@0 | 456 | case 'r': case 'R': |
aoqi@0 | 457 | return; |
aoqi@0 | 458 | case 'x': case 'X': |
aoqi@0 | 459 | throw new AssertionError("user abort"); |
aoqi@0 | 460 | default: |
aoqi@0 | 461 | } |
aoqi@0 | 462 | } |
aoqi@0 | 463 | } catch (IOException e) {} |
aoqi@0 | 464 | } |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | /** Print the faulty source code line and point to the error. |
aoqi@0 | 468 | * @param pos Buffer index of the error position, must be on current line |
aoqi@0 | 469 | */ |
aoqi@0 | 470 | private void printErrLine(int pos, PrintWriter writer) { |
aoqi@0 | 471 | String line = (source == null ? null : source.getLine(pos)); |
aoqi@0 | 472 | if (line == null) |
aoqi@0 | 473 | return; |
aoqi@0 | 474 | int col = source.getColumnNumber(pos, false); |
aoqi@0 | 475 | |
aoqi@0 | 476 | printRawLines(writer, line); |
aoqi@0 | 477 | for (int i = 0; i < col - 1; i++) { |
aoqi@0 | 478 | writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
aoqi@0 | 479 | } |
aoqi@0 | 480 | writer.println("^"); |
aoqi@0 | 481 | writer.flush(); |
aoqi@0 | 482 | } |
aoqi@0 | 483 | |
aoqi@0 | 484 | public void printNewline() { |
aoqi@0 | 485 | noticeWriter.println(); |
aoqi@0 | 486 | } |
aoqi@0 | 487 | |
aoqi@0 | 488 | public void printNewline(WriterKind wk) { |
aoqi@0 | 489 | getWriter(wk).println(); |
aoqi@0 | 490 | } |
aoqi@0 | 491 | |
aoqi@0 | 492 | public void printLines(String key, Object... args) { |
aoqi@0 | 493 | printRawLines(noticeWriter, localize(key, args)); |
aoqi@0 | 494 | } |
aoqi@0 | 495 | |
aoqi@0 | 496 | public void printLines(PrefixKind pk, String key, Object... args) { |
aoqi@0 | 497 | printRawLines(noticeWriter, localize(pk, key, args)); |
aoqi@0 | 498 | } |
aoqi@0 | 499 | |
aoqi@0 | 500 | public void printLines(WriterKind wk, String key, Object... args) { |
aoqi@0 | 501 | printRawLines(getWriter(wk), localize(key, args)); |
aoqi@0 | 502 | } |
aoqi@0 | 503 | |
aoqi@0 | 504 | public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) { |
aoqi@0 | 505 | printRawLines(getWriter(wk), localize(pk, key, args)); |
aoqi@0 | 506 | } |
aoqi@0 | 507 | |
aoqi@0 | 508 | /** Print the text of a message, translating newlines appropriately |
aoqi@0 | 509 | * for the platform. |
aoqi@0 | 510 | */ |
aoqi@0 | 511 | public void printRawLines(String msg) { |
aoqi@0 | 512 | printRawLines(noticeWriter, msg); |
aoqi@0 | 513 | } |
aoqi@0 | 514 | |
aoqi@0 | 515 | /** Print the text of a message, translating newlines appropriately |
aoqi@0 | 516 | * for the platform. |
aoqi@0 | 517 | */ |
aoqi@0 | 518 | public void printRawLines(WriterKind kind, String msg) { |
aoqi@0 | 519 | printRawLines(getWriter(kind), msg); |
aoqi@0 | 520 | } |
aoqi@0 | 521 | |
aoqi@0 | 522 | /** Print the text of a message, translating newlines appropriately |
aoqi@0 | 523 | * for the platform. |
aoqi@0 | 524 | */ |
aoqi@0 | 525 | public static void printRawLines(PrintWriter writer, String msg) { |
aoqi@0 | 526 | int nl; |
aoqi@0 | 527 | while ((nl = msg.indexOf('\n')) != -1) { |
aoqi@0 | 528 | writer.println(msg.substring(0, nl)); |
aoqi@0 | 529 | msg = msg.substring(nl+1); |
aoqi@0 | 530 | } |
aoqi@0 | 531 | if (msg.length() != 0) writer.println(msg); |
aoqi@0 | 532 | } |
aoqi@0 | 533 | |
aoqi@0 | 534 | /** |
aoqi@0 | 535 | * Print the localized text of a "verbose" message to the |
aoqi@0 | 536 | * noticeWriter stream. |
aoqi@0 | 537 | */ |
aoqi@0 | 538 | public void printVerbose(String key, Object... args) { |
aoqi@0 | 539 | printRawLines(noticeWriter, localize("verbose." + key, args)); |
aoqi@0 | 540 | } |
aoqi@0 | 541 | |
aoqi@0 | 542 | protected void directError(String key, Object... args) { |
aoqi@0 | 543 | printRawLines(errWriter, localize(key, args)); |
aoqi@0 | 544 | errWriter.flush(); |
aoqi@0 | 545 | } |
aoqi@0 | 546 | |
aoqi@0 | 547 | /** Report a warning that cannot be suppressed. |
aoqi@0 | 548 | * @param pos The source position at which to report the warning. |
aoqi@0 | 549 | * @param key The key for the localized warning message. |
aoqi@0 | 550 | * @param args Fields of the warning message. |
aoqi@0 | 551 | */ |
aoqi@0 | 552 | public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
aoqi@0 | 553 | writeDiagnostic(diags.warning(source, pos, key, args)); |
aoqi@0 | 554 | nwarnings++; |
aoqi@0 | 555 | } |
aoqi@0 | 556 | |
aoqi@0 | 557 | /** |
aoqi@0 | 558 | * Primary method to report a diagnostic. |
aoqi@0 | 559 | * @param diagnostic |
aoqi@0 | 560 | */ |
aoqi@0 | 561 | public void report(JCDiagnostic diagnostic) { |
aoqi@0 | 562 | diagnosticHandler.report(diagnostic); |
aoqi@0 | 563 | } |
aoqi@0 | 564 | |
aoqi@0 | 565 | /** |
aoqi@0 | 566 | * Common diagnostic handling. |
aoqi@0 | 567 | * The diagnostic is counted, and depending on the options and how many diagnostics have been |
aoqi@0 | 568 | * reported so far, the diagnostic may be handed off to writeDiagnostic. |
aoqi@0 | 569 | */ |
aoqi@0 | 570 | private class DefaultDiagnosticHandler extends DiagnosticHandler { |
aoqi@0 | 571 | public void report(JCDiagnostic diagnostic) { |
aoqi@0 | 572 | if (expectDiagKeys != null) |
aoqi@0 | 573 | expectDiagKeys.remove(diagnostic.getCode()); |
aoqi@0 | 574 | |
aoqi@0 | 575 | switch (diagnostic.getType()) { |
aoqi@0 | 576 | case FRAGMENT: |
aoqi@0 | 577 | throw new IllegalArgumentException(); |
aoqi@0 | 578 | |
aoqi@0 | 579 | case NOTE: |
aoqi@0 | 580 | // Print out notes only when we are permitted to report warnings |
aoqi@0 | 581 | // Notes are only generated at the end of a compilation, so should be small |
aoqi@0 | 582 | // in number. |
aoqi@0 | 583 | if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
aoqi@0 | 584 | writeDiagnostic(diagnostic); |
aoqi@0 | 585 | } |
aoqi@0 | 586 | break; |
aoqi@0 | 587 | |
aoqi@0 | 588 | case WARNING: |
aoqi@0 | 589 | if (emitWarnings || diagnostic.isMandatory()) { |
aoqi@0 | 590 | if (nwarnings < MaxWarnings) { |
aoqi@0 | 591 | writeDiagnostic(diagnostic); |
aoqi@0 | 592 | nwarnings++; |
aoqi@0 | 593 | } |
aoqi@0 | 594 | } |
aoqi@0 | 595 | break; |
aoqi@0 | 596 | |
aoqi@0 | 597 | case ERROR: |
aoqi@0 | 598 | if (nerrors < MaxErrors |
aoqi@0 | 599 | && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
aoqi@0 | 600 | writeDiagnostic(diagnostic); |
aoqi@0 | 601 | nerrors++; |
aoqi@0 | 602 | } |
aoqi@0 | 603 | break; |
aoqi@0 | 604 | } |
aoqi@0 | 605 | if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) { |
aoqi@0 | 606 | compressedOutput = true; |
aoqi@0 | 607 | } |
aoqi@0 | 608 | } |
aoqi@0 | 609 | } |
aoqi@0 | 610 | |
aoqi@0 | 611 | /** |
aoqi@0 | 612 | * Write out a diagnostic. |
aoqi@0 | 613 | */ |
aoqi@0 | 614 | protected void writeDiagnostic(JCDiagnostic diag) { |
aoqi@0 | 615 | if (diagListener != null) { |
aoqi@0 | 616 | diagListener.report(diag); |
aoqi@0 | 617 | return; |
aoqi@0 | 618 | } |
aoqi@0 | 619 | |
aoqi@0 | 620 | PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
aoqi@0 | 621 | |
aoqi@0 | 622 | printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
aoqi@0 | 623 | |
aoqi@0 | 624 | if (promptOnError) { |
aoqi@0 | 625 | switch (diag.getType()) { |
aoqi@0 | 626 | case ERROR: |
aoqi@0 | 627 | case WARNING: |
aoqi@0 | 628 | prompt(); |
aoqi@0 | 629 | } |
aoqi@0 | 630 | } |
aoqi@0 | 631 | |
aoqi@0 | 632 | if (dumpOnError) |
aoqi@0 | 633 | new RuntimeException().printStackTrace(writer); |
aoqi@0 | 634 | |
aoqi@0 | 635 | writer.flush(); |
aoqi@0 | 636 | } |
aoqi@0 | 637 | |
aoqi@0 | 638 | @Deprecated |
aoqi@0 | 639 | protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
aoqi@0 | 640 | switch (dt) { |
aoqi@0 | 641 | case FRAGMENT: |
aoqi@0 | 642 | throw new IllegalArgumentException(); |
aoqi@0 | 643 | |
aoqi@0 | 644 | case NOTE: |
aoqi@0 | 645 | return noticeWriter; |
aoqi@0 | 646 | |
aoqi@0 | 647 | case WARNING: |
aoqi@0 | 648 | return warnWriter; |
aoqi@0 | 649 | |
aoqi@0 | 650 | case ERROR: |
aoqi@0 | 651 | return errWriter; |
aoqi@0 | 652 | |
aoqi@0 | 653 | default: |
aoqi@0 | 654 | throw new Error(); |
aoqi@0 | 655 | } |
aoqi@0 | 656 | } |
aoqi@0 | 657 | |
aoqi@0 | 658 | /** Find a localized string in the resource bundle. |
aoqi@0 | 659 | * Because this method is static, it ignores the locale. |
aoqi@0 | 660 | * Use localize(key, args) when possible. |
aoqi@0 | 661 | * @param key The key for the localized string. |
aoqi@0 | 662 | * @param args Fields to substitute into the string. |
aoqi@0 | 663 | */ |
aoqi@0 | 664 | public static String getLocalizedString(String key, Object ... args) { |
aoqi@0 | 665 | return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args); |
aoqi@0 | 666 | } |
aoqi@0 | 667 | |
aoqi@0 | 668 | /** Find a localized string in the resource bundle. |
aoqi@0 | 669 | * @param key The key for the localized string. |
aoqi@0 | 670 | * @param args Fields to substitute into the string. |
aoqi@0 | 671 | */ |
aoqi@0 | 672 | public String localize(String key, Object... args) { |
aoqi@0 | 673 | return localize(PrefixKind.COMPILER_MISC, key, args); |
aoqi@0 | 674 | } |
aoqi@0 | 675 | |
aoqi@0 | 676 | /** Find a localized string in the resource bundle. |
aoqi@0 | 677 | * @param key The key for the localized string. |
aoqi@0 | 678 | * @param args Fields to substitute into the string. |
aoqi@0 | 679 | */ |
aoqi@0 | 680 | public String localize(PrefixKind pk, String key, Object... args) { |
aoqi@0 | 681 | if (useRawMessages) |
aoqi@0 | 682 | return pk.key(key); |
aoqi@0 | 683 | else |
aoqi@0 | 684 | return messages.getLocalizedString(pk.key(key), args); |
aoqi@0 | 685 | } |
aoqi@0 | 686 | // where |
aoqi@0 | 687 | // backdoor hook for testing, should transition to use -XDrawDiagnostics |
aoqi@0 | 688 | private static boolean useRawMessages = false; |
aoqi@0 | 689 | |
aoqi@0 | 690 | /*************************************************************************** |
aoqi@0 | 691 | * raw error messages without internationalization; used for experimentation |
aoqi@0 | 692 | * and quick prototyping |
aoqi@0 | 693 | ***************************************************************************/ |
aoqi@0 | 694 | |
aoqi@0 | 695 | /** print an error or warning message: |
aoqi@0 | 696 | */ |
aoqi@0 | 697 | private void printRawError(int pos, String msg) { |
aoqi@0 | 698 | if (source == null || pos == Position.NOPOS) { |
aoqi@0 | 699 | printRawLines(errWriter, "error: " + msg); |
aoqi@0 | 700 | } else { |
aoqi@0 | 701 | int line = source.getLineNumber(pos); |
aoqi@0 | 702 | JavaFileObject file = source.getFile(); |
aoqi@0 | 703 | if (file != null) |
aoqi@0 | 704 | printRawLines(errWriter, |
aoqi@0 | 705 | file.getName() + ":" + |
aoqi@0 | 706 | line + ": " + msg); |
aoqi@0 | 707 | printErrLine(pos, errWriter); |
aoqi@0 | 708 | } |
aoqi@0 | 709 | errWriter.flush(); |
aoqi@0 | 710 | } |
aoqi@0 | 711 | |
aoqi@0 | 712 | /** report an error: |
aoqi@0 | 713 | */ |
aoqi@0 | 714 | public void rawError(int pos, String msg) { |
aoqi@0 | 715 | if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
aoqi@0 | 716 | printRawError(pos, msg); |
aoqi@0 | 717 | prompt(); |
aoqi@0 | 718 | nerrors++; |
aoqi@0 | 719 | } |
aoqi@0 | 720 | errWriter.flush(); |
aoqi@0 | 721 | } |
aoqi@0 | 722 | |
aoqi@0 | 723 | /** report a warning: |
aoqi@0 | 724 | */ |
aoqi@0 | 725 | public void rawWarning(int pos, String msg) { |
aoqi@0 | 726 | if (nwarnings < MaxWarnings && emitWarnings) { |
aoqi@0 | 727 | printRawError(pos, "warning: " + msg); |
aoqi@0 | 728 | } |
aoqi@0 | 729 | prompt(); |
aoqi@0 | 730 | nwarnings++; |
aoqi@0 | 731 | errWriter.flush(); |
aoqi@0 | 732 | } |
aoqi@0 | 733 | |
aoqi@0 | 734 | public static String format(String fmt, Object... args) { |
aoqi@0 | 735 | return String.format((java.util.Locale)null, fmt, args); |
aoqi@0 | 736 | } |
aoqi@0 | 737 | |
aoqi@0 | 738 | } |