Skip to content

Commit e691b45

Browse files
johnniwintherCommit Queue
authored andcommitted
[model] Add stack trace helpers
Change-Id: I85088990e17862a6837d0a5ad03386bbf898c7f6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412282 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent 07724a0 commit e691b45

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (c) 2025, 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 file.
4+
5+
import 'dart:collection';
6+
7+
/// Debug helper that tracks updates to a property.
8+
///
9+
/// Use this in debugging to record stack traces for when a property is updated.
10+
/// For instance to track the field `bar` change
11+
///
12+
/// class Foo {
13+
/// Bar bar;
14+
/// Foo(this.bar);
15+
/// }
16+
///
17+
/// to
18+
///
19+
/// class Foo {
20+
/// StackTraceValue<Bar> _bar;
21+
/// Foo(Bar bar) : _bar = StackTraceValue(bar);
22+
/// Bar get bar => _bar.value;
23+
/// void set bar(Bar value) {
24+
/// _bar.value = value;
25+
/// }
26+
/// }
27+
///
28+
class StackTraceValue<T> {
29+
T _value;
30+
List<(T, StackTrace)> _stackTraces = [];
31+
32+
StackTraceValue(this._value) {
33+
_stackTraces.add((_value, StackTrace.current));
34+
}
35+
36+
T get value => _value;
37+
38+
void set value(T v) {
39+
_stackTraces.add((_value, StackTrace.current));
40+
_value = v;
41+
}
42+
}
43+
44+
/// Debug helper that tracks updates to a list.
45+
///
46+
/// Use this in debugging to record stack traces for when a list is updated.
47+
/// For instance to track the list `bars` change
48+
///
49+
/// class Foo {
50+
/// List<Bar> bars;
51+
/// Foo(this.bars);
52+
/// }
53+
///
54+
/// to
55+
///
56+
/// class Foo {
57+
/// StackTraceList<Bar> _bars;
58+
/// Foo(List<Bar> bars) : _bars = StackTraceList(bars);
59+
/// List<Bar> get bars => _bars;
60+
/// void set bars(List<Bar> value) {
61+
/// _bars.value = value;
62+
/// }
63+
/// }
64+
///
65+
class StackTraceList<T> with ListMixin<T> implements List<T> {
66+
List<T> _list;
67+
List<(String, Object?, StackTrace)> _stackTraces = [];
68+
69+
StackTraceList(this._list) {
70+
_stackTraces.add(('init', _list, StackTrace.current));
71+
}
72+
73+
List<T> get value => _list;
74+
75+
void set value(List<T> v) {
76+
_stackTraces.add(('value', v, StackTrace.current));
77+
_list = v;
78+
}
79+
80+
@override
81+
void add(T element) {
82+
_stackTraces.add(('add', element, StackTrace.current));
83+
_list.add(element);
84+
}
85+
86+
@override
87+
void addAll(Iterable<T> iterable) {
88+
_stackTraces.add(('add', iterable.toList(), StackTrace.current));
89+
_list.addAll(iterable);
90+
}
91+
92+
@override
93+
void clear() {
94+
_stackTraces.add(('clear', null, StackTrace.current));
95+
_list.clear();
96+
}
97+
98+
@override
99+
int get length => _list.length;
100+
101+
@override
102+
void set length(int newLength) {
103+
_stackTraces.add(('length=', newLength, StackTrace.current));
104+
_list.length = newLength;
105+
}
106+
107+
@override
108+
T operator [](int index) => _list[index];
109+
110+
@override
111+
void operator []=(int index, T value) {
112+
_stackTraces.add(('[$index]=', value, StackTrace.current));
113+
_list[index] = value;
114+
}
115+
116+
@override
117+
bool remove(Object? element) {
118+
_stackTraces.add(('remove', element, StackTrace.current));
119+
return _list.remove(element);
120+
}
121+
}

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ bail
133133
balance
134134
bang
135135
bar
136+
bars
136137
basename
137138
basically
138139
basis

0 commit comments

Comments
 (0)