| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 return hideWarnings; | 93 return hideWarnings; |
| 101 } | 94 } |
| 102 return unhandled("$severity", "isHidden", -1, null); | 95 return unhandled("$severity", "isHidden", -1, null); |
| 103 } | 96 } |
| 104 | 97 |
| 105 /// Are problems of [severity] fatal? That is, should the compiler terminate | 98 /// Are problems of [severity] fatal? That is, should the compiler terminate |
| 106 /// immediately? | 99 /// immediately? |
| 107 bool isFatal(Severity severity) { | 100 bool isFatal(Severity severity) { |
| 108 switch (severity) { | 101 switch (severity) { |
| 109 case Severity.error: | 102 case Severity.error: |
| 110 return CompilerContext.current.options.errorsAreFatal; | 103 return CompilerContext.current.options.throwOnErrors; |
| 111 | 104 |
| 112 case Severity.internalProblem: | 105 case Severity.internalProblem: |
| 113 return true; | 106 return true; |
| 114 | 107 |
| 115 case Severity.nit: | 108 case Severity.nit: |
| 116 return CompilerContext.current.options.nitsAreFatal; | 109 return CompilerContext.current.options.throwOnNits; |
| 117 | 110 |
| 118 case Severity.warning: | 111 case Severity.warning: |
| 119 return CompilerContext.current.options.warningsAreFatal; | 112 return CompilerContext.current.options.throwOnWarnings; |
| 120 } | 113 } |
| 121 return unhandled("$severity", "isFatal", -1, null); | 114 return unhandled("$severity", "isFatal", -1, null); |
| 122 } | 115 } |
| 123 | 116 |
| 124 /// Convert [severity] to a name that can be used to prefix a message. | 117 /// Convert [severity] to a name that can be used to prefix a message. |
| 125 String severityName(Severity severity, {bool capitalized: false}) { | 118 String severityName(Severity severity, {bool capitalized: false}) { |
| 126 switch (severity) { | 119 switch (severity) { |
| 127 case Severity.error: | 120 case Severity.error: |
| 128 return capitalized ? "Error" : "error"; | 121 return capitalized ? "Error" : "error"; |
| 129 | 122 |
| 130 case Severity.internalProblem: | 123 case Severity.internalProblem: |
| 131 return capitalized ? "Internal problem" : "internal problem"; | 124 return capitalized ? "Internal problem" : "internal problem"; |
| 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) { |
| 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); |
| 152 // TODO(sigmund,ahe): ensure there is no circularity when InputError is |
| 153 // handled. |
| 147 throw new deprecated_InputError(uri, charOffset, | 154 throw new deprecated_InputError(uri, charOffset, |
| 148 "Compilation aborted due to fatal ${severityName(severity)}."); | 155 "Compilation aborted due to fatal ${severityName(severity)}."); |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 | 158 |
| 152 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws | 159 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws |
| 153 /// an exception if [severity] is fatal (see [isFatal]). | 160 /// an exception if [severity] is fatal (see [isFatal]). |
| 154 /// | 161 /// |
| 155 /// This method isn't intended to be called directly. Use | 162 /// This method isn't intended to be called directly. Use |
| 156 /// [CompilerContext.report] instead. | 163 /// [CompilerContext.report] instead. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 179 message.messageObject, severity, message.uri, message.charOffset); | 186 message.messageObject, severity, message.uri, message.charOffset); |
| 180 } | 187 } |
| 181 | 188 |
| 182 /// Formats [message] as described in [formatInternal]. | 189 /// Formats [message] as described in [formatInternal]. |
| 183 /// | 190 /// |
| 184 /// This method isn't intended to be called directly. Use | 191 /// This method isn't intended to be called directly. Use |
| 185 /// [CompilerContext.formatWithoutLocation] instead. | 192 /// [CompilerContext.formatWithoutLocation] instead. |
| 186 String formatWithoutLocation(Message message, Severity severity) { | 193 String formatWithoutLocation(Message message, Severity severity) { |
| 187 return formatInternal(message, severity, null, -1); | 194 return formatInternal(message, severity, null, -1); |
| 188 } | 195 } |
| OLD | NEW |