OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Provides a default implementation of the report and format methods of |
| 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 |
| 8 /// [CompilerContext]. |
| 9 library fasta.command_line_reporting; |
| 10 |
| 11 import 'dart:io' show exitCode; |
| 12 |
| 13 import 'package:kernel/ast.dart' show Location; |
| 14 |
| 15 import 'colors.dart' show cyan, magenta, red; |
| 16 |
| 17 import 'compiler_context.dart' show CompilerContext; |
| 18 |
| 19 import 'deprecated_problems.dart' show deprecated_InputError; |
| 20 |
| 21 import 'fasta_codes.dart' show LocatedMessage, Message; |
| 22 |
| 23 import 'messages.dart' show getLocation, getSourceLine, isVerbose; |
| 24 |
| 25 import 'problems.dart' show unhandled; |
| 26 |
| 27 import 'severity.dart' show Severity; |
| 28 |
| 29 import 'util/relativize.dart' show relativizeUri; |
| 30 |
| 31 const bool hideWarnings = false; |
| 32 |
| 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 |
| 35 /// on [severity]. |
| 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 |
| 42 /// be called directly. |
| 43 String formatInternal(Message message, Severity severity, Uri uri, int offset) { |
| 44 if (CompilerContext.current.options.setExitCodeOnProblem) { |
| 45 exitCode = 1; |
| 46 } |
| 47 String text = |
| 48 "${severityName(severity, capitalized: true)}: ${message.message}"; |
| 49 if (message.tip != null) { |
| 50 text += "\n${message.tip}"; |
| 51 } |
| 52 if (CompilerContext.enableColors) { |
| 53 switch (severity) { |
| 54 case Severity.error: |
| 55 case Severity.internalProblem: |
| 56 text = red(text); |
| 57 break; |
| 58 |
| 59 case Severity.nit: |
| 60 text = cyan(text); |
| 61 break; |
| 62 |
| 63 case Severity.warning: |
| 64 text = magenta(text); |
| 65 break; |
| 66 } |
| 67 } |
| 68 |
| 69 if (uri != null) { |
| 70 String path = relativizeUri(uri); |
| 71 Location location = offset == -1 ? null : getLocation(path, offset); |
| 72 String sourceLine = getSourceLine(location); |
| 73 if (sourceLine == null) { |
| 74 sourceLine = ""; |
| 75 } else { |
| 76 // TODO(ahe): We only print a single point in the source line as we don't |
| 77 // have end positions. Also, we should be able to use package:source_span |
| 78 // to produce this. |
| 79 sourceLine = "\n$sourceLine\n" |
| 80 "${' ' * (location.column - 1)}^"; |
| 81 } |
| 82 String position = location?.toString() ?? path; |
| 83 return "$position: $text$sourceLine"; |
| 84 } else { |
| 85 return text; |
| 86 } |
| 87 } |
| 88 |
| 89 /// Are problems of [severity] suppressed? |
| 90 bool isHidden(Severity severity) { |
| 91 switch (severity) { |
| 92 case Severity.error: |
| 93 case Severity.internalProblem: |
| 94 return false; |
| 95 |
| 96 case Severity.nit: |
| 97 return !isVerbose; |
| 98 |
| 99 case Severity.warning: |
| 100 return hideWarnings; |
| 101 } |
| 102 return unhandled("$severity", "isHidden", -1, null); |
| 103 } |
| 104 |
| 105 /// Are problems of [severity] fatal? That is, should the compiler terminate |
| 106 /// immediately? |
| 107 bool isFatal(Severity severity) { |
| 108 switch (severity) { |
| 109 case Severity.error: |
| 110 return CompilerContext.current.options.errorsAreFatal; |
| 111 |
| 112 case Severity.internalProblem: |
| 113 return true; |
| 114 |
| 115 case Severity.nit: |
| 116 return CompilerContext.current.options.nitsAreFatal; |
| 117 |
| 118 case Severity.warning: |
| 119 return CompilerContext.current.options.warningsAreFatal; |
| 120 } |
| 121 return unhandled("$severity", "isFatal", -1, null); |
| 122 } |
| 123 |
| 124 /// Convert [severity] to a name that can be used to prefix a message. |
| 125 String severityName(Severity severity, {bool capitalized: false}) { |
| 126 switch (severity) { |
| 127 case Severity.error: |
| 128 return capitalized ? "Error" : "error"; |
| 129 |
| 130 case Severity.internalProblem: |
| 131 return capitalized ? "Internal problem" : "internal problem"; |
| 132 |
| 133 case Severity.nit: |
| 134 return capitalized ? "Nit" : "nit"; |
| 135 |
| 136 case Severity.warning: |
| 137 return capitalized ? "Warning" : "warning"; |
| 138 } |
| 139 return unhandled("$severity", "severityName", -1, null); |
| 140 } |
| 141 |
| 142 void _printAndThrowIfFatal( |
| 143 String text, Severity severity, Uri uri, int charOffset) { |
| 144 print(text); |
| 145 if (isFatal(severity)) { |
| 146 if (isVerbose) print(StackTrace.current); |
| 147 throw new deprecated_InputError(uri, charOffset, |
| 148 "Compilation aborted due to fatal ${severityName(severity)}."); |
| 149 } |
| 150 } |
| 151 |
| 152 /// Report [message] unless [severity] is suppressed (see [isHidden]). Throws |
| 153 /// an exception if [severity] is fatal (see [isFatal]). |
| 154 /// |
| 155 /// This method isn't intended to be called directly. Use |
| 156 /// [CompilerContext.report] instead. |
| 157 void report(LocatedMessage message, Severity severity) { |
| 158 if (isHidden(severity)) return; |
| 159 _printAndThrowIfFatal( |
| 160 format(message, severity), severity, message.uri, message.charOffset); |
| 161 } |
| 162 |
| 163 /// Similar to [report]. |
| 164 /// |
| 165 /// This method isn't intended to be called directly. Use |
| 166 /// [CompilerContext.reportWithoutLocation] instead. |
| 167 void reportWithoutLocation(Message message, Severity severity) { |
| 168 if (isHidden(severity)) return; |
| 169 _printAndThrowIfFatal( |
| 170 formatWithoutLocation(message, severity), severity, null, -1); |
| 171 } |
| 172 |
| 173 /// Formats [message] as described in [formatInternal]. |
| 174 /// |
| 175 /// This method isn't intended to be called directly. Use |
| 176 /// [CompilerContext.format] instead. |
| 177 String format(LocatedMessage message, Severity severity) { |
| 178 return formatInternal( |
| 179 message.messageObject, severity, message.uri, message.charOffset); |
| 180 } |
| 181 |
| 182 /// Formats [message] as described in [formatInternal]. |
| 183 /// |
| 184 /// This method isn't intended to be called directly. Use |
| 185 /// [CompilerContext.formatWithoutLocation] instead. |
| 186 String formatWithoutLocation(Message message, Severity severity) { |
| 187 return formatInternal(message, severity, null, -1); |
| 188 } |
OLD | NEW |