langtools/src/share/classes/com/sun/tools/javac/util/Log.java
author jjg
Fri, 25 Mar 2011 07:58:53 -0700
changeset 9071 88cd61b4e5aa
parent 8630 cd9eefe597f6
child 11052 65b9fa7eaf55
permissions -rw-r--r--
6437138: JSR 199: Compiler doesn't diagnose crash in user code 6482554: uncaught exception from annotation processor not reported through JavaCompiler.CompilationTask.call Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5001
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
    29
import java.util.Arrays;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    30
import java.util.EnumSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    33
import java.util.Queue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 167
diff changeset
    37
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    38
import com.sun.tools.javac.api.DiagnosticFormatter;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    39
import com.sun.tools.javac.main.OptionName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    44
import static com.sun.tools.javac.main.OptionName.*;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    45
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
/** A class for error logs. Reports errors and warnings, and
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  keeps track of error numbers and positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    49
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    50
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 */
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
    54
public class Log extends AbstractLog {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /** The context key for the log. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static final Context.Key<Log> logKey
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        = new Context.Key<Log>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    /** The context key for the output PrintWriter. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    public static final Context.Key<PrintWriter> outKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        new Context.Key<PrintWriter>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    public final PrintWriter errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public final PrintWriter warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public final PrintWriter noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /** The maximum number of errors/warnings that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public final int MaxErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public final int MaxWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** Switch: prompt user on each error.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public boolean promptOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /** Switch: emit warning messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public boolean emitWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
3660
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
    85
    /** Switch: suppress note messages.
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
    86
     */
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
    87
    public boolean suppressNotes;
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
    88
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /** Print stack trace on errors?
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    public boolean dumpOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /** Print multiple errors for same source locations.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public boolean multipleErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Diagnostic listener, if provided through programmatic
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * interface to javac (JSR 199).
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    protected DiagnosticListener<? super JavaFileObject> diagListener;
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   102
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   104
     * Formatter for diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 867
diff changeset
   106
    private DiagnosticFormatter<JCDiagnostic> diagFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   108
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   109
     * Keys for expected diagnostics.
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   110
     */
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   111
    public Set<String> expectDiagKeys;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   112
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   113
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   114
     * JavacMessages object used for localization.
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   115
     */
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   116
    private JavacMessages messages;
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   117
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   118
    /**
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   119
     * Deferred diagnostics
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   120
     */
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   121
    public boolean deferDiagnostics;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   122
    public Queue<JCDiagnostic> deferredDiagnostics = new ListBuffer<JCDiagnostic>();
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   123
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /** Construct a log with given I/O redirections.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   128
        super(JCDiagnostic.Factory.instance(context));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        context.put(logKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        this.errWriter = errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        this.warnWriter = warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        this.noticeWriter = noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        Options options = Options.instance(context);
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   135
        this.dumpOnError = options.isSet(DOE);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   136
        this.promptOnError = options.isSet(PROMPT);
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   137
        this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   138
        this.suppressNotes = options.isSet("suppressNotes");
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   139
        this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   140
        this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   142
        boolean rawDiagnostics = options.isSet("rawDiagnostics");
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   143
        messages = JavacMessages.instance(context);
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents: 1471
diff changeset
   144
        this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   145
                                              new BasicDiagnosticFormatter(options, messages);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        @SuppressWarnings("unchecked") // FIXME
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   147
        DiagnosticListener<? super JavaFileObject> dl =
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            context.get(DiagnosticListener.class);
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   149
        this.diagListener = dl;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   150
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   151
        String ek = options.get("expectKeys");
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   152
        if (ek != null)
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   153
            expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    // where
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
   156
        private int getIntOption(Options options, OptionName optionName, int defaultValue) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            String s = options.get(optionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            try {
4699
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   159
                if (s != null) {
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   160
                    int n = Integer.parseInt(s);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   161
                    return (n <= 0 ? Integer.MAX_VALUE : n);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   162
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            } catch (NumberFormatException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                // silently ignore ill-formed numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            return defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   169
        /** Default value for -Xmaxerrs.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   170
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   171
        protected int getDefaultMaxErrors() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   172
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   173
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   174
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   175
        /** Default value for -Xmaxwarns.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   176
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   177
        protected int getDefaultMaxWarnings() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   178
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   179
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   180
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /** The default writer for diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    static final PrintWriter defaultWriter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        PrintWriter result = context.get(outKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            context.put(outKey, result = new PrintWriter(System.err));
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /** Construct a log with default settings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    protected Log(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        this(context, defaultWriter(context));
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    /** Construct a log with all output redirected.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    protected Log(Context context, PrintWriter defaultWriter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        this(context, defaultWriter, defaultWriter, defaultWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    /** Get the Log instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    public static Log instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        Log instance = context.get(logKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            instance = new Log(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** The number of errors encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    public int nerrors = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /** The number of warnings encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    public int nwarnings = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /** A set of all errors generated so far. This is used to avoid printing an
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     *  error message more than once. For each error, a pair consisting of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     *  source file name and source code position of the error is added to the set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    public boolean hasDiagnosticListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        return diagListener != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   229
        name.getClass(); // null check
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   230
        getSource(name).setEndPosTable(table);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   233
    /** Return current sourcefile.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     */
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   235
    public JavaFileObject currentSourceFile() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        return source == null ? null : source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   239
    /** Get the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   240
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   241
    public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   242
        return diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   243
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   244
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   245
    /** Set the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   246
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   247
    public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   248
        this.diagFormatter = diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   249
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   250
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    /** Flush the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        warnWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        noticeWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /** Returns true if an error needs to be reported for a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     * source name and pos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    protected boolean shouldReport(JavaFileObject file, int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        if (multipleErrors || file == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        boolean shouldReport = !recorded.contains(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        if (shouldReport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            recorded.add(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        return shouldReport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    /** Prompt user after an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    public void prompt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        if (promptOnError) {
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   277
            System.err.println(localize("resume.abort"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            char ch;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                    switch (System.in.read()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                    case 'a': case 'A':
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                        System.exit(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                    case 'r': case 'R':
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                    case 'x': case 'X':
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                        throw new AssertionError("user abort");
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                    default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            } catch (IOException e) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    /** Print the faulty source code line and point to the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     *  @param pos   Buffer index of the error position, must be on current line
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    private void printErrLine(int pos, PrintWriter writer) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   300
        String line = (source == null ? null : source.getLine(pos));
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   301
        if (line == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            return;
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   303
        int col = source.getColumnNumber(pos, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   305
        printLines(writer, line);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   306
        for (int i = 0; i < col - 1; i++) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   307
            writer.print((line.charAt(i) == '\t') ? "\t" : " ");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        writer.println("^");
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    /** Print the text of a message, translating newlines appropriately
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     *  for the platform.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    public static void printLines(PrintWriter writer, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        int nl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        while ((nl = msg.indexOf('\n')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            writer.println(msg.substring(0, nl));
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            msg = msg.substring(nl+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        if (msg.length() != 0) writer.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   325
    /** Print the text of a message to the errWriter stream,
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   326
     *  translating newlines appropriately for the platform.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   327
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   328
    public void printErrLines(String key, Object... args) {
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   329
        printLines(errWriter, localize(key, args));
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   330
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   331
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   332
    /** Print the text of a message to the noticeWriter stream,
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   333
     *  translating newlines appropriately for the platform.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   334
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   335
    public void printNoteLines(String key, Object... args) {
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   336
        printLines(noticeWriter, localize(key, args));
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   337
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   338
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   339
    /**
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   340
     * Print the localized text of a "verbose" message to the
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   341
     * noticeWriter stream.
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   342
     */
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   343
    public void printVerbose(String key, Object... args) {
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   344
        printLines(noticeWriter, localize("verbose." + key, args));
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   345
    }
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   346
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   347
    protected void directError(String key, Object... args) {
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   348
        printErrLines(key, args);
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   349
        errWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    /** Report a warning that cannot be suppressed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        writeDiagnostic(diags.warning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   362
    /** Report all deferred diagnostics, and clear the deferDiagnostics flag. */
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   363
    public void reportDeferredDiagnostics() {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   364
        reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   365
    }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   366
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   367
    /** Report selected deferred diagnostics, and clear the deferDiagnostics flag. */
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   368
    public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   369
        deferDiagnostics = false;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   370
        JCDiagnostic d;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   371
        while ((d = deferredDiagnostics.poll()) != null) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   372
            if (kinds.contains(d.getKind()))
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   373
                report(d);
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   374
        }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   375
    }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   376
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     * Common diagnostic handling.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     * The diagnostic is counted, and depending on the options and how many diagnostics have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
     * reported so far, the diagnostic may be handed off to writeDiagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    public void report(JCDiagnostic diagnostic) {
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   383
        if (deferDiagnostics) {
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   384
            deferredDiagnostics.add(diagnostic);
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   385
            return;
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   386
        }
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   387
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   388
        if (expectDiagKeys != null)
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   389
            expectDiagKeys.remove(diagnostic.getCode());
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   390
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        switch (diagnostic.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            // Print out notes only when we are permitted to report warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            // Notes are only generated at the end of a compilation, so should be small
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            // in number.
3660
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   399
            if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            if (emitWarnings || diagnostic.isMandatory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                if (nwarnings < MaxWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                    writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                    nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            if (nerrors < MaxErrors
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
                nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     * Write out a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
    protected void writeDiagnostic(JCDiagnostic diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        if (diagListener != null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   428
            diagListener.report(diag);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   429
            return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        PrintWriter writer = getWriterForDiagnosticType(diag.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   434
        printLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            switch (diag.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        if (dumpOnError)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            new RuntimeException().printStackTrace(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        switch (dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            return noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            return warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            return errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    /** Find a localized string in the resource bundle.
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   471
     *  Because this method is static, it ignores the locale.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   472
     *  Use localize(key, args) when possible.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
     *  @param key    The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     *  @param args   Fields to substitute into the string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    public static String getLocalizedString(String key, Object ... args) {
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   477
        return JavacMessages.getDefaultLocalizedString("compiler.misc." + key, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   480
    /** Find a localized string in the resource bundle.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   481
     *  @param key    The key for the localized string.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   482
     *  @param args   Fields to substitute into the string.
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   483
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   484
    public String localize(String key, Object... args) {
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   485
        return messages.getLocalizedString("compiler.misc." + key, args);
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   486
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   487
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
 * raw error messages without internationalization; used for experimentation
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
 * and quick prototyping
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   493
    /** print an error or warning message:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   494
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
    private void printRawError(int pos, String msg) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   496
        if (source == null || pos == Position.NOPOS) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            printLines(errWriter, "error: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        } else {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   499
            int line = source.getLineNumber(pos);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   500
            JavaFileObject file = source.getFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            if (file != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                printLines(errWriter,
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3660
diff changeset
   503
                           file.getName() + ":" +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                           line + ": " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            printErrLine(pos, errWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   510
    /** report an error:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   511
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    public void rawError(int pos, String msg) {
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   513
        if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            printRawError(pos, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
            nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   521
    /** report a warning:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   522
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
    public void rawWarning(int pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        if (nwarnings < MaxWarnings && emitWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            printRawError(pos, "warning: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    public static String format(String fmt, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        return String.format((java.util.Locale)null, fmt, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
}