| 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 | 8 |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:path/path.dart' as pathos; | 11 import 'package:path/path.dart' as pathos; |
| 12 | 12 |
| 13 import 'generated/ast.dart'; |
| 14 import 'generated/engine.dart'; |
| 15 import 'generated/element.dart'; |
| 16 import 'generated/error.dart'; |
| 13 import 'generated/java_io.dart'; | 17 import 'generated/java_io.dart'; |
| 14 import 'generated/engine.dart'; | |
| 15 import 'generated/error.dart'; | |
| 16 import 'generated/source_io.dart'; | |
| 17 import 'generated/sdk.dart'; | 18 import 'generated/sdk.dart'; |
| 18 import 'generated/sdk_io.dart'; | 19 import 'generated/sdk_io.dart'; |
| 19 import 'generated/element.dart'; | 20 import 'generated/source_io.dart'; |
| 20 import '../options.dart'; | 21 import '../options.dart'; |
| 21 | 22 |
| 22 import 'package:analyzer/src/generated/java_core.dart' show JavaSystem; | 23 import 'package:analyzer/src/generated/java_core.dart' show JavaSystem; |
| 23 import 'package:analyzer/src/error_formatter.dart'; | 24 import 'package:analyzer/src/error_formatter.dart'; |
| 24 | 25 |
| 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 */ | 28 */ |
| 28 const int _MAX_CACHE_SIZE = 512; | 29 const int _MAX_CACHE_SIZE = 512; |
| 29 | 30 |
| 30 DartSdk sdk; | 31 DartSdk sdk; |
| 31 | 32 |
| 32 /// Analyzes single library [File]. | 33 /// Analyzes single library [File]. |
| 33 class AnalyzerImpl { | 34 class AnalyzerImpl { |
| 34 final String sourcePath; | 35 final String sourcePath; |
| 35 final CommandLineOptions options; | 36 final CommandLineOptions options; |
| 36 final int startTime; | 37 final int startTime; |
| 37 | 38 |
| 38 ContentCache contentCache = new ContentCache(); | 39 ContentCache contentCache = new ContentCache(); |
| 39 SourceFactory sourceFactory; | 40 SourceFactory sourceFactory; |
| 40 AnalysisContext context; | 41 AnalysisContext context; |
| 42 Source librarySource; |
| 41 | 43 |
| 42 /// All [Source]s references by the analyzed library. | 44 /// All [Source]s references by the analyzed library. |
| 43 final Set<Source> sources = new Set<Source>(); | 45 final Set<Source> sources = new Set<Source>(); |
| 44 | 46 |
| 45 /// All [AnalysisErrorInfo]s in the analyzed library. | 47 /// All [AnalysisErrorInfo]s in the analyzed library. |
| 46 final List<AnalysisErrorInfo> errorInfos = new List<AnalysisErrorInfo>(); | 48 final List<AnalysisErrorInfo> errorInfos = new List<AnalysisErrorInfo>(); |
| 47 | 49 |
| 48 AnalyzerImpl(this.sourcePath, this.options, this.startTime) { | 50 AnalyzerImpl(this.sourcePath, this.options, this.startTime) { |
| 49 if (sdk == null) { | 51 if (sdk == null) { |
| 50 sdk = new DirectoryBasedDartSdk(new JavaFile(options.dartSdkPath)); | 52 sdk = new DirectoryBasedDartSdk(new JavaFile(options.dartSdkPath)); |
| 51 } | 53 } |
| 52 } | 54 } |
| 53 | 55 |
| 54 /** | 56 /** |
| 55 * Treats the [sourcePath] as the top level library and analyzes it. | 57 * Treats the [sourcePath] as the top level library and analyzes it using a |
| 58 * synchronous algorithm over the analysis engine. |
| 56 */ | 59 */ |
| 57 void analyze() { | 60 ErrorSeverity analyzeSync() { |
| 61 setupForAnalysis(); |
| 62 return _analyzeSync(); |
| 63 } |
| 64 |
| 65 /** |
| 66 * Treats the [sourcePath] as the top level library and analyzes it using a |
| 67 * asynchronous algorithm over the analysis engine. |
| 68 */ |
| 69 void analyzeAsync() { |
| 70 setupForAnalysis(); |
| 71 _analyzeAsync(); |
| 72 } |
| 73 |
| 74 /** |
| 75 * Setup local fields such as the analysis context for analysis. |
| 76 */ |
| 77 void setupForAnalysis() { |
| 58 sources.clear(); | 78 sources.clear(); |
| 59 errorInfos.clear(); | 79 errorInfos.clear(); |
| 60 if (sourcePath == null) { | 80 if (sourcePath == null) { |
| 61 throw new ArgumentError("sourcePath cannot be null"); | 81 throw new ArgumentError("sourcePath cannot be null"); |
| 62 } | 82 } |
| 63 JavaFile sourceFile = new JavaFile(sourcePath); | 83 JavaFile sourceFile = new JavaFile(sourcePath); |
| 64 UriKind uriKind = getUriKind(sourceFile); | 84 UriKind uriKind = getUriKind(sourceFile); |
| 65 Source librarySource = new FileBasedSource.con2(sourceFile, uriKind); | 85 librarySource = new FileBasedSource.con2(sourceFile, uriKind); |
| 66 | 86 |
| 67 // prepare context | 87 // prepare context |
| 68 prepareAnalysisContext(sourceFile, librarySource); | 88 prepareAnalysisContext(sourceFile, librarySource); |
| 69 | |
| 70 // async perform all tasks in context | |
| 71 _analyze(); | |
| 72 } | 89 } |
| 73 | 90 |
| 74 void _analyze() { | 91 /// The sync version of analysis |
| 92 ErrorSeverity _analyzeSync() { |
| 93 // don't try to analyzer parts |
| 94 var unit = context.parseCompilationUnit(librarySource); |
| 95 var hasLibraryDirective = false; |
| 96 var hasPartOfDirective = false; |
| 97 for (var directive in unit.directives) { |
| 98 if (directive is LibraryDirective) hasLibraryDirective = true; |
| 99 if (directive is PartOfDirective) hasPartOfDirective = true; |
| 100 } |
| 101 if (hasPartOfDirective && !hasLibraryDirective) { |
| 102 print("Only libraries can be analyzed."); |
| 103 print("$sourcePath is a part and can not be analyzed."); |
| 104 return ErrorSeverity.ERROR; |
| 105 } |
| 106 // resolve library |
| 107 var libraryElement = context.computeLibraryElement(librarySource); |
| 108 // prepare source and errors |
| 109 prepareSources(libraryElement); |
| 110 prepareErrors(); |
| 111 |
| 112 // compute max severity and set exitCode |
| 113 ErrorSeverity status = maxErrorSeverity; |
| 114 if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
| 115 status = ErrorSeverity.ERROR; |
| 116 } |
| 117 return status; |
| 118 } |
| 119 |
| 120 /// The async version of the analysis |
| 121 void _analyzeAsync() { |
| 75 new Future(context.performAnalysisTask).then((AnalysisResult result) { | 122 new Future(context.performAnalysisTask).then((AnalysisResult result) { |
| 76 List<ChangeNotice> notices = result.changeNotices; | 123 List<ChangeNotice> notices = result.changeNotices; |
| 77 // TODO(jwren) change 'notices != null' to 'result.hasMoreWork()' after | 124 if (result.hasMoreWork) { |
| 78 // next dart translation is landed for the analyzer | |
| 79 if (notices != null) { | |
| 80 // There is more work, record the set of sources, and then call self | 125 // There is more work, record the set of sources, and then call self |
| 81 // again to perform next task | 126 // again to perform next task |
| 82 for (ChangeNotice notice in notices) { | 127 for (ChangeNotice notice in notices) { |
| 83 sources.add(notice.source); | 128 sources.add(notice.source); |
| 84 } | 129 } |
| 85 return _analyze(); | 130 return _analyzeAsync(); |
| 86 } | 131 } |
| 87 // | 132 // |
| 88 // There are not any more tasks, set error code and print performance | 133 // There are not any more tasks, set error code and print performance |
| 89 // numbers. | 134 // numbers. |
| 90 // | 135 // |
| 91 // prepare errors | 136 // prepare errors |
| 92 prepareErrors(); | 137 prepareErrors(); |
| 93 | 138 |
| 94 // compute max severity and set exitCode | 139 // compute max severity and set exitCode |
| 95 ErrorSeverity status = maxErrorSeverity; | 140 ErrorSeverity status = maxErrorSeverity; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 } | 251 } |
| 207 // add referenced libraries | 252 // add referenced libraries |
| 208 for (LibraryElement child in library.importedLibraries) { | 253 for (LibraryElement child in library.importedLibraries) { |
| 209 addLibrarySources(child, libraries, units); | 254 addLibrarySources(child, libraries, units); |
| 210 } | 255 } |
| 211 for (LibraryElement child in library.exportedLibraries) { | 256 for (LibraryElement child in library.exportedLibraries) { |
| 212 addLibrarySources(child, libraries, units); | 257 addLibrarySources(child, libraries, units); |
| 213 } | 258 } |
| 214 } | 259 } |
| 215 | 260 |
| 261 /// Fills [sources]. |
| 262 void prepareSources(LibraryElement library) { |
| 263 var units = new Set<CompilationUnitElement>(); |
| 264 var libraries = new Set<LibraryElement>(); |
| 265 addLibrarySources(library, libraries, units); |
| 266 } |
| 267 |
| 216 /// Fills [errorInfos] using [sources]. | 268 /// Fills [errorInfos] using [sources]. |
| 217 void prepareErrors() { | 269 void prepareErrors() { |
| 218 for (Source source in sources) { | 270 for (Source source in sources) { |
| 219 context.computeErrors(source); | 271 context.computeErrors(source); |
| 220 var sourceErrors = context.getErrors(source); | 272 var sourceErrors = context.getErrors(source); |
| 221 errorInfos.add(sourceErrors); | 273 errorInfos.add(sourceErrors); |
| 222 } | 274 } |
| 223 } | 275 } |
| 224 | 276 |
| 225 static JavaFile getPackageDirectoryFor(JavaFile sourceFile) { | 277 static JavaFile getPackageDirectoryFor(JavaFile sourceFile) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 340 } |
| 289 } | 341 } |
| 290 | 342 |
| 291 @override | 343 @override |
| 292 void logInformation2(String message, Exception exception) { | 344 void logInformation2(String message, Exception exception) { |
| 293 if (log) { | 345 if (log) { |
| 294 stdout.writeln(message); | 346 stdout.writeln(message); |
| 295 } | 347 } |
| 296 } | 348 } |
| 297 } | 349 } |
| OLD | NEW |