test/tools/javac/processing/TestWarnErrorCount.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1466
b52a38d4536c
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial