Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Unified Diff: pkg/front_end/lib/dependency_grapher.dart

Issue 2617483007: Extract code from dependency_grapher.dart for easier re-use elsewhere in the front end. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/front_end/lib/src/dependency_grapher_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/dependency_grapher.dart
diff --git a/pkg/front_end/lib/dependency_grapher.dart b/pkg/front_end/lib/dependency_grapher.dart
index fcb9a210b426230a863a09de04bfae51cf32c00c..61204902d7dabc3337dece178534a556eaab0c5f 100644
--- a/pkg/front_end/lib/dependency_grapher.dart
+++ b/pkg/front_end/lib/dependency_grapher.dart
@@ -4,31 +4,18 @@
import 'dart:async';
-import 'package:analyzer/dart/ast/ast.dart';
-import 'package:analyzer/error/listener.dart';
-import 'package:analyzer/src/dart/scanner/reader.dart';
-import 'package:analyzer/src/generated/parser.dart';
-import 'package:front_end/file_system.dart';
-import 'package:front_end/src/async_dependency_walker.dart';
+import 'package:front_end/compiler_options.dart';
import 'package:front_end/src/base/processed_options.dart';
-import 'package:front_end/src/base/uri_resolver.dart';
-import 'package:front_end/src/scanner/scanner.dart';
-
-import 'compiler_options.dart';
+import 'package:front_end/src/dependency_grapher_impl.dart' as impl;
/// Generates a representation of the dependency graph of a program.
///
/// Given the Uri of one or more files, this function follows `import`,
/// `export`, and `part` declarations to discover a graph of all files involved
/// in the program.
-Future<Graph> graphForProgram(
- List<Uri> sources, CompilerOptions options) async {
+Future<Graph> graphForProgram(List<Uri> sources, CompilerOptions options) {
var processedOptions = new ProcessedOptions(options);
- var uriResolver = await processedOptions.getUriResolver();
- var walker = new _Walker(processedOptions.fileSystem, uriResolver, processedOptions.compileSdk);
- var startingPoint = new _StartingPoint(walker, sources);
- await walker.walk(startingPoint);
- return walker.graph;
+ return impl.graphForProgram(sources, processedOptions);
}
/// A representation of the dependency graph of a program.
@@ -38,8 +25,6 @@ class Graph {
/// A list of all library cycles in the program, in topologically sorted order
/// (each cycle only depends on libraries in the cycles that precede it).
final topologicallySortedCycles = <LibraryCycleNode>[];
-
- Graph._();
}
/// A representation of a single library cycle in the dependency graph of a
@@ -50,8 +35,6 @@ class LibraryCycleNode {
/// A map of all the libraries in the cycle, keyed by the URI of their
/// defining compilation unit.
final libraries = <Uri, LibraryNode>{};
-
- LibraryCycleNode._();
}
/// A representation of a single library in the dependency graph of a program.
@@ -67,115 +50,5 @@ class LibraryNode {
/// A list of all the other libraries this library directly depends on.
final dependencies = <LibraryNode>[];
- LibraryNode._(this.uri);
-}
-
-class _Scanner extends Scanner {
- _Scanner(String contents) : super(new CharSequenceReader(contents)) {
- preserveComments = false;
- }
-
- @override
- void reportError(errorCode, int offset, List<Object> arguments) {
- // TODO(paulberry): report errors.
- }
-}
-
-class _StartingPoint extends _WalkerNode {
- final List<Uri> sources;
-
- _StartingPoint(_Walker walker, this.sources) : super(walker, null);
-
- @override
- Future<List<_WalkerNode>> computeDependencies() async =>
- sources.map(walker.nodeForUri).toList();
-}
-
-class _Walker extends AsyncDependencyWalker<_WalkerNode> {
- final FileSystem fileSystem;
- final UriResolver uriResolver;
- final _nodesByUri = <Uri, _WalkerNode>{};
- final graph = new Graph._();
- final bool compileSdk;
-
- _Walker(this.fileSystem, this.uriResolver, this.compileSdk);
-
- @override
- Future<Null> evaluate(_WalkerNode v) {
- if (v is _StartingPoint) return new Future.value();
- return evaluateScc([v]);
- }
-
- @override
- Future<Null> evaluateScc(List<_WalkerNode> scc) {
- var cycle = new LibraryCycleNode._();
- for (var walkerNode in scc) {
- cycle.libraries[walkerNode.uri] = walkerNode.library;
- }
- graph.topologicallySortedCycles.add(cycle);
- return new Future.value();
- }
-
- _WalkerNode nodeForUri(Uri referencedUri) {
- var dependencyNode = _nodesByUri.putIfAbsent(
- referencedUri, () => new _WalkerNode(this, referencedUri));
- return dependencyNode;
- }
-}
-
-class _WalkerNode extends Node<_WalkerNode> {
- static final dartCoreUri = Uri.parse('dart:core');
- final _Walker walker;
- final Uri uri;
- final LibraryNode library;
-
- _WalkerNode(this.walker, Uri uri)
- : uri = uri,
- library = new LibraryNode._(uri);
-
- @override
- Future<List<_WalkerNode>> computeDependencies() async {
- var dependencies = <_WalkerNode>[];
- // TODO(paulberry): add error recovery if the file can't be read.
- var path = walker.uriResolver.resolve(uri);
- if (path == null) {
- // TODO(paulberry): If an error reporter was provided, report the error
- // in the proper way and continue.
- throw new StateError('Invalid URI: $uri');
- }
- var contents = await walker.fileSystem.entityForPath(path).readAsString();
- var scanner = new _Scanner(contents);
- var token = scanner.tokenize();
- // TODO(paulberry): report errors.
- var parser = new Parser(null, AnalysisErrorListener.NULL_LISTENER);
- var unit = parser.parseDirectives(token);
- bool coreUriFound = false;
- void handleDependency(Uri referencedUri) {
- _WalkerNode dependencyNode = walker.nodeForUri(referencedUri);
- library.dependencies.add(dependencyNode.library);
- if (referencedUri.scheme != 'dart' || walker.compileSdk) {
- dependencies.add(dependencyNode);
- }
- if (referencedUri == dartCoreUri) {
- coreUriFound = true;
- }
- }
-
- for (var directive in unit.directives) {
- if (directive is UriBasedDirective) {
- // TODO(paulberry): when we support SDK libraries, we'll need more
- // complex logic here to find SDK parts correctly.
- var referencedUri = uri.resolve(directive.uri.stringValue);
- if (directive is PartDirective) {
- library.parts.add(referencedUri);
- } else {
- handleDependency(referencedUri);
- }
- }
- }
- if (!coreUriFound) {
- handleDependency(dartCoreUri);
- }
- return dependencies;
- }
+ LibraryNode(this.uri);
}
« no previous file with comments | « no previous file | pkg/front_end/lib/src/dependency_grapher_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698