OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_impl; | 5 library 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 * synchronous algorithm over the analysis engine. If [printMode] is `0`, | 137 * synchronous algorithm over the analysis engine. If [printMode] is `0`, |
138 * then no error or performance information is printed. If [printMode] is `1`, | 138 * then no error or performance information is printed. If [printMode] is `1`, |
139 * then both will be printed. If [printMode] is `2`, then only performance | 139 * then both will be printed. If [printMode] is `2`, then only performance |
140 * information will be printed, and it will be marked as being for a cold VM. | 140 * information will be printed, and it will be marked as being for a cold VM. |
141 */ | 141 */ |
142 ErrorSeverity analyzeSync({int printMode: 1}) { | 142 ErrorSeverity analyzeSync({int printMode: 1}) { |
143 setupForAnalysis(); | 143 setupForAnalysis(); |
144 return _analyzeSync(printMode); | 144 return _analyzeSync(printMode); |
145 } | 145 } |
146 | 146 |
147 void prepareAnalysisContext(JavaFile sourceFile, Source source) { | 147 Source computeLibrarySource() { |
| 148 JavaFile sourceFile = new JavaFile(sourcePath); |
| 149 Source source = sdk.fromFileUri(sourceFile.toURI()); |
| 150 if (source != null) { |
| 151 return source; |
| 152 } |
| 153 source = new FileBasedSource.con2(sourceFile.toURI(), sourceFile); |
| 154 Uri uri = context.sourceFactory.restoreUri(source); |
| 155 return new FileBasedSource.con2(uri, sourceFile); |
| 156 } |
| 157 |
| 158 /** |
| 159 * Create and return the source factory to be used by the analysis context. |
| 160 */ |
| 161 SourceFactory createSourceFactory() { |
148 List<UriResolver> resolvers = [ | 162 List<UriResolver> resolvers = [ |
149 new CustomUriResolver(options.customUrlMappings), | 163 new CustomUriResolver(options.customUrlMappings), |
150 new DartUriResolver(sdk), | 164 new DartUriResolver(sdk) |
151 new FileUriResolver() | |
152 ]; | 165 ]; |
153 // maybe add package resolver | 166 if (options.packageRootPath != null) { |
154 { | 167 JavaFile packageDirectory = new JavaFile(options.packageRootPath); |
155 JavaFile packageDirectory; | 168 resolvers.add(new PackageUriResolver([packageDirectory])); |
156 if (options.packageRootPath != null) { | 169 } else { |
157 packageDirectory = new JavaFile(options.packageRootPath); | 170 PubPackageMapProvider pubPackageMapProvider = |
158 resolvers.add(new PackageUriResolver([packageDirectory])); | 171 new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); |
159 } else { | 172 PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( |
160 PubPackageMapProvider pubPackageMapProvider = | 173 PhysicalResourceProvider.INSTANCE.getResource('.')); |
161 new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); | 174 Map<String, List<Folder>> packageMap = packageMapInfo.packageMap; |
162 PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( | 175 if (packageMap != null) { |
163 PhysicalResourceProvider.INSTANCE.getResource('.')); | 176 resolvers.add(new PackageMapUriResolver( |
164 Map<String, List<Folder>> packageMap = packageMapInfo.packageMap; | 177 PhysicalResourceProvider.INSTANCE, packageMap)); |
165 if (packageMap != null) { | |
166 resolvers.add(new PackageMapUriResolver( | |
167 PhysicalResourceProvider.INSTANCE, packageMap)); | |
168 } | |
169 } | 178 } |
170 } | 179 } |
171 sourceFactory = new SourceFactory(resolvers); | 180 resolvers.add(new FileUriResolver()); |
| 181 return new SourceFactory(resolvers); |
| 182 } |
| 183 |
| 184 void prepareAnalysisContext() { |
| 185 sourceFactory = createSourceFactory(); |
172 context = AnalysisEngine.instance.createAnalysisContext(); | 186 context = AnalysisEngine.instance.createAnalysisContext(); |
173 context.sourceFactory = sourceFactory; | 187 context.sourceFactory = sourceFactory; |
174 Map<String, String> definedVariables = options.definedVariables; | 188 Map<String, String> definedVariables = options.definedVariables; |
175 if (!definedVariables.isEmpty) { | 189 if (!definedVariables.isEmpty) { |
176 DeclaredVariables declaredVariables = context.declaredVariables; | 190 DeclaredVariables declaredVariables = context.declaredVariables; |
177 definedVariables.forEach((String variableName, String value) { | 191 definedVariables.forEach((String variableName, String value) { |
178 declaredVariables.define(variableName, value); | 192 declaredVariables.define(variableName, value); |
179 }); | 193 }); |
180 } | 194 } |
181 // Uncomment the following to have errors reported on stdout and stderr | 195 // Uncomment the following to have errors reported on stdout and stderr |
182 AnalysisEngine.instance.logger = new StdLogger(options.log); | 196 AnalysisEngine.instance.logger = new StdLogger(options.log); |
183 | 197 |
184 // set options for context | 198 // set options for context |
185 AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl(); | 199 AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl(); |
186 contextOptions.cacheSize = _MAX_CACHE_SIZE; | 200 contextOptions.cacheSize = _MAX_CACHE_SIZE; |
187 contextOptions.hint = !options.disableHints; | 201 contextOptions.hint = !options.disableHints; |
188 contextOptions.analyzeFunctionBodiesPredicate = | 202 contextOptions.analyzeFunctionBodiesPredicate = |
189 _analyzeFunctionBodiesPredicate; | 203 _analyzeFunctionBodiesPredicate; |
190 contextOptions.generateImplicitErrors = options.showPackageWarnings; | 204 contextOptions.generateImplicitErrors = options.showPackageWarnings; |
191 contextOptions.generateSdkErrors = options.showSdkWarnings; | 205 contextOptions.generateSdkErrors = options.showSdkWarnings; |
192 context.analysisOptions = contextOptions; | 206 context.analysisOptions = contextOptions; |
193 | 207 |
| 208 librarySource = computeLibrarySource(); |
| 209 |
194 // Create and add a ChangeSet | 210 // Create and add a ChangeSet |
195 ChangeSet changeSet = new ChangeSet(); | 211 ChangeSet changeSet = new ChangeSet(); |
196 changeSet.addedSource(source); | 212 changeSet.addedSource(librarySource); |
197 context.applyChanges(changeSet); | 213 context.applyChanges(changeSet); |
198 } | 214 } |
199 | 215 |
200 /// Fills [errorInfos] using [sources]. | 216 /// Fills [errorInfos] using [sources]. |
201 void prepareErrors() { | 217 void prepareErrors() { |
202 for (Source source in sources) { | 218 for (Source source in sources) { |
203 context.computeErrors(source); | 219 context.computeErrors(source); |
204 var sourceErrors = context.getErrors(source); | 220 var sourceErrors = context.getErrors(source); |
205 errorInfos.add(sourceErrors); | 221 errorInfos.add(sourceErrors); |
206 } | 222 } |
207 } | 223 } |
208 | 224 |
209 /// Fills [sources]. | 225 /// Fills [sources]. |
210 void prepareSources(LibraryElement library) { | 226 void prepareSources(LibraryElement library) { |
211 var units = new Set<CompilationUnitElement>(); | 227 var units = new Set<CompilationUnitElement>(); |
212 var libraries = new Set<LibraryElement>(); | 228 var libraries = new Set<LibraryElement>(); |
213 addLibrarySources(library, libraries, units); | 229 addLibrarySources(library, libraries, units); |
214 } | 230 } |
215 | 231 |
216 /** | 232 /** |
217 * Setup local fields such as the analysis context for analysis. | 233 * Setup local fields such as the analysis context for analysis. |
218 */ | 234 */ |
219 void setupForAnalysis() { | 235 void setupForAnalysis() { |
220 sources.clear(); | 236 sources.clear(); |
221 errorInfos.clear(); | 237 errorInfos.clear(); |
222 if (sourcePath == null) { | 238 if (sourcePath == null) { |
223 throw new ArgumentError("sourcePath cannot be null"); | 239 throw new ArgumentError("sourcePath cannot be null"); |
224 } | 240 } |
225 JavaFile sourceFile = new JavaFile(sourcePath); | |
226 Uri uri = getUri(sourceFile); | |
227 librarySource = new FileBasedSource.con2(uri, sourceFile); | |
228 | |
229 // prepare context | 241 // prepare context |
230 prepareAnalysisContext(sourceFile, librarySource); | 242 prepareAnalysisContext(); |
231 } | 243 } |
232 | 244 |
233 /// The async version of the analysis | 245 /// The async version of the analysis |
234 void _analyzeAsync() { | 246 void _analyzeAsync() { |
235 new Future(context.performAnalysisTask).then((AnalysisResult result) { | 247 new Future(context.performAnalysisTask).then((AnalysisResult result) { |
236 List<ChangeNotice> notices = result.changeNotices; | 248 List<ChangeNotice> notices = result.changeNotices; |
237 if (result.hasMoreWork) { | 249 if (result.hasMoreWork) { |
238 // There is more work, record the set of sources, and then call self | 250 // There is more work, record the set of sources, and then call self |
239 // again to perform next task | 251 // again to perform next task |
240 for (ChangeNotice notice in notices) { | 252 for (ChangeNotice notice in notices) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 if (packagesDir.exists()) { | 408 if (packagesDir.exists()) { |
397 return packagesDir; | 409 return packagesDir; |
398 } | 410 } |
399 dir = dir.getParentFile(); | 411 dir = dir.getParentFile(); |
400 } | 412 } |
401 // not found | 413 // not found |
402 return null; | 414 return null; |
403 } | 415 } |
404 | 416 |
405 /** | 417 /** |
406 * Returns the [Uri] for the given input file. | |
407 * | |
408 * Usually it is a `file:` [Uri], but if [file] is located in the `lib` | |
409 * directory of the [sdk], then returns a `dart:` [Uri]. | |
410 */ | |
411 static Uri getUri(JavaFile file) { | |
412 // may be file in SDK | |
413 { | |
414 Source source = sdk.fromFileUri(file.toURI()); | |
415 if (source != null) { | |
416 return source.uri; | |
417 } | |
418 } | |
419 // some generic file | |
420 return file.toURI(); | |
421 } | |
422 | |
423 /** | |
424 * Convert [sourcePath] into an absolute path. | 418 * Convert [sourcePath] into an absolute path. |
425 */ | 419 */ |
426 static String _normalizeSourcePath(String sourcePath) { | 420 static String _normalizeSourcePath(String sourcePath) { |
427 return new File(sourcePath).absolute.path; | 421 return new File(sourcePath).absolute.path; |
428 } | 422 } |
429 } | 423 } |
430 | 424 |
431 /** | 425 /** |
432 * This [Logger] prints out information comments to [stdout] and error messages | 426 * This [Logger] prints out information comments to [stdout] and error messages |
433 * to [stderr]. | 427 * to [stderr]. |
(...skipping 26 matching lines...) Expand all Loading... |
460 } | 454 } |
461 } | 455 } |
462 | 456 |
463 @override | 457 @override |
464 void logInformation2(String message, Object exception) { | 458 void logInformation2(String message, Object exception) { |
465 if (log) { | 459 if (log) { |
466 stdout.writeln(message); | 460 stdout.writeln(message); |
467 } | 461 } |
468 } | 462 } |
469 } | 463 } |
OLD | NEW |