1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7022337
* @summary repeated warnings about bootclasspath not set
* @library /tools/javac/lib
* @modules jdk.compiler
* @build JavacTestingAbstractProcessor TestWarnErrorCount
* @run main TestWarnErrorCount
*/
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.*;
@SupportedOptions({"errKind", "msgrWarnKind", "javaWarnKind"})
public class TestWarnErrorCount extends JavacTestingAbstractProcessor {
public static void main(String... args) throws Exception {
new TestWarnErrorCount().run(args);
}
final int MAX_GEN = 10;
final int ERROR_ROUND = MAX_GEN / 2; // when to generate error
/**
* Type of errors to generate in test case.
*/
enum ErrorKind {
/** No errors. */
NONE,
/** Source code errors. */
JAVA,
/** Errors reported to Messager. */
MESSAGER,
/** Error as a result of using -Werror. */
WERROR,
}
/**
* Frequency of warnings in test case.
*/
enum WarnKind {
/** No warnings. */
NONE {
boolean warn(int round) { return false; }
int count(int start, int end) { return 0; }
},
/** Generate a warning if round count is a multiple of 2. */
EVERY_TWO {
boolean warn(int round) { return (round % 2) == 0; }
int count(int start, int end) { return (end / 2) - ((start - 1)/ 2); }
},
/** Generate a warning if round count is a multiple of 3. */
EVERY_THREE {
boolean warn(int round) { return (round % 3) == 0; }
int count(int start, int end) { return (end / 3) - ((start - 1)/ 3); }
},
/** Generate a warning every round. */
ALL {
boolean warn(int round) { return true; }
int count(int start, int end) { return (end - start + 1); }
};
/** whether to generate a warning in round 'round'. */
abstract boolean warn(int round);
/** number of warnings generated in a range of rounds, inclusive. */
abstract int count(int start, int end);
}
/**
* Run test.
* @param args provide ability to specify particular test cases for debugging.
*/
void run(String... args) throws Exception {
for (String arg: args) {
if (arg.matches("[0-9]+")) {
if (testCases == null)
testCases = new HashSet<Integer>();
testCases.add(Integer.valueOf(arg));
} else if (arg.equals("-stopOnError")) {
stopOnError = true;
} else
throw new IllegalArgumentException(arg);
}
run ();
if (errors > 0)
throw new Exception(errors + " errors found");
}
/**
* Run test.
*/
void run() throws Exception {
for (ErrorKind ek: ErrorKind.values()) {
for (WarnKind mwk: WarnKind.values()) {
for (WarnKind jwk: WarnKind.values()) {
test(ek, mwk, jwk);
if (stopOnError && errors > 0)
throw new Exception(errors + " errors found");
}
}
}
}
boolean stopOnError;
Set<Integer> testCases;
int testNum = 0;
/**
* Run a test case.
* @param ek The type of errors to generate
* @param mwk The frequency of Messager warnings to generate
* @param jwk The frequency of Java warnings to generate
*/
void test(ErrorKind ek, WarnKind mwk, WarnKind jwk) {
testNum++;
if (testCases != null && !testCases.contains(testNum))
return;
System.err.println("Test " + testNum + ": ek:" + ek + " mwk:" + mwk + " jwk:" + jwk);
File testDir = new File("test" + testNum);
testDir.mkdirs();
String myName = TestWarnErrorCount.class.getSimpleName();
File testSrc = new File(System.getProperty("test.src"));
File file = new File(testSrc, myName + ".java");
List<String> args = new ArrayList<String>();
args.addAll(Arrays.asList(
"-XDrawDiagnostics",
"-d", testDir.getPath(),
"-processor", myName,
// "-XprintRounds",
"-Xlint:all,-path",
"-AerrKind=" + ek,
"-AmsgrWarnKind=" + mwk,
"-AjavaWarnKind=" + jwk));
if (ek == ErrorKind.WERROR)
args.add("-Werror");
args.add(file.getPath());
String out = compile(args.toArray(new String[args.size()]));
int errsFound = 0;
int errsReported = 0;
int warnsFound = 0;
int warnsReported = 0;
// Scan the output looking for messages of interest.
for (String line: out.split("[\r\n]+")) {
if (line.contains("compiler.err.")) {
errsFound++;
} else if (line.contains("compiler.warn.")) {
warnsFound++;
} else if (line.matches("[0-9]+ error(?:s?)")) {
errsReported = Integer.valueOf(line.substring(0, line.indexOf("error")).trim());
} else if (line.matches("[0-9]+ warning(?:s?)")) {
warnsReported = Integer.valueOf(line.substring(0, line.indexOf("warning")).trim());
}
}
// Compute the expected number of errors and warnings, based on
// the test case parameters.
// This is highly specific to the annotation processor below, and to
// the files it generates.
// Generally, the rules are:
// -- errors stop annotation processing, allowing for one extra "last round"
// -- messager warnings are immediate
// -- javac warnings are not shown before the final compilation
// (FIXME? -Werror does not stop processing for java warnings)
int errsExpected;
int msgrWarnsExpected;
int javaWarnsExpected;
switch (ek) {
case NONE:
errsExpected = 0;
msgrWarnsExpected = mwk.count(1, 1 + MAX_GEN + 1);
javaWarnsExpected = jwk.count(2, 1 + MAX_GEN);
break;
case MESSAGER:
errsExpected = 1;
msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
javaWarnsExpected = 0;
break;
case JAVA:
errsExpected = 1;
msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
javaWarnsExpected = 0;
break;
case WERROR:
errsExpected = (mwk != WarnKind.NONE || jwk != WarnKind.NONE) ? 1 : 0;
switch (mwk) {
case NONE:
msgrWarnsExpected = 0;
javaWarnsExpected = (jwk == WarnKind.NONE)
? 0
: 1; // this is surprising: javac only reports warning in first file
break;
case EVERY_TWO:
msgrWarnsExpected = mwk.count(1, 2 + 1);
javaWarnsExpected = 0;
break;
case EVERY_THREE:
msgrWarnsExpected = mwk.count(1, 3 + 1);
javaWarnsExpected = 0;
break;
case ALL:
msgrWarnsExpected = mwk.count(1, 1 + 1);
javaWarnsExpected = 0;
break;
default:
throw new IllegalStateException();
}
break;
default:
throw new IllegalStateException();
}
int warnsExpected = msgrWarnsExpected + javaWarnsExpected;
System.err.println("mwk: " + msgrWarnsExpected
+ ", jwk: " + javaWarnsExpected
+ ", total: " + warnsExpected);
boolean ok;
ok = checkEqual("errors", "reported", errsFound, errsReported);
ok &= checkEqual("errors", "expected", errsFound, errsExpected);
ok &= checkEqual("warnings", "reported", warnsFound, warnsReported);
ok &= checkEqual("warnings", "expected", warnsFound, warnsExpected);
if (ok)
System.err.println("OK");
System.err.println();
}
String compile(String... args) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
int rc = com.sun.tools.javac.Main.compile(args, pw);
pw.close();
String out = sw.toString();
if (!out.isEmpty())
System.err.println(out);
if (rc != 0)
System.err.println("compilation failed: rc=" + rc);
return out;
}
boolean checkEqual(String l1, String l2, int i1, int i2) {
if (i1 != i2)
error("number of " + l1 + " found, " + i1 + ", does not match number " + l2 + ", " + i2);
return (i1 == i2);
}
void error(String msg) {
System.err.println("Error: " + msg);
errors++;
}
int errors = 0;
// ----- Annotation processor -----
int round = 0;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
round++;
ErrorKind ek = ErrorKind.valueOf(options.get("errKind"));
WarnKind mwk = WarnKind.valueOf(options.get("msgrWarnKind"));
WarnKind jwk = WarnKind.valueOf(options.get("javaWarnKind"));
messager.printMessage(Diagnostic.Kind.NOTE,
"Round " + round
+ " " + roundEnv.getRootElements()
+ ", last round: " + roundEnv.processingOver());
messager.printMessage(Diagnostic.Kind.NOTE,
"ek: " + ek + ", mwk: " + mwk + ", jwk: " + jwk);
if (round <= MAX_GEN && !roundEnv.processingOver())
generate("Gen" + round,
(ek == ErrorKind.JAVA) && (round == ERROR_ROUND),
jwk.warn(round));
if (mwk.warn(round))
messager.printMessage(Diagnostic.Kind.WARNING, "round " + round);
if ((ek == ErrorKind.MESSAGER) && (round == ERROR_ROUND))
messager.printMessage(Diagnostic.Kind.ERROR, "round " + round);
return true;
}
void generate(String name, boolean error, boolean warn) {
try {
JavaFileObject fo = filer.createSourceFile(name);
Writer out = fo.openWriter();
try {
out.write("class " + name + " {\n"
+ (warn ? " void m() throws Exception { try (AutoCloseable ac = null) { } }" : "")
+ (error ? " ERROR\n" : "")
+ "}\n");
} finally {
out.close();
}
} catch (IOException e) {
throw new Error(e);
}
}
}
|