8066843: Messager.printMessage cannot print multiple errors for same source position

Using a DiagnosticFlag to mark diagnostics that should be always printed even if multiple of them have the same source position.

Reviewed-by: darcy, jjg, mcimadamore
This commit is contained in:
Jan Lahoda 2015-01-13 19:13:42 +01:00
parent 2b3c75aa36
commit 1b90760ef4
16 changed files with 136 additions and 56 deletions

View file

@ -1,5 +1,5 @@
# #
# Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. # Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# #
# This code is free software; you can redistribute it and/or modify it # This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
# questions. # questions.
# #
dc.anchor.already.defined = anchor already defined: {0} dc.anchor.already.defined = anchor already defined: "{0}"
dc.anchor.value.missing = no value given for anchor dc.anchor.value.missing = no value given for anchor
dc.attr.lacks.value = attribute lacks value dc.attr.lacks.value = attribute lacks value
dc.attr.not.number = attribute value is not a number dc.attr.not.number = attribute value is not a number

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -98,6 +98,7 @@ import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.DefinedBy; import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api; import com.sun.tools.javac.util.DefinedBy.Api;
import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Log;
@ -924,12 +925,7 @@ public class JavacTrees extends DocTrees {
try { try {
switch (kind) { switch (kind) {
case ERROR: case ERROR:
boolean prev = log.multipleErrors; log.error(DiagnosticFlag.MULTIPLE, pos, "proc.messager", msg.toString());
try {
log.error(pos, "proc.messager", msg.toString());
} finally {
log.multipleErrors = prev;
}
break; break;
case WARNING: case WARNING:

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,7 @@ package com.sun.tools.javac.processing;
import com.sun.tools.javac.model.JavacElements; import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.DefinedBy.Api; 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 com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.tree.JCTree.*;
import javax.lang.model.element.*; import javax.lang.model.element.*;
@ -113,13 +114,7 @@ public class JavacMessager implements Messager {
switch (kind) { switch (kind) {
case ERROR: case ERROR:
errorCount++; errorCount++;
boolean prev = log.multipleErrors; log.error(DiagnosticFlag.MULTIPLE, pos, "proc.messager", msg.toString());
log.multipleErrors = true;
try {
log.error(pos, "proc.messager", msg.toString());
} finally {
log.multipleErrors = prev;
}
break; break;
case WARNING: case WARNING:

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -112,7 +112,7 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
*/ */
public JCDiagnostic error( public JCDiagnostic error(
DiagnosticFlag flag, DiagnosticSource source, DiagnosticPosition pos, Error errorKey) { DiagnosticFlag flag, DiagnosticSource source, DiagnosticPosition pos, Error errorKey) {
JCDiagnostic diag = create(null, defaultErrorFlags, source, pos, errorKey); JCDiagnostic diag = create(null, EnumSet.copyOf(defaultErrorFlags), source, pos, errorKey);
if (flag != null) { if (flag != null) {
diag.setFlag(flag); diag.setFlag(flag);
} }
@ -432,7 +432,10 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
SYNTAX, SYNTAX,
RECOVERABLE, RECOVERABLE,
NON_DEFERRABLE, NON_DEFERRABLE,
COMPRESSED COMPRESSED,
/** Print multiple errors for same source locations.
*/
MULTIPLE;
} }
private final DiagnosticSource source; private final DiagnosticSource source;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -38,6 +38,7 @@ import com.sun.tools.javac.api.DiagnosticFormatter;
import com.sun.tools.javac.main.Main; import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.main.Option; import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
@ -191,10 +192,6 @@ public class Log extends AbstractLog {
*/ */
public boolean dumpOnError; public boolean dumpOnError;
/** Print multiple errors for same source locations.
*/
public boolean multipleErrors;
/** /**
* Diagnostic listener, if provided through programmatic * Diagnostic listener, if provided through programmatic
* interface to javac (JSR 199). * interface to javac (JSR 199).
@ -417,7 +414,7 @@ public class Log extends AbstractLog {
* source name and pos. * source name and pos.
*/ */
protected boolean shouldReport(JavaFileObject file, int pos) { protected boolean shouldReport(JavaFileObject file, int pos) {
if (multipleErrors || file == null) if (file == null)
return true; return true;
Pair<JavaFileObject,Integer> coords = new Pair<>(file, pos); Pair<JavaFileObject,Integer> coords = new Pair<>(file, pos);
@ -580,8 +577,9 @@ public class Log extends AbstractLog {
break; break;
case ERROR: case ERROR:
if (nerrors < MaxErrors if (nerrors < MaxErrors &&
&& shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { (diagnostic.isFlagSet(DiagnosticFlag.MULTIPLE) ||
shouldReport(diagnostic.getSource(), diagnostic.getIntPosition()))) {
writeDiagnostic(diagnostic); writeDiagnostic(diagnostic);
nerrors++; nerrors++;
} }

View file

@ -1,4 +1,4 @@
AnchorTest.java:19: error: anchor already defined: foo AnchorTest.java:19: error: anchor already defined: "foo"
* <a name=foo></a> * <a name=foo></a>
^ ^
AnchorTest.java:24: error: invalid name for anchor: "" AnchorTest.java:24: error: invalid name for anchor: ""
@ -10,28 +10,40 @@ AnchorTest.java:29: error: invalid name for anchor: "123"
AnchorTest.java:34: error: no value given for anchor AnchorTest.java:34: error: no value given for anchor
* <a name ></a> * <a name ></a>
^ ^
AnchorTest.java:46: error: anchor already defined: foo AnchorTest.java:46: error: anchor already defined: "foo"
* <a id=foo></a> * <a id=foo></a>
^ ^
AnchorTest.java:51: error: invalid name for anchor: "" AnchorTest.java:51: error: invalid name for anchor: ""
* <a id=></a> * <a id=></a>
^ ^
AnchorTest.java:51: error: anchor already defined: ""
* <a id=></a>
^
AnchorTest.java:56: error: invalid name for anchor: "123" AnchorTest.java:56: error: invalid name for anchor: "123"
* <a id=123 ></a> * <a id=123 ></a>
^ ^
AnchorTest.java:56: error: anchor already defined: "123"
* <a id=123 ></a>
^
AnchorTest.java:61: error: no value given for anchor AnchorTest.java:61: error: no value given for anchor
* <a id ></a> * <a id ></a>
^ ^
AnchorTest.java:73: error: anchor already defined: foo AnchorTest.java:73: error: anchor already defined: "foo"
* <p id=foo>text</p> * <p id=foo>text</p>
^ ^
AnchorTest.java:78: error: invalid name for anchor: "" AnchorTest.java:78: error: invalid name for anchor: ""
* <p id=>text</p> * <p id=>text</p>
^ ^
AnchorTest.java:78: error: anchor already defined: ""
* <p id=>text</p>
^
AnchorTest.java:83: error: invalid name for anchor: "123" AnchorTest.java:83: error: invalid name for anchor: "123"
* <p id=123 >text</p> * <p id=123 >text</p>
^ ^
AnchorTest.java:83: error: anchor already defined: "123"
* <p id=123 >text</p>
^
AnchorTest.java:88: error: no value given for anchor AnchorTest.java:88: error: no value given for anchor
* <p id >text</p> * <p id >text</p>
^ ^
12 errors 16 errors

View file

@ -1,4 +1,4 @@
AnchorTest2.java:15: error: anchor already defined: AnchorTest2 AnchorTest2.java:15: error: anchor already defined: "AnchorTest2"
/** <a name="AnchorTest2"> </a> */ /** <a name="AnchorTest2"> </a> */
^ ^
1 error 1 error

View file

@ -13,6 +13,9 @@ HtmlTagsTest.java:23: error: self-closing element not allowed
HtmlTagsTest.java:28: error: element not allowed in documentation comments: <html> HtmlTagsTest.java:28: error: element not allowed in documentation comments: <html>
* <html> * <html>
^ ^
HtmlTagsTest.java:28: error: element not closed: html
* <html>
^
HtmlTagsTest.java:33: error: block element not allowed within inline element <span>: p HtmlTagsTest.java:33: error: block element not allowed within inline element <span>: p
* <span> <p> </span> * <span> <p> </span>
^ ^
@ -40,5 +43,5 @@ HtmlTagsTest.java:59: error: text not allowed in <ul> element
HtmlTagsTest.java:64: error: tag not allowed here: <b> HtmlTagsTest.java:64: error: tag not allowed here: <b>
* <ul> <b>text</b> <li> ... </li> </ul> * <ul> <b>text</b> <li> ... </li> </ul>
^ ^
13 errors 14 errors
1 warning 1 warning

View file

@ -1,7 +1,7 @@
Test.java:14:7: compiler.err.proc.messager: anchor already defined: dupTest Test.java:14:7: compiler.err.proc.messager: anchor already defined: "dupTest"
Test.java:24:12: compiler.err.proc.messager: anchor already defined: dupTestField Test.java:24:12: compiler.err.proc.messager: anchor already defined: "dupTestField"
Test.java:27:12: compiler.err.proc.messager: anchor already defined: dupTestMethod Test.java:27:12: compiler.err.proc.messager: anchor already defined: "dupTestMethod"
Test.java:32:11: compiler.err.proc.messager: anchor already defined: dupNested Test.java:32:11: compiler.err.proc.messager: anchor already defined: "dupNested"
Test.java:40:15: compiler.err.proc.messager: anchor already defined: dupNestedField Test.java:40:15: compiler.err.proc.messager: anchor already defined: "dupNestedField"
Test.java:47:15: compiler.err.proc.messager: anchor already defined: dupNestedMethod Test.java:47:15: compiler.err.proc.messager: anchor already defined: "dupNestedMethod"
6 errors 6 errors

View file

@ -1,19 +1,19 @@
Test.java:14: error: anchor already defined: dupTest Test.java:14: error: anchor already defined: "dupTest"
* <a name="dupTest">dupTest again</a> * <a name="dupTest">dupTest again</a>
^ ^
Test.java:24: error: anchor already defined: dupTestField Test.java:24: error: anchor already defined: "dupTestField"
/** <a name="dupTestField">dupTestField again</a> */ /** <a name="dupTestField">dupTestField again</a> */
^ ^
Test.java:27: error: anchor already defined: dupTestMethod Test.java:27: error: anchor already defined: "dupTestMethod"
/** <a name="dupTestMethod">dupTestMethod again</a> */ /** <a name="dupTestMethod">dupTestMethod again</a> */
^ ^
Test.java:32: error: anchor already defined: dupNested Test.java:32: error: anchor already defined: "dupNested"
* <a name="dupNested">dupNested again</a> * <a name="dupNested">dupNested again</a>
^ ^
Test.java:40: error: anchor already defined: dupNestedField Test.java:40: error: anchor already defined: "dupNestedField"
* <a name="dupNestedField">dupNestedField</a> * <a name="dupNestedField">dupNestedField</a>
^ ^
Test.java:47: error: anchor already defined: dupNestedMethod Test.java:47: error: anchor already defined: "dupNestedMethod"
* <a name="dupNestedMethod">dupNestedMethod</a> * <a name="dupNestedMethod">dupNestedMethod</a>
^ ^
6 errors 6 errors

View file

@ -1,2 +1,2 @@
package-info.java:12:7: compiler.err.proc.messager: anchor already defined: here package-info.java:12:7: compiler.err.proc.messager: anchor already defined: "here"
1 error 1 error

View file

@ -1,4 +1,4 @@
package-info.java:12: error: anchor already defined: here package-info.java:12: error: anchor already defined: "here"
* <a name=here>here again</a> * <a name=here>here again</a>
^ ^
1 error 1 error

View file

@ -1,7 +1,7 @@
AnchorAlreadyDefined.java:14: error: anchor already defined: here AnchorAlreadyDefined.java:14: error: anchor already defined: "here"
* <a name="here">duplicate</a> * <a name="here">duplicate</a>
^ ^
AnchorAlreadyDefined.java:15: error: anchor already defined: here AnchorAlreadyDefined.java:15: error: anchor already defined: "here"
* <h1 id="here">duplicate</h1> * <h1 id="here">duplicate</h1>
^ ^
2 errors 2 errors

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -26,10 +26,11 @@
* @bug 6304912 * @bug 6304912
* @summary unit test for Log * @summary unit test for Log
*/ */
import java.lang.reflect.Field;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.util.Set;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject; import javax.tools.SimpleJavaFileObject;
import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.file.JavacFileManager;
@ -41,23 +42,34 @@ import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log; import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.JCDiagnostic; import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JCDiagnostic.Factory;
import com.sun.tools.javac.util.Options; import com.sun.tools.javac.util.Options;
public class TestLog public class TestLog
{ {
public static void main(String... args) throws IOException { public static void main(String... args) throws Exception {
test(false); test(false);
test(true); test(true);
} }
static void test(boolean genEndPos) throws IOException { static void test(boolean genEndPos) throws Exception {
Context context = new Context(); Context context = new Context();
Options options = Options.instance(context); Options options = Options.instance(context);
options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m"); options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");
Log log = Log.instance(context); Log log = Log.instance(context);
log.multipleErrors = true; Factory diagnosticFactory = JCDiagnostic.Factory.instance(context);
Field defaultErrorFlagsField =
JCDiagnostic.Factory.class.getDeclaredField("defaultErrorFlags");
defaultErrorFlagsField.setAccessible(true);
Set<DiagnosticFlag> defaultErrorFlags =
(Set<DiagnosticFlag>) defaultErrorFlagsField.get(diagnosticFactory);
defaultErrorFlags.add(DiagnosticFlag.MULTIPLE);
JavacFileManager.preRegister(context); JavacFileManager.preRegister(context);
ParserFactory pfac = ParserFactory.instance(context); ParserFactory pfac = ParserFactory.instance(context);

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2014, 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 8066843
* @summary Annotation processors should be able to print multiple errors at the same location.
* @library /tools/javac/lib
* @build JavacTestingAbstractProcessor TestMultipleErrors
* @compile/fail/ref=TestMultipleErrors.out -XDrawDiagnostics -processor TestMultipleErrors TestMultipleErrors.java
*/
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.Diagnostic.Kind;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
public class TestMultipleErrors extends JavacTestingAbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element root : roundEnv.getRootElements()) {
processingEnv.getMessager().printMessage(Kind.ERROR, "error1", root);
processingEnv.getMessager().printMessage(Kind.ERROR, "error2", root);
Trees trees = Trees.instance(processingEnv);
TreePath path = trees.getPath(root);
trees.printMessage(Kind.ERROR, "error3", path.getLeaf(), path.getCompilationUnit());
trees.printMessage(Kind.ERROR, "error4", path.getLeaf(), path.getCompilationUnit());
}
return true;
}
}

View file

@ -0,0 +1,5 @@
TestMultipleErrors.java:41:8: compiler.err.proc.messager: error1
TestMultipleErrors.java:41:8: compiler.err.proc.messager: error2
TestMultipleErrors.java:41:8: compiler.err.proc.messager: error3
TestMultipleErrors.java:41:8: compiler.err.proc.messager: error4
4 errors