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

Side by Side Diff: pkg/analyzer_cli/lib/src/build_mode.dart

Issue 1838883002: Add the '--build-summary-only-ast' flag for generating summaries using only ASTs. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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_cli.src.build_mode; 5 library analyzer_cli.src.build_mode;
6 6
7 import 'dart:core' hide Resource; 7 import 'dart:core' hide Resource;
8 import 'dart:io' as io; 8 import 'dart:io' as io;
9 9
10 import 'package:analyzer/dart/ast/ast.dart' show CompilationUnit;
10 import 'package:analyzer/dart/element/element.dart'; 11 import 'package:analyzer/dart/element/element.dart';
11 import 'package:analyzer/file_system/file_system.dart'; 12 import 'package:analyzer/file_system/file_system.dart';
12 import 'package:analyzer/file_system/physical_file_system.dart'; 13 import 'package:analyzer/file_system/physical_file_system.dart';
13 import 'package:analyzer/src/generated/engine.dart'; 14 import 'package:analyzer/src/generated/engine.dart';
14 import 'package:analyzer/src/generated/error.dart'; 15 import 'package:analyzer/src/generated/error.dart';
15 import 'package:analyzer/src/generated/java_io.dart'; 16 import 'package:analyzer/src/generated/java_io.dart';
16 import 'package:analyzer/src/generated/sdk_io.dart'; 17 import 'package:analyzer/src/generated/sdk_io.dart';
17 import 'package:analyzer/src/generated/source.dart'; 18 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/generated/source_io.dart'; 19 import 'package:analyzer/src/generated/source_io.dart';
19 import 'package:analyzer/src/summary/format.dart'; 20 import 'package:analyzer/src/summary/format.dart';
21 import 'package:analyzer/src/summary/idl.dart';
20 import 'package:analyzer/src/summary/package_bundle_reader.dart'; 22 import 'package:analyzer/src/summary/package_bundle_reader.dart';
23 import 'package:analyzer/src/summary/prelink.dart';
24 import 'package:analyzer/src/summary/summarize_ast.dart';
21 import 'package:analyzer/src/summary/summarize_elements.dart'; 25 import 'package:analyzer/src/summary/summarize_elements.dart';
26 import 'package:analyzer/task/dart.dart';
22 import 'package:analyzer_cli/src/analyzer_impl.dart'; 27 import 'package:analyzer_cli/src/analyzer_impl.dart';
23 import 'package:analyzer_cli/src/driver.dart'; 28 import 'package:analyzer_cli/src/driver.dart';
24 import 'package:analyzer_cli/src/error_formatter.dart'; 29 import 'package:analyzer_cli/src/error_formatter.dart';
25 import 'package:analyzer_cli/src/options.dart'; 30 import 'package:analyzer_cli/src/options.dart';
26 31
27 /** 32 /**
28 * Analyzer used when the "--build-mode" option is supplied. 33 * Analyzer used when the "--build-mode" option is supplied.
29 */ 34 */
30 class BuildMode { 35 class BuildMode {
31 final CommandLineOptions options; 36 final CommandLineOptions options;
32 final AnalysisStats stats; 37 final AnalysisStats stats;
33 38
34 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; 39 final ResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE;
40 SummaryDataStore summaryDataStore;
35 InternalAnalysisContext context; 41 InternalAnalysisContext context;
36 Map<Uri, JavaFile> uriToFileMap; 42 Map<Uri, JavaFile> uriToFileMap;
37 final List<Source> explicitSources = <Source>[]; 43 final List<Source> explicitSources = <Source>[];
38 44
45 PackageBundleAssembler assembler = new PackageBundleAssembler();
46 final Set<Source> processedSources = new Set<Source>();
47 final Map<Uri, UnlinkedUnit> uriToUnit = <Uri, UnlinkedUnit>{};
48
39 BuildMode(this.options, this.stats); 49 BuildMode(this.options, this.stats);
40 50
41 /** 51 /**
42 * Perform package analysis according to the given [options]. 52 * Perform package analysis according to the given [options].
43 */ 53 */
44 ErrorSeverity analyze() { 54 ErrorSeverity analyze() {
45 // Write initial progress message. 55 // Write initial progress message.
46 if (!options.machineFormat) { 56 if (!options.machineFormat) {
47 outSink.writeln("Analyzing sources ${options.sourceFiles}..."); 57 outSink.writeln("Analyzing sources ${options.sourceFiles}...");
48 } 58 }
(...skipping 28 matching lines...) Expand all
77 while (true) { 87 while (true) {
78 AnalysisResult analysisResult = context.performAnalysisTask(); 88 AnalysisResult analysisResult = context.performAnalysisTask();
79 if (!analysisResult.hasMoreWork) { 89 if (!analysisResult.hasMoreWork) {
80 break; 90 break;
81 } 91 }
82 } 92 }
83 } 93 }
84 94
85 // Write summary. 95 // Write summary.
86 if (options.buildSummaryOutput != null) { 96 if (options.buildSummaryOutput != null) {
87 PackageBundleAssembler assembler = new PackageBundleAssembler();
88 for (Source source in explicitSources) { 97 for (Source source in explicitSources) {
89 if (context.computeKindOf(source) == SourceKind.LIBRARY) { 98 if (context.computeKindOf(source) == SourceKind.LIBRARY) {
90 if (options.buildSummaryFallback) { 99 if (options.buildSummaryFallback) {
91 assembler.addFallbackLibrary(source); 100 assembler.addFallbackLibrary(source);
101 } else if (options.buildSummaryOnlyAst) {
102 _serializeAstBasedSummary(source);
92 } else { 103 } else {
93 LibraryElement libraryElement = context.computeLibraryElement(source ); 104 LibraryElement libraryElement =
105 context.computeLibraryElement(source);
94 assembler.serializeLibraryElement(libraryElement); 106 assembler.serializeLibraryElement(libraryElement);
95 } 107 }
96 } 108 }
97 if (options.buildSummaryFallback) { 109 if (options.buildSummaryFallback) {
98 assembler.addFallbackUnit(source); 110 assembler.addFallbackUnit(source);
99 } 111 }
100 } 112 }
101 // Write the whole package bundle. 113 // Write the whole package bundle.
102 PackageBundleBuilder sdkBundle = assembler.assemble(); 114 PackageBundleBuilder sdkBundle = assembler.assemble();
103 if (options.buildSummaryExcludeInformative) { 115 if (options.buildSummaryExcludeInformative) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 } 147 }
136 148
137 void _createContext() { 149 void _createContext() {
138 DirectoryBasedDartSdk sdk = 150 DirectoryBasedDartSdk sdk =
139 new DirectoryBasedDartSdk(new JavaFile(options.dartSdkPath)); 151 new DirectoryBasedDartSdk(new JavaFile(options.dartSdkPath));
140 sdk.analysisOptions = 152 sdk.analysisOptions =
141 Driver.createAnalysisOptionsForCommandLineOptions(options); 153 Driver.createAnalysisOptionsForCommandLineOptions(options);
142 sdk.useSummary = true; 154 sdk.useSummary = true;
143 155
144 // Read the summaries. 156 // Read the summaries.
145 SummaryDataStore summaryDataStore = 157 summaryDataStore = new SummaryDataStore(options.buildSummaryInputs);
146 new SummaryDataStore(options.buildSummaryInputs); 158
159 // In AST mode include SDK bundle to avoid parsing SDK sources.
160 if (options.buildSummaryOnlyAst) {
161 summaryDataStore.addBundle(null, sdk.getSummarySdkBundle());
162 }
147 163
148 // Create the context. 164 // Create the context.
149 context = AnalysisEngine.instance.createAnalysisContext(); 165 context = AnalysisEngine.instance.createAnalysisContext();
150 context.sourceFactory = new SourceFactory(<UriResolver>[ 166 context.sourceFactory = new SourceFactory(<UriResolver>[
151 new DartUriResolver(sdk), 167 new DartUriResolver(sdk),
152 new InSummaryPackageUriResolver(summaryDataStore), 168 new InSummaryPackageUriResolver(summaryDataStore),
153 new ExplicitSourceResolver(uriToFileMap) 169 new ExplicitSourceResolver(uriToFileMap)
154 ]); 170 ]);
155 171
156 // Set context options. 172 // Set context options.
157 Driver.setAnalysisContextOptions( 173 Driver.setAnalysisContextOptions(context, options,
158 context, options, (AnalysisOptionsImpl contextOptions) { 174 (AnalysisOptionsImpl contextOptions) {
159 if (options.buildSummaryOnlyDiet) { 175 if (options.buildSummaryOnlyDiet) {
160 contextOptions.analyzeFunctionBodies = false; 176 contextOptions.analyzeFunctionBodies = false;
161 } 177 }
162 }); 178 });
163 179
164 // Configure using summaries. 180 // Configure using summaries.
165 context.typeProvider = sdk.context.typeProvider; 181 context.typeProvider = sdk.context.typeProvider;
166 context.resultProvider = 182 context.resultProvider =
167 new InputPackagesResultProvider(context, summaryDataStore); 183 new InputPackagesResultProvider(context, summaryDataStore);
168 } 184 }
(...skipping 19 matching lines...) Expand all
188 } 204 }
189 if (outputPath == null) { 205 if (outputPath == null) {
190 StringSink sink = options.machineFormat ? errorSink : outSink; 206 StringSink sink = options.machineFormat ? errorSink : outSink;
191 sink.write(buffer); 207 sink.write(buffer);
192 } else { 208 } else {
193 new io.File(outputPath).writeAsStringSync(buffer.toString()); 209 new io.File(outputPath).writeAsStringSync(buffer.toString());
194 } 210 }
195 } 211 }
196 212
197 /** 213 /**
214 * Serialize the library with the given [source] into [assembler] using only
215 * its AST, [UnlinkedUnit]s of input packages and ASTs (via [UnlinkedUnit]s)
216 * of package sources.
217 */
218 void _serializeAstBasedSummary(Source source) {
219 Source resolveRelativeUri(String relativeUri) {
220 Source resolvedSource =
221 context.sourceFactory.resolveUri(source, relativeUri);
222 if (resolvedSource == null) {
223 context.sourceFactory.resolveUri(source, relativeUri);
224 throw new StateError('Could not resolve $relativeUri in the context of '
225 '$source (${source.runtimeType})');
226 }
227 return resolvedSource;
228 }
229
230 UnlinkedUnit _getUnlinkedUnit(Source source) {
231 // Maybe an input package contains the source.
232 {
233 String uriStr = source.uri.toString();
234 UnlinkedUnit unlinkedUnit = summaryDataStore.unlinkedMap[uriStr];
235 if (unlinkedUnit != null) {
236 return unlinkedUnit;
237 }
238 }
239 // Parse the source and serialize its AST.
240 return uriToUnit.putIfAbsent(source.uri, () {
241 CompilationUnit unit = context.computeResult(source, PARSED_UNIT);
242 UnlinkedUnitBuilder unlinkedUnit = serializeAstUnlinked(unit);
243 assembler.addUnlinkedUnit(source, unlinkedUnit);
244 return unlinkedUnit;
245 });
246 }
247
248 UnlinkedUnit getPart(String relativeUri) {
249 return _getUnlinkedUnit(resolveRelativeUri(relativeUri));
250 }
251
252 UnlinkedPublicNamespace getImport(String relativeUri) {
253 return getPart(relativeUri).publicNamespace;
254 }
255
256 UnlinkedUnitBuilder definingUnit = _getUnlinkedUnit(source);
257 LinkedLibraryBuilder linkedLibrary =
258 prelink(definingUnit, getPart, getImport);
259 assembler.addLinkedLibrary(source.uri.toString(), linkedLibrary);
260 }
261
262 /**
198 * Convert [sourceEntities] (a list of file specifications of the form 263 * Convert [sourceEntities] (a list of file specifications of the form
199 * "$uri|$path") to a map from URI to path. If an error occurs, report the 264 * "$uri|$path") to a map from URI to path. If an error occurs, report the
200 * error and return null. 265 * error and return null.
201 */ 266 */
202 static Map<Uri, JavaFile> _createUriToFileMap(List<String> sourceEntities) { 267 static Map<Uri, JavaFile> _createUriToFileMap(List<String> sourceEntities) {
203 Map<Uri, JavaFile> uriToFileMap = <Uri, JavaFile>{}; 268 Map<Uri, JavaFile> uriToFileMap = <Uri, JavaFile>{};
204 for (String sourceFile in sourceEntities) { 269 for (String sourceFile in sourceEntities) {
205 int pipeIndex = sourceFile.indexOf('|'); 270 int pipeIndex = sourceFile.indexOf('|');
206 if (pipeIndex == -1) { 271 if (pipeIndex == -1) {
207 // TODO(paulberry): add the ability to guess the URI from the path. 272 // TODO(paulberry): add the ability to guess the URI from the path.
208 errorSink.writeln( 273 errorSink.writeln(
209 'Illegal input file (must be "\$uri|\$path"): $sourceFile'); 274 'Illegal input file (must be "\$uri|\$path"): $sourceFile');
210 return null; 275 return null;
211 } 276 }
212 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex)); 277 Uri uri = Uri.parse(sourceFile.substring(0, pipeIndex));
213 String path = sourceFile.substring(pipeIndex + 1); 278 String path = sourceFile.substring(pipeIndex + 1);
214 uriToFileMap[uri] = new JavaFile(path); 279 uriToFileMap[uri] = new JavaFile(path);
215 } 280 }
216 return uriToFileMap; 281 return uriToFileMap;
217 } 282 }
218 } 283 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/summary/package_bundle_reader.dart ('k') | pkg/analyzer_cli/lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698