| 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 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 /// specified on the command line as though it is reached via a "package:" | 59 /// specified on the command line as though it is reached via a "package:" |
| 60 /// URI, but avoid suppressing its output in the event that the user has not | 60 /// URI, but avoid suppressing its output in the event that the user has not |
| 61 /// specified the "--package-warnings" option. | 61 /// specified the "--package-warnings" option. |
| 62 String _selfPackageName; | 62 String _selfPackageName; |
| 63 | 63 |
| 64 AnalyzerImpl(this.analysisOptions, this.context, this.analysisDriver, | 64 AnalyzerImpl(this.analysisOptions, this.context, this.analysisDriver, |
| 65 this.librarySource, this.options, this.stats, this.startTime); | 65 this.librarySource, this.options, this.stats, this.startTime); |
| 66 | 66 |
| 67 /// Returns the maximal [ErrorSeverity] of the recorded errors. | 67 /// Returns the maximal [ErrorSeverity] of the recorded errors. |
| 68 ErrorSeverity get maxErrorSeverity { | 68 ErrorSeverity get maxErrorSeverity { |
| 69 var status = ErrorSeverity.NONE; | 69 ErrorSeverity status = ErrorSeverity.NONE; |
| 70 for (AnalysisErrorInfo errorInfo in errorInfos) { | 70 for (AnalysisErrorInfo errorInfo in errorInfos) { |
| 71 for (AnalysisError error in errorInfo.errors) { | 71 for (AnalysisError error in errorInfo.errors) { |
| 72 if (_processError(error) == null) { | 72 if (_defaultSeverityProcessor(error) == null) { |
| 73 continue; | 73 continue; |
| 74 } | 74 } |
| 75 var severity = computeSeverity(error, options); | 75 ErrorSeverity severity = computeSeverity(error, options); |
| 76 status = status.max(severity); | 76 status = status.max(severity); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 return status; | 79 return status; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void addCompilationUnitSource( | 82 void addCompilationUnitSource( |
| 83 CompilationUnitElement unit, Set<CompilationUnitElement> units) { | 83 CompilationUnitElement unit, Set<CompilationUnitElement> units) { |
| 84 if (unit == null || units.contains(unit)) { | 84 if (unit == null || units.contains(unit)) { |
| 85 return; | 85 return; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 109 for (LibraryElement child in library.exportedLibraries) { | 109 for (LibraryElement child in library.exportedLibraries) { |
| 110 addLibrarySources(child, libraries, units); | 110 addLibrarySources(child, libraries, units); |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 /// Treats the [sourcePath] as the top level library and analyzes it using | 114 /// Treats the [sourcePath] as the top level library and analyzes it using |
| 115 /// the analysis engine. If [printMode] is `0`, then no error or performance | 115 /// the analysis engine. If [printMode] is `0`, then no error or performance |
| 116 /// information is printed. If [printMode] is `1`, then errors will be printed
. | 116 /// information is printed. If [printMode] is `1`, then errors will be printed
. |
| 117 /// If [printMode] is `2`, then performance information will be printed, and | 117 /// If [printMode] is `2`, then performance information will be printed, and |
| 118 /// it will be marked as being for a cold VM. | 118 /// it will be marked as being for a cold VM. |
| 119 Future<ErrorSeverity> analyze({int printMode: 1}) async { | 119 Future<ErrorSeverity> analyze(ErrorFormatter formatter, |
| 120 {int printMode: 1}) async { |
| 120 setupForAnalysis(); | 121 setupForAnalysis(); |
| 121 return await _analyze(printMode); | 122 return await _analyze(printMode, formatter); |
| 122 } | 123 } |
| 123 | 124 |
| 124 /// Fills [errorInfos] using [sources]. | 125 /// Fills [errorInfos] using [sources]. |
| 125 Future<Null> prepareErrors() async { | 126 Future<Null> prepareErrors() async { |
| 126 PerformanceTag previous = _prepareErrorsTag.makeCurrent(); | 127 PerformanceTag previous = _prepareErrorsTag.makeCurrent(); |
| 127 try { | 128 try { |
| 128 for (Source source in sources) { | 129 for (Source source in sources) { |
| 129 if (analysisDriver != null) { | 130 if (analysisDriver != null) { |
| 130 String path = source.fullName; | 131 String path = source.fullName; |
| 131 ErrorsResult errorsResult = await analysisDriver.getErrors(path); | 132 ErrorsResult errorsResult = await analysisDriver.getErrors(path); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 /// Setup local fields such as the analysis context for analysis. | 152 /// Setup local fields such as the analysis context for analysis. |
| 152 void setupForAnalysis() { | 153 void setupForAnalysis() { |
| 153 sources.clear(); | 154 sources.clear(); |
| 154 errorInfos.clear(); | 155 errorInfos.clear(); |
| 155 Uri libraryUri = librarySource.uri; | 156 Uri libraryUri = librarySource.uri; |
| 156 if (libraryUri.scheme == 'package' && libraryUri.pathSegments.length > 0) { | 157 if (libraryUri.scheme == 'package' && libraryUri.pathSegments.length > 0) { |
| 157 _selfPackageName = libraryUri.pathSegments[0]; | 158 _selfPackageName = libraryUri.pathSegments[0]; |
| 158 } | 159 } |
| 159 } | 160 } |
| 160 | 161 |
| 161 Future<ErrorSeverity> _analyze(int printMode) async { | 162 Future<ErrorSeverity> _analyze( |
| 163 int printMode, ErrorFormatter formatter) async { |
| 162 // Don't try to analyze parts. | 164 // Don't try to analyze parts. |
| 163 String path = librarySource.fullName; | 165 String path = librarySource.fullName; |
| 164 SourceKind librarySourceKind = analysisDriver != null | 166 SourceKind librarySourceKind = analysisDriver != null |
| 165 ? await analysisDriver.getSourceKind(path) | 167 ? await analysisDriver.getSourceKind(path) |
| 166 : context.computeKindOf(librarySource); | 168 : context.computeKindOf(librarySource); |
| 167 if (librarySourceKind == SourceKind.PART) { | 169 if (librarySourceKind == SourceKind.PART) { |
| 168 stderr.writeln("Only libraries can be analyzed."); | 170 stderr.writeln("Only libraries can be analyzed."); |
| 169 stderr.writeln("${path} is a part and can not be analyzed."); | 171 stderr.writeln("${path} is a part and can not be analyzed."); |
| 170 return ErrorSeverity.ERROR; | 172 return ErrorSeverity.ERROR; |
| 171 } | 173 } |
| 172 | 174 |
| 173 LibraryElement libraryElement = await _resolveLibrary(); | 175 LibraryElement libraryElement = await _resolveLibrary(); |
| 174 prepareSources(libraryElement); | 176 prepareSources(libraryElement); |
| 175 await prepareErrors(); | 177 await prepareErrors(); |
| 176 | 178 |
| 177 // Print errors and performance numbers. | 179 // Print errors and performance numbers. |
| 178 if (printMode == 1) { | 180 if (printMode == 1) { |
| 179 _printErrors(); | 181 formatter.formatErrors(errorInfos); |
| 180 } else if (printMode == 2) { | 182 } else if (printMode == 2) { |
| 181 _printColdPerf(); | 183 _printColdPerf(); |
| 182 } | 184 } |
| 183 | 185 |
| 184 // Compute max severity and set exitCode. | 186 // Compute max severity and set exitCode. |
| 185 ErrorSeverity status = maxErrorSeverity; | 187 ErrorSeverity status = maxErrorSeverity; |
| 186 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { | 188 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 187 status = ErrorSeverity.ERROR; | 189 status = ErrorSeverity.ERROR; |
| 188 } | 190 } |
| 189 return status; | 191 return status; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if (tag != PerformanceTag.UNKNOWN) { | 233 if (tag != PerformanceTag.UNKNOWN) { |
| 232 int tagTime = tag.elapsedMs; | 234 int tagTime = tag.elapsedMs; |
| 233 outSink.writeln('${tag.label}-cold:$tagTime'); | 235 outSink.writeln('${tag.label}-cold:$tagTime'); |
| 234 otherTime -= tagTime; | 236 otherTime -= tagTime; |
| 235 } | 237 } |
| 236 } | 238 } |
| 237 outSink.writeln('other-cold:$otherTime'); | 239 outSink.writeln('other-cold:$otherTime'); |
| 238 outSink.writeln("total-cold:$totalTime"); | 240 outSink.writeln("total-cold:$totalTime"); |
| 239 } | 241 } |
| 240 | 242 |
| 241 void _printErrors() { | 243 ProcessedSeverity _defaultSeverityProcessor(AnalysisError error) => |
| 242 // The following is a hack. We currently print out to stderr to ensure that | 244 determineProcessedSeverity(error, options, analysisOptions); |
| 243 // when in batch mode we print to stderr, this is because the prints from | |
| 244 // batch are made to stderr. The reason that options.shouldBatch isn't used | |
| 245 // is because when the argument flags are constructed in BatchRunner and | |
| 246 // passed in from batch mode which removes the batch flag to prevent the | |
| 247 // "cannot have the batch flag and source file" error message. | |
| 248 StringSink sink = options.machineFormat ? errorSink : outSink; | |
| 249 | |
| 250 // Print errors. | |
| 251 ErrorFormatter formatter = | |
| 252 new ErrorFormatter(sink, options, stats, _processError); | |
| 253 formatter.formatErrors(errorInfos); | |
| 254 } | |
| 255 | |
| 256 ProcessedSeverity _processError(AnalysisError error) => | |
| 257 processError(error, options, analysisOptions); | |
| 258 | 245 |
| 259 Future<LibraryElement> _resolveLibrary() async { | 246 Future<LibraryElement> _resolveLibrary() async { |
| 260 PerformanceTag previous = _resolveLibraryTag.makeCurrent(); | 247 PerformanceTag previous = _resolveLibraryTag.makeCurrent(); |
| 261 try { | 248 try { |
| 262 if (analysisDriver != null) { | 249 if (analysisDriver != null) { |
| 263 String path = librarySource.fullName; | 250 String path = librarySource.fullName; |
| 264 analysisDriver.priorityFiles = [path]; | 251 analysisDriver.priorityFiles = [path]; |
| 265 UnitElementResult elementResult = | 252 UnitElementResult elementResult = |
| 266 await analysisDriver.getUnitElement(path); | 253 await analysisDriver.getUnitElement(path); |
| 267 return elementResult.element.library; | 254 return elementResult.element.library; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 } | 306 } |
| 320 | 307 |
| 321 @override | 308 @override |
| 322 void logInformation(String message, [CaughtException exception]) { | 309 void logInformation(String message, [CaughtException exception]) { |
| 323 outSink.writeln(message); | 310 outSink.writeln(message); |
| 324 if (exception != null) { | 311 if (exception != null) { |
| 325 outSink.writeln(exception); | 312 outSink.writeln(exception); |
| 326 } | 313 } |
| 327 } | 314 } |
| 328 } | 315 } |
| OLD | NEW |