Skip to content

Commit ffd7576

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Duplicate kernel package's graph.dart in analyzer, and prepare to publish 0.39.2+1.
Analyzer has dropped its dependency on kernel, so we can no longer share this code. This will allow us to re-publish analyzer and address issue #39702. Bug: #39702 Change-Id: I20c3505c450dddb1e833fa6e56291ab39c32fe0c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127560 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent eb862ca commit ffd7576

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

pkg/analyzer/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.39.2+1
2+
* Fixed bug #39702.
3+
14
## 0.39.2
25
* Deprecated `AnalysisSession.typeProvider` and `AnalysisSession.typeSystem`.
36
Please use the corresponding getters in `LibraryElement` instead.

pkg/analyzer/lib/src/summary2/default_types_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:analyzer/src/summary2/function_type_builder.dart';
1212
import 'package:analyzer/src/summary2/lazy_ast.dart';
1313
import 'package:analyzer/src/summary2/named_type_builder.dart';
1414
import 'package:analyzer/src/summary2/type_builder.dart';
15-
import 'package:kernel/util/graph.dart' show Graph, computeStrongComponents;
15+
import 'package:analyzer/src/util/graph.dart';
1616

1717
class DefaultTypesBuilder {
1818
void build(List<AstNode> nodes) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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.md file.
4+
5+
abstract class Graph<T> {
6+
Iterable<T> get vertices;
7+
8+
Iterable<T> neighborsOf(T vertex);
9+
}
10+
11+
/// Computes the strongly connected components of [graph].
12+
///
13+
/// This implementation is based on [Dijkstra's path-based strong component
14+
/// algorithm]
15+
/// (https://en.wikipedia.org/wiki/Path-based_strong_component_algorithm#Description).
16+
List<List<T>> computeStrongComponents<T>(Graph<T> graph) {
17+
List<List<T>> result = <List<T>>[];
18+
int count = 0;
19+
Map<T, int> preorderNumbers = <T, int>{};
20+
List<T> unassigned = <T>[];
21+
List<T> candidates = <T>[];
22+
Set<T> assigned = new Set<T>();
23+
24+
void recursivelySearch(T vertex) {
25+
// Step 1: Set the preorder number of [vertex] to [count], and increment
26+
// [count].
27+
preorderNumbers[vertex] = count++;
28+
29+
// Step 2: Push [vertex] onto [unassigned] and also onto [candidates].
30+
unassigned.add(vertex);
31+
candidates.add(vertex);
32+
33+
// Step 3: For each edge from [vertex] to a neighboring vertex [neighbor]:
34+
for (T neighbor in graph.neighborsOf(vertex)) {
35+
int neighborPreorderNumber = preorderNumbers[neighbor];
36+
if (neighborPreorderNumber == null) {
37+
// If the preorder number of [neighbor] has not yet been assigned,
38+
// recursively search [neighbor];
39+
recursivelySearch(neighbor);
40+
} else if (!assigned.contains(neighbor)) {
41+
// Otherwise, if [neighbor] has not yet been assigned to a strongly
42+
// connected component:
43+
//
44+
// * Repeatedly pop vertices from [candidates] until the top element of
45+
// [candidates] has a preorder number less than or equal to the
46+
// preorder number of [neighbor].
47+
while (preorderNumbers[candidates.last] > neighborPreorderNumber) {
48+
candidates.removeLast();
49+
}
50+
}
51+
}
52+
// Step 4: If [vertex] is the top element of [candidates]:
53+
if (candidates.last == vertex) {
54+
// Pop vertices from [unassigned] until [vertex] has been popped, and
55+
// assign the popped vertices to a new component.
56+
List<T> component = <T>[];
57+
while (true) {
58+
T top = unassigned.removeLast();
59+
component.add(top);
60+
assigned.add(top);
61+
if (top == vertex) break;
62+
}
63+
result.add(component);
64+
65+
// Pop [vertex] from [candidates].
66+
candidates.removeLast();
67+
}
68+
}
69+
70+
for (T vertex in graph.vertices) {
71+
if (preorderNumbers[vertex] == null) {
72+
recursivelySearch(vertex);
73+
}
74+
}
75+
76+
return result;
77+
}

pkg/analyzer/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: analyzer
2-
version: 0.39.2
2+
version: 0.39.2+1
33
author: Dart Team <misc@dartlang.org>
44
description: This package provides a library that performs static analysis of Dart code.
55
homepage: https://github.com/dart-lang/sdk/tree/master/pkg/analyzer

0 commit comments

Comments
 (0)