src/share/classes/com/sun/tools/javac/util/Log.java

Fri, 21 Aug 2009 11:25:45 -0700

author
jjg
date
Fri, 21 Aug 2009 11:25:45 -0700
changeset 376
61c1f735df67
parent 229
03bcd66bd8e7
child 415
49359d0e6a9c
permissions
-rw-r--r--

6873849: suppress notes generated by javac
Reviewed-by: darcy

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

mercurial