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

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

Issue 2825503002: Revert "Revert "Report error if the same uri appears in multiple summaries."" (Closed)
Patch Set: Fix broken test 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
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/package_bundle_reader_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 import 'dart:io' as io; 1 import 'dart:io' as io;
2 import 'dart:math' show min;
2 3
3 import 'package:analyzer/dart/element/element.dart'; 4 import 'package:analyzer/dart/element/element.dart';
4 import 'package:analyzer/file_system/file_system.dart'; 5 import 'package:analyzer/file_system/file_system.dart';
5 import 'package:analyzer/src/context/cache.dart'; 6 import 'package:analyzer/src/context/cache.dart';
6 import 'package:analyzer/src/context/context.dart'; 7 import 'package:analyzer/src/context/context.dart';
7 import 'package:analyzer/src/dart/element/element.dart'; 8 import 'package:analyzer/src/dart/element/element.dart';
8 import 'package:analyzer/src/generated/engine.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
9 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; 10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider;
10 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
11 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 } 300 }
300 301
301 @override 302 @override
302 bool hasLibrarySummary(String uri) { 303 bool hasLibrarySummary(String uri) {
303 LinkedLibrary linkedLibrary = _dataStore.linkedMap[uri]; 304 LinkedLibrary linkedLibrary = _dataStore.linkedMap[uri];
304 return linkedLibrary != null; 305 return linkedLibrary != null;
305 } 306 }
306 } 307 }
307 308
308 /** 309 /**
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 /**
309 * 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
310 * 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
311 * and unlinked summaries by URI. 362 * and unlinked summaries by URI.
312 */ 363 */
313 class SummaryDataStore { 364 class SummaryDataStore {
314 /** 365 /**
315 * List of all [PackageBundle]s. 366 * List of all [PackageBundle]s.
316 */ 367 */
317 final List<PackageBundle> bundles = <PackageBundle>[]; 368 final List<PackageBundle> bundles = <PackageBundle>[];
318 369
(...skipping 16 matching lines...) Expand all
335 * Map from the URI of a library to the linked summary of that library. 386 * Map from the URI of a library to the linked summary of that library.
336 */ 387 */
337 final Map<String, LinkedLibrary> linkedMap = <String, LinkedLibrary>{}; 388 final Map<String, LinkedLibrary> linkedMap = <String, LinkedLibrary>{};
338 389
339 /** 390 /**
340 * Map from the URI of a library to the summary path that contained it. 391 * Map from the URI of a library to the summary path that contained it.
341 */ 392 */
342 final Map<String, String> uriToSummaryPath = <String, String>{}; 393 final Map<String, String> uriToSummaryPath = <String, String>{};
343 394
344 /** 395 /**
396 * List of summary paths.
397 */
398 final Iterable<String> _summaryPaths;
399
400 /**
345 * Create a [SummaryDataStore] and populate it with the summaries in 401 * Create a [SummaryDataStore] and populate it with the summaries in
346 * [summaryPaths]. If [recordDependencyInfo] is `true`, record 402 * [summaryPaths]. If [recordDependencyInfo] is `true`, record
347 * [PackageDependencyInfo] for each summary, for later access via 403 * [PackageDependencyInfo] for each summary, for later access via
348 * [dependencies]. 404 * [dependencies].
349 */ 405 */
350 SummaryDataStore(Iterable<String> summaryPaths, 406 SummaryDataStore(Iterable<String> summaryPaths,
351 {bool recordDependencyInfo: false, ResourceProvider resourceProvider}) 407 {bool recordDependencyInfo: false, ResourceProvider resourceProvider})
352 : dependencies = 408 : _summaryPaths = summaryPaths,
409 dependencies =
353 recordDependencyInfo ? <PackageDependencyInfoBuilder>[] : null { 410 recordDependencyInfo ? <PackageDependencyInfoBuilder>[] : null {
354 summaryPaths.forEach((String path) => _fillMaps(path, resourceProvider)); 411 summaryPaths.forEach((String path) => _fillMaps(path, resourceProvider));
355 } 412 }
356 413
357 /** 414 /**
358 * Add the given [bundle] loaded from the file with the given [path]. 415 * Add the given [bundle] loaded from the file with the given [path].
359 */ 416 */
360 void addBundle(String path, PackageBundle bundle) { 417 void addBundle(String path, PackageBundle bundle) {
361 bundles.add(bundle); 418 bundles.add(bundle);
362 if (dependencies != null) { 419 if (dependencies != null) {
(...skipping 14 matching lines...) Expand all
377 } 434 }
378 dependencies.add(new PackageDependencyInfoBuilder( 435 dependencies.add(new PackageDependencyInfoBuilder(
379 includedPackageNames: includedPackageNames.toList()..sort(), 436 includedPackageNames: includedPackageNames.toList()..sort(),
380 includesDartUris: includesDartUris, 437 includesDartUris: includesDartUris,
381 includesFileUris: includesFileUris, 438 includesFileUris: includesFileUris,
382 apiSignature: bundle.apiSignature, 439 apiSignature: bundle.apiSignature,
383 summaryPath: path)); 440 summaryPath: path));
384 } 441 }
385 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) { 442 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) {
386 String uri = bundle.unlinkedUnitUris[i]; 443 String uri = bundle.unlinkedUnitUris[i];
444 if (uriToSummaryPath.containsKey(uri) &&
445 (uriToSummaryPath[uri] != path)) {
446 throw new ConflictingSummaryException(
447 _summaryPaths, uri, uriToSummaryPath[uri], path);
448 }
387 uriToSummaryPath[uri] = path; 449 uriToSummaryPath[uri] = path;
388 addUnlinkedUnit(uri, bundle.unlinkedUnits[i]); 450 addUnlinkedUnit(uri, bundle.unlinkedUnits[i]);
389 } 451 }
390 for (int i = 0; i < bundle.linkedLibraryUris.length; i++) { 452 for (int i = 0; i < bundle.linkedLibraryUris.length; i++) {
391 String uri = bundle.linkedLibraryUris[i]; 453 String uri = bundle.linkedLibraryUris[i];
392 addLinkedLibrary(uri, bundle.linkedLibraries[i]); 454 addLinkedLibrary(uri, bundle.linkedLibraries[i]);
393 } 455 }
394 } 456 }
395 457
396 /** 458 /**
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 var file = resourceProvider.getFile(path); 500 var file = resourceProvider.getFile(path);
439 buffer = file.readAsBytesSync(); 501 buffer = file.readAsBytesSync();
440 } else { 502 } else {
441 io.File file = new io.File(path); 503 io.File file = new io.File(path);
442 buffer = file.readAsBytesSync(); 504 buffer = file.readAsBytesSync();
443 } 505 }
444 PackageBundle bundle = new PackageBundle.fromBuffer(buffer); 506 PackageBundle bundle = new PackageBundle.fromBuffer(buffer);
445 addBundle(path, bundle); 507 addBundle(path, bundle);
446 } 508 }
447 } 509 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/package_bundle_reader_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698