| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library fasta.messages; | 5 library fasta.messages; |
| 6 | 6 |
| 7 import 'dart:io' show exitCode; | |
| 8 | |
| 9 import 'package:kernel/ast.dart' show Library, Location, Program, TreeNode; | 7 import 'package:kernel/ast.dart' show Library, Location, Program, TreeNode; |
| 10 | 8 |
| 11 import 'util/relativize.dart' show relativizeUri; | 9 import 'util/relativize.dart' show relativizeUri; |
| 12 | 10 |
| 13 import 'compiler_context.dart' show CompilerContext; | 11 import 'compiler_context.dart' show CompilerContext; |
| 14 | 12 |
| 15 import 'deprecated_problems.dart' show deprecated_InputError; | 13 import 'fasta_codes.dart' show Message; |
| 16 | 14 |
| 17 import 'colors.dart' show cyan, magenta; | 15 import 'severity.dart' show Severity; |
| 18 | |
| 19 import 'fasta_codes.dart' show Message; | |
| 20 | 16 |
| 21 export 'fasta_codes.dart'; | 17 export 'fasta_codes.dart'; |
| 22 | 18 |
| 23 const bool hideWarnings = false; | |
| 24 | |
| 25 bool get errorsAreFatal => CompilerContext.current.options.errorsAreFatal; | |
| 26 | |
| 27 bool get nitsAreFatal => CompilerContext.current.options.nitsAreFatal; | |
| 28 | |
| 29 bool get warningsAreFatal => CompilerContext.current.options.warningsAreFatal; | |
| 30 | |
| 31 bool get isVerbose => CompilerContext.current.options.verbose; | 19 bool get isVerbose => CompilerContext.current.options.verbose; |
| 32 | 20 |
| 33 bool get hideNits => !isVerbose; | |
| 34 | |
| 35 bool get setExitCodeOnProblem { | |
| 36 return CompilerContext.current.options.setExitCodeOnProblem; | |
| 37 } | |
| 38 | |
| 39 void warning(Message message, int charOffset, Uri uri) { | 21 void warning(Message message, int charOffset, Uri uri) { |
| 40 if (hideWarnings) return; | 22 CompilerContext.current |
| 41 print(deprecated_format( | 23 .report(message.withLocation(uri, charOffset), Severity.warning); |
| 42 uri, charOffset, colorWarning("Warning: ${message.message}"))); | |
| 43 if (warningsAreFatal) { | |
| 44 if (isVerbose) print(StackTrace.current); | |
| 45 throw new deprecated_InputError( | |
| 46 uri, charOffset, "Compilation aborted due to fatal warnings."); | |
| 47 } | |
| 48 } | 24 } |
| 49 | 25 |
| 50 void nit(Message message, int charOffset, Uri uri) { | 26 void nit(Message message, int charOffset, Uri uri) { |
| 51 if (hideNits) return; | 27 CompilerContext.current |
| 52 print( | 28 .report(message.withLocation(uri, charOffset), Severity.nit); |
| 53 deprecated_format(uri, charOffset, colorNit("Nit: ${message.message}"))); | |
| 54 if (nitsAreFatal) { | |
| 55 if (isVerbose) print(StackTrace.current); | |
| 56 throw new deprecated_InputError( | |
| 57 uri, charOffset, "Compilation aborted due to fatal nits."); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 String colorWarning(String message) { | |
| 62 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on | |
| 63 // Windows. | |
| 64 return magenta(message); | |
| 65 } | |
| 66 | |
| 67 String colorNit(String message) { | |
| 68 // TODO(ahe): Colors need to be optional. Doesn't work well in Emacs or on | |
| 69 // Windows. | |
| 70 return cyan(message); | |
| 71 } | |
| 72 | |
| 73 String deprecated_format(Uri uri, int charOffset, String message) { | |
| 74 if (setExitCodeOnProblem) { | |
| 75 exitCode = 1; | |
| 76 } | |
| 77 if (uri != null) { | |
| 78 String path = relativizeUri(uri); | |
| 79 Location location = charOffset == -1 ? null : getLocation(path, charOffset); | |
| 80 String sourceLine = getSourceLine(location); | |
| 81 if (sourceLine == null) { | |
| 82 sourceLine = ""; | |
| 83 } else { | |
| 84 sourceLine = "\n$sourceLine\n" | |
| 85 "${' ' * (location.column - 1)}^"; | |
| 86 } | |
| 87 String position = location?.toString() ?? path; | |
| 88 return "$position: $message$sourceLine"; | |
| 89 } else { | |
| 90 return message; | |
| 91 } | |
| 92 } | 29 } |
| 93 | 30 |
| 94 Location getLocation(String path, int charOffset) { | 31 Location getLocation(String path, int charOffset) { |
| 95 return CompilerContext.current.uriToSource[path] | 32 return CompilerContext.current.uriToSource[path] |
| 96 ?.getLocation(path, charOffset); | 33 ?.getLocation(path, charOffset); |
| 97 } | 34 } |
| 98 | 35 |
| 99 Location getLocationFromUri(Uri uri, int charOffset) { | 36 Location getLocationFromUri(Uri uri, int charOffset) { |
| 100 if (charOffset == -1) return null; | 37 if (charOffset == -1) return null; |
| 101 String path = relativizeUri(uri); | 38 String path = relativizeUri(uri); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 123 program.libraries.clear(); | 60 program.libraries.clear(); |
| 124 parent.parent = null; | 61 parent.parent = null; |
| 125 return result; | 62 return result; |
| 126 } else { | 63 } else { |
| 127 return null; | 64 return null; |
| 128 } | 65 } |
| 129 } else { | 66 } else { |
| 130 return node.location; | 67 return node.location; |
| 131 } | 68 } |
| 132 } | 69 } |
| OLD | NEW |