| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 return !isVerbose; | 90 return !isVerbose; |
| 91 | 91 |
| 92 case Severity.warning: | 92 case Severity.warning: |
| 93 return hideWarnings; | 93 return hideWarnings; |
| 94 } | 94 } |
| 95 return unhandled("$severity", "isHidden", -1, null); | 95 return unhandled("$severity", "isHidden", -1, null); |
| 96 } | 96 } |
| 97 | 97 |
| 98 /// Are problems of [severity] fatal? That is, should the compiler terminate | 98 /// Are problems of [severity] fatal? That is, should the compiler terminate |
| 99 /// immediately? | 99 /// immediately? |
| 100 bool isFatal(Severity severity) { | 100 bool shouldThrowOn(Severity severity) { |
| 101 switch (severity) { | 101 switch (severity) { |
| 102 case Severity.error: | 102 case Severity.error: |
| 103 return CompilerContext.current.options.throwOnErrors; | 103 return CompilerContext.current.options.throwOnErrorsForDebugging; |
| 104 | 104 |
| 105 case Severity.internalProblem: | 105 case Severity.internalProblem: |
| 106 return true; | 106 return true; |
| 107 | 107 |
| 108 case Severity.nit: | 108 case Severity.nit: |
| 109 return CompilerContext.current.options.throwOnNits; | 109 return CompilerContext.current.options.throwOnNitsForDebugging; |
| 110 | 110 |
| 111 case Severity.warning: | 111 case Severity.warning: |
| 112 return CompilerContext.current.options.throwOnWarnings; | 112 return CompilerContext.current.options.throwOnWarningsForDebugging; |
| 113 } | 113 } |
| 114 return unhandled("$severity", "isFatal", -1, null); | 114 return unhandled("$severity", "shouldThrowOn", -1, null); |
| 115 } | 115 } |
| 116 | 116 |
| 117 /// 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. |
| 118 String severityName(Severity severity, {bool capitalized: false}) { | 118 String severityName(Severity severity, {bool capitalized: false}) { |
| 119 switch (severity) { | 119 switch (severity) { |
| 120 case Severity.error: | 120 case Severity.error: |
| 121 return capitalized ? "Error" : "error"; | 121 return capitalized ? "Error" : "error"; |
| 122 | 122 |
| 123 case Severity.internalProblem: | 123 case Severity.internalProblem: |
| 124 return capitalized ? "Internal problem" : "internal problem"; | 124 return capitalized ? "Internal problem" : "internal problem"; |
| 125 | 125 |
| 126 case Severity.nit: | 126 case Severity.nit: |
| 127 return capitalized ? "Nit" : "nit"; | 127 return capitalized ? "Nit" : "nit"; |
| 128 | 128 |
| 129 case Severity.warning: | 129 case Severity.warning: |
| 130 return capitalized ? "Warning" : "warning"; | 130 return capitalized ? "Warning" : "warning"; |
| 131 } | 131 } |
| 132 return unhandled("$severity", "severityName", -1, null); | 132 return unhandled("$severity", "severityName", -1, null); |
| 133 } | 133 } |
| 134 | 134 |
| 135 /// Print a formatted message and throw when errors are treated as fatal. | 135 /// Print a formatted message and throw when errors are treated as fatal. |
| 136 /// Also set [exitCode] depending on the value of | 136 /// Also set [exitCode] depending on the value of |
| 137 /// `CompilerContext.current.options.setExitCodeOnProblem`. | 137 /// `CompilerContext.current.options.setExitCodeOnProblem`. |
| 138 void _printAndThrowIfFatal( | 138 void _printAndThrowIfDebugging( |
| 139 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 | 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 | 141 // formatting to embed the error in the program, then we probably don't want |
| 142 // to do it in format. | 142 // to do it in format. |
| 143 // Note: I also want to limit dependencies to dart:io for when we use the FE | 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 | 144 // outside of the VM. This default reporting is likely not going to be used in |
| 145 // that context, but the default formatter is. | 145 // that context, but the default formatter is. |
| 146 if (CompilerContext.current.options.setExitCodeOnProblem) { | 146 if (CompilerContext.current.options.setExitCodeOnProblem) { |
| 147 exitCode = 1; | 147 exitCode = 1; |
| 148 } | 148 } |
| 149 print(text); | 149 print(text); |
| 150 if (isFatal(severity)) { | 150 if (shouldThrowOn(severity)) { |
| 151 if (isVerbose) print(StackTrace.current); | 151 if (isVerbose) print(StackTrace.current); |
| 152 // TODO(sigmund,ahe): ensure there is no circularity when InputError is | 152 // TODO(sigmund,ahe): ensure there is no circularity when InputError is |
| 153 // handled. | 153 // handled. |
| 154 throw new deprecated_InputError(uri, charOffset, | 154 throw new deprecated_InputError(uri, charOffset, |
| 155 "Compilation aborted due to fatal ${severityName(severity)}."); | 155 "Compilation aborted due to fatal ${severityName(severity)}."); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws | 159 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws |
| 160 /// an exception if [severity] is fatal (see [isFatal]). | 160 /// an exception if [severity] is fatal (see [isFatal]). |
| 161 /// | 161 /// |
| 162 /// This method isn't intended to be called directly. Use | 162 /// This method isn't intended to be called directly. Use |
| 163 /// [CompilerContext.report] instead. | 163 /// [CompilerContext.report] instead. |
| 164 void report(LocatedMessage message, Severity severity) { | 164 void report(LocatedMessage message, Severity severity) { |
| 165 if (isHidden(severity)) return; | 165 if (isHidden(severity)) return; |
| 166 _printAndThrowIfFatal( | 166 _printAndThrowIfDebugging( |
| 167 format(message, severity), severity, message.uri, message.charOffset); | 167 format(message, severity), severity, message.uri, message.charOffset); |
| 168 } | 168 } |
| 169 | 169 |
| 170 /// Similar to [report]. | 170 /// Similar to [report]. |
| 171 /// | 171 /// |
| 172 /// This method isn't intended to be called directly. Use | 172 /// This method isn't intended to be called directly. Use |
| 173 /// [CompilerContext.reportWithoutLocation] instead. | 173 /// [CompilerContext.reportWithoutLocation] instead. |
| 174 void reportWithoutLocation(Message message, Severity severity) { | 174 void reportWithoutLocation(Message message, Severity severity) { |
| 175 if (isHidden(severity)) return; | 175 if (isHidden(severity)) return; |
| 176 _printAndThrowIfFatal( | 176 _printAndThrowIfDebugging( |
| 177 formatWithoutLocation(message, severity), severity, null, -1); | 177 formatWithoutLocation(message, severity), severity, null, -1); |
| 178 } | 178 } |
| 179 | 179 |
| 180 /// Formats [message] as described in [formatInternal]. | 180 /// Formats [message] as described in [formatInternal]. |
| 181 /// | 181 /// |
| 182 /// This method isn't intended to be called directly. Use | 182 /// This method isn't intended to be called directly. Use |
| 183 /// [CompilerContext.format] instead. | 183 /// [CompilerContext.format] instead. |
| 184 String format(LocatedMessage message, Severity severity) { | 184 String format(LocatedMessage message, Severity severity) { |
| 185 return formatInternal( | 185 return formatInternal( |
| 186 message.messageObject, severity, message.uri, message.charOffset); | 186 message.messageObject, severity, message.uri, message.charOffset); |
| 187 } | 187 } |
| 188 | 188 |
| 189 /// Formats [message] as described in [formatInternal]. | 189 /// Formats [message] as described in [formatInternal]. |
| 190 /// | 190 /// |
| 191 /// This method isn't intended to be called directly. Use | 191 /// This method isn't intended to be called directly. Use |
| 192 /// [CompilerContext.formatWithoutLocation] instead. | 192 /// [CompilerContext.formatWithoutLocation] instead. |
| 193 String formatWithoutLocation(Message message, Severity severity) { | 193 String formatWithoutLocation(Message message, Severity severity) { |
| 194 return formatInternal(message, severity, null, -1); | 194 return formatInternal(message, severity, null, -1); |
| 195 } | 195 } |
| OLD | NEW |