jjg@898: /* jjh@1305: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. jjg@898: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@898: * jjg@898: * This code is free software; you can redistribute it and/or modify it jjg@898: * under the terms of the GNU General Public License version 2 only, as jjg@898: * published by the Free Software Foundation. jjg@898: * jjg@898: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@898: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@898: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@898: * version 2 for more details (a copy is included in the LICENSE file that jjg@898: * accompanied this code). jjg@898: * jjg@898: * You should have received a copy of the GNU General Public License version jjg@898: * 2 along with this work; if not, write to the Free Software Foundation, jjg@898: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@898: * jjg@898: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@898: * or visit www.oracle.com if you need additional information or have any jjg@898: * questions. jjg@898: */ jjg@898: jjg@898: jjg@898: /* jjg@898: * @test jjg@898: * @bug 7022337 jjg@898: * @summary repeated warnings about bootclasspath not set darcy@1466: * @library /tools/javac/lib jjg@898: * @build JavacTestingAbstractProcessor TestWarnErrorCount jjg@898: * @run main TestWarnErrorCount jjg@898: */ jjg@898: jjg@898: import java.io.*; jjg@898: import java.util.*; jjg@898: import javax.annotation.processing.*; jjg@898: import javax.lang.model.element.*; jjg@898: import javax.tools.*; jjg@898: jjg@898: @SupportedOptions({"errKind", "msgrWarnKind", "javaWarnKind"}) jjg@898: public class TestWarnErrorCount extends JavacTestingAbstractProcessor { jjg@898: public static void main(String... args) throws Exception { jjg@898: new TestWarnErrorCount().run(args); jjg@898: } jjg@898: jjg@898: final int MAX_GEN = 10; jjg@898: final int ERROR_ROUND = MAX_GEN / 2; // when to generate error jjg@898: jjg@898: /** jjg@898: * Type of errors to generate in test case. jjg@898: */ jjg@898: enum ErrorKind { jjg@898: /** No errors. */ jjg@898: NONE, jjg@898: /** Source code errors. */ jjg@898: JAVA, jjg@898: /** Errors reported to Messager. */ jjg@898: MESSAGER, jjg@898: /** Error as a result of using -Werror. */ jjg@898: WERROR, jjg@898: } jjg@898: jjg@898: /** jjg@898: * Frequency of warnings in test case. jjg@898: */ jjg@898: enum WarnKind { jjg@898: /** No warnings. */ jjg@898: NONE { jjg@898: boolean warn(int round) { return false; } jjg@898: int count(int start, int end) { return 0; } jjg@898: }, jjg@898: /** Generate a warning if round count is a multiple of 2. */ jjg@898: EVERY_TWO { jjg@898: boolean warn(int round) { return (round % 2) == 0; } jjg@898: int count(int start, int end) { return (end / 2) - ((start - 1)/ 2); } jjg@898: }, jjg@898: /** Generate a warning if round count is a multiple of 3. */ jjg@898: EVERY_THREE { jjg@898: boolean warn(int round) { return (round % 3) == 0; } jjg@898: int count(int start, int end) { return (end / 3) - ((start - 1)/ 3); } jjg@898: }, jjg@898: /** Generate a warning every round. */ jjg@898: ALL { jjg@898: boolean warn(int round) { return true; } jjg@898: int count(int start, int end) { return (end - start + 1); } jjg@898: }; jjg@898: jjg@898: /** whether to generate a warning in round 'round'. */ jjg@898: abstract boolean warn(int round); jjg@898: jjg@898: /** number of warnings generated in a range of rounds, inclusive. */ jjg@898: abstract int count(int start, int end); jjg@898: } jjg@898: jjg@898: jjg@898: /** jjg@898: * Run test. jjg@898: * @param args provide ability to specify particular test cases for debugging. jjg@898: */ jjg@898: void run(String... args) throws Exception { jjg@898: for (String arg: args) { jjg@898: if (arg.matches("[0-9]+")) { jjg@898: if (testCases == null) jjg@898: testCases = new HashSet(); jjg@898: testCases.add(Integer.valueOf(arg)); jjg@898: } else if (arg.equals("-stopOnError")) { jjg@898: stopOnError = true; jjg@898: } else jjg@898: throw new IllegalArgumentException(arg); jjg@898: } jjg@898: jjg@898: run (); jjg@898: jjg@898: if (errors > 0) jjg@898: throw new Exception(errors + " errors found"); jjg@898: } jjg@898: jjg@898: /** jjg@898: * Run test. jjg@898: */ jjg@898: void run() throws Exception { jjg@898: for (ErrorKind ek: ErrorKind.values()) { jjg@898: for (WarnKind mwk: WarnKind.values()) { jjg@898: for (WarnKind jwk: WarnKind.values()) { jjg@898: test(ek, mwk, jwk); jjg@898: if (stopOnError && errors > 0) jjg@898: throw new Exception(errors + " errors found"); jjg@898: } jjg@898: } jjg@898: } jjg@898: } jjg@898: jjg@898: boolean stopOnError; jjg@898: Set testCases; jjg@898: int testNum = 0; jjg@898: jjg@898: /** jjg@898: * Run a test case. jjg@898: * @param ek The type of errors to generate jjg@898: * @param mwk The frequency of Messager warnings to generate jjg@898: * @param jwk The frequency of Java warnings to generate jjg@898: */ jjg@898: void test(ErrorKind ek, WarnKind mwk, WarnKind jwk) { jjg@898: testNum++; jjg@898: jjg@898: if (testCases != null && !testCases.contains(testNum)) jjg@898: return; jjg@898: jjg@898: System.err.println("Test " + testNum + ": ek:" + ek + " mwk:" + mwk + " jwk:" + jwk); jjg@898: jjg@898: File testDir = new File("test" + testNum); jjg@898: testDir.mkdirs(); jjg@898: jjg@898: String myName = TestWarnErrorCount.class.getSimpleName(); jjg@898: File testSrc = new File(System.getProperty("test.src")); jjg@898: File file = new File(testSrc, myName + ".java"); jjg@898: jjg@898: List args = new ArrayList(); jjg@898: args.addAll(Arrays.asList( jjg@898: "-XDrawDiagnostics", jjg@898: "-d", testDir.getPath(), jjg@898: "-processor", myName, jjg@898: // "-XprintRounds", mcimadamore@908: "-Xlint:all,-path", jjg@898: "-AerrKind=" + ek, jjg@898: "-AmsgrWarnKind=" + mwk, jjg@898: "-AjavaWarnKind=" + jwk)); jjg@898: if (ek == ErrorKind.WERROR) jjg@898: args.add("-Werror"); jjg@898: args.add(file.getPath()); jjg@898: jjg@898: String out = compile(args.toArray(new String[args.size()])); jjg@898: jjg@898: int errsFound = 0; jjg@898: int errsReported = 0; jjg@898: int warnsFound = 0; jjg@898: int warnsReported = 0; jjg@898: jjg@898: // Scan the output looking for messages of interest. jjg@898: jjg@898: for (String line: out.split("[\r\n]+")) { jjg@898: if (line.contains("compiler.err.")) { jjg@898: errsFound++; jjg@898: } else if (line.contains("compiler.warn.")) { jjg@898: warnsFound++; jjg@898: } else if (line.matches("[0-9]+ error(?:s?)")) { jjg@898: errsReported = Integer.valueOf(line.substring(0, line.indexOf("error")).trim()); jjg@898: } else if (line.matches("[0-9]+ warning(?:s?)")) { jjg@898: warnsReported = Integer.valueOf(line.substring(0, line.indexOf("warning")).trim()); jjg@898: } jjg@898: } jjg@898: jjg@898: // Compute the expected number of errors and warnings, based on jjg@898: // the test case parameters. jjg@898: // This is highly specific to the annotation processor below, and to jjg@898: // the files it generates. jjg@898: // Generally, the rules are: jjg@898: // -- errors stop annotation processing, allowing for one extra "last round" jjg@898: // -- messager warnings are immediate jjg@898: // -- javac warnings are not shown before the final compilation jjg@898: // (FIXME? -Werror does not stop processing for java warnings) jjg@898: int errsExpected; jjg@898: int msgrWarnsExpected; jjg@898: int javaWarnsExpected; jjg@898: switch (ek) { jjg@898: case NONE: jjg@898: errsExpected = 0; jjg@898: msgrWarnsExpected = mwk.count(1, 1 + MAX_GEN + 1); jjg@898: javaWarnsExpected = jwk.count(2, 1 + MAX_GEN); jjg@898: break; jjg@898: case MESSAGER: jjg@898: errsExpected = 1; jjg@898: msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1); jjg@898: javaWarnsExpected = 0; jjg@898: break; jjg@898: case JAVA: jjg@898: errsExpected = 2; jjg@898: msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1); jjg@898: javaWarnsExpected = 0; jjg@898: break; jjg@898: case WERROR: jjg@898: errsExpected = (mwk != WarnKind.NONE || jwk != WarnKind.NONE) ? 1 : 0; jjg@898: switch (mwk) { jjg@898: case NONE: jjg@898: msgrWarnsExpected = 0; jjg@898: javaWarnsExpected = (jwk == WarnKind.NONE) jjg@898: ? 0 jjg@898: : 1; // this is surprising: javac only reports warning in first file jjg@898: break; jjg@898: case EVERY_TWO: jjg@898: msgrWarnsExpected = mwk.count(1, 2 + 1); jjg@898: javaWarnsExpected = 0; jjg@898: break; jjg@898: case EVERY_THREE: jjg@898: msgrWarnsExpected = mwk.count(1, 3 + 1); jjg@898: javaWarnsExpected = 0; jjg@898: break; jjg@898: case ALL: jjg@898: msgrWarnsExpected = mwk.count(1, 1 + 1); jjg@898: javaWarnsExpected = 0; jjg@898: break; jjg@898: default: jjg@898: throw new IllegalStateException(); jjg@898: } jjg@898: break; jjg@898: default: jjg@898: throw new IllegalStateException(); jjg@898: } jjg@898: jjg@898: int warnsExpected = msgrWarnsExpected + javaWarnsExpected; jjg@898: System.err.println("mwk: " + msgrWarnsExpected jjg@898: + ", jwk: " + javaWarnsExpected jjg@898: + ", total: " + warnsExpected); jjg@898: jjg@898: boolean ok; jjg@898: ok = checkEqual("errors", "reported", errsFound, errsReported); jjg@898: ok &= checkEqual("errors", "expected", errsFound, errsExpected); jjg@898: ok &= checkEqual("warnings", "reported", warnsFound, warnsReported); jjg@898: ok &= checkEqual("warnings", "expected", warnsFound, warnsExpected); jjg@898: if (ok) jjg@898: System.err.println("OK"); jjg@898: jjg@898: System.err.println(); jjg@898: } jjg@898: jjg@898: String compile(String... args) { jjg@898: StringWriter sw = new StringWriter(); jjg@898: PrintWriter pw = new PrintWriter(sw); jjg@898: int rc = com.sun.tools.javac.Main.compile(args, pw); jjg@898: pw.close(); jjg@898: String out = sw.toString(); jjg@898: if (!out.isEmpty()) jjg@898: System.err.println(out); jjg@898: if (rc != 0) jjg@898: System.err.println("compilation failed: rc=" + rc); jjg@898: return out; jjg@898: } jjg@898: jjg@898: boolean checkEqual(String l1, String l2, int i1, int i2) { jjg@898: if (i1 != i2) jjg@898: error("number of " + l1 + " found, " + i1 + ", does not match number " + l2 + ", " + i2); jjg@898: return (i1 == i2); jjg@898: } jjg@898: jjg@898: void error(String msg) { jjg@898: System.err.println("Error: " + msg); jjg@898: errors++; jjg@898: } jjg@898: jjg@898: int errors = 0; jjg@898: jjg@898: // ----- Annotation processor ----- jjg@898: jjg@898: int round = 0; jjg@898: jjg@898: @Override jjg@898: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@898: round++; jjg@898: jjg@898: ErrorKind ek = ErrorKind.valueOf(options.get("errKind")); jjg@898: WarnKind mwk = WarnKind.valueOf(options.get("msgrWarnKind")); jjg@898: WarnKind jwk = WarnKind.valueOf(options.get("javaWarnKind")); jjg@898: messager.printMessage(Diagnostic.Kind.NOTE, jjg@898: "Round " + round jjg@898: + " " + roundEnv.getRootElements() jjg@898: + ", last round: " + roundEnv.processingOver()); jjg@898: messager.printMessage(Diagnostic.Kind.NOTE, jjg@898: "ek: " + ek + ", mwk: " + mwk + ", jwk: " + jwk); jjg@898: jjg@898: if (round <= MAX_GEN && !roundEnv.processingOver()) jjg@898: generate("Gen" + round, jjg@898: (ek == ErrorKind.JAVA) && (round == ERROR_ROUND), jjg@898: jwk.warn(round)); jjg@898: jjg@898: if (mwk.warn(round)) jjg@898: messager.printMessage(Diagnostic.Kind.WARNING, "round " + round); jjg@898: jjg@898: if ((ek == ErrorKind.MESSAGER) && (round == ERROR_ROUND)) jjg@898: messager.printMessage(Diagnostic.Kind.ERROR, "round " + round); jjg@898: jjg@898: return true; jjg@898: } jjg@898: jjg@898: void generate(String name, boolean error, boolean warn) { jjg@898: try { jjg@898: JavaFileObject fo = filer.createSourceFile(name); jjg@898: Writer out = fo.openWriter(); jjg@898: try { jjg@898: out.write("class " + name + " {\n" mcimadamore@1237: + (warn ? " void m() throws Exception { try (AutoCloseable ac = null) { } }" : "") jjg@898: + (error ? " ERROR\n" : "") jjg@898: + "}\n"); jjg@898: } finally { jjg@898: out.close(); jjg@898: } jjg@898: } catch (IOException e) { jjg@898: throw new Error(e); jjg@898: } jjg@898: } jjg@898: }