Improve sorting of compiler messages in GroupingMessageCollector

Ensure that messages reported on a file but with no line information are
reported before diagnostics related to code. This fixes
Java9ModulesIntegrationTest.testSeveralModulesWithTheSameName on some
machines
This commit is contained in:
Alexander Udalov
2017-07-05 17:29:46 +03:00
parent 3808ecbd1e
commit 158afdea2b
2 changed files with 34 additions and 13 deletions
@@ -18,16 +18,19 @@ package org.jetbrains.kotlin.cli.common.messages;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
public class GroupingMessageCollector implements MessageCollector {
private final MessageCollector delegate;
// File path (nullable) -> message
private final Multimap<String, Message> groupedMessages = LinkedHashMultimap.create();
// Note that the key in this map can be null
private final Multimap<CompilerMessageLocation, Message> groupedMessages = LinkedHashMultimap.create();
public GroupingMessageCollector(@NotNull MessageCollector delegate) {
this.delegate = delegate;
@@ -48,7 +51,7 @@ public class GroupingMessageCollector implements MessageCollector {
delegate.report(severity, message, location);
}
else {
groupedMessages.put(location != null ? location.getPath() : null, new Message(severity, message, location));
groupedMessages.put(location, new Message(severity, message, location));
}
}
@@ -60,8 +63,10 @@ public class GroupingMessageCollector implements MessageCollector {
public void flush() {
boolean hasErrors = hasErrors();
for (String path : sortedKeys()) {
for (Message message : groupedMessages.get(path)) {
List<CompilerMessageLocation> sortedKeys =
CollectionsKt.sortedWith(groupedMessages.keySet(), Comparator.nullsFirst(CompilerMessageLocationComparator.INSTANCE));
for (CompilerMessageLocation location : sortedKeys) {
for (Message message : groupedMessages.get(location)) {
if (!hasErrors || message.severity.isError() || message.severity == CompilerMessageSeverity.STRONG_WARNING) {
delegate.report(message.severity, message.message, message.location);
}
@@ -71,12 +76,28 @@ public class GroupingMessageCollector implements MessageCollector {
groupedMessages.clear();
}
@NotNull
private Collection<String> sortedKeys() {
// ensure that messages with no location i.e. perf, incomplete hierarchy are always reported first
List<String> sortedKeys = new ArrayList<>(groupedMessages.keySet());
sortedKeys.sort(Comparator.nullsFirst(Comparator.naturalOrder()));
return sortedKeys;
private static class CompilerMessageLocationComparator implements Comparator<CompilerMessageLocation> {
public static final CompilerMessageLocationComparator INSTANCE = new CompilerMessageLocationComparator();
// First, output all messages without any location information. Then, only those with the file path.
// Next, all messages with the file path and the line number. Next, all messages with file path, line number and column number.
//
// Example of the order of compiler messages:
//
// error: bad classpath
// foo.kt: error: bad file
// foo.kt:42: error: bad line
// foo.kt:42:43: error: bad character
@Override
public int compare(CompilerMessageLocation o1, CompilerMessageLocation o2) {
if (o1.getColumn() == -1 && o2.getColumn() != -1) return -1;
if (o1.getColumn() != -1 && o2.getColumn() == -1) return 1;
if (o1.getLine() == -1 && o2.getLine() != -1) return -1;
if (o1.getLine() != -1 && o2.getLine() == -1) return 1;
return o1.getPath().compareTo(o2.getPath());
}
}
private static class Message {