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

Tue, 04 Mar 2008 15:45:20 +0000

author
mcimadamore
date
Tue, 04 Mar 2008 15:45:20 +0000
changeset 8
38bd6375f37d
parent 1
9a66ca7c79fa
child 50
b9bcea8bbe24
permissions
-rw-r--r--

6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
Summary: interplay between cyclic inheritance and tvar bounds hangs javac
Reviewed-by: jjg

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 import javax.tools.JavaFileObject;
duke@1 29
duke@1 30 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticSource;
duke@1 31 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 32
duke@1 33 /**
duke@1 34 * A formatter for diagnostic messages.
duke@1 35 * The formatter will format a diagnostic according to one of two format strings, depending on whether
duke@1 36 * or not the source name and position are set. The format is a printf-like string,
duke@1 37 * with the following special characters:
duke@1 38 * <ul>
duke@1 39 * <li>%b: the base of the source name, or "-" if not set
duke@1 40 * <li>%f: the source name, or "-" if not set
duke@1 41 * <li>%l: the line number of the diagnostic, derived from the character offset if set, or "-" otherwise
duke@1 42 * <li>%c: the column number of the diagnostic, derived from the character offset if set, or "-" otherwise
duke@1 43 * <li>%o: the character offset of the diagnostic if set, or "-" otherwise
duke@1 44 * <li>%p: the prefix for the diagnostic, derived from the diagnostic type
duke@1 45 * <li>%t: the prefix as it normally appears in standard diagnostics. In this case, no prefix is
duke@1 46 * shown if the type is ERROR and if a source name is set
duke@1 47 * <li>%m: the text or the diagnostic, including any appropriate arguments
duke@1 48 * </ul>
duke@1 49 */
duke@1 50 public class DiagnosticFormatter {
duke@1 51 /**
duke@1 52 * A format string to be used for diagnostics with a given position.
duke@1 53 */
duke@1 54 protected String posFormat;
duke@1 55
duke@1 56 /**
duke@1 57 * A format string to be used for diagnostics regarding classfiles
duke@1 58 */
duke@1 59 protected String classFormat = DEFAULT_CLASS_FORMAT;
duke@1 60
duke@1 61 /**
duke@1 62 * A format string to be used for diagnostics without a given position.
duke@1 63 */
duke@1 64 protected String noPosFormat;
duke@1 65
duke@1 66 /**
duke@1 67 * A value to indicate whether to output the i18n key and args, instead of
duke@1 68 * the derived l10n message.
duke@1 69 */
duke@1 70 protected boolean raw;
duke@1 71
duke@1 72 /** The context key for the formatter. */
duke@1 73 protected static final Context.Key<DiagnosticFormatter> formatterKey =
duke@1 74 new Context.Key<DiagnosticFormatter>();
duke@1 75
duke@1 76 /** Get the DiagnosticFormatter instance for this context. */
duke@1 77 public static DiagnosticFormatter instance(Context context) {
duke@1 78 DiagnosticFormatter instance = context.get(formatterKey);
duke@1 79 if (instance == null)
duke@1 80 instance = new DiagnosticFormatter(context);
duke@1 81 return instance;
duke@1 82 }
duke@1 83
duke@1 84 /**
duke@1 85 * Create a formatter based on the supplied options.
duke@1 86 */
duke@1 87 protected DiagnosticFormatter(Context context) {
duke@1 88 Options options = Options.instance(context);
duke@1 89 raw = options.get("rawDiagnostics") != null;
duke@1 90 String fmt = options.get("diags");
duke@1 91 if (fmt != null) {
duke@1 92 int sep = fmt.indexOf('|');
duke@1 93 if (sep == -1)
duke@1 94 posFormat = noPosFormat = fmt;
duke@1 95 else {
duke@1 96 posFormat = fmt.substring(0, sep);
duke@1 97 noPosFormat = fmt.substring(sep + 1);
duke@1 98 }
duke@1 99 }
duke@1 100 else {
duke@1 101 posFormat = DEFAULT_POS_FORMAT;
duke@1 102 noPosFormat = DEFAULT_NO_POS_FORMAT;
duke@1 103 }
duke@1 104 }
duke@1 105
duke@1 106 public static final String DEFAULT_POS_FORMAT = "%f:%l: %t%m";
duke@1 107 public static final String DEFAULT_CLASS_FORMAT = "%f: %t%m";
duke@1 108 public static final String DEFAULT_NO_POS_FORMAT = "%p%m";
duke@1 109
duke@1 110 public DiagnosticFormatter() {
duke@1 111 posFormat = DEFAULT_POS_FORMAT;
duke@1 112 noPosFormat = DEFAULT_NO_POS_FORMAT;
duke@1 113 raw = false;
duke@1 114 }
duke@1 115
duke@1 116 public DiagnosticFormatter(String pos, String noPos) {
duke@1 117 posFormat = pos;
duke@1 118 noPosFormat = noPos;
duke@1 119 raw = false;
duke@1 120 }
duke@1 121
duke@1 122 String format(JCDiagnostic d) {
duke@1 123 return (raw ? format_raw(d) : format_std(d));
duke@1 124 }
duke@1 125
duke@1 126 private String format_raw(JCDiagnostic d) {
duke@1 127 DiagnosticSource source = d.getDiagnosticSource();
duke@1 128 int position = d.getIntPosition();
duke@1 129
duke@1 130 StringBuilder sb = new StringBuilder();
duke@1 131 if (position == Position.NOPOS)
duke@1 132 sb.append("-");
duke@1 133 else {
duke@1 134 sb.append(source.getName() + ":" + source.getLineNumber(position) + ":" + source.getColumnNumber(position) + ":");
duke@1 135 }
duke@1 136 sb.append(" ");
duke@1 137 sb.append(d.getCode());
duke@1 138 String sep = ": ";
duke@1 139 for (Object arg: d.getArgs()) {
duke@1 140 sb.append(sep);
duke@1 141 if (arg instanceof JCDiagnostic) {
duke@1 142 sb.append('(');
duke@1 143 sb.append(format_raw((JCDiagnostic) arg));
duke@1 144 sb.append(')');
duke@1 145 }
duke@1 146 else if (arg instanceof JavaFileObject)
duke@1 147 sb.append(JavacFileManager.getJavacBaseFileName((JavaFileObject) arg));
duke@1 148 else
duke@1 149 sb.append(arg);
duke@1 150 sep = ", ";
duke@1 151 }
duke@1 152 return sb.toString();
duke@1 153 }
duke@1 154
duke@1 155 private String format_std(JCDiagnostic d) {
duke@1 156 DiagnosticSource source = d.getDiagnosticSource();
duke@1 157 DiagnosticType type = d.getType();
duke@1 158 int position = d.getIntPosition();
duke@1 159
duke@1 160
duke@1 161 String format = noPosFormat;
duke@1 162 if (source != null) {
duke@1 163 if (position != Position.NOPOS) {
duke@1 164 format = posFormat;
duke@1 165 } else if (source.getFile() != null &&
duke@1 166 source.getFile().getKind() == JavaFileObject.Kind.CLASS) {
duke@1 167 format = classFormat;
duke@1 168 }
duke@1 169 }
duke@1 170
duke@1 171 StringBuilder sb = new StringBuilder();
duke@1 172
duke@1 173 for (int i = 0; i < format.length(); i++) {
duke@1 174 char c = format.charAt(i);
duke@1 175 if (c == '%' && i < format.length() - 1) {
duke@1 176 c = format.charAt(++i);
duke@1 177 switch (c) {
duke@1 178 case 'b':
duke@1 179 sb.append(source == null ? "-" : source.getName());
duke@1 180 break;
duke@1 181
duke@1 182 case 'e':
duke@1 183 sb.append(position == Position.NOPOS ? "-" : String.valueOf(d.getEndPosition()));
duke@1 184 break;
duke@1 185
duke@1 186 case 'f':
duke@1 187 sb.append(source == null ? "-" : d.getSourceName());
duke@1 188 break;
duke@1 189
duke@1 190 case 'l':
duke@1 191 sb.append(position == Position.NOPOS ? "-" : String.valueOf(d.getLineNumber()));
duke@1 192 break;
duke@1 193
duke@1 194 case 'c':
duke@1 195 sb.append(position == Position.NOPOS ? "-" : String.valueOf(d.getColumnNumber()));
duke@1 196 break;
duke@1 197
duke@1 198 case 'o':
duke@1 199 sb.append(position == Position.NOPOS ? "-" : String.valueOf(position));
duke@1 200 break;
duke@1 201
duke@1 202 case 'p':
duke@1 203 sb.append(d.getPrefix());
duke@1 204 break;
duke@1 205
duke@1 206 case 's':
duke@1 207 sb.append(position == Position.NOPOS ? "-" : String.valueOf(d.getStartPosition()));
duke@1 208 break;
duke@1 209
duke@1 210 case 't': {
duke@1 211 boolean usePrefix;
duke@1 212 switch (type) {
duke@1 213 case FRAGMENT:
duke@1 214 usePrefix = false;
duke@1 215 break;
duke@1 216
duke@1 217 case ERROR:
duke@1 218 usePrefix = (position == Position.NOPOS);
duke@1 219 break;
duke@1 220
duke@1 221 default:
duke@1 222 usePrefix = true;
duke@1 223 }
duke@1 224
duke@1 225 if (usePrefix)
duke@1 226 sb.append(d.getPrefix());
duke@1 227 break;
duke@1 228 }
duke@1 229
duke@1 230 case 'm':
duke@1 231 sb.append(d.getMessage(null));
duke@1 232 break;
duke@1 233
duke@1 234 case '_':
duke@1 235 sb.append(' ');
duke@1 236 break;
duke@1 237
duke@1 238 case '%':
duke@1 239 sb.append('%');
duke@1 240 break;
duke@1 241
duke@1 242 default:
duke@1 243 sb.append(c);
duke@1 244 break;
duke@1 245 }
duke@1 246 }
duke@1 247 else
duke@1 248 sb.append(c);
duke@1 249 }
duke@1 250 return sb.toString();
duke@1 251 }
duke@1 252 }

mercurial