Index: pkg/analyzer/lib/src/analyzer_impl.dart |
diff --git a/pkg/analyzer/lib/src/analyzer_impl.dart b/pkg/analyzer/lib/src/analyzer_impl.dart |
index 25bac6ec833330acd646510f10e4c2a464036a81..879a7d9a8f6111d1ff3403602713a7fce8bf38c3 100644 |
--- a/pkg/analyzer/lib/src/analyzer_impl.dart |
+++ b/pkg/analyzer/lib/src/analyzer_impl.dart |
@@ -5,59 +5,44 @@ |
library analyzer_impl; |
import 'dart:async'; |
+import 'dart:collection'; |
import 'dart:io'; |
+import 'package:analyzer/file_system/physical_file_system.dart'; |
+import 'package:analyzer/source/package_map_provider.dart'; |
+import 'package:analyzer/source/package_map_resolver.dart'; |
+import 'package:analyzer/source/pub_package_map_provider.dart'; |
+import 'package:analyzer/src/error_formatter.dart'; |
+import 'package:analyzer/src/generated/java_core.dart' show JavaSystem; |
+import 'package:analyzer/src/generated/java_engine.dart'; |
+ |
+import '../options.dart'; |
import 'generated/constant.dart'; |
-import 'generated/engine.dart'; |
import 'generated/element.dart'; |
+import 'generated/engine.dart'; |
import 'generated/error.dart'; |
import 'generated/java_io.dart'; |
import 'generated/sdk_io.dart'; |
import 'generated/source_io.dart'; |
-import '../options.dart'; |
-import 'dart:collection'; |
- |
-import 'package:analyzer/src/generated/java_core.dart' show JavaSystem; |
-import 'package:analyzer/src/error_formatter.dart'; |
-import 'package:analyzer/file_system/physical_file_system.dart'; |
-import 'package:analyzer/source/package_map_resolver.dart'; |
-import 'package:analyzer/source/package_map_provider.dart'; |
-import 'package:analyzer/source/pub_package_map_provider.dart'; |
-import 'package:analyzer/src/generated/java_engine.dart'; |
+DirectoryBasedDartSdk sdk; |
/** |
* The maximum number of sources for which AST structures should be kept in the cache. |
*/ |
const int _MAX_CACHE_SIZE = 512; |
-DirectoryBasedDartSdk sdk; |
- |
/// Analyzes single library [File]. |
class AnalyzerImpl { |
- /** |
- * Compute the severity of the error; however, if |
- * [enableTypeChecks] is false, then de-escalate checked-mode compile time |
- * errors to a severity of [ErrorSeverity.INFO]. |
- */ |
- static ErrorSeverity computeSeverity( |
- AnalysisError error, bool enableTypeChecks) { |
- if (!enableTypeChecks |
- && error.errorCode.type == ErrorType.CHECKED_MODE_COMPILE_TIME_ERROR) { |
- return ErrorSeverity.INFO; |
- } |
- return error.errorCode.errorSeverity; |
- } |
- |
final String sourcePath; |
+ |
final CommandLineOptions options; |
final int startTime; |
- |
ContentCache contentCache = new ContentCache(); |
+ |
SourceFactory sourceFactory; |
AnalysisContext context; |
Source librarySource; |
- |
/// All [Source]s references by the analyzed library. |
final Set<Source> sources = new Set<Source>(); |
@@ -75,6 +60,70 @@ class AnalyzerImpl { |
} |
} |
+ /// Returns the maximal [ErrorSeverity] of the recorded errors. |
+ ErrorSeverity get maxErrorSeverity { |
+ var status = ErrorSeverity.NONE; |
+ for (AnalysisErrorInfo errorInfo in errorInfos) { |
+ for (AnalysisError error in errorInfo.errors) { |
+ if (!_isDesiredError(error)) { |
+ continue; |
+ } |
+ var severity = computeSeverity(error, options.enableTypeChecks); |
+ status = status.max(severity); |
+ } |
+ } |
+ return status; |
+ } |
+ |
+ void addCompilationUnitSource(CompilationUnitElement unit, |
+ Set<LibraryElement> libraries, Set<CompilationUnitElement> units) { |
+ if (unit == null || units.contains(unit)) { |
+ return; |
+ } |
+ units.add(unit); |
+ sources.add(unit.source); |
+ } |
+ |
+ void addLibrarySources(LibraryElement library, Set<LibraryElement> libraries, |
+ Set<CompilationUnitElement> units) { |
+ if (library == null || !libraries.add(library)) { |
+ return; |
+ } |
+ // may be skip library |
+ { |
+ UriKind uriKind = library.source.uriKind; |
+ // Optionally skip package: libraries. |
+ if (!options.showPackageWarnings && uriKind == UriKind.PACKAGE_URI) { |
+ return; |
+ } |
+ // Optionally skip SDK libraries. |
+ if (!options.showSdkWarnings && uriKind == UriKind.DART_URI) { |
+ return; |
+ } |
+ } |
+ // add compilation units |
+ addCompilationUnitSource(library.definingCompilationUnit, libraries, units); |
+ for (CompilationUnitElement child in library.parts) { |
+ addCompilationUnitSource(child, libraries, units); |
+ } |
+ // add referenced libraries |
+ for (LibraryElement child in library.importedLibraries) { |
+ addLibrarySources(child, libraries, units); |
+ } |
+ for (LibraryElement child in library.exportedLibraries) { |
+ addLibrarySources(child, libraries, units); |
+ } |
+ } |
+ |
+ /** |
+ * Treats the [sourcePath] as the top level library and analyzes it using a |
+ * asynchronous algorithm over the analysis engine. |
+ */ |
+ void analyzeAsync() { |
+ setupForAnalysis(); |
+ _analyzeAsync(); |
+ } |
+ |
/** |
* Treats the [sourcePath] as the top level library and analyzes it using a |
* synchronous algorithm over the analysis engine. If [printMode] is `0`, |
@@ -87,13 +136,68 @@ class AnalyzerImpl { |
return _analyzeSync(printMode); |
} |
- /** |
- * Treats the [sourcePath] as the top level library and analyzes it using a |
- * asynchronous algorithm over the analysis engine. |
- */ |
- void analyzeAsync() { |
- setupForAnalysis(); |
- _analyzeAsync(); |
+ void prepareAnalysisContext(JavaFile sourceFile, Source source) { |
+ List<UriResolver> resolvers = [ |
+ new DartUriResolver(sdk), |
+ new FileUriResolver()]; |
+ // may be add package resolver |
+ { |
+ JavaFile packageDirectory; |
+ if (options.packageRootPath != null) { |
+ packageDirectory = new JavaFile(options.packageRootPath); |
+ resolvers.add(new PackageUriResolver([packageDirectory])); |
+ } else { |
+ PubPackageMapProvider pubPackageMapProvider = |
+ new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); |
+ PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( |
+ PhysicalResourceProvider.INSTANCE.getResource('.')); |
+ resolvers.add( |
+ new PackageMapUriResolver( |
+ PhysicalResourceProvider.INSTANCE, |
+ packageMapInfo.packageMap)); |
+ } |
+ } |
+ sourceFactory = new SourceFactory(resolvers); |
+ context = AnalysisEngine.instance.createAnalysisContext(); |
+ context.sourceFactory = sourceFactory; |
+ Map<String, String> definedVariables = options.definedVariables; |
+ if (!definedVariables.isEmpty) { |
+ DeclaredVariables declaredVariables = context.declaredVariables; |
+ definedVariables.forEach((String variableName, String value) { |
+ declaredVariables.define(variableName, value); |
+ }); |
+ } |
+ // Uncomment the following to have errors reported on stdout and stderr |
+ AnalysisEngine.instance.logger = new StdLogger(options.log); |
+ |
+ // set options for context |
+ AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl(); |
+ contextOptions.cacheSize = _MAX_CACHE_SIZE; |
+ contextOptions.hint = !options.disableHints; |
+ contextOptions.enableAsync = options.enableAsync; |
+ contextOptions.enableEnum = options.enableEnum; |
+ context.analysisOptions = contextOptions; |
+ |
+ // Create and add a ChangeSet |
+ ChangeSet changeSet = new ChangeSet(); |
+ changeSet.addedSource(source); |
+ context.applyChanges(changeSet); |
+ } |
+ |
+ /// Fills [errorInfos] using [sources]. |
+ void prepareErrors() { |
+ for (Source source in sources) { |
+ context.computeErrors(source); |
+ var sourceErrors = context.getErrors(source); |
+ errorInfos.add(sourceErrors); |
+ } |
+ } |
+ |
+ /// Fills [sources]. |
+ void prepareSources(LibraryElement library) { |
+ var units = new Set<CompilationUnitElement>(); |
+ var libraries = new Set<LibraryElement>(); |
+ addLibrarySources(library, libraries, units); |
} |
/** |
@@ -113,42 +217,6 @@ class AnalyzerImpl { |
prepareAnalysisContext(sourceFile, librarySource); |
} |
- /** |
- * Convert [sourcePath] into an absolute path. |
- */ |
- static String _normalizeSourcePath(String sourcePath) { |
- return new File(sourcePath).absolute.path; |
- } |
- |
- /// The sync version of analysis. |
- ErrorSeverity _analyzeSync(int printMode) { |
- // don't try to analyze parts |
- if (context.computeKindOf(librarySource) == SourceKind.PART) { |
- print("Only libraries can be analyzed."); |
- print("$sourcePath is a part and can not be analyzed."); |
- return ErrorSeverity.ERROR; |
- } |
- // resolve library |
- var libraryElement = context.computeLibraryElement(librarySource); |
- // prepare source and errors |
- prepareSources(libraryElement); |
- prepareErrors(); |
- |
- // print errors and performance numbers |
- if (printMode == 1) { |
- _printErrorsAndPerf(); |
- } else if (printMode == 2) { |
- _printColdPerf(); |
- } |
- |
- // compute max severity and set exitCode |
- ErrorSeverity status = maxErrorSeverity; |
- if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
- status = ErrorSeverity.ERROR; |
- } |
- return status; |
- } |
- |
/// The async version of the analysis |
void _analyzeAsync() { |
new Future(context.performAnalysisTask).then((AnalysisResult result) { |
@@ -185,17 +253,70 @@ class AnalyzerImpl { |
}); |
} |
+ /// The sync version of analysis. |
+ ErrorSeverity _analyzeSync(int printMode) { |
+ // don't try to analyze parts |
+ if (context.computeKindOf(librarySource) == SourceKind.PART) { |
+ print("Only libraries can be analyzed."); |
+ print("$sourcePath is a part and can not be analyzed."); |
+ return ErrorSeverity.ERROR; |
+ } |
+ // resolve library |
+ var libraryElement = context.computeLibraryElement(librarySource); |
+ // prepare source and errors |
+ prepareSources(libraryElement); |
+ prepareErrors(); |
+ |
+ // print errors and performance numbers |
+ if (printMode == 1) { |
+ _printErrorsAndPerf(); |
+ } else if (printMode == 2) { |
+ _printColdPerf(); |
+ } |
+ |
+ // compute max severity and set exitCode |
+ ErrorSeverity status = maxErrorSeverity; |
+ if (status == ErrorSeverity.WARNING && options.warningsAreFatal) { |
+ status = ErrorSeverity.ERROR; |
+ } |
+ return status; |
+ } |
+ |
bool _isDesiredError(AnalysisError error) { |
if (error.errorCode.type == ErrorType.TODO) { |
return false; |
} |
- if (computeSeverity(error, options.enableTypeChecks) == ErrorSeverity.INFO |
- && options.disableHints) { |
+ if (computeSeverity(error, options.enableTypeChecks) == |
+ ErrorSeverity.INFO && |
+ options.disableHints) { |
return false; |
} |
return true; |
} |
+ _printColdPerf() { |
+ // print cold VM performance numbers |
+ int totalTime = JavaSystem.currentTimeMillis() - startTime; |
+ int ioTime = PerformanceStatistics.io.result; |
+ int scanTime = PerformanceStatistics.scan.result; |
+ int parseTime = PerformanceStatistics.parse.result; |
+ int resolveTime = PerformanceStatistics.resolve.result; |
+ int errorsTime = PerformanceStatistics.errors.result; |
+ int hintsTime = PerformanceStatistics.hints.result; |
+ int angularTime = PerformanceStatistics.angular.result; |
+ stdout.writeln("io-cold:$ioTime"); |
+ stdout.writeln("scan-cold:$scanTime"); |
+ stdout.writeln("parse-cold:$parseTime"); |
+ stdout.writeln("resolve-cold:$resolveTime"); |
+ stdout.writeln("errors-cold:$errorsTime"); |
+ stdout.writeln("hints-cold:$hintsTime"); |
+ stdout.writeln("angular-cold:$angularTime"); |
+ stdout.writeln("other-cold:${totalTime |
+ - (ioTime + scanTime + parseTime + resolveTime + errorsTime + hintsTime |
+ + angularTime)}"); |
+ stdout.writeln("total-cold:$totalTime"); |
+ } |
+ |
_printErrorsAndPerf() { |
// The following is a hack. We currently print out to stderr to ensure that |
// when in batch mode we print to stderr, this is because the prints from |
@@ -206,7 +327,8 @@ class AnalyzerImpl { |
IOSink sink = options.machineFormat ? stderr : stdout; |
// print errors |
- ErrorFormatter formatter = new ErrorFormatter(sink, options, _isDesiredError); |
+ ErrorFormatter formatter = |
+ new ErrorFormatter(sink, options, _isDesiredError); |
formatter.formatErrors(errorInfos); |
// print performance numbers |
@@ -233,146 +355,18 @@ class AnalyzerImpl { |
} |
} |
- _printColdPerf() { |
- // print cold VM performance numbers |
- int totalTime = JavaSystem.currentTimeMillis() - startTime; |
- int ioTime = PerformanceStatistics.io.result; |
- int scanTime = PerformanceStatistics.scan.result; |
- int parseTime = PerformanceStatistics.parse.result; |
- int resolveTime = PerformanceStatistics.resolve.result; |
- int errorsTime = PerformanceStatistics.errors.result; |
- int hintsTime = PerformanceStatistics.hints.result; |
- int angularTime = PerformanceStatistics.angular.result; |
- stdout.writeln("io-cold:$ioTime"); |
- stdout.writeln("scan-cold:$scanTime"); |
- stdout.writeln("parse-cold:$parseTime"); |
- stdout.writeln("resolve-cold:$resolveTime"); |
- stdout.writeln("errors-cold:$errorsTime"); |
- stdout.writeln("hints-cold:$hintsTime"); |
- stdout.writeln("angular-cold:$angularTime"); |
- stdout.writeln("other-cold:${totalTime |
- - (ioTime + scanTime + parseTime + resolveTime + errorsTime + hintsTime |
- + angularTime)}"); |
- stdout.writeln("total-cold:$totalTime"); |
- } |
- |
- /// Returns the maximal [ErrorSeverity] of the recorded errors. |
- ErrorSeverity get maxErrorSeverity { |
- var status = ErrorSeverity.NONE; |
- for (AnalysisErrorInfo errorInfo in errorInfos) { |
- for (AnalysisError error in errorInfo.errors) { |
- if (!_isDesiredError(error)) { |
- continue; |
- } |
- var severity = computeSeverity(error, options.enableTypeChecks); |
- status = status.max(severity); |
- } |
- } |
- return status; |
- } |
- |
- void prepareAnalysisContext(JavaFile sourceFile, Source source) { |
- List<UriResolver> resolvers = [ |
- new DartUriResolver(sdk), |
- new FileUriResolver()]; |
- // may be add package resolver |
- { |
- JavaFile packageDirectory; |
- if (options.packageRootPath != null) { |
- packageDirectory = new JavaFile(options.packageRootPath); |
- resolvers.add(new PackageUriResolver([packageDirectory])); |
- } else { |
- PubPackageMapProvider pubPackageMapProvider = |
- new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); |
- PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( |
- PhysicalResourceProvider.INSTANCE.getResource('.')); |
- resolvers.add( |
- new PackageMapUriResolver( |
- PhysicalResourceProvider.INSTANCE, |
- packageMapInfo.packageMap)); |
- } |
- } |
- sourceFactory = new SourceFactory(resolvers); |
- context = AnalysisEngine.instance.createAnalysisContext(); |
- context.sourceFactory = sourceFactory; |
- Map<String, String> definedVariables = options.definedVariables; |
- if (!definedVariables.isEmpty) { |
- DeclaredVariables declaredVariables = context.declaredVariables; |
- definedVariables.forEach((String variableName, String value) { |
- declaredVariables.define(variableName, value); |
- }); |
- } |
- // Uncomment the following to have errors reported on stdout and stderr |
- AnalysisEngine.instance.logger = new StdLogger(options.log); |
- |
- // set options for context |
- AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl(); |
- contextOptions.cacheSize = _MAX_CACHE_SIZE; |
- contextOptions.hint = !options.disableHints; |
- contextOptions.enableAsync = options.enableAsync; |
- contextOptions.enableEnum = options.enableEnum; |
- context.analysisOptions = contextOptions; |
- |
- // Create and add a ChangeSet |
- ChangeSet changeSet = new ChangeSet(); |
- changeSet.addedSource(source); |
- context.applyChanges(changeSet); |
- } |
- |
- void addCompilationUnitSource(CompilationUnitElement unit, |
- Set<LibraryElement> libraries, Set<CompilationUnitElement> units) { |
- if (unit == null || units.contains(unit)) { |
- return; |
- } |
- units.add(unit); |
- sources.add(unit.source); |
- } |
- |
- void addLibrarySources(LibraryElement library, Set<LibraryElement> libraries, |
- Set<CompilationUnitElement> units) { |
- if (library == null || !libraries.add(library)) { |
- return; |
- } |
- // may be skip library |
- { |
- UriKind uriKind = library.source.uriKind; |
- // Optionally skip package: libraries. |
- if (!options.showPackageWarnings && uriKind == UriKind.PACKAGE_URI) { |
- return; |
- } |
- // Optionally skip SDK libraries. |
- if (!options.showSdkWarnings && uriKind == UriKind.DART_URI) { |
- return; |
- } |
- } |
- // add compilation units |
- addCompilationUnitSource(library.definingCompilationUnit, libraries, units); |
- for (CompilationUnitElement child in library.parts) { |
- addCompilationUnitSource(child, libraries, units); |
- } |
- // add referenced libraries |
- for (LibraryElement child in library.importedLibraries) { |
- addLibrarySources(child, libraries, units); |
- } |
- for (LibraryElement child in library.exportedLibraries) { |
- addLibrarySources(child, libraries, units); |
- } |
- } |
- |
- /// Fills [sources]. |
- void prepareSources(LibraryElement library) { |
- var units = new Set<CompilationUnitElement>(); |
- var libraries = new Set<LibraryElement>(); |
- addLibrarySources(library, libraries, units); |
- } |
- |
- /// Fills [errorInfos] using [sources]. |
- void prepareErrors() { |
- for (Source source in sources) { |
- context.computeErrors(source); |
- var sourceErrors = context.getErrors(source); |
- errorInfos.add(sourceErrors); |
+ /** |
+ * Compute the severity of the error; however, if |
+ * [enableTypeChecks] is false, then de-escalate checked-mode compile time |
+ * errors to a severity of [ErrorSeverity.INFO]. |
+ */ |
+ static ErrorSeverity computeSeverity(AnalysisError error, |
+ bool enableTypeChecks) { |
+ if (!enableTypeChecks && |
+ error.errorCode.type == ErrorType.CHECKED_MODE_COMPILE_TIME_ERROR) { |
+ return ErrorSeverity.INFO; |
} |
+ return error.errorCode.errorSeverity; |
} |
static JavaFile getPackageDirectoryFor(JavaFile sourceFile) { |
@@ -408,6 +402,13 @@ class AnalyzerImpl { |
// some generic file |
return file.toURI(); |
} |
+ |
+ /** |
+ * Convert [sourcePath] into an absolute path. |
+ */ |
+ static String _normalizeSourcePath(String sourcePath) { |
+ return new File(sourcePath).absolute.path; |
+ } |
} |
/** |