langtools/src/share/classes/com/sun/tools/javac/util/Log.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 20249 93f8eae31092
child 23810 b92eb80925f0
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
     2
 * Copyright (c) 1999, 2013, 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;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    32
import java.util.Queue;
10
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;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 167
diff changeset
    36
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    37
import com.sun.tools.javac.api.DiagnosticFormatter;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    38
import com.sun.tools.javac.main.Main;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    39
import com.sun.tools.javac.main.Option;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11316
diff changeset
    40
import com.sun.tools.javac.tree.EndPosTable;
10
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
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    44
import static com.sun.tools.javac.main.Option.*;
6721
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. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    56
    public static final Context.Key<Log> logKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /** The context key for the output PrintWriter. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    59
    public static final Context.Key<PrintWriter> outKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    61
    /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    62
    public enum PrefixKind {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    63
        JAVAC("javac."),
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    64
        COMPILER_MISC("compiler.misc.");
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    65
        PrefixKind(String v) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    66
            value = v;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    67
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    68
        public String key(String k) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    69
            return value + k;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    70
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    71
        final String value;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    72
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    73
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    74
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    75
     * DiagnosticHandler's provide the initial handling for diagnostics.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    76
     * When a diagnostic handler is created and has been initialized, it
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    77
     * should install itself as the current diagnostic handler. When a
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    78
     * client has finished using a handler, the client should call
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    79
     * {@code log.removeDiagnosticHandler();}
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    80
     *
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    81
     * Note that javax.tools.DiagnosticListener (if set) is called later in the
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    82
     * diagnostic pipeline.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    83
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    84
    public static abstract class DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    85
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    86
         * The previously installed diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    87
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    88
        protected DiagnosticHandler prev;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    89
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    90
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    91
         * Install this diagnostic handler as the current one,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    92
         * recording the previous one.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    93
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    94
        protected void install(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    95
            prev = log.diagnosticHandler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    96
            log.diagnosticHandler = this;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    97
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    98
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    99
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   100
         * Handle a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   101
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   102
        public abstract void report(JCDiagnostic diag);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   103
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   104
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   105
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   106
     * A DiagnosticHandler that discards all diagnostics.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   107
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   108
    public static class DiscardDiagnosticHandler extends DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   109
        public DiscardDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   110
            install(log);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   111
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   112
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   113
        public void report(JCDiagnostic diag) { }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   114
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   115
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   116
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   117
     * A DiagnosticHandler that can defer some or all diagnostics,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   118
     * by buffering them for later examination and/or reporting.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   119
     * If a diagnostic is not deferred, or is subsequently reported
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   120
     * with reportAllDiagnostics(), it will be reported to the previously
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   121
     * active diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   122
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   123
    public static class DeferredDiagnosticHandler extends DiagnosticHandler {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 17582
diff changeset
   124
        private Queue<JCDiagnostic> deferred = new ListBuffer<>();
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   125
        private final Filter<JCDiagnostic> filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   126
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   127
        public DeferredDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   128
            this(log, null);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   129
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   130
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   131
        public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   132
            this.filter = filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   133
            install(log);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   134
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   135
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   136
        public void report(JCDiagnostic diag) {
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   137
            if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) &&
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   138
                (filter == null || filter.accepts(diag))) {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   139
                deferred.add(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   140
            } else {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   141
                prev.report(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   142
            }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   143
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   144
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   145
        public Queue<JCDiagnostic> getDiagnostics() {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   146
            return deferred;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   147
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   148
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   149
        /** Report all deferred diagnostics. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   150
        public void reportDeferredDiagnostics() {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   151
            reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   152
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   153
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   154
        /** Report selected deferred diagnostics. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   155
        public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   156
            JCDiagnostic d;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   157
            while ((d = deferred.poll()) != null) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   158
                if (kinds.contains(d.getKind()))
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   159
                    prev.report(d);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   160
            }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   161
            deferred = null; // prevent accidental ongoing use
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   162
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   163
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   164
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   165
    public enum WriterKind { NOTICE, WARNING, ERROR }
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   166
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   167
    protected PrintWriter errWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   169
    protected PrintWriter warnWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   171
    protected PrintWriter noticeWriter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /** The maximum number of errors/warnings that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     */
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   175
    protected int MaxErrors;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   176
    protected int MaxWarnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /** Switch: prompt user on each error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    public boolean promptOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /** Switch: emit warning messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    public boolean emitWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
3660
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   186
    /** Switch: suppress note messages.
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   187
     */
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   188
    public boolean suppressNotes;
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   189
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    /** Print stack trace on errors?
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    public boolean dumpOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** Print multiple errors for same source locations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    public boolean multipleErrors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * Diagnostic listener, if provided through programmatic
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * interface to javac (JSR 199).
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    protected DiagnosticListener<? super JavaFileObject> diagListener;
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   203
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   205
     * Formatter for diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     */
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 867
diff changeset
   207
    private DiagnosticFormatter<JCDiagnostic> diagFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   209
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   210
     * Keys for expected diagnostics.
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   211
     */
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   212
    public Set<String> expectDiagKeys;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   213
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   214
    /**
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   215
     * Set to true if a compressed diagnostic is reported
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   216
     */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   217
    public boolean compressedOutput;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   218
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   219
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   220
     * JavacMessages object used for localization.
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   221
     */
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   222
    private JavacMessages messages;
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   223
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   224
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   225
     * Handler for initial dispatch of diagnostics.
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   226
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   227
    private DiagnosticHandler diagnosticHandler;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   228
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    /** Construct a log with given I/O redirections.
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    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
   232
        super(JCDiagnostic.Factory.instance(context));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        context.put(logKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        this.errWriter = errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        this.warnWriter = warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        this.noticeWriter = noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        @SuppressWarnings("unchecked") // FIXME
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   239
        DiagnosticListener<? super JavaFileObject> dl =
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            context.get(DiagnosticListener.class);
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   241
        this.diagListener = dl;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   242
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   243
        diagnosticHandler = new DefaultDiagnosticHandler();
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   244
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   245
        messages = JavacMessages.instance(context);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   246
        messages.add(Main.javacBundleName);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   247
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   248
        final Options options = Options.instance(context);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   249
        initOptions(options);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   250
        options.addListener(new Runnable() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   251
            public void run() {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   252
                initOptions(options);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   253
            }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   254
        });
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    // where
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   257
        private void initOptions(Options options) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   258
            this.dumpOnError = options.isSet(DOE);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   259
            this.promptOnError = options.isSet(PROMPT);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   260
            this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   261
            this.suppressNotes = options.isSet("suppressNotes");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   262
            this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   263
            this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   264
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   265
            boolean rawDiagnostics = options.isSet("rawDiagnostics");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   266
            this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   267
                                                  new BasicDiagnosticFormatter(options, messages);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   268
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   269
            String ek = options.get("expectKeys");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   270
            if (ek != null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   271
                expectDiagKeys = new HashSet<>(Arrays.asList(ek.split(", *")));
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   272
        }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   273
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   274
        private int getIntOption(Options options, Option option, int defaultValue) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   275
            String s = options.get(option);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            try {
4699
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   277
                if (s != null) {
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   278
                    int n = Integer.parseInt(s);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   279
                    return (n <= 0 ? Integer.MAX_VALUE : n);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   280
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            } catch (NumberFormatException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                // silently ignore ill-formed numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            return defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   287
        /** Default value for -Xmaxerrs.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   288
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   289
        protected int getDefaultMaxErrors() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   290
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   291
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   292
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   293
        /** Default value for -Xmaxwarns.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   294
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   295
        protected int getDefaultMaxWarnings() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   296
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   297
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   298
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    /** The default writer for diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     */
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   301
    static PrintWriter defaultWriter(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        PrintWriter result = context.get(outKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        if (result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            context.put(outKey, result = new PrintWriter(System.err));
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    /** Construct a log with default settings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    protected Log(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        this(context, defaultWriter(context));
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
    /** Construct a log with all output redirected.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    protected Log(Context context, PrintWriter defaultWriter) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        this(context, defaultWriter, defaultWriter, defaultWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
    /** Get the Log instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    public static Log instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        Log instance = context.get(logKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            instance = new Log(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    /** The number of errors encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    public int nerrors = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    /** The number of warnings encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    public int nwarnings = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    /** A set of all errors generated so far. This is used to avoid printing an
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     *  error message more than once. For each error, a pair consisting of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     *  source file name and source code position of the error is added to the set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   340
    private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    public boolean hasDiagnosticListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        return diagListener != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   346
    public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   347
        name.getClass(); // null check
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   348
        getSource(name).setEndPosTable(endPosTable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   351
    /** Return current sourcefile.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     */
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   353
    public JavaFileObject currentSourceFile() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        return source == null ? null : source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   357
    /** Get the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   358
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   359
    public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   360
        return diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   361
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   362
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   363
    /** Set the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   364
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   365
    public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   366
        this.diagFormatter = diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   367
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   368
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   369
    public PrintWriter getWriter(WriterKind kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   370
        switch (kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   371
            case NOTICE:    return noticeWriter;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   372
            case WARNING:   return warnWriter;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   373
            case ERROR:     return errWriter;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   374
            default:        throw new IllegalArgumentException();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   375
        }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   376
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   377
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   378
    public void setWriter(WriterKind kind, PrintWriter pw) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   379
        pw.getClass();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   380
        switch (kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   381
            case NOTICE:    noticeWriter = pw;  break;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   382
            case WARNING:   warnWriter = pw;    break;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   383
            case ERROR:     errWriter = pw;     break;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   384
            default:        throw new IllegalArgumentException();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   385
        }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   386
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   387
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   388
    public void setWriters(PrintWriter pw) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   389
        pw.getClass();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   390
        noticeWriter = warnWriter = errWriter = pw;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   391
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   392
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   393
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   394
     * Propagate the previous log's information.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   395
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   396
    public void initRound(Log other) {
11316
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 11314
diff changeset
   397
        this.noticeWriter = other.noticeWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 11314
diff changeset
   398
        this.warnWriter = other.warnWriter;
4dcad625e72e 7111022: javac no long prints last round of processing
jjg
parents: 11314
diff changeset
   399
        this.errWriter = other.errWriter;
13841
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents: 13077
diff changeset
   400
        this.sourceMap = other.sourceMap;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   401
        this.recorded = other.recorded;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   402
        this.nerrors = other.nerrors;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   403
        this.nwarnings = other.nwarnings;
13841
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents: 13077
diff changeset
   404
    }
a304aa57c550 7196760: tree end positions incorrect after anno processing
jjg
parents: 13077
diff changeset
   405
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   406
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   407
     * Replace the specified diagnostic handler with the
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   408
     * handler that was current at the time this handler was created.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   409
     * The given handler must be the currently installed handler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   410
     * it must be specified explicitly for clarity and consistency checking.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   411
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   412
    public void popDiagnosticHandler(DiagnosticHandler h) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   413
        Assert.check(diagnosticHandler == h);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   414
        diagnosticHandler = h.prev;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   415
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   416
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    /** Flush the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        warnWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        noticeWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   425
    public void flush(WriterKind kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   426
        getWriter(kind).flush();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   427
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   428
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    /** Returns true if an error needs to be reported for a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
     * source name and pos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    protected boolean shouldReport(JavaFileObject file, int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        if (multipleErrors || file == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   436
        Pair<JavaFileObject,Integer> coords = new Pair<>(file, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        boolean shouldReport = !recorded.contains(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        if (shouldReport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
            recorded.add(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        return shouldReport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    /** Prompt user after an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    public void prompt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        if (promptOnError) {
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   447
            System.err.println(localize("resume.abort"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                    switch (System.in.read()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                    case 'a': case 'A':
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
                        System.exit(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                    case 'r': case 'R':
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                    case 'x': case 'X':
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                        throw new AssertionError("user abort");
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                    default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            } catch (IOException e) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    /** Print the faulty source code line and point to the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
     *  @param pos   Buffer index of the error position, must be on current line
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    private void printErrLine(int pos, PrintWriter writer) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   469
        String line = (source == null ? null : source.getLine(pos));
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   470
        if (line == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
            return;
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   472
        int col = source.getColumnNumber(pos, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   474
        printRawLines(writer, line);
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   475
        for (int i = 0; i < col - 1; i++) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   476
            writer.print((line.charAt(i) == '\t') ? "\t" : " ");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        writer.println("^");
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   482
    public void printNewline() {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   483
        noticeWriter.println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   484
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   485
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   486
    public void printNewline(WriterKind wk) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   487
        getWriter(wk).println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   488
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   489
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   490
    public void printLines(String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   491
        printRawLines(noticeWriter, localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   492
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   493
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   494
    public void printLines(PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   495
        printRawLines(noticeWriter, localize(pk, key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   496
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   497
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   498
    public void printLines(WriterKind wk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   499
        printRawLines(getWriter(wk), localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   500
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   501
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   502
    public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   503
        printRawLines(getWriter(wk), localize(pk, key, args));
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   504
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   505
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   506
    /** Print the text of a message, translating newlines appropriately
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   507
     *  for the platform.
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   508
     */
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   509
    public void printRawLines(String msg) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   510
        printRawLines(noticeWriter, msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   511
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   512
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   513
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   514
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   515
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   516
    public void printRawLines(WriterKind kind, String msg) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   517
        printRawLines(getWriter(kind), msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   518
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   519
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   520
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   521
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   522
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   523
    public static void printRawLines(PrintWriter writer, String msg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        int nl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        while ((nl = msg.indexOf('\n')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            writer.println(msg.substring(0, nl));
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            msg = msg.substring(nl+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        if (msg.length() != 0) writer.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   532
    /**
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   533
     * Print the localized text of a "verbose" message to the
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   534
     * noticeWriter stream.
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   535
     */
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   536
    public void printVerbose(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   537
        printRawLines(noticeWriter, localize("verbose." + key, args));
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   538
    }
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   539
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   540
    protected void directError(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   541
        printRawLines(errWriter, localize(key, args));
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   542
        errWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    /** Report a warning that cannot be suppressed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        writeDiagnostic(diags.warning(source, pos, key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   555
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   556
     * Primary method to report a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   557
     * @param diagnostic
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   558
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   559
    public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   560
        diagnosticHandler.report(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   561
     }
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   562
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
     * Common diagnostic handling.
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     * The diagnostic is counted, and depending on the options and how many diagnostics have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
     * reported so far, the diagnostic may be handed off to writeDiagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   568
    private class DefaultDiagnosticHandler extends DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   569
        public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   570
            if (expectDiagKeys != null)
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   571
                expectDiagKeys.remove(diagnostic.getCode());
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   572
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   573
            switch (diagnostic.getType()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   574
            case FRAGMENT:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   575
                throw new IllegalArgumentException();
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   576
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   577
            case NOTE:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   578
                // Print out notes only when we are permitted to report warnings
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   579
                // Notes are only generated at the end of a compilation, so should be small
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   580
                // in number.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   581
                if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   582
                    writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   583
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   584
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   586
            case WARNING:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   587
                if (emitWarnings || diagnostic.isMandatory()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   588
                    if (nwarnings < MaxWarnings) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   589
                        writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   590
                        nwarnings++;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   591
                    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   592
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   593
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   595
            case ERROR:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   596
                if (nerrors < MaxErrors
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   597
                    && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                    writeDiagnostic(diagnostic);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   599
                    nerrors++;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
                }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   601
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   603
            if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   604
                compressedOutput = true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   605
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
     * Write out a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    protected void writeDiagnostic(JCDiagnostic diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
        if (diagListener != null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   614
            diagListener.report(diag);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   615
            return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        PrintWriter writer = getWriterForDiagnosticType(diag.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   620
        printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            switch (diag.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        if (dumpOnError)
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            new RuntimeException().printStackTrace(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        switch (dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
            return noticeWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            return warnWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            return errWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
    /** 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
   657
     *  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
   658
     *  Use localize(key, args) when possible.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
     *  @param key    The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
     *  @param args   Fields to substitute into the string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    public static String getLocalizedString(String key, Object ... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   663
        return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   666
    /** 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
   667
     *  @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
   668
     *  @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
   669
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   670
    public String localize(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   671
        return localize(PrefixKind.COMPILER_MISC, key, args);
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   672
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   673
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   674
    /** Find a localized string in the resource bundle.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   675
     *  @param key    The key for the localized string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   676
     *  @param args   Fields to substitute into the string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   677
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   678
    public String localize(PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   679
        if (useRawMessages)
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   680
            return pk.key(key);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   681
        else
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   682
            return messages.getLocalizedString(pk.key(key), args);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   683
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   684
    // where
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   685
        // backdoor hook for testing, should transition to use -XDrawDiagnostics
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   686
        private static boolean useRawMessages = false;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   687
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
 * raw error messages without internationalization; used for experimentation
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
 * and quick prototyping
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   693
    /** print an error or warning message:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   694
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
    private void printRawError(int pos, String msg) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   696
        if (source == null || pos == Position.NOPOS) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   697
            printRawLines(errWriter, "error: " + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
        } else {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   699
            int line = source.getLineNumber(pos);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   700
            JavaFileObject file = source.getFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            if (file != null)
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   702
                printRawLines(errWriter,
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3660
diff changeset
   703
                           file.getName() + ":" +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                           line + ": " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            printErrLine(pos, errWriter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   710
    /** report an error:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   711
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
    public void rawError(int pos, String msg) {
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   713
        if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            printRawError(pos, msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   721
    /** report a warning:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   722
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
    public void rawWarning(int pos, String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        if (nwarnings < MaxWarnings && emitWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
            printRawError(pos, "warning: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
    public static String format(String fmt, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
        return String.format((java.util.Locale)null, fmt, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
}