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 |
(...skipping 16 matching lines...) Expand all Loading... | |
27 import 'severity.dart' show Severity; | 27 import 'severity.dart' show Severity; |
28 | 28 |
29 import 'util/relativize.dart' show relativizeUri; | 29 import 'util/relativize.dart' show relativizeUri; |
30 | 30 |
31 const bool hideWarnings = false; | 31 const bool hideWarnings = false; |
32 | 32 |
33 /// Formats [message] as a string that is suitable for output from a | 33 /// Formats [message] as a string that is suitable for output from a |
34 /// command-line tool. This includes source snippets and different colors based | 34 /// command-line tool. This includes source snippets and different colors based |
35 /// on [severity]. | 35 /// on [severity]. |
36 /// | 36 /// |
37 /// It is a assumed that a formatted message is reported to a user, so | |
38 /// [exitCode] is also set depending on the value of | |
39 /// `CompilerContext.current.options.setExitCodeOnProblem`. | |
40 /// | |
41 /// This is shared implementation used by methods below, and isn't intended to | 37 /// This is shared implementation used by methods below, and isn't intended to |
42 /// be called directly. | 38 /// be called directly. |
43 String formatInternal(Message message, Severity severity, Uri uri, int offset) { | 39 String formatInternal(Message message, Severity severity, Uri uri, int offset) { |
44 if (CompilerContext.current.options.setExitCodeOnProblem) { | |
45 exitCode = 1; | |
46 } | |
47 String text = | 40 String text = |
48 "${severityName(severity, capitalized: true)}: ${message.message}"; | 41 "${severityName(severity, capitalized: true)}: ${message.message}"; |
49 if (message.tip != null) { | 42 if (message.tip != null) { |
50 text += "\n${message.tip}"; | 43 text += "\n${message.tip}"; |
51 } | 44 } |
52 if (CompilerContext.enableColors) { | 45 if (CompilerContext.enableColors) { |
53 switch (severity) { | 46 switch (severity) { |
54 case Severity.error: | 47 case Severity.error: |
55 case Severity.internalProblem: | 48 case Severity.internalProblem: |
56 text = red(text); | 49 text = red(text); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 | 125 |
133 case Severity.nit: | 126 case Severity.nit: |
134 return capitalized ? "Nit" : "nit"; | 127 return capitalized ? "Nit" : "nit"; |
135 | 128 |
136 case Severity.warning: | 129 case Severity.warning: |
137 return capitalized ? "Warning" : "warning"; | 130 return capitalized ? "Warning" : "warning"; |
138 } | 131 } |
139 return unhandled("$severity", "severityName", -1, null); | 132 return unhandled("$severity", "severityName", -1, null); |
140 } | 133 } |
141 | 134 |
135 /// Print a formatted message and throw when errors are treated as fatal. | |
136 /// Also set [exitCode] depending on the value of | |
137 /// `CompilerContext.current.options.setExitCodeOnProblem`. | |
142 void _printAndThrowIfFatal( | 138 void _printAndThrowIfFatal( |
143 String text, Severity severity, Uri uri, int charOffset) { | 139 String text, Severity severity, Uri uri, int charOffset) { |
140 // I believe we should only set it if we are reporting something, if we are | |
141 // formatting to embed the error in the program, then we probably don't want | |
142 // to do it in format. | |
143 // Note: I also want to limit dependencies to dart:io for when we use the FE | |
144 // outside of the VM. This default reporting is likely not going to be used in | |
145 // that context, but the default formatter is. | |
146 if (CompilerContext.current.options.setExitCodeOnProblem) { | |
Siggi Cherem (dart-lang)
2017/07/18 00:11:07
moved here because I believe we'd like to use `for
ahe
2017/07/18 16:54:37
I think this makes sense.
| |
147 exitCode = 1; | |
148 } | |
144 print(text); | 149 print(text); |
145 if (isFatal(severity)) { | 150 if (isFatal(severity)) { |
146 if (isVerbose) print(StackTrace.current); | 151 if (isVerbose) print(StackTrace.current); |
147 throw new deprecated_InputError(uri, charOffset, | 152 throw new deprecated_InputError(uri, charOffset, |
148 "Compilation aborted due to fatal ${severityName(severity)}."); | 153 "Compilation aborted due to fatal ${severityName(severity)}."); |
ahe
2017/07/18 16:54:37
FYI: When this InputError is handled, we end up ba
Siggi Cherem (dart-lang)
2017/07/18 22:50:43
Done. Added TODO
| |
149 } | 154 } |
150 } | 155 } |
151 | 156 |
152 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws | 157 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws |
153 /// an exception if [severity] is fatal (see [isFatal]). | 158 /// an exception if [severity] is fatal (see [isFatal]). |
154 /// | 159 /// |
155 /// This method isn't intended to be called directly. Use | 160 /// This method isn't intended to be called directly. Use |
156 /// [CompilerContext.report] instead. | 161 /// [CompilerContext.report] instead. |
157 void report(LocatedMessage message, Severity severity) { | 162 void report(LocatedMessage message, Severity severity) { |
158 if (isHidden(severity)) return; | 163 if (isHidden(severity)) return; |
(...skipping 20 matching lines...) Expand all Loading... | |
179 message.messageObject, severity, message.uri, message.charOffset); | 184 message.messageObject, severity, message.uri, message.charOffset); |
180 } | 185 } |
181 | 186 |
182 /// Formats [message] as described in [formatInternal]. | 187 /// Formats [message] as described in [formatInternal]. |
183 /// | 188 /// |
184 /// This method isn't intended to be called directly. Use | 189 /// This method isn't intended to be called directly. Use |
185 /// [CompilerContext.formatWithoutLocation] instead. | 190 /// [CompilerContext.formatWithoutLocation] instead. |
186 String formatWithoutLocation(Message message, Severity severity) { | 191 String formatWithoutLocation(Message message, Severity severity) { |
187 return formatInternal(message, severity, null, -1); | 192 return formatInternal(message, severity, null, -1); |
188 } | 193 } |
OLD | NEW |