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

Side by Side Diff: pkg/analyzer/lib/src/summary/package_bundle_reader.dart

Issue 2829253002: Add support for external summaries bundle in AnalysisDriver. (Closed)
Patch Set: Fixes for review comments. Created 3 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 import 'dart:io' as io; 1 import 'dart:io' as io;
2 import 'dart:math' show min; 2 import 'dart:math' show min;
3 3
4 import 'package:analyzer/dart/element/element.dart'; 4 import 'package:analyzer/dart/element/element.dart';
5 import 'package:analyzer/file_system/file_system.dart'; 5 import 'package:analyzer/file_system/file_system.dart';
6 import 'package:analyzer/src/context/cache.dart'; 6 import 'package:analyzer/src/context/cache.dart';
7 import 'package:analyzer/src/context/context.dart'; 7 import 'package:analyzer/src/context/context.dart';
8 import 'package:analyzer/src/dart/element/element.dart'; 8 import 'package:analyzer/src/dart/element/element.dart';
9 import 'package:analyzer/src/generated/engine.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; 10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider;
11 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
13 import 'package:analyzer/src/generated/utilities_dart.dart'; 13 import 'package:analyzer/src/generated/utilities_dart.dart';
14 import 'package:analyzer/src/summary/format.dart'; 14 import 'package:analyzer/src/summary/format.dart';
15 import 'package:analyzer/src/summary/idl.dart'; 15 import 'package:analyzer/src/summary/idl.dart';
16 import 'package:analyzer/src/summary/resynthesize.dart'; 16 import 'package:analyzer/src/summary/resynthesize.dart';
17 import 'package:analyzer/src/task/dart.dart'; 17 import 'package:analyzer/src/task/dart.dart';
18 import 'package:analyzer/task/dart.dart'; 18 import 'package:analyzer/task/dart.dart';
19 import 'package:analyzer/task/general.dart'; 19 import 'package:analyzer/task/general.dart';
20 import 'package:analyzer/task/model.dart'; 20 import 'package:analyzer/task/model.dart';
21 import 'package:front_end/src/base/source.dart'; 21 import 'package:front_end/src/base/source.dart';
22 22
23 /** 23 /**
24 * A [ConflictingSummaryException] indicates that two different summaries
25 * provided to a [SummaryDataStore] conflict.
26 */
27 class ConflictingSummaryException implements Exception {
28 final String duplicatedUri;
29 final String summary1Uri;
30 final String summary2Uri;
31 String _message;
32
33 ConflictingSummaryException(Iterable<String> summaryPaths, this.duplicatedUri,
34 this.summary1Uri, this.summary2Uri) {
35 // Paths are often quite long. Find and extract out a common prefix to
36 // build a more readable error message.
37 var prefix = _commonPrefix(summaryPaths.toList());
38 _message = '''
39 These summaries conflict because they overlap:
40 - ${summary1Uri.substring(prefix)}
41 - ${summary2Uri.substring(prefix)}
42 Both contain the file: ${duplicatedUri}.
43 This typically indicates an invalid build rule where two or more targets
44 include the same source.
45 ''';
46 }
47
48 String toString() => _message;
49
50 /// Given a set of file paths, find a common prefix.
51 int _commonPrefix(List<String> strings) {
52 if (strings.isEmpty) return 0;
53 var first = strings.first;
54 int common = first.length;
55 for (int i = 1; i < strings.length; ++i) {
56 var current = strings[i];
57 common = min(common, current.length);
58 for (int j = 0; j < common; ++j) {
59 if (first[j] != current[j]) {
60 common = j;
61 if (common == 0) return 0;
62 break;
63 }
64 }
65 }
66 // The prefix should end with a file separator.
67 var last =
68 first.substring(0, common).lastIndexOf(io.Platform.pathSeparator);
69 return last < 0 ? 0 : last + 1;
70 }
71 }
72
73 /**
24 * The [ResultProvider] that provides results from input package summaries. 74 * The [ResultProvider] that provides results from input package summaries.
25 */ 75 */
26 class InputPackagesResultProvider extends ResynthesizerResultProvider { 76 class InputPackagesResultProvider extends ResynthesizerResultProvider {
27 InputPackagesResultProvider( 77 InputPackagesResultProvider(
28 InternalAnalysisContext context, SummaryDataStore dataStore) 78 InternalAnalysisContext context, SummaryDataStore dataStore)
29 : super(context, dataStore) { 79 : super(context, dataStore) {
30 createResynthesizer(); 80 createResynthesizer();
31 context.typeProvider = resynthesizer.typeProvider; 81 context.typeProvider = resynthesizer.typeProvider;
32 } 82 }
33 83
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 } 350 }
301 351
302 @override 352 @override
303 bool hasLibrarySummary(String uri) { 353 bool hasLibrarySummary(String uri) {
304 LinkedLibrary linkedLibrary = _dataStore.linkedMap[uri]; 354 LinkedLibrary linkedLibrary = _dataStore.linkedMap[uri];
305 return linkedLibrary != null; 355 return linkedLibrary != null;
306 } 356 }
307 } 357 }
308 358
309 /** 359 /**
310 * A [ConflictingSummaryException] indicates that two different summaries
311 * provided to a [SummaryDataStore] conflict.
312 */
313 class ConflictingSummaryException implements Exception {
314 final String duplicatedUri;
315 final String summary1Uri;
316 final String summary2Uri;
317 String _message;
318
319 ConflictingSummaryException(Iterable<String> summaryPaths, this.duplicatedUri,
320 this.summary1Uri, this.summary2Uri) {
321 // Paths are often quite long. Find and extract out a common prefix to
322 // build a more readable error message.
323 var prefix = _commonPrefix(summaryPaths.toList());
324 _message = '''
325 These summaries conflict because they overlap:
326 - ${summary1Uri.substring(prefix)}
327 - ${summary2Uri.substring(prefix)}
328 Both contain the file: ${duplicatedUri}.
329 This typically indicates an invalid build rule where two or more targets
330 include the same source.
331 ''';
332 }
333
334 /// Given a set of file paths, find a common prefix.
335 int _commonPrefix(List<String> strings) {
336 if (strings.isEmpty) return 0;
337 var first = strings.first;
338 int common = first.length;
339 for (int i = 1; i < strings.length; ++i) {
340 var current = strings[i];
341 common = min(common, current.length);
342 for (int j = 0; j < common; ++j) {
343 if (first[j] != current[j]) {
344 common = j;
345 if (common == 0) return 0;
346 break;
347 }
348 }
349 }
350 // The prefix should end with a file separator.
351 var last =
352 first.substring(0, common).lastIndexOf(io.Platform.pathSeparator);
353 return last < 0 ? 0 : last + 1;
354 }
355
356 String toString() => _message;
357 }
358
359 /**
360 * A [SummaryDataStore] is a container for the data extracted from a set of 360 * A [SummaryDataStore] is a container for the data extracted from a set of
361 * summary package bundles. It contains maps which can be used to find linked 361 * summary package bundles. It contains maps which can be used to find linked
362 * and unlinked summaries by URI. 362 * and unlinked summaries by URI.
363 */ 363 */
364 class SummaryDataStore { 364 class SummaryDataStore {
365 /** 365 /**
366 * List of all [PackageBundle]s. 366 * List of all [PackageBundle]s.
367 */ 367 */
368 final List<PackageBundle> bundles = <PackageBundle>[]; 368 final List<PackageBundle> bundles = <PackageBundle>[];
369 369
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 } 465 }
466 466
467 /** 467 /**
468 * Add the given [linkedLibrary] with the given [uri]. 468 * Add the given [linkedLibrary] with the given [uri].
469 */ 469 */
470 void addLinkedLibrary(String uri, LinkedLibrary linkedLibrary) { 470 void addLinkedLibrary(String uri, LinkedLibrary linkedLibrary) {
471 linkedMap[uri] = linkedLibrary; 471 linkedMap[uri] = linkedLibrary;
472 } 472 }
473 473
474 /** 474 /**
475 * Add into this store the unlinked units and linked libraries of [other].
476 */
477 void addStore(SummaryDataStore other) {
478 unlinkedMap.addAll(other.unlinkedMap);
479 linkedMap.addAll(other.linkedMap);
480 }
481
482 /**
475 * Add the given [unlinkedUnit] with the given [uri]. 483 * Add the given [unlinkedUnit] with the given [uri].
476 */ 484 */
477 void addUnlinkedUnit(String uri, UnlinkedUnit unlinkedUnit) { 485 void addUnlinkedUnit(String uri, UnlinkedUnit unlinkedUnit) {
478 unlinkedMap[uri] = unlinkedUnit; 486 unlinkedMap[uri] = unlinkedUnit;
479 } 487 }
480 488
481 /** 489 /**
482 * Return a list of absolute URIs of the libraries that contain the unit with 490 * Return a list of absolute URIs of the libraries that contain the unit with
483 * the given [unitUriString], or `null` if no such library is in the store. 491 * the given [unitUriString], or `null` if no such library is in the store.
484 */ 492 */
(...skipping 11 matching lines...) Expand all
496 String partAbsoluteUriString = 504 String partAbsoluteUriString =
497 resolveRelativeUri(libraryUri, partUri).toString(); 505 resolveRelativeUri(libraryUri, partUri).toString();
498 if (partAbsoluteUriString == unitUriString) { 506 if (partAbsoluteUriString == unitUriString) {
499 libraryUriStrings.add(unlinkedUnitUriString); 507 libraryUriStrings.add(unlinkedUnitUriString);
500 } 508 }
501 } 509 }
502 }); 510 });
503 return libraryUriStrings.isNotEmpty ? libraryUriStrings : null; 511 return libraryUriStrings.isNotEmpty ? libraryUriStrings : null;
504 } 512 }
505 513
514 /**
515 * Return `true` if the store contains the unlinked summary for the unit
516 * with the given absolute [uri].
517 */
518 bool hasUnlinkedUnit(String uri) {
519 return unlinkedMap.containsKey(uri);
520 }
521
506 void _fillMaps(String path, ResourceProvider resourceProvider) { 522 void _fillMaps(String path, ResourceProvider resourceProvider) {
507 List<int> buffer; 523 List<int> buffer;
508 if (resourceProvider != null) { 524 if (resourceProvider != null) {
509 var file = resourceProvider.getFile(path); 525 var file = resourceProvider.getFile(path);
510 buffer = file.readAsBytesSync(); 526 buffer = file.readAsBytesSync();
511 } else { 527 } else {
512 io.File file = new io.File(path); 528 io.File file = new io.File(path);
513 buffer = file.readAsBytesSync(); 529 buffer = file.readAsBytesSync();
514 } 530 }
515 PackageBundle bundle = new PackageBundle.fromBuffer(buffer); 531 PackageBundle bundle = new PackageBundle.fromBuffer(buffer);
516 addBundle(path, bundle); 532 addBundle(path, bundle);
517 } 533 }
518 } 534 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/library_context.dart ('k') | pkg/analyzer/test/src/dart/analysis/base.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698