src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java
author jlahoda
Fri, 15 Feb 2019 12:09:53 +0100
changeset 53773 454d54d8af1c
parent 49822 53aae0c219e6
child 54332 9a8fe0bc38c3
permissions -rw-r--r--
8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round Summary: When an annotation processor reports and error, defer reporting recoverable errors from the erroneous round to the last round, to avoid reporting errors that were resolved in the erroneous round. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
49822
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
     2
 * Copyright (c) 1999, 2018, 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;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    30
import java.util.EnumMap;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    31
import java.util.EnumSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.HashSet;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    33
import java.util.Map;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
    34
import java.util.Queue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.Set;
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 49822
diff changeset
    36
import java.util.function.Predicate;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    37
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.DiagnosticListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 167
diff changeset
    40
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    41
import com.sun.tools.javac.api.DiagnosticFormatter;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    42
import com.sun.tools.javac.main.Main;
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    43
import com.sun.tools.javac.main.Option;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 11316
diff changeset
    44
import com.sun.tools.javac.tree.EndPosTable;
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
    45
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
49822
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
    46
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
    50
import static com.sun.tools.javac.main.Option.*;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6582
diff changeset
    51
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
/** A class for error logs. Reports errors and warnings, and
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *  keeps track of error numbers and positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    55
 *  <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
    56
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 */
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
    60
