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