blob: d523a530b3cc828a06cfd6819e145e1d09ce7d8c [file] [log] [blame]
Yohann Roussel078c9942017-11-28 15:55:46 +01001// 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
5package com.android.tools.apiusagesample;
6
7import com.android.tools.r8.Diagnostic;
8import com.android.tools.r8.DiagnosticsHandler;
Ian Zernyb4ba44e2019-05-15 16:11:50 +02009import com.android.tools.r8.errors.InterfaceDesugarMissingTypeDiagnostic;
Yohann Roussel078c9942017-11-28 15:55:46 +010010import com.android.tools.r8.origin.ArchiveEntryOrigin;
11import com.android.tools.r8.origin.Origin;
12import com.android.tools.r8.origin.PathOrigin;
Yohann Roussel34eef832017-12-04 18:58:28 +010013import com.android.tools.r8.position.Position;
14import com.android.tools.r8.position.TextPosition;
15import com.android.tools.r8.position.TextRange;
Yohann Roussel078c9942017-11-28 15:55:46 +010016import java.nio.file.Files;
17import java.nio.file.Path;
18
19class 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 Zernyb4ba44e2019-05-15 16:11:50 +020039 if (warning instanceof InterfaceDesugarMissingTypeDiagnostic) {
40 desugarInterfaceMethodInfo((InterfaceDesugarMissingTypeDiagnostic) warning);
41 } else {
42 convertToMessage(warning);
43 }
Yohann Roussel078c9942017-11-28 15:55:46 +010044 }
45
46 @Override
47 public void info(Diagnostic info) {
48 convertToMessage(info);
49 }
50
Ian Zernyb4ba44e2019-05-15 16:11:50 +020051 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 Roussel078c9942017-11-28 15:55:46 +010057 protected void convertToMessage(Diagnostic diagnostic) {
58 String textMessage = diagnostic.getDiagnosticMessage();
59
Yohann Rousseled2ecc52017-12-01 09:59:59 +010060 Origin origin = diagnostic.getOrigin();
Yohann Roussel34eef832017-12-04 18:58:28 +010061 Position positionInOrigin = diagnostic.getPosition();
Yohann Roussel078c9942017-11-28 15:55:46 +010062 String position;
Yohann Roussele280bdf2018-01-05 10:41:36 +010063 if (origin instanceof PathOrigin) {
Yohann Rousseld3f3b4d2018-01-05 11:32:03 +010064 Path originFile = ((PathOrigin) origin).getPath();
Yohann Roussel34eef832017-12-04 18:58:28 +010065 if (positionInOrigin instanceof TextRange) {
66 TextRange textRange = (TextRange) positionInOrigin;
Yohann Roussele280bdf2018-01-05 10:41:36 +010067 position = originFile + ": "
Yohann Roussel34eef832017-12-04 18:58:28 +010068 + 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 Roussele280bdf2018-01-05 10:41:36 +010072 position = originFile + ": "
Yohann Roussel34eef832017-12-04 18:58:28 +010073 + textPosition.getLine() + "," + textPosition.getColumn();
74 } else {
Yohann Roussele280bdf2018-01-05 10:41:36 +010075 position = originFile.toString();
Yohann Roussel34eef832017-12-04 18:58:28 +010076 }
Yohann Roussele280bdf2018-01-05 10:41:36 +010077 } else if (origin.parent() instanceof PathOrigin) {
Yohann Rousseld3f3b4d2018-01-05 11:32:03 +010078 Path originFile = ((PathOrigin) origin.parent()).getPath();
Yohann Roussele280bdf2018-01-05 10:41:36 +010079 position = originFile.toString();
Yohann Roussel078c9942017-11-28 15:55:46 +010080 } else {
81 position = "UNKNOWN";
Yohann Rousseled2ecc52017-12-01 09:59:59 +010082 if (origin != Origin.unknown()) {
83 textMessage = origin.toString() + ": " + textMessage;
Yohann Roussel078c9942017-11-28 15:55:46 +010084 }
85 }
86
87 System.out.println(position + ": " + textMessage);
88 }
89}