langtools/src/share/classes/com/sun/tools/javac/util/Log.java
author jjg
Thu, 06 Mar 2008 10:25:04 -0800
changeset 167 f4b81c733bea
parent 10 06bc494ca11e
child 731 1dd22bdb9ca5
permissions -rw-r--r--
6668802: javac handles diagnostics for last line badly, if line not terminated by newline Summary: use CharBuffer.limit(), not the length of the backing array Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.code.Source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import static com.sun.tools.javac.util.LayoutCharacters.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
/** A class for error logs. Reports errors and warnings, and
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  keeps track of error numbers and positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class Log {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /** The context key for the log. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static final Context.Key<Log> logKey
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        = new Context.Key<Log>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /** The context key for the output PrintWriter. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public static final Context.Key<PrintWriter> outKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        new Context.Key<PrintWriter>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    //@Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    public final PrintWriter errWriter;
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 warnWriter;
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 noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** The maximum number of errors/warnings that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public final int MaxErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    public final int MaxWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** Whether or not to display the line of source containing a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private final boolean showSourceLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /** Switch: prompt user on each error.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public boolean promptOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /** Switch: emit warning messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    public boolean emitWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** Enforce mandatory warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    private boolean enforceMandatoryWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    /** Print stack trace on errors?
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    public boolean dumpOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    /** Print multiple errors for same source locations.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    public boolean multipleErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * Diagnostic listener, if provided through programmatic
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * interface to javac (JSR 199).
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    protected DiagnosticListener<? super JavaFileObject> diagListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * Formatter for diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private DiagnosticFormatter diagFormatter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * Factory for diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    private JCDiagnostic.Factory diags;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    /** Construct a log with given I/O redirections.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        context.put(logKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        this.errWriter = errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        this.warnWriter = warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        this.noticeWriter = noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        this.diags = JCDiagnostic.Factory.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        Options options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        this.dumpOnError = options.get("-doe") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        this.promptOnError = options.get("-prompt") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        this.emitWarnings = options.get("-Xlint:none") == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        this.showSourceLine = options.get("rawDiagnostics") == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        this.diagFormatter = DiagnosticFormatter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        @SuppressWarnings("unchecked") // FIXME
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        DiagnosticListener<? super JavaFileObject> diagListener =
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            context.get(DiagnosticListener.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        this.diagListener = diagListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        this.enforceMandatoryWarnings = source.enforceMandatoryWarnings();
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        private int getIntOption(Options options, String optionName, int defaultValue) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            String s = options.get(optionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                if (s != null) return Integer.parseInt(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            } catch (NumberFormatException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                // silently ignore ill-formed numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            return defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /** The default writer for diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    static final PrintWriter defaultWriter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        PrintWriter result = context.get(outKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        if (result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            context.put(outKey, result = new PrintWriter(System.err));
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /** Construct a log with default settings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    protected Log(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        this(context, defaultWriter(context));
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    /** Construct a log with all output redirected.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    protected Log(Context context, PrintWriter defaultWriter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        this(context, defaultWriter, defaultWriter, defaultWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /** Get the Log instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    public static Log instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        Log instance = context.get(logKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            instance = new Log(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /** The file that's currently translated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    protected JCDiagnostic.DiagnosticSource source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    /** The number of errors encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    public int nerrors = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /** The number of warnings encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    public int nwarnings = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** A set of all errors generated so far. This is used to avoid printing an
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     *  error message more than once. For each error, a pair consisting of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     *  source file name and source code position of the error is added to the set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    private Map<JavaFileObject, Map<JCTree, Integer>> endPosTables;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    /** The buffer containing the file that's currently translated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    private char[] buf = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   206
    /** The length of useful data in buf
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   207
     */
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   208
    private int bufLen = 0;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   209
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** The position in the buffer at which last error was reported
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    private int bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /** number of the current source line; first line is 1
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    private int line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /**  buffer index of the first character of the current source line
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    private int lineStart;
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    public boolean hasDiagnosticListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        return diagListener != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        if (endPosTables == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            endPosTables = new HashMap<JavaFileObject, Map<JCTree, Integer>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        endPosTables.put(name, table);
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    /** Re-assign source, returning previous setting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    public JavaFileObject useSource(final JavaFileObject name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        JavaFileObject prev = currentSource();
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        if (name != prev) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            source = new JCDiagnostic.DiagnosticSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                    public JavaFileObject getFile() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                        return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                    public CharSequence getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                        return JavacFileManager.getJavacBaseFileName(getFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                    public int getLineNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                        return Log.this.getLineNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
                    public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                        return Log.this.getColumnNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                    public Map<JCTree, Integer> getEndPosTable() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                        return (endPosTables == null ? null : endPosTables.get(name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            buf = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        return prev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /** Re-assign source buffer for existing source name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    protected void setBuf(char[] newBuf) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        buf = newBuf;
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   263
        bufLen = buf.length;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        bp = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        lineStart = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        line = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    protected char[] getBuf() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        return buf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    /** Return current source name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    public JavaFileObject currentSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        return source == null ? null : source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    /** Flush the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        warnWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        noticeWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    /** Returns true if an error needs to be reported for a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * source name and pos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    protected boolean shouldReport(JavaFileObject file, int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        if (multipleErrors || file == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        boolean shouldReport = !recorded.contains(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        if (shouldReport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            recorded.add(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        return shouldReport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    /** Prompt user after an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    public void prompt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            System.err.println(getLocalizedString("resume.abort"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            char ch;
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                    switch (System.in.read()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                    case 'a': case 'A':
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                        System.exit(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                    case 'r': case 'R':
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    case 'x': case 'X':
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                        throw new AssertionError("user abort");
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                    default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            } catch (IOException e) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    /** Print the faulty source code line and point to the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     *  @param pos   Buffer index of the error position, must be on current line
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    private void printErrLine(int pos, PrintWriter writer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        if (!findLine(pos))
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        int lineEnd = lineStart;
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   332
        while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            lineEnd++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        if (lineEnd - lineStart == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        printLines(writer, new String(buf, lineStart, lineEnd - lineStart));
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        for (bp = lineStart; bp < pos; bp++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            writer.print((buf[bp] == '\t') ? "\t" : " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        writer.println("^");
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   344
    protected void initBuf(JavaFileObject fileObject) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        CharSequence cs = fileObject.getCharContent(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        if (cs instanceof CharBuffer) {
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   347
            CharBuffer cb = (CharBuffer) cs;
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   348
            buf = JavacFileManager.toArray(cb);
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   349
            bufLen = cb.limit();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        } else {
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   351
            buf = cs.toString().toCharArray();
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   352
            bufLen = buf.length;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    /** Find the line in the buffer that contains the current position
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
     * @param pos      Character offset into the buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    private boolean findLine(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        if (pos == Position.NOPOS || currentSource() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            if (buf == null) {
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   364
                initBuf(currentSource());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                lineStart = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                line = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            } else if (lineStart > pos) { // messages don't come in order
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                lineStart = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                line = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            bp = lineStart;
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   372
            while (bp < bufLen && bp < pos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                switch (buf[bp++]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                case CR:
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   375
                    if (bp < bufLen && buf[bp] == LF) bp++;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                    line++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                    lineStart = bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                case LF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    line++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    lineStart = bp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            }
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   385
            return bp <= bufLen;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            //e.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            // FIXME: include e.getLocalizedMessage() in error message
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            printLines(errWriter, getLocalizedString("source.unavailable"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            buf = new char[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    /** Print the text of a message, translating newlines appropriately
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
     *  for the platform.
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    public static void printLines(PrintWriter writer, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        int nl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        while ((nl = msg.indexOf('\n')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            writer.println(msg.substring(0, nl));
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            msg = msg.substring(nl+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        if (msg.length() != 0) writer.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    /** Report an error, unless another error was already reported at same
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     *  source position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
    public void error(String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        report(diags.error(source, null, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    /** Report an error, unless another error was already reported at same
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     *  source position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     *  @param pos    The source position at which to report the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    public void error(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        report(diags.error(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    /** Report an error, unless another error was already reported at same
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     *  source position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     *  @param pos    The source position at which to report the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    public void error(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        report(diags.error(source, wrap(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    /** Report a warning, unless suppressed by the  -nowarn option or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
     *  maximum number of warnings has been reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    public void warning(String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        report(diags.warning(source, null, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    /** Report a warning, unless suppressed by the  -nowarn option or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
     *  maximum number of warnings has been reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    public void warning(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        report(diags.warning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    /** Report a warning, unless suppressed by the  -nowarn option or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
     *  maximum number of warnings has been reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    public void warning(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        report(diags.warning(source, wrap(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    /** Report a warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        if (enforceMandatoryWarnings)
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
            report(diags.mandatoryWarning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            report(diags.warning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    /** Report a warning that cannot be suppressed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
    public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        writeDiagnostic(diags.warning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
     *  @param key    The key for the localized notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
     *  @param args   Fields of the notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
    public void note(String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
        report(diags.note(source, null, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     *  @param key    The key for the localized notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     *  @param args   Fields of the notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    public void note(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        report(diags.note(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
     *  @param key    The key for the localized notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
     *  @param args   Fields of the notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    public void note(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        report(diags.note(source, wrap(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
     *  @param key    The key for the localized notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
     *  @param args   Fields of the notification message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        JCDiagnostic.DiagnosticSource wrapper = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        if (file != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            wrapper = new JCDiagnostic.DiagnosticSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                    public JavaFileObject getFile() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
                        return file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
                    public CharSequence getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
                        return JavacFileManager.getJavacBaseFileName(getFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
                    public int getLineNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
                        return Log.this.getLineNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                    public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                        return Log.this.getColumnNumber(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                    public Map<JCTree, Integer> getEndPosTable() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                        return (endPosTables == null ? null : endPosTables.get(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        if (enforceMandatoryWarnings)
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            report(diags.mandatoryNote(wrapper, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            report(diags.note(wrapper, null, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
    private DiagnosticPosition wrap(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     * Common diagnostic handling.
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
     * The diagnostic is counted, and depending on the options and how many diagnostics have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
     * reported so far, the diagnostic may be handed off to writeDiagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    public void report(JCDiagnostic diagnostic) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        switch (diagnostic.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
            // Print out notes only when we are permitted to report warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            // Notes are only generated at the end of a compilation, so should be small
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
            // in number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            if (emitWarnings || diagnostic.isMandatory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
                writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
            if (emitWarnings || diagnostic.isMandatory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                if (nwarnings < MaxWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
                    writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                    nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            if (nerrors < MaxErrors
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
                && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                writeDiagnostic(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
                nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
     * Write out a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
    protected void writeDiagnostic(JCDiagnostic diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        if (diagListener != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
                diagListener.report(diag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
                throw new ClientCodeException(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        PrintWriter writer = getWriterForDiagnosticType(diag.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        printLines(writer, diagFormatter.format(diag));
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        if (showSourceLine) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            int pos = diag.getIntPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
            if (pos != Position.NOPOS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                JavaFileObject prev = useSource(diag.getSource());
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
                printErrLine(pos, writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            switch (diag.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        if (dumpOnError)
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            new RuntimeException().printStackTrace(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
    protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        switch (dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
            return noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            return warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
            return errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
    /** Find a localized string in the resource bundle.
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
     *  @param key    The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
     *  @param args   Fields to substitute into the string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    public static String getLocalizedString(String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        return Messages.getDefaultLocalizedString("compiler.misc." + key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
 * raw error messages without internationalization; used for experimentation
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
 * and quick prototyping
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
/** print an error or warning message:
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
    private void printRawError(int pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
        if (!findLine(pos)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
            printLines(errWriter, "error: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            JavaFileObject file = currentSource();
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
            if (file != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                printLines(errWriter,
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
                           JavacFileManager.getJavacFileName(file) + ":" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
                           line + ": " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
            printErrLine(pos, errWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
/** report an error:
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
    public void rawError(int pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            printRawError(pos, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
            prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
/** report a warning:
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
    public void rawWarning(int pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
        if (nwarnings < MaxWarnings && emitWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
            printRawError(pos, "warning: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
    /** Return the one-based line number associated with a given pos
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
     * for the current source file.  Zero is returned if no line exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
     * for the given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
    protected int getLineNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        if (findLine(pos))
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            return line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
        return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
    /** Return the one-based column number associated with a given pos
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
     * for the current source file.  Zero is returned if no column exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
     * for the given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
    protected int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
        if (findLine(pos)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            int column = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            for (bp = lineStart; bp < pos; bp++) {
167
f4b81c733bea 6668802: javac handles diagnostics for last line badly, if line not terminated by newline
jjg
parents: 10
diff changeset
   715
                if (bp >= bufLen)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                    return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
                if (buf[bp] == '\t')
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                    column = (column / TabInc * TabInc) + TabInc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
                    column++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
            return column + 1; // positions are one-based
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
    public static String format(String fmt, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        return String.format((java.util.Locale)null, fmt, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
}