OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Provides a default implementation of the report and format methods of | 5 /// Provides a default implementation of the report and format methods of |
6 /// [CompilerContext] that are suitable for command-line tools. The methods in | 6 /// [CompilerContext] that are suitable for command-line tools. The methods in |
7 /// this library aren't intended to be called directly, instead, one should use | 7 /// this library aren't intended to be called directly, instead, one should use |
8 /// [CompilerContext]. | 8 /// [CompilerContext]. |
9 library fasta.command_line_reporting; | 9 library fasta.command_line_reporting; |
10 | 10 |
11 import 'dart:io' show exitCode; | 11 import 'dart:io' show exitCode; |
12 | 12 |
13 import 'package:kernel/ast.dart' show Location; | 13 import 'package:kernel/ast.dart' show Location; |
14 | 14 |
15 import 'colors.dart' show cyan, magenta, red; | 15 import 'colors.dart' show cyan, magenta, red; |
16 | 16 |
17 import 'compiler_context.dart' show CompilerContext; | 17 import 'compiler_context.dart' show CompilerContext; |
18 | 18 |
19 import 'deprecated_problems.dart' show deprecated_InputError; | 19 import 'deprecated_problems.dart' |
| 20 show Crash, deprecated_InputError, safeToString; |
20 | 21 |
21 import 'fasta_codes.dart' show LocatedMessage, Message; | 22 import 'fasta_codes.dart' show LocatedMessage, Message; |
22 | 23 |
23 import 'messages.dart' show getLocation, getSourceLine, isVerbose; | 24 import 'messages.dart' show getLocation, getSourceLine, isVerbose; |
24 | 25 |
25 import 'problems.dart' show unhandled; | 26 import 'problems.dart' show unhandled; |
26 | 27 |
27 import 'severity.dart' show Severity; | 28 import 'severity.dart' show Severity; |
28 | 29 |
29 import 'util/relativize.dart' show relativizeUri; | 30 import 'util/relativize.dart' show relativizeUri; |
30 | 31 |
31 const bool hideWarnings = false; | 32 const bool hideWarnings = false; |
32 | 33 |
33 /// Formats [message] as a string that is suitable for output from a | 34 /// Formats [message] as a string that is suitable for output from a |
34 /// command-line tool. This includes source snippets and different colors based | 35 /// command-line tool. This includes source snippets and different colors based |
35 /// on [severity]. | 36 /// on [severity]. |
36 /// | 37 /// |
37 /// This is shared implementation used by methods below, and isn't intended to | 38 /// This is shared implementation used by methods below, and isn't intended to |
38 /// be called directly. | 39 /// be called directly. |
39 String formatInternal(Message message, Severity severity, Uri uri, int offset) { | 40 String formatInternal(Message message, Severity severity, Uri uri, int offset) { |
40 String text = | 41 try { |
41 "${severityName(severity, capitalized: true)}: ${message.message}"; | 42 String text = |
42 if (message.tip != null) { | 43 "${severityName(severity, capitalized: true)}: ${message.message}"; |
43 text += "\n${message.tip}"; | 44 if (message.tip != null) { |
44 } | 45 text += "\n${message.tip}"; |
45 if (CompilerContext.enableColors) { | 46 } |
46 switch (severity) { | 47 if (CompilerContext.enableColors) { |
47 case Severity.error: | 48 switch (severity) { |
48 case Severity.internalProblem: | 49 case Severity.error: |
49 text = red(text); | 50 case Severity.internalProblem: |
50 break; | 51 text = red(text); |
| 52 break; |
51 | 53 |
52 case Severity.nit: | 54 case Severity.nit: |
53 text = cyan(text); | 55 text = cyan(text); |
54 break; | 56 break; |
55 | 57 |
56 case Severity.warning: | 58 case Severity.warning: |
57 text = magenta(text); | 59 text = magenta(text); |
58 break; | 60 break; |
| 61 } |
59 } | 62 } |
60 } | |
61 | 63 |
62 if (uri != null) { | 64 if (uri != null) { |
63 String path = relativizeUri(uri); | 65 String path = relativizeUri(uri); |
64 Location location = offset == -1 ? null : getLocation(path, offset); | 66 Location location = offset == -1 ? null : getLocation(path, offset); |
65 String sourceLine = getSourceLine(location); | 67 String sourceLine = getSourceLine(location); |
66 if (sourceLine == null) { | 68 if (sourceLine == null) { |
67 sourceLine = ""; | 69 sourceLine = ""; |
| 70 } else { |
| 71 // TODO(ahe): We only print a single point in the source line as we |
| 72 // don't have end positions. Also, we should be able to use |
| 73 // package:source_span to produce this. |
| 74 sourceLine = "\n$sourceLine\n" |
| 75 "${' ' * (location.column - 1)}^"; |
| 76 } |
| 77 String position = location?.toString() ?? path; |
| 78 return "$position: $text$sourceLine"; |
68 } else { | 79 } else { |
69 // TODO(ahe): We only print a single point in the source line as we don't | 80 return text; |
70 // have end positions. Also, we should be able to use package:source_span | |
71 // to produce this. | |
72 sourceLine = "\n$sourceLine\n" | |
73 "${' ' * (location.column - 1)}^"; | |
74 } | 81 } |
75 String position = location?.toString() ?? path; | 82 } catch (error, trace) { |
76 return "$position: $text$sourceLine"; | 83 print("Crash when formatting: " |
77 } else { | 84 "[${message.code.name}] ${safeToString(message.message)}\n" |
78 return text; | 85 "${safeToString(error)}\n" |
| 86 "$trace"); |
| 87 throw new Crash(uri, offset, error, trace); |
79 } | 88 } |
80 } | 89 } |
81 | 90 |
82 /// Are problems of [severity] suppressed? | 91 /// Are problems of [severity] suppressed? |
83 bool isHidden(Severity severity) { | 92 bool isHidden(Severity severity) { |
84 switch (severity) { | 93 switch (severity) { |
85 case Severity.error: | 94 case Severity.error: |
86 case Severity.internalProblem: | 95 case Severity.internalProblem: |
87 return false; | 96 return false; |
88 | 97 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 message.messageObject, severity, message.uri, message.charOffset); | 195 message.messageObject, severity, message.uri, message.charOffset); |
187 } | 196 } |
188 | 197 |
189 /// Formats [message] as described in [formatInternal]. | 198 /// Formats [message] as described in [formatInternal]. |
190 /// | 199 /// |
191 /// This method isn't intended to be called directly. Use | 200 /// This method isn't intended to be called directly. Use |
192 /// [CompilerContext.formatWithoutLocation] instead. | 201 /// [CompilerContext.formatWithoutLocation] instead. |
193 String formatWithoutLocation(Message message, Severity severity) { | 202 String formatWithoutLocation(Message message, Severity severity) { |
194 return formatInternal(message, severity, null, -1); | 203 return formatInternal(message, severity, null, -1); |
195 } | 204 } |
OLD | NEW |