| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer_cli.src.analyzer_impl; | 5 library analyzer_cli.src.analyzer_impl; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:analyzer/dart/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| 11 import 'package:analyzer/source/error_processor.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 13 import 'package:analyzer/src/generated/java_engine.dart'; | 14 import 'package:analyzer/src/generated/java_engine.dart'; |
| 14 import 'package:analyzer/src/generated/java_io.dart'; | 15 import 'package:analyzer/src/generated/java_io.dart'; |
| 15 import 'package:analyzer/src/generated/sdk_io.dart'; | 16 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/source_io.dart'; | 18 import 'package:analyzer/src/generated/source_io.dart'; |
| 18 import 'package:analyzer/src/generated/utilities_general.dart'; | 19 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 19 import 'package:analyzer_cli/src/driver.dart'; | 20 import 'package:analyzer_cli/src/driver.dart'; |
| 20 import 'package:analyzer_cli/src/error_formatter.dart'; | 21 import 'package:analyzer_cli/src/error_formatter.dart'; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 /// specified the "--package-warnings" option. | 53 /// specified the "--package-warnings" option. |
| 53 String _selfPackageName; | 54 String _selfPackageName; |
| 54 | 55 |
| 55 AnalyzerImpl(this.context, this.librarySource, this.options, this.startTime); | 56 AnalyzerImpl(this.context, this.librarySource, this.options, this.startTime); |
| 56 | 57 |
| 57 /// Returns the maximal [ErrorSeverity] of the recorded errors. | 58 /// Returns the maximal [ErrorSeverity] of the recorded errors. |
| 58 ErrorSeverity get maxErrorSeverity { | 59 ErrorSeverity get maxErrorSeverity { |
| 59 var status = ErrorSeverity.NONE; | 60 var status = ErrorSeverity.NONE; |
| 60 for (AnalysisErrorInfo errorInfo in errorInfos) { | 61 for (AnalysisErrorInfo errorInfo in errorInfos) { |
| 61 for (AnalysisError error in errorInfo.errors) { | 62 for (AnalysisError error in errorInfo.errors) { |
| 62 if (!_isDesiredError(error)) { | 63 if (_processError(error) == null) { |
| 63 continue; | 64 continue; |
| 64 } | 65 } |
| 65 var severity = computeSeverity(error, options); | 66 var severity = computeSeverity(error, options); |
| 66 status = status.max(severity); | 67 status = status.max(severity); |
| 67 } | 68 } |
| 68 } | 69 } |
| 69 return status; | 70 return status; |
| 70 } | 71 } |
| 71 | 72 |
| 72 void addCompilationUnitSource(CompilationUnitElement unit, | 73 void addCompilationUnitSource(CompilationUnitElement unit, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 169 } |
| 169 | 170 |
| 170 // Compute max severity and set exitCode. | 171 // Compute max severity and set exitCode. |
| 171 ErrorSeverity status = maxErrorSeverity; | 172 ErrorSeverity status = maxErrorSeverity; |
| 172 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { | 173 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 173 status = ErrorSeverity.ERROR; | 174 status = ErrorSeverity.ERROR; |
| 174 } | 175 } |
| 175 return status; | 176 return status; |
| 176 } | 177 } |
| 177 | 178 |
| 178 bool _isDesiredError(AnalysisError error) { | |
| 179 if (error.errorCode.type == ErrorType.TODO) { | |
| 180 return false; | |
| 181 } | |
| 182 if (computeSeverity(error, options) == ErrorSeverity.INFO && | |
| 183 options.disableHints) { | |
| 184 return false; | |
| 185 } | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 /// Determine whether the given URI refers to a package other than the package | 179 /// Determine whether the given URI refers to a package other than the package |
| 190 /// being analyzed. | 180 /// being analyzed. |
| 191 bool _isOtherPackage(Uri uri) { | 181 bool _isOtherPackage(Uri uri) { |
| 192 if (uri.scheme != 'package') { | 182 if (uri.scheme != 'package') { |
| 193 return false; | 183 return false; |
| 194 } | 184 } |
| 195 if (_selfPackageName != null && | 185 if (_selfPackageName != null && |
| 196 uri.pathSegments.length > 0 && | 186 uri.pathSegments.length > 0 && |
| 197 uri.pathSegments[0] == _selfPackageName) { | 187 uri.pathSegments[0] == _selfPackageName) { |
| 198 return false; | 188 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 218 _printErrorsAndPerf() { | 208 _printErrorsAndPerf() { |
| 219 // The following is a hack. We currently print out to stderr to ensure that | 209 // The following is a hack. We currently print out to stderr to ensure that |
| 220 // when in batch mode we print to stderr, this is because the prints from | 210 // when in batch mode we print to stderr, this is because the prints from |
| 221 // batch are made to stderr. The reason that options.shouldBatch isn't used | 211 // batch are made to stderr. The reason that options.shouldBatch isn't used |
| 222 // is because when the argument flags are constructed in BatchRunner and | 212 // is because when the argument flags are constructed in BatchRunner and |
| 223 // passed in from batch mode which removes the batch flag to prevent the | 213 // passed in from batch mode which removes the batch flag to prevent the |
| 224 // "cannot have the batch flag and source file" error message. | 214 // "cannot have the batch flag and source file" error message. |
| 225 StringSink sink = options.machineFormat ? errorSink : outSink; | 215 StringSink sink = options.machineFormat ? errorSink : outSink; |
| 226 | 216 |
| 227 // Print errors. | 217 // Print errors. |
| 228 ErrorFormatter formatter = | 218 ErrorFormatter formatter = new ErrorFormatter(sink, options, _processError); |
| 229 new ErrorFormatter(sink, options, _isDesiredError); | |
| 230 formatter.formatErrors(errorInfos); | 219 formatter.formatErrors(errorInfos); |
| 231 } | 220 } |
| 232 | 221 |
| 222 /// Check various configuration options to get a desired severity for this |
| 223 /// [error] (or `null` if it's to be suppressed). |
| 224 ProcessedSeverity _processError(AnalysisError error) { |
| 225 ErrorSeverity severity = computeSeverity(error, options, context); |
| 226 bool isOverridden = false; |
| 227 |
| 228 // First check for a filter. |
| 229 if (severity == null) { |
| 230 // Null severity means the error has been explicitly ignored. |
| 231 return null; |
| 232 } else { |
| 233 isOverridden = true; |
| 234 } |
| 235 |
| 236 // If not overridden, some "natural" severities get globally filtered. |
| 237 if (!isOverridden) { |
| 238 // Check for global hint filtering. |
| 239 if (severity == ErrorSeverity.INFO && options.disableHints) { |
| 240 return null; |
| 241 } |
| 242 |
| 243 // Skip TODOs. |
| 244 if (severity == ErrorType.TODO) { |
| 245 return null; |
| 246 } |
| 247 } |
| 248 |
| 249 return new ProcessedSeverity(severity, isOverridden); |
| 250 } |
| 251 |
| 233 /// Compute the severity of the error; however: | 252 /// Compute the severity of the error; however: |
| 234 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode | 253 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode |
| 235 /// compile time errors to a severity of [ErrorSeverity.INFO]. | 254 /// compile time errors to a severity of [ErrorSeverity.INFO]. |
| 236 /// * if [options.hintsAreFatal] is true, escalate hints to errors. | 255 /// * if [options.hintsAreFatal] is true, escalate hints to errors. |
| 237 static ErrorSeverity computeSeverity( | 256 static ErrorSeverity computeSeverity( |
| 238 AnalysisError error, CommandLineOptions options) { | 257 AnalysisError error, CommandLineOptions options, |
| 258 [AnalysisContext context]) { |
| 259 if (context != null) { |
| 260 ErrorProcessor processor = ErrorProcessor.getProcessor(context, error); |
| 261 // If there is a processor for this error, defer to it. |
| 262 if (processor != null) { |
| 263 return processor.severity; |
| 264 } |
| 265 } |
| 266 |
| 239 if (!options.enableTypeChecks && | 267 if (!options.enableTypeChecks && |
| 240 error.errorCode.type == ErrorType.CHECKED_MODE_COMPILE_TIME_ERROR) { | 268 error.errorCode.type == ErrorType.CHECKED_MODE_COMPILE_TIME_ERROR) { |
| 241 return ErrorSeverity.INFO; | 269 return ErrorSeverity.INFO; |
| 242 } | 270 } |
| 271 |
| 243 if (options.hintsAreFatal && error.errorCode is HintCode) { | 272 if (options.hintsAreFatal && error.errorCode is HintCode) { |
| 244 return ErrorSeverity.ERROR; | 273 return ErrorSeverity.ERROR; |
| 245 } | 274 } |
| 275 |
| 246 return error.errorCode.errorSeverity; | 276 return error.errorCode.errorSeverity; |
| 247 } | 277 } |
| 248 | 278 |
| 249 /// Return the corresponding package directory or `null` if none is found. | 279 /// Return the corresponding package directory or `null` if none is found. |
| 250 static JavaFile getPackageDirectoryFor(JavaFile sourceFile) { | 280 static JavaFile getPackageDirectoryFor(JavaFile sourceFile) { |
| 251 // We are going to ask parent file, so get absolute path. | 281 // We are going to ask parent file, so get absolute path. |
| 252 sourceFile = sourceFile.getAbsoluteFile(); | 282 sourceFile = sourceFile.getAbsoluteFile(); |
| 253 // Look in the containing directories. | 283 // Look in the containing directories. |
| 254 JavaFile dir = sourceFile.getParentFile(); | 284 JavaFile dir = sourceFile.getParentFile(); |
| 255 while (dir != null) { | 285 while (dir != null) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 278 } | 308 } |
| 279 | 309 |
| 280 @override | 310 @override |
| 281 void logInformation(String message, [CaughtException exception]) { | 311 void logInformation(String message, [CaughtException exception]) { |
| 282 outSink.writeln(message); | 312 outSink.writeln(message); |
| 283 if (exception != null) { | 313 if (exception != null) { |
| 284 outSink.writeln(exception); | 314 outSink.writeln(exception); |
| 285 } | 315 } |
| 286 } | 316 } |
| 287 } | 317 } |
| OLD | NEW |