| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:analyzer/dart/element/element.dart'; | 11 import 'package:analyzer/dart/element/element.dart'; |
| 12 import 'package:analyzer/error/error.dart'; | 12 import 'package:analyzer/error/error.dart'; |
| 13 import 'package:analyzer/exception/exception.dart'; | 13 import 'package:analyzer/exception/exception.dart'; |
| 14 import 'package:analyzer/source/error_processor.dart'; | 14 import 'package:analyzer/source/error_processor.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 15 import 'package:analyzer/src/error/codes.dart'; | 16 import 'package:analyzer/src/error/codes.dart'; |
| 16 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart' hide AnalysisResult; |
| 17 import 'package:analyzer/src/generated/java_io.dart'; | 18 import 'package:analyzer/src/generated/java_io.dart'; |
| 18 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 19 import 'package:analyzer/src/generated/source_io.dart'; | 20 import 'package:analyzer/src/generated/source_io.dart'; |
| 20 import 'package:analyzer/src/generated/utilities_general.dart'; | 21 import 'package:analyzer/src/generated/utilities_general.dart'; |
| 21 import 'package:analyzer_cli/src/driver.dart'; | 22 import 'package:analyzer_cli/src/driver.dart'; |
| 22 import 'package:analyzer_cli/src/error_formatter.dart'; | 23 import 'package:analyzer_cli/src/error_formatter.dart'; |
| 23 import 'package:analyzer_cli/src/options.dart'; | 24 import 'package:analyzer_cli/src/options.dart'; |
| 24 import 'package:path/path.dart' as pathos; | 25 import 'package:path/path.dart' as pathos; |
| 25 | 26 |
| 26 /// The maximum number of sources for which AST structures should be kept in the
cache. | 27 /// The maximum number of sources for which AST structures should be kept in the
cache. |
| 27 const int _maxCacheSize = 512; | 28 const int _maxCacheSize = 512; |
| 28 | 29 |
| 29 int currentTimeMillis() => new DateTime.now().millisecondsSinceEpoch; | 30 int currentTimeMillis() => new DateTime.now().millisecondsSinceEpoch; |
| 30 | 31 |
| 31 /// Analyzes single library [File]. | 32 /// Analyzes single library [File]. |
| 32 class AnalyzerImpl { | 33 class AnalyzerImpl { |
| 33 static final PerformanceTag _prepareErrorsTag = | 34 static final PerformanceTag _prepareErrorsTag = |
| 34 new PerformanceTag("AnalyzerImpl.prepareErrors"); | 35 new PerformanceTag("AnalyzerImpl.prepareErrors"); |
| 35 static final PerformanceTag _resolveLibraryTag = | 36 static final PerformanceTag _resolveLibraryTag = |
| 36 new PerformanceTag("AnalyzerImpl._resolveLibrary"); | 37 new PerformanceTag("AnalyzerImpl._resolveLibrary"); |
| 37 | 38 |
| 38 final CommandLineOptions options; | 39 final CommandLineOptions options; |
| 39 final int startTime; | 40 final int startTime; |
| 40 | 41 |
| 41 final AnalysisOptions analysisOptions; | 42 final AnalysisOptions analysisOptions; |
| 42 final AnalysisContext context; | 43 final AnalysisContext context; |
| 44 final AnalysisDriver analysisDriver; |
| 43 | 45 |
| 44 /// Accumulated analysis statistics. | 46 /// Accumulated analysis statistics. |
| 45 final AnalysisStats stats; | 47 final AnalysisStats stats; |
| 46 | 48 |
| 47 final Source librarySource; | 49 final Source librarySource; |
| 48 | 50 |
| 49 /// All [Source]s references by the analyzed library. | 51 /// All [Source]s references by the analyzed library. |
| 50 final Set<Source> sources = new Set<Source>(); | 52 final Set<Source> sources = new Set<Source>(); |
| 51 | 53 |
| 52 /// All [AnalysisErrorInfo]s in the analyzed library. | 54 /// All [AnalysisErrorInfo]s in the analyzed library. |
| 53 final List<AnalysisErrorInfo> errorInfos = new List<AnalysisErrorInfo>(); | 55 final List<AnalysisErrorInfo> errorInfos = new List<AnalysisErrorInfo>(); |
| 54 | 56 |
| 55 /// [HashMap] between sources and analysis error infos. | 57 /// [HashMap] between sources and analysis error infos. |
| 56 final HashMap<Source, AnalysisErrorInfo> sourceErrorsMap = | 58 final HashMap<Source, AnalysisErrorInfo> sourceErrorsMap = |
| 57 new HashMap<Source, AnalysisErrorInfo>(); | 59 new HashMap<Source, AnalysisErrorInfo>(); |
| 58 | 60 |
| 59 /// If the file specified on the command line is part of a package, the name | 61 /// If the file specified on the command line is part of a package, the name |
| 60 /// of that package. Otherwise `null`. This allows us to analyze the file | 62 /// of that package. Otherwise `null`. This allows us to analyze the file |
| 61 /// specified on the command line as though it is reached via a "package:" | 63 /// specified on the command line as though it is reached via a "package:" |
| 62 /// URI, but avoid suppressing its output in the event that the user has not | 64 /// URI, but avoid suppressing its output in the event that the user has not |
| 63 /// specified the "--package-warnings" option. | 65 /// specified the "--package-warnings" option. |
| 64 String _selfPackageName; | 66 String _selfPackageName; |
| 65 | 67 |
| 66 AnalyzerImpl(this.analysisOptions, this.context, this.librarySource, | 68 AnalyzerImpl(this.analysisOptions, this.context, this.analysisDriver, |
| 67 this.options, this.stats, this.startTime); | 69 this.librarySource, this.options, this.stats, this.startTime); |
| 68 | 70 |
| 69 /// Returns the maximal [ErrorSeverity] of the recorded errors. | 71 /// Returns the maximal [ErrorSeverity] of the recorded errors. |
| 70 ErrorSeverity get maxErrorSeverity { | 72 ErrorSeverity get maxErrorSeverity { |
| 71 var status = ErrorSeverity.NONE; | 73 var status = ErrorSeverity.NONE; |
| 72 for (AnalysisErrorInfo errorInfo in errorInfos) { | 74 for (AnalysisErrorInfo errorInfo in errorInfos) { |
| 73 for (AnalysisError error in errorInfo.errors) { | 75 for (AnalysisError error in errorInfo.errors) { |
| 74 if (_processError(error) == null) { | 76 if (_processError(error) == null) { |
| 75 continue; | 77 continue; |
| 76 } | 78 } |
| 77 var severity = computeSeverity(error, options); | 79 var severity = computeSeverity(error, options); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 Future<ErrorSeverity> analyze({int printMode: 1}) async { | 123 Future<ErrorSeverity> analyze({int printMode: 1}) async { |
| 122 setupForAnalysis(); | 124 setupForAnalysis(); |
| 123 return await _analyze(printMode); | 125 return await _analyze(printMode); |
| 124 } | 126 } |
| 125 | 127 |
| 126 /// Fills [errorInfos] using [sources]. | 128 /// Fills [errorInfos] using [sources]. |
| 127 Future<Null> prepareErrors() async { | 129 Future<Null> prepareErrors() async { |
| 128 PerformanceTag previous = _prepareErrorsTag.makeCurrent(); | 130 PerformanceTag previous = _prepareErrorsTag.makeCurrent(); |
| 129 try { | 131 try { |
| 130 for (Source source in sources) { | 132 for (Source source in sources) { |
| 131 context.computeErrors(source); | 133 if (analysisDriver != null) { |
| 132 errorInfos.add(context.getErrors(source)); | 134 String path = source.fullName; |
| 135 AnalysisResult analysisResult = await analysisDriver.getResult(path); |
| 136 errorInfos.add(new AnalysisErrorInfoImpl( |
| 137 analysisResult.errors, analysisResult.lineInfo)); |
| 138 } else { |
| 139 context.computeErrors(source); |
| 140 errorInfos.add(context.getErrors(source)); |
| 141 } |
| 133 } | 142 } |
| 134 } finally { | 143 } finally { |
| 135 previous.makeCurrent(); | 144 previous.makeCurrent(); |
| 136 } | 145 } |
| 137 } | 146 } |
| 138 | 147 |
| 139 /// Fills [sources]. | 148 /// Fills [sources]. |
| 140 void prepareSources(LibraryElement library) { | 149 void prepareSources(LibraryElement library) { |
| 141 var units = new Set<CompilationUnitElement>(); | 150 var units = new Set<CompilationUnitElement>(); |
| 142 var libraries = new Set<LibraryElement>(); | 151 var libraries = new Set<LibraryElement>(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 } | 163 } |
| 155 | 164 |
| 156 Future<ErrorSeverity> _analyze(int printMode) async { | 165 Future<ErrorSeverity> _analyze(int printMode) async { |
| 157 // Don't try to analyze parts. | 166 // Don't try to analyze parts. |
| 158 if (context.computeKindOf(librarySource) == SourceKind.PART) { | 167 if (context.computeKindOf(librarySource) == SourceKind.PART) { |
| 159 stderr.writeln("Only libraries can be analyzed."); | 168 stderr.writeln("Only libraries can be analyzed."); |
| 160 stderr.writeln( | 169 stderr.writeln( |
| 161 "${librarySource.fullName} is a part and can not be analyzed."); | 170 "${librarySource.fullName} is a part and can not be analyzed."); |
| 162 return ErrorSeverity.ERROR; | 171 return ErrorSeverity.ERROR; |
| 163 } | 172 } |
| 173 |
| 164 LibraryElement libraryElement = await _resolveLibrary(); | 174 LibraryElement libraryElement = await _resolveLibrary(); |
| 165 prepareSources(libraryElement); | 175 prepareSources(libraryElement); |
| 166 await prepareErrors(); | 176 await prepareErrors(); |
| 167 | 177 |
| 168 // Print errors and performance numbers. | 178 // Print errors and performance numbers. |
| 169 if (printMode == 1) { | 179 if (printMode == 1) { |
| 170 _printErrors(); | 180 _printErrors(); |
| 171 } else if (printMode == 2) { | 181 } else if (printMode == 2) { |
| 172 _printColdPerf(); | 182 _printColdPerf(); |
| 173 } | 183 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 new ErrorFormatter(sink, options, stats, _processError); | 252 new ErrorFormatter(sink, options, stats, _processError); |
| 243 formatter.formatErrors(errorInfos); | 253 formatter.formatErrors(errorInfos); |
| 244 } | 254 } |
| 245 | 255 |
| 246 ProcessedSeverity _processError(AnalysisError error) => | 256 ProcessedSeverity _processError(AnalysisError error) => |
| 247 processError(error, options, analysisOptions); | 257 processError(error, options, analysisOptions); |
| 248 | 258 |
| 249 Future<LibraryElement> _resolveLibrary() async { | 259 Future<LibraryElement> _resolveLibrary() async { |
| 250 PerformanceTag previous = _resolveLibraryTag.makeCurrent(); | 260 PerformanceTag previous = _resolveLibraryTag.makeCurrent(); |
| 251 try { | 261 try { |
| 252 return context.computeLibraryElement(librarySource); | 262 if (analysisDriver != null) { |
| 263 String path = librarySource.fullName; |
| 264 analysisDriver.priorityFiles = [path]; |
| 265 AnalysisResult analysisResult = await analysisDriver.getResult(path); |
| 266 return analysisResult.unit.element.library; |
| 267 } else { |
| 268 return context.computeLibraryElement(librarySource); |
| 269 } |
| 253 } finally { | 270 } finally { |
| 254 previous.makeCurrent(); | 271 previous.makeCurrent(); |
| 255 } | 272 } |
| 256 } | 273 } |
| 257 | 274 |
| 258 /// Compute the severity of the error; however: | 275 /// Compute the severity of the error; however: |
| 259 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode | 276 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode |
| 260 /// compile time errors to a severity of [ErrorSeverity.INFO]. | 277 /// compile time errors to a severity of [ErrorSeverity.INFO]. |
| 261 /// * if [options.hintsAreFatal] is true, escalate hints to errors. | 278 /// * if [options.hintsAreFatal] is true, escalate hints to errors. |
| 262 /// * if [options.lintsAreFatal] is true, escalate lints to errors. | 279 /// * if [options.lintsAreFatal] is true, escalate lints to errors. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } | 379 } |
| 363 | 380 |
| 364 @override | 381 @override |
| 365 void logInformation(String message, [CaughtException exception]) { | 382 void logInformation(String message, [CaughtException exception]) { |
| 366 outSink.writeln(message); | 383 outSink.writeln(message); |
| 367 if (exception != null) { | 384 if (exception != null) { |
| 368 outSink.writeln(exception); | 385 outSink.writeln(exception); |
| 369 } | 386 } |
| 370 } | 387 } |
| 371 } | 388 } |
| OLD | NEW |