public class Log extends AbstractLog {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /** The context key for the log. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    62
    public static final Context.Key<Log> logKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    64
    /** The context key for the standard output PrintWriter. */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
    65
    public static final Context.Key<PrintWriter> outKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    67
    /** The context key for the diagnostic PrintWriter. */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    68
    public static final Context.Key<PrintWriter> errKey = new Context.Key<>();
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
    69
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    70
    /* 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
    71
    public enum PrefixKind {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    72
        JAVAC("javac."),
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    73
        COMPILER_MISC("compiler.misc.");
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    74
        PrefixKind(String v) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    75
            value = v;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    76
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    77
        public String key(String k) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    78
            return value + k;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    79
        }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    80
        final String value;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    81
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
    82
14538
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
     * DiagnosticHandler's provide the initial handling for diagnostics.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    85
     * 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
    86
     * should install itself as the current diagnostic handler. When a
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    87
     * client has finished using a handler, the client should call
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    88
     * {@code log.removeDiagnosticHandler();}
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
     * 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
    91
     * diagnostic pipeline.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    92
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    93
    public static abstract class DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    94
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    95
         * The previously installed diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    96
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
    97
        protected DiagnosticHandler prev;
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
         * Install this diagnostic handler as the current one,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   101
         * recording the previous one.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   102
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   103
        protected void install(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   104
            prev = log.diagnosticHandler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   105
            log.diagnosticHandler = this;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   106
        }
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
        /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   109
         * Handle a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   110
         */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   111
        public abstract void report(JCDiagnostic diag);
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
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
     * A DiagnosticHandler that discards all diagnostics.
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
    public static class DiscardDiagnosticHandler extends DiagnosticHandler {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   118
        public DiscardDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   119
            install(log);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   120
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   121
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   122
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   123
        public void report(JCDiagnostic diag) { }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   124
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   125
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
     * A DiagnosticHandler that can defer some or all diagnostics,
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   128
     * by buffering them for later examination and/or reporting.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   129
     * If a diagnostic is not deferred, or is subsequently reported
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   130
     * with reportAllDiagnostics(), it will be reported to the previously
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   131
     * active diagnostic handler.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   132
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   133
    public static class DeferredDiagnosticHandler extends DiagnosticHandler {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 17582
diff changeset
   134
        private Queue<JCDiagnostic> deferred = new ListBuffer<>();
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   135
        private final Filter<JCDiagnostic> filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   136
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   137
        public DeferredDiagnosticHandler(Log log) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   138
            this(log, null);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   139
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   140
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   141
        public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   142
            this.filter = filter;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   143
            install(log);
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
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   146
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   147
        public void report(JCDiagnostic diag) {
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   148
            if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) &&
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   149
                (filter == null || filter.accepts(diag))) {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   150
                deferred.add(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   151
            } else {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   152
                prev.report(diag);
16326
9b94bafef533 8009227: Certain diagnostics should not be deferred
mcimadamore
parents: 15385
diff changeset
   153
            }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   154
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   155
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   156
        public Queue<JCDiagnostic> getDiagnostics() {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   157
            return deferred;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   158
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   159
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   160
        /** Report all deferred diagnostics. */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   161
        public void reportDeferredDiagnostics() {
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 49822
diff changeset
   162
            reportDeferredDiagnostics(d -> true);
14538
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
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   165
        /** Report selected deferred diagnostics. */
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 49822
diff changeset
   166
        public void reportDeferredDiagnostics(Predicate<JCDiagnostic> accepter) {
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   167
            JCDiagnostic d;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   168
            while ((d = deferred.poll()) != null) {
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 49822
diff changeset
   169
                if (accepter.test(d))
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   170
                    prev.report(d);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   171
            }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   172
            deferred = null; // prevent accidental ongoing use
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   173
        }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   174
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   175
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   176
    public enum WriterKind { NOTICE, WARNING, ERROR, STDOUT, STDERR }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   178
    private final Map<WriterKind, PrintWriter> writers;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    /** The maximum number of errors/warnings that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     */
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   182
    protected int MaxErrors;
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   183
    protected int MaxWarnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    /** Switch: prompt user on each error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    public boolean promptOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    /** Switch: emit warning messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    public boolean emitWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
3660
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   193
    /** Switch: suppress note messages.
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   194
     */
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   195
    public boolean suppressNotes;
ee583c342a41 6873849: suppress notes generated by javac
jjg
parents: 2212
diff changeset
   196
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    /** Print stack trace on errors?
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    public boolean dumpOnError;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * Diagnostic listener, if provided through programmatic
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     * interface to javac (JSR 199).
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    protected DiagnosticListener<? super JavaFileObject> diagListener;
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   206
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   208
     * Formatter for diagnostics.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     */
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 867
diff changeset
   210
    private DiagnosticFormatter<JCDiagnostic> diagFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   212
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   213
     * Keys for expected diagnostics.
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   214
     */
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   215
    public Set<String> expectDiagKeys;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   216
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   217
    /**
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   218
     * 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
   219
     */
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   220
    public boolean compressedOutput;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   221
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   222
    /**
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   223
     * JavacMessages object used for localization.
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   224
     */
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   225
    private JavacMessages messages;
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1109
diff changeset
   226
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   227
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   228
     * 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
   229
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   230
    private DiagnosticHandler diagnosticHandler;
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   231
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   232
    /** Get the Log instance for this context. */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   233
    public static Log instance(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   234
        Log instance = context.get(logKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   235
        if (instance == null)
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   236
            instance = new Log(context);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   237
        return instance;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   238
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   239
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   240
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   241
     * Register a Context.Factory to create a Log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   242
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   243
    public static void preRegister(Context context, PrintWriter w) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   244
        context.put(Log.class, (Context.Factory<Log>) (c -> new Log(c, w)));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   245
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   246
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   247
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   248
     * Construct a log with default settings.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   249
     * If no streams are set in the context, the log will be initialized to use
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   250
     * System.out for normal output, and System.err for all diagnostic output.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   251
     * If one stream is set in the context, with either Log.outKey or Log.errKey,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   252
     * it will be used for all output.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   253
     * Otherwise, the log will be initialized to use both streams found in the context.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   254
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   255
    protected Log(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   256
        this(context, initWriters(context));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   257
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   258
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   259
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   260
     * Initialize a map of writers based on values found in the context
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   261
     * @param context the context in which to find writers to use
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   262
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   263
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   264
    private static Map<WriterKind, PrintWriter> initWriters(Context context) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   265
        PrintWriter out = context.get(outKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   266
        PrintWriter err = context.get(errKey);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   267
        if (out == null && err == null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   268
            out = new PrintWriter(System.out, true);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   269
            err = new PrintWriter(System.err, true);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   270
            return initWriters(out, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   271
        } else if (out == null || err == null) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   272
            PrintWriter pw = (out != null) ? out : err;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   273
            return initWriters(pw, pw);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   274
        } else {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   275
            return initWriters(out, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   276
        }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   277
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   278
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   279
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   280
     * Construct a log with all output sent to a single output stream.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   281
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   282
    protected Log(Context context, PrintWriter writer) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   283
        this(context, initWriters(writer, writer));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   284
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   285
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   286
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   287
     * Construct a log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   288
     * The log will be initialized to use stdOut for normal output, and stdErr
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   289
     * for all diagnostic output.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   291
    protected Log(Context context, PrintWriter out, PrintWriter err) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   292
        this(context, initWriters(out, err));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   293
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   294
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   295
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   296
     * Initialize a writer map for a stream for normal output, and a stream for diagnostics.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   297
     * @param out a stream to be used for normal output
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   298
     * @param err a stream to be used for diagnostic messages, such as errors, warnings, etc
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   299
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   300
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   301
    private static Map<WriterKind, PrintWriter> initWriters(PrintWriter out, PrintWriter err) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   302
        Map<WriterKind, PrintWriter> writers = new EnumMap<>(WriterKind.class);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   303
        writers.put(WriterKind.ERROR, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   304
        writers.put(WriterKind.WARNING, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   305
        writers.put(WriterKind.NOTICE, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   306
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   307
        writers.put(WriterKind.STDOUT, out);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   308
        writers.put(WriterKind.STDERR, err);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   309
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   310
        return writers;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   311
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   312
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   313
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   314
     * Construct a log with given I/O redirections.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   315
     * @deprecated
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   316
     * This constructor is provided to support the supported but now-deprecated javadoc entry point
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   317
     *      com.sun.tools.javadoc.Main.execute(String programName,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   318
     *          PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter,
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   319
     *          String defaultDocletClassName, String... args)
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   320
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   321
    @Deprecated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   323
        this(context, initWriters(errWriter, warnWriter, noticeWriter));
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   324
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   325
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   326
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   327
     * Initialize a writer map with different streams for different types of diagnostics.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   328
     * @param errWriter a stream for writing error messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   329
     * @param warnWriter a stream for writing warning messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   330
     * @param noticeWriter a stream for writing notice messages
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   331
     * @return a map of writers
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   332
     * @deprecated This method exists to support a supported but now deprecated javadoc entry point.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   333
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   334
    @Deprecated
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   335
    private static Map<WriterKind, PrintWriter>  initWriters(PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   336
        Map<WriterKind, PrintWriter> writers = new EnumMap<>(WriterKind.class);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   337
        writers.put(WriterKind.ERROR, errWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   338
        writers.put(WriterKind.WARNING, warnWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   339
        writers.put(WriterKind.NOTICE, noticeWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   340
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   341
        writers.put(WriterKind.STDOUT, noticeWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   342
        writers.put(WriterKind.STDERR, errWriter);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   343
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   344
        return writers;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   345
    }
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   346
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   347
    /**
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   348
     * Creates a log.
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   349
     * @param context the context in which the log should be registered
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   350
     * @param writers a map of writers that can be accessed by the kind of writer required
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   351
     */
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   352
    private Log(Context context, Map<WriterKind, PrintWriter> writers) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   353
        super(JCDiagnostic.Factory.instance(context));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
        context.put(logKey, this);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   355
        this.writers = writers;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        @SuppressWarnings("unchecked") // FIXME
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   358
        DiagnosticListener<? super JavaFileObject> dl =
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            context.get(DiagnosticListener.class);
1866
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   360
        this.diagListener = dl;
734286a0cc38 6794959: add new switch -XDexpectKeys=key,key....
jjg
parents: 1591
diff changeset
   361
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   362
        diagnosticHandler = new DefaultDiagnosticHandler();
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   363
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   364
        messages = JavacMessages.instance(context);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   365
        messages.add(Main.javacBundleName);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   366
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   367
        final Options options = Options.instance(context);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   368
        initOptions(options);
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 40504
diff changeset
   369
        options.addListener(() -> initOptions(options));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    // where
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   372
        private void initOptions(Options options) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   373
            this.dumpOnError = options.isSet(DOE);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   374
            this.promptOnError = options.isSet(PROMPT);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   375
            this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   376
            this.suppressNotes = options.isSet("suppressNotes");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   377
            this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   378
            this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   379
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   380
            boolean rawDiagnostics = options.isSet("rawDiagnostics");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   381
            this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   382
                                                  new BasicDiagnosticFormatter(options, messages);
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   383
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   384
            String ek = options.get("expectKeys");
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   385
            if (ek != null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   386
                expectDiagKeys = new HashSet<>(Arrays.asList(ek.split(", *")));
11052
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
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   389
        private int getIntOption(Options options, Option option, int defaultValue) {
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 11055
diff changeset
   390
            String s = options.get(option);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            try {
4699
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   392
                if (s != null) {
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   393
                    int n = Integer.parseInt(s);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   394
                    return (n <= 0 ? Integer.MAX_VALUE : n);
e58edf3542b3 6326754: Compiler will fail to handle -Xmaxerrs with -ve numbers
jjg
parents: 3995
diff changeset
   395
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            } catch (NumberFormatException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                // silently ignore ill-formed numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            return defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
5850
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   402
        /** Default value for -Xmaxerrs.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   403
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   404
        protected int getDefaultMaxErrors() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   405
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   406
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   407
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   408
        /** Default value for -Xmaxwarns.
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   409
         */
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   410
        protected int getDefaultMaxWarnings() {
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   411
            return 100;
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   412
        }
6f095ff5b469 6958836: javadoc should support -Xmaxerrs and -Xmaxwarns
jjg
parents: 5847
diff changeset
   413
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    /** The number of errors encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    public int nerrors = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    /** The number of warnings encountered so far.
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    public int nwarnings = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
    /** A set of all errors generated so far. This is used to avoid printing an
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     *  error message more than once. For each error, a pair consisting of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     *  source file name and source code position of the error is added to the set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     */
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents: 29291
diff changeset
   426
    protected Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   428
    /** A set of "not-supported-in-source-X" errors produced so far. This is used to only generate
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   429
     *  one such error per file.
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   430
     */
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   431
    protected Set<Pair<JavaFileObject, List<String>>>  recordedSourceLevelErrors = new HashSet<>();
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   432
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    public boolean hasDiagnosticListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        return diagListener != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   437
    public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28455
diff changeset
   438
        Assert.checkNonNull(name);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 11053
diff changeset
   439
        getSource(name).setEndPosTable(endPosTable);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   442
    /** Return current sourcefile.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
     */
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   444
    public JavaFileObject currentSourceFile() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        return source == null ? null : source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
2085
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   448
    /** Get the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   449
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   450
    public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   451
        return diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   452
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   453
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   454
    /** Set the current diagnostic formatter.
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   455
     */
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   456
    public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   457
        this.diagFormatter = diagFormatter;
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   458
    }
4792e12a8ca2 6769027: Source line should be displayed immediately after the first diagnostic line
mcimadamore
parents: 1866
diff changeset
   459
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   460
    public PrintWriter getWriter(WriterKind kind) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   461
        return writers.get(kind);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   462
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   463
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   464
    public void setWriter(WriterKind kind, PrintWriter pw) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 28455
diff changeset
   465
        Assert.checkNonNull(pw);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   466
        writers.put(kind, pw);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   467
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   468
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   469
    public void setWriters(PrintWriter pw) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   470
        Assert.checkNonNull(pw);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   471
        for (WriterKind k: WriterKind.values())
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   472
            writers.put(k, pw);
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   473
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   474
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14538
diff changeset
   475
    /**
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   476
     * Replace the specified diagnostic handler with the
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   477
     * 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
   478
     * The given handler must be the currently installed handler;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   479
     * it must be specified explicitly for clarity and consistency checking.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   480
     */
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   481
    public void popDiagnosticHandler(DiagnosticHandler h) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   482
        Assert.check(diagnosticHandler == h);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   483
        diagnosticHandler = h.prev;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   484
    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   485
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    /** Flush the logs
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
    public void flush() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   489
        for (PrintWriter pw: writers.values()) {
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   490
            pw.flush();
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   491
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   494
    public void flush(WriterKind kind) {
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   495
        getWriter(kind).flush();
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   496
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   497
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
    /** Returns true if an error needs to be reported for a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     * source name and pos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    protected boolean shouldReport(JavaFileObject file, int pos) {
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   502
        if (file == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   505
        Pair<JavaFileObject,Integer> coords = new Pair<>(file, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        boolean shouldReport = !recorded.contains(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        if (shouldReport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            recorded.add(coords);
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        return shouldReport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   512
    /** Returns true if a diagnostics needs to be reported.
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   513
     */
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   514
    private boolean shouldReport(JCDiagnostic d) {
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   515
        JavaFileObject file = d.getSource();
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   516
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   517
        if (file == null)
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   518
            return true;
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   519
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   520
        if (!shouldReport(file, d.getIntPosition()))
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   521
            return false;
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   522
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   523
        if (!d.isFlagSet(DiagnosticFlag.SOURCE_LEVEL))
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   524
            return true;
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   525
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   526
        Pair<JavaFileObject, List<String>> coords = new Pair<>(file, getCode(d));
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   527
        boolean shouldReport = !recordedSourceLevelErrors.contains(coords);
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   528
        if (shouldReport)
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   529
            recordedSourceLevelErrors.add(coords);
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   530
        return shouldReport;
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   531
    }
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   532
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   533
    //where
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   534
        private List<String> getCode(JCDiagnostic d) {
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   535
            ListBuffer<String> buf = new ListBuffer<>();
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   536
            getCodeRecursive(buf, d);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   537
            return buf.toList();
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   538
        }
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   539
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   540
        private void getCodeRecursive(ListBuffer<String> buf, JCDiagnostic d) {
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   541
            buf.add(d.getCode());
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   542
            for (Object o : d.getArgs()) {
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   543
                if (o instanceof JCDiagnostic) {
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   544
                    getCodeRecursive(buf, (JCDiagnostic)o);
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   545
                }
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   546
            }
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   547
        }
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   548
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
    /** Prompt user after an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    public void prompt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        if (promptOnError) {
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   553
            System.err.println(localize("resume.abort"));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
                while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
                    switch (System.in.read()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
                    case 'a': case 'A':
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
                        System.exit(-1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
                    case 'r': case 'R':
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                    case 'x': case 'X':
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
                        throw new AssertionError("user abort");
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
                    default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
            } catch (IOException e) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
    /** Print the faulty source code line and point to the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
     *  @param pos   Buffer index of the error position, must be on current line
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
    private void printErrLine(int pos, PrintWriter writer) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   575
        String line = (source == null ? null : source.getLine(pos));
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   576
        if (line == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            return;
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 942
diff changeset
   578
        int col = source.getColumnNumber(pos, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   580
        printRawLines(writer, line);
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   581
        for (int i = 0; i < col - 1; i++) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   582
            writer.print((line.charAt(i) == '\t') ? "\t" : " ");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        writer.println("^");
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   588
    public void printNewline() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   589
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   590
        noticeWriter.println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   591
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   592
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   593
    public void printNewline(WriterKind wk) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   594
        getWriter(wk).println();
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   595
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   596
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   597
    public void printLines(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   598
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   599
        printRawLines(noticeWriter, localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   600
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   601
49822
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   602
    public void printLines(DiagnosticInfo diag) {
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   603
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   604
        printRawLines(noticeWriter, localize(diag));
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   605
    }
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   606
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   607
    public void printLines(PrefixKind pk, String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   608
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   609
        printRawLines(noticeWriter, localize(pk, key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   610
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   611
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   612
    public void printLines(WriterKind wk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   613
        printRawLines(getWriter(wk), localize(key, args));
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   614
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   615
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   616
    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
   617
        printRawLines(getWriter(wk), localize(pk, key, args));
11052
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   618
    }
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   619
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   620
    /** Print the text of a message, translating newlines appropriately
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   621
     *  for the platform.
65b9fa7eaf55 7108668: allow Log to be initialized and used earlier
jjg
parents: 9071
diff changeset
   622
     */
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   623
    public void printRawLines(String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   624
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   625
        printRawLines(noticeWriter, msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   626
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   627
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   628
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   629
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   630
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   631
    public void printRawLines(WriterKind kind, String msg) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   632
        printRawLines(getWriter(kind), msg);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   633
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   634
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   635
    /** Print the text of a message, translating newlines appropriately
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   636
     *  for the platform.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   637
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   638
    public static void printRawLines(PrintWriter writer, String msg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        int nl;
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        while ((nl = msg.indexOf('\n')) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            writer.println(msg.substring(0, nl));
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            msg = msg.substring(nl+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        if (msg.length() != 0) writer.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   647
    /**
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   648
     * Print the localized text of a "verbose" message to the
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   649
     * noticeWriter stream.
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   650
     */
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   651
    public void printVerbose(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   652
        PrintWriter noticeWriter = writers.get(WriterKind.NOTICE);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   653
        printRawLines(noticeWriter, localize("verbose." + key, args));
8630
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   654
    }
cd9eefe597f6 6966736: javac verbose output is inconsistent
jjg
parents: 7681
diff changeset
   655
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   656
    @Override
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   657
    protected void directError(String key, Object... args) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   658
        PrintWriter errWriter = writers.get(WriterKind.ERROR);
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   659
        printRawLines(errWriter, localize(key, args));
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   660
        errWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
    /** Report a warning that cannot be suppressed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
    public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
28334
1633de6070ae 8058542: Devise scheme for better diagnostic creation
mcimadamore
parents: 25874
diff changeset
   669
        writeDiagnostic(diags.warning(null, source, pos, key, args));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
        nwarnings++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   673
    /**
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   674
     * Primary method to report a diagnostic.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   675
     * @param diagnostic
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   676
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   677
    @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   678
    public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   679
        diagnosticHandler.report(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   680
     }
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   681
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
     * Common diagnostic handling.
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
     * The diagnostic is counted, and depending on the options and how many diagnostics have been
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
     * reported so far, the diagnostic may be handed off to writeDiagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
     */
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   687
    private class DefaultDiagnosticHandler extends DiagnosticHandler {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   688
        @Override
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   689
        public void report(JCDiagnostic diagnostic) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   690
            if (expectDiagKeys != null)
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   691
                expectDiagKeys.remove(diagnostic.getCode());
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   692
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   693
            switch (diagnostic.getType()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   694
            case FRAGMENT:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   695
                throw new IllegalArgumentException();
6582
c7a4fb5a2f86 6403465: javac should defer diagnostics until it can be determined they are persistent
jjg
parents: 6143
diff changeset
   696
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   697
            case NOTE:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   698
                // 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
   699
                // 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
   700
                // in number.
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   701
                if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   702
                    writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   703
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   704
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   706
            case WARNING:
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   707
                if (emitWarnings || diagnostic.isMandatory()) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   708
                    if (nwarnings < MaxWarnings) {
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   709
                        writeDiagnostic(diagnostic);
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   710
                        nwarnings++;
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   711
                    }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   712
                }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   713
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   715
            case ERROR:
28455
41245007c074 8066843: Messager.printMessage cannot print multiple errors for same source position
jlahoda
parents: 28334
diff changeset
   716
                if (nerrors < MaxErrors &&
53773
454d54d8af1c 8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round
jlahoda
parents: 49822
diff changeset
   717
                    (diagnostic.isFlagSet(DiagnosticFlag.API) ||
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40308
diff changeset
   718
                     shouldReport(diagnostic))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                    writeDiagnostic(diagnostic);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   720
                    nerrors++;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                }
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14057
diff changeset
   722
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
            }
17582
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   724
            if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) {
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   725
                compressedOutput = true;
4079713129dd 8012003: Method diagnostics resolution need to be simplified in some cases
mcimadamore
parents: 16326
diff changeset
   726
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
     * Write out a diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    protected void writeDiagnostic(JCDiagnostic diag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        if (diagListener != null) {
9071
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   735
            diagListener.report(diag);
88cd61b4e5aa 6437138: JSR 199: Compiler doesn't diagnose crash in user code
jjg
parents: 8630
diff changeset
   736
            return;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        PrintWriter writer = getWriterForDiagnosticType(diag.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   741
        printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        if (promptOnError) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
            switch (diag.getType()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
            case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
        if (dumpOnError)
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            new RuntimeException().printStackTrace(writer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
        writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
    protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        switch (dt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
        case FRAGMENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
        case NOTE:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   764
            return writers.get(WriterKind.NOTICE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
        case WARNING:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   767
            return writers.get(WriterKind.WARNING);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
        case ERROR:
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   770
            return writers.get(WriterKind.ERROR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
            throw new Error();
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
    /** 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
   778
     *  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
   779
     *  Use localize(key, args) when possible.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
     *  @param key    The key for the localized string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
     *  @param args   Fields to substitute into the string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
    public static String getLocalizedString(String key, Object ... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   784
        return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
6143
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   787
    /** 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
   788
     *  @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
   789
     *  @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
   790
     */
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   791
    public String localize(String key, Object... args) {
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   792
        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
   793
    }
79b7dee406cc 6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents: 5850
diff changeset
   794
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   795
    public String localize(JCDiagnostic.DiagnosticInfo diagInfo) {
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   796
        if (useRawMessages) {
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   797
            return diagInfo.key();
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   798
        } else {
49822
53aae0c219e6 8196433: use the new error diagnostic approach at javac.Main
vromero
parents: 48054
diff changeset
   799
            return messages.getLocalizedString(diagInfo);
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   800
        }
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   801
    }
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   802
11053
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   803
    /** Find a localized string in the resource bundle.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   804
     *  @param key    The key for the localized string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   805
     *  @param args   Fields to substitute into the string.
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   806
     */
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   807
    public String localize(PrefixKind pk, String key, Object... args) {
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   808
        if (useRawMessages)
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   809
            return pk.key(key);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   810
        else
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   811
            return messages.getLocalizedString(pk.key(key), args);
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   812
    }
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   813
    // where
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   814
        // backdoor hook for testing, should transition to use -XDrawDiagnostics
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   815
        private static boolean useRawMessages = false;
48713f779b1d 7108669: cleanup Log methods for direct printing to streams
jjg
parents: 11052
diff changeset
   816
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
/***************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
 * raw error messages without internationalization; used for experimentation
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
 * and quick prototyping
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
 ***************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   822
    /** print an error or warning message:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   823
     */
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   824
    private void printRawDiag(PrintWriter pw, String prefix, int pos, String msg) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   825
        if (source == null || pos == Position.NOPOS) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   826
            printRawLines(pw, prefix + msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
        } else {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   828
            int line = source.getLineNumber(pos);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   829
            JavaFileObject file = source.getFile();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
            if (file != null)
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   831
                printRawLines(pw,
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3660
diff changeset
   832
                           file.getName() + ":" +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
                           line + ": " + msg);
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   834
            printErrLine(pos, pw);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        }
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   836
        pw.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   839
    /** report an error:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   840
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
    public void rawError(int pos, String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   842
        PrintWriter errWriter = writers.get(WriterKind.ERROR);
1591
e5a618442f5f 6768932: Add support for multiline diagnostics
mcimadamore
parents: 1472
diff changeset
   843
        if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   844
            printRawDiag(errWriter, "error: ", pos, msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
            nerrors++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        errWriter.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   851
    /** report a warning:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents: 815
diff changeset
   852
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
    public void rawWarning(int pos, String msg) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   854
        PrintWriter warnWriter = writers.get(WriterKind.ERROR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        if (nwarnings < MaxWarnings && emitWarnings) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   856
            printRawDiag(warnWriter, "warning: ", pos, msg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
        prompt();
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
        nwarnings++;
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 32799
diff changeset
   860
        warnWriter.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
    public static String format(String fmt, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
        return String.format((java.util.Locale)null, fmt, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
}