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'; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 /// then no error or performance information is printed. If [printMode] is `1`
, | 115 /// then no error or performance information is printed. If [printMode] is `1`
, |
116 /// then both will be printed. If [printMode] is `2`, then only performance | 116 /// then both will be printed. If [printMode] is `2`, then only performance |
117 /// information will be printed, and it will be marked as being for a cold VM. | 117 /// information will be printed, and it will be marked as being for a cold VM. |
118 ErrorSeverity analyzeSync({int printMode: 1}) { | 118 ErrorSeverity analyzeSync({int printMode: 1}) { |
119 setupForAnalysis(); | 119 setupForAnalysis(); |
120 return _analyzeSync(printMode); | 120 return _analyzeSync(printMode); |
121 } | 121 } |
122 | 122 |
123 /// Fills [errorInfos] using [sources]. | 123 /// Fills [errorInfos] using [sources]. |
124 void prepareErrors() { | 124 void prepareErrors() { |
125 for (Source source in sources) { | 125 return _prepareErrorsTag.makeCurrentWhile(() { |
126 context.computeErrors(source); | 126 for (Source source in sources) { |
| 127 context.computeErrors(source); |
| 128 errorInfos.add(context.getErrors(source)); |
| 129 } |
| 130 }); |
| 131 } |
127 | 132 |
128 errorInfos.add(context.getErrors(source)); | 133 static final PerformanceTag _prepareErrorsTag = |
129 } | 134 new PerformanceTag("AnalyzerImpl.prepareErrors"); |
130 } | |
131 | 135 |
132 /// Fills [sources]. | 136 /// Fills [sources]. |
133 void prepareSources(LibraryElement library) { | 137 void prepareSources(LibraryElement library) { |
134 var units = new Set<CompilationUnitElement>(); | 138 var units = new Set<CompilationUnitElement>(); |
135 var libraries = new Set<LibraryElement>(); | 139 var libraries = new Set<LibraryElement>(); |
136 addLibrarySources(library, libraries, units); | 140 addLibrarySources(library, libraries, units); |
137 } | 141 } |
138 | 142 |
139 /// Setup local fields such as the analysis context for analysis. | 143 /// Setup local fields such as the analysis context for analysis. |
140 void setupForAnalysis() { | 144 void setupForAnalysis() { |
141 sources.clear(); | 145 sources.clear(); |
142 errorInfos.clear(); | 146 errorInfos.clear(); |
143 Uri libraryUri = librarySource.uri; | 147 Uri libraryUri = librarySource.uri; |
144 if (libraryUri.scheme == 'package' && libraryUri.pathSegments.length > 0) { | 148 if (libraryUri.scheme == 'package' && libraryUri.pathSegments.length > 0) { |
145 _selfPackageName = libraryUri.pathSegments[0]; | 149 _selfPackageName = libraryUri.pathSegments[0]; |
146 } | 150 } |
147 } | 151 } |
148 | 152 |
149 /// The sync version of analysis. | 153 /// The sync version of analysis. |
150 ErrorSeverity _analyzeSync(int printMode) { | 154 ErrorSeverity _analyzeSync(int printMode) { |
151 // Don't try to analyze parts. | 155 // Don't try to analyze parts. |
152 if (context.computeKindOf(librarySource) == SourceKind.PART) { | 156 if (context.computeKindOf(librarySource) == SourceKind.PART) { |
153 stderr.writeln("Only libraries can be analyzed."); | 157 stderr.writeln("Only libraries can be analyzed."); |
154 stderr.writeln( | 158 stderr.writeln( |
155 "${librarySource.fullName} is a part and can not be analyzed."); | 159 "${librarySource.fullName} is a part and can not be analyzed."); |
156 return ErrorSeverity.ERROR; | 160 return ErrorSeverity.ERROR; |
157 } | 161 } |
158 // Resolve library. | 162 var libraryElement = _resolveLibrary(); |
159 var libraryElement = context.computeLibraryElement(librarySource); | |
160 // Prepare source and errors. | |
161 prepareSources(libraryElement); | 163 prepareSources(libraryElement); |
162 prepareErrors(); | 164 prepareErrors(); |
163 | 165 |
164 // Print errors and performance numbers. | 166 // Print errors and performance numbers. |
165 if (printMode == 1) { | 167 if (printMode == 1) { |
166 _printErrorsAndPerf(); | 168 _printErrors(); |
167 } else if (printMode == 2) { | 169 } else if (printMode == 2) { |
168 _printColdPerf(); | 170 _printColdPerf(); |
169 } | 171 } |
170 | 172 |
171 // Compute max severity and set exitCode. | 173 // Compute max severity and set exitCode. |
172 ErrorSeverity status = maxErrorSeverity; | 174 ErrorSeverity status = maxErrorSeverity; |
173 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { | 175 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
174 status = ErrorSeverity.ERROR; | 176 status = ErrorSeverity.ERROR; |
175 } | 177 } |
176 return status; | 178 return status; |
(...skipping 21 matching lines...) Expand all Loading... |
198 if (tag != PerformanceTag.UNKNOWN) { | 200 if (tag != PerformanceTag.UNKNOWN) { |
199 int tagTime = tag.elapsedMs; | 201 int tagTime = tag.elapsedMs; |
200 outSink.writeln('${tag.label}-cold:$tagTime'); | 202 outSink.writeln('${tag.label}-cold:$tagTime'); |
201 otherTime -= tagTime; | 203 otherTime -= tagTime; |
202 } | 204 } |
203 } | 205 } |
204 outSink.writeln('other-cold:$otherTime'); | 206 outSink.writeln('other-cold:$otherTime'); |
205 outSink.writeln("total-cold:$totalTime"); | 207 outSink.writeln("total-cold:$totalTime"); |
206 } | 208 } |
207 | 209 |
208 _printErrorsAndPerf() { | 210 _printErrors() { |
209 // The following is a hack. We currently print out to stderr to ensure that | 211 // The following is a hack. We currently print out to stderr to ensure that |
210 // when in batch mode we print to stderr, this is because the prints from | 212 // when in batch mode we print to stderr, this is because the prints from |
211 // batch are made to stderr. The reason that options.shouldBatch isn't used | 213 // batch are made to stderr. The reason that options.shouldBatch isn't used |
212 // is because when the argument flags are constructed in BatchRunner and | 214 // is because when the argument flags are constructed in BatchRunner and |
213 // passed in from batch mode which removes the batch flag to prevent the | 215 // passed in from batch mode which removes the batch flag to prevent the |
214 // "cannot have the batch flag and source file" error message. | 216 // "cannot have the batch flag and source file" error message. |
215 StringSink sink = options.machineFormat ? errorSink : outSink; | 217 StringSink sink = options.machineFormat ? errorSink : outSink; |
216 | 218 |
217 // Print errors. | 219 // Print errors. |
218 ErrorFormatter formatter = new ErrorFormatter(sink, options, _processError); | 220 ErrorFormatter formatter = new ErrorFormatter(sink, options, _processError); |
(...skipping 23 matching lines...) Expand all Loading... |
242 | 244 |
243 // Skip TODOs. | 245 // Skip TODOs. |
244 if (severity == ErrorType.TODO) { | 246 if (severity == ErrorType.TODO) { |
245 return null; | 247 return null; |
246 } | 248 } |
247 } | 249 } |
248 | 250 |
249 return new ProcessedSeverity(severity, isOverridden); | 251 return new ProcessedSeverity(severity, isOverridden); |
250 } | 252 } |
251 | 253 |
| 254 LibraryElement _resolveLibrary() { |
| 255 return _resolveLibraryTag.makeCurrentWhile(() { |
| 256 return context.computeLibraryElement(librarySource); |
| 257 }); |
| 258 } |
| 259 |
| 260 static final PerformanceTag _resolveLibraryTag = |
| 261 new PerformanceTag("AnalyzerImpl._resolveLibrary"); |
| 262 |
252 /// Compute the severity of the error; however: | 263 /// Compute the severity of the error; however: |
253 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode | 264 /// * if [options.enableTypeChecks] is false, then de-escalate checked-mode |
254 /// compile time errors to a severity of [ErrorSeverity.INFO]. | 265 /// compile time errors to a severity of [ErrorSeverity.INFO]. |
255 /// * if [options.hintsAreFatal] is true, escalate hints to errors. | 266 /// * if [options.hintsAreFatal] is true, escalate hints to errors. |
256 static ErrorSeverity computeSeverity( | 267 static ErrorSeverity computeSeverity( |
257 AnalysisError error, CommandLineOptions options, | 268 AnalysisError error, CommandLineOptions options, |
258 [AnalysisContext context]) { | 269 [AnalysisContext context]) { |
259 if (context != null) { | 270 if (context != null) { |
260 ErrorProcessor processor = ErrorProcessor.getProcessor(context, error); | 271 ErrorProcessor processor = ErrorProcessor.getProcessor(context, error); |
261 // If there is a processor for this error, defer to it. | 272 // If there is a processor for this error, defer to it. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 319 } |
309 | 320 |
310 @override | 321 @override |
311 void logInformation(String message, [CaughtException exception]) { | 322 void logInformation(String message, [CaughtException exception]) { |
312 outSink.writeln(message); | 323 outSink.writeln(message); |
313 if (exception != null) { | 324 if (exception != null) { |
314 outSink.writeln(exception); | 325 outSink.writeln(exception); |
315 } | 326 } |
316 } | 327 } |
317 } | 328 } |
OLD | NEW |