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

Side by Side Diff: pkg/front_end/lib/dependency_grapher.dart

Issue 2581263002: dependency_grapher: handle dependencies on SDK. (Closed)
Patch Set: Created 4 years 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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/front_end/test/dependency_grapher_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/error/listener.dart'; 8 import 'package:analyzer/error/listener.dart';
9 import 'package:analyzer/src/dart/scanner/reader.dart'; 9 import 'package:analyzer/src/dart/scanner/reader.dart';
10 import 'package:analyzer/src/generated/parser.dart'; 10 import 'package:analyzer/src/generated/parser.dart';
(...skipping 21 matching lines...) Expand all
32 var contents = await options.fileSystem 32 var contents = await options.fileSystem
33 .entityForPath(options.packagesFilePath) 33 .entityForPath(options.packagesFilePath)
34 .readAsBytes(); 34 .readAsBytes();
35 var baseLocation = 35 var baseLocation =
36 options.fileSystem.context.toUri(options.packagesFilePath); 36 options.fileSystem.context.toUri(options.packagesFilePath);
37 packages = package_config.parse(contents, baseLocation); 37 packages = package_config.parse(contents, baseLocation);
38 } 38 }
39 var sdkLibraries = <String, Uri>{}; // TODO(paulberry): support SDK libraries 39 var sdkLibraries = <String, Uri>{}; // TODO(paulberry): support SDK libraries
40 var uriResolver = 40 var uriResolver =
41 new UriResolver(packages, sdkLibraries, options.fileSystem.context); 41 new UriResolver(packages, sdkLibraries, options.fileSystem.context);
42 var walker = new _Walker(options.fileSystem, uriResolver); 42 var walker = new _Walker(options.fileSystem, uriResolver, options.compileSdk);
43 var startingPoint = new _StartingPoint(walker, sources); 43 var startingPoint = new _StartingPoint(walker, sources);
44 await walker.walk(startingPoint); 44 await walker.walk(startingPoint);
45 return walker.graph; 45 return walker.graph;
46 } 46 }
47 47
48 /// A representation of the dependency graph of a program. 48 /// A representation of the dependency graph of a program.
49 /// 49 ///
50 /// Not intended to be extended, implemented, or mixed in by clients. 50 /// Not intended to be extended, implemented, or mixed in by clients.
51 class Graph { 51 class Graph {
52 /// A list of all library cycles in the program, in topologically sorted order 52 /// A list of all library cycles in the program, in topologically sorted order
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 @override 103 @override
104 Future<List<_WalkerNode>> computeDependencies() async => 104 Future<List<_WalkerNode>> computeDependencies() async =>
105 sources.map(walker.nodeForUri).toList(); 105 sources.map(walker.nodeForUri).toList();
106 } 106 }
107 107
108 class _Walker extends AsyncDependencyWalker<_WalkerNode> { 108 class _Walker extends AsyncDependencyWalker<_WalkerNode> {
109 final FileSystem fileSystem; 109 final FileSystem fileSystem;
110 final UriResolver uriResolver; 110 final UriResolver uriResolver;
111 final _nodesByUri = <Uri, _WalkerNode>{}; 111 final _nodesByUri = <Uri, _WalkerNode>{};
112 final graph = new Graph._(); 112 final graph = new Graph._();
113 final bool compileSdk;
113 114
114 _Walker(this.fileSystem, this.uriResolver); 115 _Walker(this.fileSystem, this.uriResolver, this.compileSdk);
115 116
116 @override 117 @override
117 Future<Null> evaluate(_WalkerNode v) { 118 Future<Null> evaluate(_WalkerNode v) {
118 if (v is _StartingPoint) return new Future.value(); 119 if (v is _StartingPoint) return new Future.value();
119 return evaluateScc([v]); 120 return evaluateScc([v]);
120 } 121 }
121 122
122 @override 123 @override
123 Future<Null> evaluateScc(List<_WalkerNode> scc) { 124 Future<Null> evaluateScc(List<_WalkerNode> scc) {
124 var cycle = new LibraryCycleNode._(); 125 var cycle = new LibraryCycleNode._();
125 for (var walkerNode in scc) { 126 for (var walkerNode in scc) {
126 cycle.libraries[walkerNode.uri] = walkerNode.library; 127 cycle.libraries[walkerNode.uri] = walkerNode.library;
127 } 128 }
128 graph.topologicallySortedCycles.add(cycle); 129 graph.topologicallySortedCycles.add(cycle);
129 return new Future.value(); 130 return new Future.value();
130 } 131 }
131 132
132 _WalkerNode nodeForUri(Uri referencedUri) { 133 _WalkerNode nodeForUri(Uri referencedUri) {
133 var dependencyNode = _nodesByUri.putIfAbsent( 134 var dependencyNode = _nodesByUri.putIfAbsent(
134 referencedUri, () => new _WalkerNode(this, referencedUri)); 135 referencedUri, () => new _WalkerNode(this, referencedUri));
135 return dependencyNode; 136 return dependencyNode;
136 } 137 }
137 } 138 }
138 139
139 class _WalkerNode extends Node<_WalkerNode> { 140 class _WalkerNode extends Node<_WalkerNode> {
141 static final dartCoreUri = Uri.parse('dart:core');
140 final _Walker walker; 142 final _Walker walker;
141 final Uri uri; 143 final Uri uri;
142 final LibraryNode library; 144 final LibraryNode library;
143 145
144 _WalkerNode(this.walker, Uri uri) 146 _WalkerNode(this.walker, Uri uri)
145 : uri = uri, 147 : uri = uri,
146 library = new LibraryNode._(uri); 148 library = new LibraryNode._(uri);
147 149
148 @override 150 @override
149 Future<List<_WalkerNode>> computeDependencies() async { 151 Future<List<_WalkerNode>> computeDependencies() async {
150 var dependencies = <_WalkerNode>[]; 152 var dependencies = <_WalkerNode>[];
151 // TODO(paulberry): add error recovery if the file can't be read. 153 // TODO(paulberry): add error recovery if the file can't be read.
152 var path = walker.uriResolver.resolve(uri); 154 var path = walker.uriResolver.resolve(uri);
153 if (path == null) { 155 if (path == null) {
154 // TODO(paulberry): If an error reporter was provided, report the error 156 // TODO(paulberry): If an error reporter was provided, report the error
155 // in the proper way and continue. 157 // in the proper way and continue.
156 throw new StateError('Invalid URI: $uri'); 158 throw new StateError('Invalid URI: $uri');
157 } 159 }
158 var contents = await walker.fileSystem.entityForPath(path).readAsString(); 160 var contents = await walker.fileSystem.entityForPath(path).readAsString();
159 var scanner = new _Scanner(contents); 161 var scanner = new _Scanner(contents);
160 var token = scanner.tokenize(); 162 var token = scanner.tokenize();
161 // TODO(paulberry): report errors. 163 // TODO(paulberry): report errors.
162 var parser = new Parser(null, AnalysisErrorListener.NULL_LISTENER); 164 var parser = new Parser(null, AnalysisErrorListener.NULL_LISTENER);
163 var unit = parser.parseDirectives(token); 165 var unit = parser.parseDirectives(token);
166 bool coreUriFound = false;
167 void handleDependency(Uri referencedUri) {
168 _WalkerNode dependencyNode = walker.nodeForUri(referencedUri);
169 library.dependencies.add(dependencyNode.library);
170 if (referencedUri.scheme != 'dart' || walker.compileSdk) {
171 dependencies.add(dependencyNode);
172 }
173 if (referencedUri == dartCoreUri) {
174 coreUriFound = true;
175 }
176 }
177
164 for (var directive in unit.directives) { 178 for (var directive in unit.directives) {
165 if (directive is UriBasedDirective) { 179 if (directive is UriBasedDirective) {
166 // TODO(paulberry): when we support SDK libraries, we'll need more 180 // TODO(paulberry): when we support SDK libraries, we'll need more
167 // complex logic here to find SDK parts correctly. 181 // complex logic here to find SDK parts correctly.
168 var referencedUri = uri.resolve(directive.uri.stringValue); 182 var referencedUri = uri.resolve(directive.uri.stringValue);
169 if (directive is PartDirective) { 183 if (directive is PartDirective) {
170 library.parts.add(referencedUri); 184 library.parts.add(referencedUri);
171 } else { 185 } else {
172 _WalkerNode dependencyNode = walker.nodeForUri(referencedUri); 186 handleDependency(referencedUri);
173 dependencies.add(dependencyNode);
174 library.dependencies.add(dependencyNode.library);
175 } 187 }
176 } 188 }
177 } 189 }
190 if (!coreUriFound) {
191 handleDependency(dartCoreUri);
192 }
178 return dependencies; 193 return dependencies;
179 } 194 }
180 } 195 }
OLDNEW
« no previous file with comments | « no previous file | pkg/front_end/test/dependency_grapher_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698