8217381: Incovenient errors reported when annotation processor generates source file and errors in the same round

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
This commit is contained in:
Jan Lahoda 2019-02-15 12:09:53 +01:00
parent 8c4106ff56
commit 68d32a9a8a
12 changed files with 104 additions and 21 deletions

View File

@ -1152,7 +1152,7 @@ public class JavacTrees extends DocTrees {
try {
switch (kind) {
case ERROR:
log.error(DiagnosticFlag.MULTIPLE, pos, Errors.ProcMessager(msg.toString()));
log.error(DiagnosticFlag.API, pos, Errors.ProcMessager(msg.toString()));
break;
case WARNING:

View File

@ -1014,7 +1014,11 @@ public class JavaCompiler {
* Parses a list of files.
*/
public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
if (shouldStop(CompileState.PARSE))
return parseFiles(fileObjects, false);
}
public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects, boolean force) {
if (!force && shouldStop(CompileState.PARSE))
return List.nil();
//parse all files

View File

@ -34,6 +34,7 @@ import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import java.util.Set;
import javax.lang.model.element.*;
import javax.tools.JavaFileObject;
import javax.tools.Diagnostic;
@ -117,7 +118,7 @@ public class JavacMessager implements Messager {
switch (kind) {
case ERROR:
errorCount++;
log.error(DiagnosticFlag.MULTIPLE, pos, Errors.ProcMessager(msg.toString()));
log.error(DiagnosticFlag.API, pos, Errors.ProcMessager(msg.toString()));
break;
case WARNING:

View File

@ -35,6 +35,7 @@ import java.net.URL;
import java.nio.file.Path;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.regex.*;
import java.util.stream.Collectors;
@ -83,6 +84,7 @@ import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.Iterators;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Log;
@ -1066,7 +1068,9 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
prev.newRound();
this.genClassFiles = prev.genClassFiles;
List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles);
//parse the generated files even despite errors reported so far, to eliminate
//recoverable errors related to the type declared in the generated files:
List<JCCompilationUnit> parsedFiles = compiler.parseFiles(newSourceFiles, true);
roots = prev.roots.appendList(parsedFiles);
// Check for errors after parsing
@ -1233,15 +1237,17 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
}
void showDiagnostics(boolean showAll) {
Set<JCDiagnostic.Kind> kinds = EnumSet.allOf(JCDiagnostic.Kind.class);
if (!showAll) {
// suppress errors, which are all presumed to be transient resolve errors
kinds.remove(JCDiagnostic.Kind.ERROR);
}
deferredDiagnosticHandler.reportDeferredDiagnostics(kinds);
deferredDiagnosticHandler.reportDeferredDiagnostics(showAll ? ACCEPT_ALL
: ACCEPT_NON_RECOVERABLE);
log.popDiagnosticHandler(deferredDiagnosticHandler);
compiler.setDeferredDiagnosticHandler(null);
}
//where:
private final Predicate<JCDiagnostic> ACCEPT_NON_RECOVERABLE =
d -> d.getKind() != JCDiagnostic.Kind.ERROR ||
!d.isFlagSet(DiagnosticFlag.RECOVERABLE) ||
d.isFlagSet(DiagnosticFlag.API);
private final Predicate<JCDiagnostic> ACCEPT_ALL = d -> true;
/** Print info about this round. */
private void printRoundInfo(boolean lastRound) {
@ -1335,7 +1341,7 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
errorStatus = round.unrecoverableError();
moreToDo = moreToDo();
round.showDiagnostics(errorStatus || showResolveErrors);
round.showDiagnostics(showResolveErrors);
// Set up next round.
// Copy mutable collections returned from filer.

View File

@ -429,9 +429,9 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
RECOVERABLE,
NON_DEFERRABLE,
COMPRESSED,
/** Print multiple errors for same source locations.
/** Flag for diagnostics that were reported through API methods.
*/
MULTIPLE,
API,
/** Flag for not-supported-in-source-X errors.
*/
SOURCE_LEVEL;

View File

@ -33,6 +33,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.function.Predicate;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
@ -158,14 +159,14 @@ public class Log extends AbstractLog {
/** Report all deferred diagnostics. */
public void reportDeferredDiagnostics() {
reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
reportDeferredDiagnostics(d -> true);
}
/** Report selected deferred diagnostics. */
public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
public void reportDeferredDiagnostics(Predicate<JCDiagnostic> accepter) {
JCDiagnostic d;
while ((d = deferred.poll()) != null) {
if (kinds.contains(d.getKind()))
if (accepter.test(d))
prev.report(d);
}
deferred = null; // prevent accidental ongoing use
@ -713,7 +714,7 @@ public class Log extends AbstractLog {
case ERROR:
if (nerrors < MaxErrors &&
(diagnostic.isFlagSet(DiagnosticFlag.MULTIPLE) ||
(diagnostic.isFlagSet(DiagnosticFlag.API) ||
shouldReport(diagnostic))) {
writeDiagnostic(diagnostic);
nerrors++;

View File

@ -76,7 +76,7 @@ public class TestLog
Set<DiagnosticFlag> defaultErrorFlags =
(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);
defaultErrorFlags.add(DiagnosticFlag.MULTIPLE);
defaultErrorFlags.add(DiagnosticFlag.API);
JavacFileManager.preRegister(context);
ParserFactory pfac = ParserFactory.instance(context);

View File

@ -1,3 +1,4 @@
SemanticErrorTest.java:13:46: compiler.err.repeated.interface
- compiler.err.proc.messager: Deliberate Error
2 errors
SemanticErrorTest.java:13:1: compiler.err.proc.messager: Deliberate Error on Trees
SemanticErrorTest.java:13:46: compiler.err.repeated.interface
3 errors

View File

@ -27,13 +27,21 @@ import javax.lang.model.*;
import javax.lang.model.element.*;
import static javax.tools.Diagnostic.Kind.*;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
public class TestProcessor extends JavacTestingAbstractProcessor {
private int round = 0;
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
if (++round == 1)
if (++round == 1) {
messager.printMessage(ERROR, "Deliberate Error");
Trees trees = Trees.instance(processingEnv);
TreePath elPath = trees.getPath(roundEnv.getRootElements().iterator().next());
trees.printMessage(ERROR, "Deliberate Error on Trees",
elPath.getLeaf(), elPath.getCompilationUnit());
}
return false;
}
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2019, 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 8217381
* @summary Check error are convenient when AP generates a source file and
* an error in the same round
* @library /tools/javac/lib
* @modules jdk.compiler
* @build JavacTestingAbstractProcessor GenerateAndError
* @compile/fail/ref=GenerateAndError.out -XDrawDiagnostics -processor GenerateAndError GenerateAndErrorTest.java
*/
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.Diagnostic.Kind;
public class GenerateAndError extends JavacTestingAbstractProcessor {
int round = 0;
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (round++ == 0) {
try (Writer w = processingEnv.getFiler().createSourceFile("Extra").openWriter()) {
w.write("public class Extra {}");
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
processingEnv.getMessager().printMessage(Kind.ERROR, "error");
}
return false;
}
}

View File

@ -0,0 +1,3 @@
- compiler.err.proc.messager: error
GenerateAndErrorTest.java:2:60: compiler.err.cant.resolve: kindname.class, ExtraExtra, ,
2 errors

View File

@ -0,0 +1,2 @@
/* /nodynamiccopyright/ */
public class GenerateAndErrorTest extends Extra implements ExtraExtra {}