Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 1 | // Copyright (c) 2017, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package com.android.tools.apiusagesample; |
| 6 | |
| 7 | import com.android.tools.r8.Diagnostic; |
| 8 | import com.android.tools.r8.DiagnosticsHandler; |
Ian Zerny | b4ba44e | 2019-05-15 16:11:50 +0200 | [diff] [blame] | 9 | import com.android.tools.r8.errors.InterfaceDesugarMissingTypeDiagnostic; |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 10 | import com.android.tools.r8.origin.ArchiveEntryOrigin; |
| 11 | import com.android.tools.r8.origin.Origin; |
| 12 | import com.android.tools.r8.origin.PathOrigin; |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 13 | import com.android.tools.r8.position.Position; |
| 14 | import com.android.tools.r8.position.TextPosition; |
| 15 | import com.android.tools.r8.position.TextRange; |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 16 | import java.nio.file.Files; |
| 17 | import java.nio.file.Path; |
| 18 | |
| 19 | class D8DiagnosticsHandler implements DiagnosticsHandler { |
| 20 | |
| 21 | public D8DiagnosticsHandler() { |
| 22 | } |
| 23 | |
| 24 | public static Origin getOrigin(Path root, Path entry) { |
| 25 | if (Files.isRegularFile(root)) { |
| 26 | return new ArchiveEntryOrigin(entry.toString(), new PathOrigin(root)); |
| 27 | } else { |
| 28 | return new PathOrigin(root.resolve(entry.toString())); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public void error(Diagnostic error) { |
| 34 | convertToMessage(error); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void warning(Diagnostic warning) { |
Ian Zerny | b4ba44e | 2019-05-15 16:11:50 +0200 | [diff] [blame] | 39 | if (warning instanceof InterfaceDesugarMissingTypeDiagnostic) { |
| 40 | desugarInterfaceMethodInfo((InterfaceDesugarMissingTypeDiagnostic) warning); |
| 41 | } else { |
| 42 | convertToMessage(warning); |
| 43 | } |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public void info(Diagnostic info) { |
| 48 | convertToMessage(info); |
| 49 | } |
| 50 | |
Ian Zerny | b4ba44e | 2019-05-15 16:11:50 +0200 | [diff] [blame] | 51 | void desugarInterfaceMethodInfo(InterfaceDesugarMissingTypeDiagnostic info) { |
| 52 | System.out.println("desugar is missing: " + info.getMissingType().toString()); |
| 53 | System.out.println(" used from: " + info.getContextType().toString()); |
| 54 | convertToMessage(info); |
| 55 | } |
| 56 | |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 57 | protected void convertToMessage(Diagnostic diagnostic) { |
| 58 | String textMessage = diagnostic.getDiagnosticMessage(); |
| 59 | |
Yohann Roussel | ed2ecc5 | 2017-12-01 09:59:59 +0100 | [diff] [blame] | 60 | Origin origin = diagnostic.getOrigin(); |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 61 | Position positionInOrigin = diagnostic.getPosition(); |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 62 | String position; |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 63 | if (origin instanceof PathOrigin) { |
Yohann Roussel | d3f3b4d | 2018-01-05 11:32:03 +0100 | [diff] [blame] | 64 | Path originFile = ((PathOrigin) origin).getPath(); |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 65 | if (positionInOrigin instanceof TextRange) { |
| 66 | TextRange textRange = (TextRange) positionInOrigin; |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 67 | position = originFile + ": " |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 68 | + textRange.getStart().getLine() + "," + textRange.getStart().getColumn() |
| 69 | + " - " + textRange.getEnd().getLine() + "," + textRange.getEnd().getColumn(); |
| 70 | } else if (positionInOrigin instanceof TextPosition) { |
| 71 | TextPosition textPosition = (TextPosition) positionInOrigin; |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 72 | position = originFile + ": " |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 73 | + textPosition.getLine() + "," + textPosition.getColumn(); |
| 74 | } else { |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 75 | position = originFile.toString(); |
Yohann Roussel | 34eef83 | 2017-12-04 18:58:28 +0100 | [diff] [blame] | 76 | } |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 77 | } else if (origin.parent() instanceof PathOrigin) { |
Yohann Roussel | d3f3b4d | 2018-01-05 11:32:03 +0100 | [diff] [blame] | 78 | Path originFile = ((PathOrigin) origin.parent()).getPath(); |
Yohann Roussel | e280bdf | 2018-01-05 10:41:36 +0100 | [diff] [blame] | 79 | position = originFile.toString(); |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 80 | } else { |
| 81 | position = "UNKNOWN"; |
Yohann Roussel | ed2ecc5 | 2017-12-01 09:59:59 +0100 | [diff] [blame] | 82 | if (origin != Origin.unknown()) { |
| 83 | textMessage = origin.toString() + ": " + textMessage; |
Yohann Roussel | 078c994 | 2017-11-28 15:55:46 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | System.out.println(position + ": " + textMessage); |
| 88 | } |
| 89 | } |