diff options
author | Anton Hansson <hansson@google.com> | 2023-10-10 09:51:20 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-10-10 09:51:20 +0000 |
commit | 431c58a99f0e47af6784d7ece7055b024e55e2a0 (patch) | |
tree | d64c048dd8344b08e20285a81a359e91200627d1 | |
parent | c3ea0845018534ecec54254485a495295bad8e33 (diff) | |
parent | 69f08ead8a09c7b83c3c6ed83af10854505f53a1 (diff) | |
download | doclava-431c58a99f0e47af6784d7ece7055b024e55e2a0.tar.gz |
Merge "Make baseline checks also match error message" into main am: c3fa779369 am: 69f08ead8a
Original change: https://android-review.googlesource.com/c/platform/external/doclava/+/2775198
Change-Id: I06d6394bbe3c668c4c378f24b97a586060277e8d
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | src/com/google/doclava/Doclava.java | 20 | ||||
-rw-r--r-- | src/com/google/doclava/Errors.java | 80 |
2 files changed, 68 insertions, 32 deletions
diff --git a/src/com/google/doclava/Doclava.java b/src/com/google/doclava/Doclava.java index 5ab2b65..101e45f 100644 --- a/src/com/google/doclava/Doclava.java +++ b/src/com/google/doclava/Doclava.java @@ -25,6 +25,7 @@ import com.google.clearsilver.jsilver.resourceloader.CompositeResourceLoader; import com.google.clearsilver.jsilver.resourceloader.FileSystemResourceLoader; import com.google.clearsilver.jsilver.resourceloader.ResourceLoader; import com.google.doclava.Errors.ErrorMessage; +import com.google.doclava.Errors.LintBaselineEntry; import com.google.doclava.javadoc.RootDocImpl; import com.sun.javadoc.ClassDoc; import com.sun.javadoc.Doc; @@ -1842,24 +1843,9 @@ public class Doclava implements Doclet { if (lintBaselineFile == null) { return true; } - - Function<String, SourcePositionInfo> mapper = (line) -> { - if (line.trim().length() == 0) { - return null; - } - String[] words = line.split(" "); - String[] cols = words[0].split(":"); - if (cols.length != 2) { - System.err.println("ignored baseline entry: " + line); - return null; - } - int row = Integer.parseInt(cols[1]); - return new SourcePositionInfo(cols[0], row, 0); - - }; try (BufferedReader reader = new BufferedReader(new FileReader(lintBaselineFile))) { - List<SourcePositionInfo> baseline = - reader.lines().map(mapper).filter(line -> line != null).collect(toList()); + List<LintBaselineEntry> baseline = + reader.lines().map(ErrorMessage::parse).filter(e -> e != null).collect(toList()); Errors.setLintBaseline(baseline); return true; } catch (IOException exception) { diff --git a/src/com/google/doclava/Errors.java b/src/com/google/doclava/Errors.java index 2a40716..83aac96 100644 --- a/src/com/google/doclava/Errors.java +++ b/src/com/google/doclava/Errors.java @@ -25,9 +25,21 @@ public class Errors { public static boolean hadError = false; private static boolean lintsAreErrors = false; private static boolean warningsAreErrors = false; - private static List<SourcePositionInfo> baseline; + private static List<LintBaselineEntry> baseline; private static TreeSet<ErrorMessage> allErrors = new TreeSet<ErrorMessage>(); + public static class LintBaselineEntry { + final String file; + final String message; + final int code; + + LintBaselineEntry(String file, String message, int code) { + this.file = file; + this.message = message; + this.code = code; + } + } + public static class ErrorMessage implements Comparable<ErrorMessage> { final int resolvedLevel; final Error error; @@ -48,6 +60,36 @@ public class Errors { return msg.compareTo(other.msg); } + public static LintBaselineEntry parse(String line) { + if (line.trim().length() == 0) { + return null; + } + // Format, per toString() below, is: + // file:row: lint: multi-word-message [code] + // We ignore the row number, so that edits don't invalidate baselines entries. + String[] words = line.split(" "); + + String[] fileAndRow = words[0].split(":"); + if (fileAndRow.length != 2) { + System.err.println("ignored baseline entry (no file & row): " + line); + return null; + } + String file = fileAndRow[0]; + + String lastWord = words[words.length - 1]; + if (lastWord.length() <= 3 || lastWord.charAt(0) != '[' + || lastWord.charAt(lastWord.length() - 1) != ']') { + System.err.println("ignored baseline entry (no error code): " + line); + return null; + } + int code = Integer.parseInt(lastWord.substring(1, lastWord.length() - 1)); + + int messageStart = (words[0] + " lint: ").length(); + int messageEnd = line.length() - lastWord.length() - 1; + String message = line.substring(messageStart, messageEnd); + return new LintBaselineEntry(file, message, code); + } + @Override public String toString() { StringBuilder res = new StringBuilder(); @@ -76,7 +118,7 @@ public class Errors { } return res.toString(); } - + public Error error() { return error; } @@ -109,7 +151,7 @@ public class Errors { int resolvedLevel = error.getLevel(); if (resolvedLevel == LINT && lintsAreErrors) { - resolvedLevel = isBaselined(where) ? LINT : ERROR; + resolvedLevel = isBaselined(error, where, text) ? LINT : ERROR; } if (resolvedLevel == WARNING && warningsAreErrors) { resolvedLevel = ERROR; @@ -125,7 +167,7 @@ public class Errors { hadError = true; } } - + public static void clearErrors() { hadError = false; allErrors.clear(); @@ -134,14 +176,14 @@ public class Errors { public static void printErrors() { printErrors(allErrors); } - + public static void printErrors(Set<ErrorMessage> errors) { for (ErrorMessage m : errors) { System.err.println(m.toString()); } System.err.flush(); } - + public static Set<ErrorMessage> getErrors() { return allErrors; } @@ -175,21 +217,29 @@ public class Errors { warningsAreErrors = val; } - public static void setLintBaseline(List<SourcePositionInfo> val) { + public static void setLintBaseline(List<LintBaselineEntry> val) { baseline = val; } - private static boolean isBaselined(SourcePositionInfo errorPosition) { + private static boolean isBaselined(Error error, SourcePositionInfo where, String text) { if (baseline == null) { return false; } - for (SourcePositionInfo baselinedPosition : baseline) { - if (errorPosition.file.endsWith(baselinedPosition.file)) { - // The line number information - // 1) seems to be wrong, at least for broken link/see tags - // 2) will lead to baselines not working when files are edited - // So we ignore that information and just allow all lint errors in the file - return true; + // Baselines entries have had newlines removed, so remove newlines in + // what we're trying to match with the baselines as well. + text = text.replace("\n", ""); + for (LintBaselineEntry entry : baseline) { + if (where.file.endsWith(entry.file) && error.code == entry.code) { + if (text.equals(entry.message)) { + return true; + } + if (entry.message.startsWith(text.replaceAll(" in android$", ""))) { + // This is a hack: when we generate a compatchanges.html, we generate + // a bunch of comments without context, placed naively the "android" package. + // TODO: Remove this once all the lint violations in ChangeId constant javadoc + // have been cleaned up. + return true; + } } } return false; |