Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bd660d9

Browse files
stereotype441Commit Bot
authored andcommitted
Flow analysis: additional debug support.
I've found these changes helpful as part of developing the new "field promotion" feature. These changes have no effect unless the `FlowAnalysisDebug` class is used. Bug: dart-lang/language#2020 Change-Id: I7badadc14bf901e77b8c166920aedf902093d7e1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250220 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent a602f74 commit bd660d9

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,10 @@ abstract class FlowAnalysis<Node extends Object, Statement extends Node,
10321032
class FlowAnalysisDebug<Node extends Object, Statement extends Node,
10331033
Expression extends Object, Variable extends Object, Type extends Object>
10341034
implements FlowAnalysis<Node, Statement, Expression, Variable, Type> {
1035+
static int _nextCallbackId = 0;
1036+
1037+
static Expando<String> _description = new Expando<String>();
1038+
10351039
FlowAnalysis<Node, Statement, Expression, Variable, Type> _wrapped;
10361040

10371041
bool _exceptionOccurred = false;
@@ -1538,16 +1542,18 @@ class FlowAnalysisDebug<Node extends Object, Statement extends Node,
15381542

15391543
@override
15401544
Map<Type, NonPromotionReason> Function() whyNotPromoted(Expression target) {
1541-
return _wrap(
1542-
'whyNotPromoted($target)', () => _wrapped.whyNotPromoted(target),
1545+
return _wrap('whyNotPromoted($target)',
1546+
() => _trackWhyNotPromoted(_wrapped.whyNotPromoted(target)),
15431547
isQuery: true);
15441548
}
15451549

15461550
@override
15471551
Map<Type, NonPromotionReason> Function() whyNotPromotedImplicitThis(
15481552
Type staticType) {
1549-
return _wrap('whyNotPromotedImplicitThis($staticType)',
1550-
() => _wrapped.whyNotPromotedImplicitThis(staticType),
1553+
return _wrap(
1554+
'whyNotPromotedImplicitThis($staticType)',
1555+
() => _trackWhyNotPromoted(
1556+
_wrapped.whyNotPromotedImplicitThis(staticType)),
15511557
isQuery: true);
15521558
}
15531559

@@ -1561,6 +1567,19 @@ class FlowAnalysisDebug<Node extends Object, Statement extends Node,
15611567
@override
15621568
void _dumpState() => _wrapped._dumpState();
15631569

1570+
/// Wraps [callback] so that when it is called, the call (and its return
1571+
/// value) will be printed to the console. Also registers the wrapped
1572+
/// callback in [_description] so that it will be given a unique identifier
1573+
/// when printed to the console.
1574+
Map<Type, NonPromotionReason> Function() _trackWhyNotPromoted(
1575+
Map<Type, NonPromotionReason> Function() callback) {
1576+
String callbackToString = '#CALLBACK${_nextCallbackId++}';
1577+
Map<Type, NonPromotionReason> Function() wrappedCallback =
1578+
() => _wrap('$callbackToString()', callback, isQuery: true);
1579+
_description[wrappedCallback] = callbackToString;
1580+
return wrappedCallback;
1581+
}
1582+
15641583
T _wrap<T>(String description, T callback(),
15651584
{bool isQuery: false, bool? isPure}) {
15661585
isPure ??= isQuery;
@@ -1578,10 +1597,18 @@ class FlowAnalysisDebug<Node extends Object, Statement extends Node,
15781597
_wrapped._dumpState();
15791598
}
15801599
if (isQuery) {
1581-
print(' => $result');
1600+
print(' => ${_describe(result)}');
15821601
}
15831602
return result;
15841603
}
1604+
1605+
static String _describe(Object? value) {
1606+
if (value != null && value is! String && value is! num && value is! bool) {
1607+
String? description = _description[value];
1608+
if (description != null) return description;
1609+
}
1610+
return value.toString();
1611+
}
15851612
}
15861613

15871614
/// An instance of the [FlowModel] class represents the information gathered by
@@ -2613,6 +2640,9 @@ class ReferenceWithType<Variable extends Object, Type extends Object> {
26132640
final Type type;
26142641

26152642
ReferenceWithType(this.reference, this.type);
2643+
2644+
@override
2645+
String toString() => 'ReferenceWithType($reference, $type)';
26162646
}
26172647

26182648
/// Data structure representing a unique value that a variable might take on
@@ -2859,6 +2889,9 @@ class VariableModel<Variable extends Object, Type extends Object> {
28592889
if (nonPromotionHistory != null) {
28602890
parts.add('nonPromotionHistory: $nonPromotionHistory');
28612891
}
2892+
if (properties.isNotEmpty) {
2893+
parts.add('properties: $properties');
2894+
}
28622895
return 'VariableModel(${parts.join(', ')})';
28632896
}
28642897

@@ -3357,6 +3390,9 @@ class VariableReference<Variable extends Object, Type extends Object>
33573390
variableInfo[variable] = variableModel;
33583391
}
33593392

3393+
@override
3394+
String toString() => 'VariableReference($variable)';
3395+
33603396
@override
33613397
VariableModel<Variable, Type>? _getInfo(
33623398
Map<Variable?, VariableModel<Variable, Type>> variableInfo) =>
@@ -5025,6 +5061,10 @@ class _PropertyGetReference<Variable extends Object, Type extends Object>
50255061
target.storeInfo(variableInfo, targetInfo.setProperties(newProperties));
50265062
}
50275063

5064+
@override
5065+
String toString() =>
5066+
'_PropertyGetReference($target, $propertyName, $propertyMember)';
5067+
50285068
@override
50295069
VariableModel<Variable, Type>? _getInfo(
50305070
Map<Variable?, VariableModel<Variable, Type>> variableInfo) {

0 commit comments

Comments
 (0)