tla-ts 0.0.8-dev.d9bffda → 0.0.9-dev.0dd11f695
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +40 -40
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +80 -10
- package/dist/index.d.ts +80 -10
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +116 -36
- package/dist/prove.js +116 -36
- package/dist/run.cjs +143 -31
- package/dist/run.js +143 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +128 -103
- package/dist/transfer.js +128 -103
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +3 -1
- package/src/ast.ts +7 -21
- package/src/bags.ts +99 -99
- package/src/builtins.ts +44 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check.ts +605 -311
- package/src/cli/options.ts +12 -3
- package/src/cli/run.ts +2 -2
- package/src/cli/runtime-invariants.ts +135 -0
- package/src/cli/shared.ts +20 -9
- package/src/cli/tlc-arguments.ts +646 -0
- package/src/cli/tlc-usage.ts +362 -0
- package/src/cli/tlc.ts +571 -185
- package/src/cli/typescript.ts +26 -0
- package/src/cli/xml-exporter.ts +4 -10
- package/src/codegen.ts +190 -46
- package/src/config.ts +101 -68
- package/src/coverage.ts +855 -0
- package/src/dependencies.ts +20 -28
- package/src/dfid.ts +636 -0
- package/src/environment.ts +266 -98
- package/src/errors.ts +142 -45
- package/src/evaluate.ts +632 -298
- package/src/exit-codes.ts +37 -1
- package/src/index.ts +3 -0
- package/src/instances.ts +85 -9
- package/src/java-overrides.ts +2 -12
- package/src/json-reader.ts +456 -0
- package/src/json.ts +262 -139
- package/src/levels.ts +2 -3
- package/src/lexer.ts +17 -26
- package/src/liveness-checker.ts +10 -18
- package/src/liveness.ts +121 -49
- package/src/parallel-worker.ts +14 -10
- package/src/parallel.ts +41 -17
- package/src/parser.ts +436 -90
- package/src/pcal/ast.ts +7 -28
- package/src/pcal/parse.ts +11 -39
- package/src/pcal/tokenize.ts +4 -20
- package/src/pcal/translate.ts +10 -42
- package/src/pcal/validate.ts +3 -21
- package/src/pretty-print.ts +1 -2
- package/src/prover/smt/solver/solver.ts +31 -27
- package/src/randomization.ts +84 -35
- package/src/resolve.ts +18 -60
- package/src/runtime.ts +11 -4
- package/src/sany/builtins.ts +3 -4
- package/src/sany/generator.ts +128 -176
- package/src/sany/levels.ts +6 -30
- package/src/sany/lexer.ts +7 -23
- package/src/sany/linter.ts +31 -29
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +4 -28
- package/src/sany/parse-error.ts +44 -100
- package/src/sany/report.ts +25 -40
- package/src/sany/selectors.ts +8 -22
- package/src/sany/source.ts +8 -15
- package/src/sany/xml.ts +11 -42
- package/src/search.ts +547 -144
- package/src/simulate.ts +694 -0
- package/src/spec-description.ts +166 -48
- package/src/standard-definitions.ts +2 -3
- package/src/standard-modules.ts +264 -68
- package/src/state-graph.ts +51 -18
- package/src/subexpressions.ts +31 -5
- package/src/successors.ts +267 -77
- package/src/symmetry.ts +31 -8
- package/src/tableau.ts +1 -2
- package/src/tlc/fingerprint.ts +183 -61
- package/src/tlc/jar-data.ts +113 -6
- package/src/tlc/messages.ts +122 -37
- package/src/tlc/order.ts +33 -38
- package/src/tlc/random.ts +16 -16
- package/src/tlc/replay.ts +563 -211
- package/src/tlc/summary.ts +2 -4
- package/src/tlc/tlc-operators.ts +81 -36
- package/src/tlc/tlcext-operators.ts +138 -117
- package/src/tlc/trace-spec.ts +273 -0
- package/src/tlc/value-stream.ts +143 -0
- package/src/tlc/version.ts +2 -4
- package/src/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +148 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +832 -255
package/src/coverage.ts
ADDED
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isLocallyResolved,
|
|
3
|
+
type Bound,
|
|
4
|
+
type Definition,
|
|
5
|
+
type Expression,
|
|
6
|
+
type SourceLocation,
|
|
7
|
+
type SourceRange,
|
|
8
|
+
} from "./ast";
|
|
9
|
+
import { formatRange } from "./errors";
|
|
10
|
+
import type { Environment, Scope } from "./evaluate";
|
|
11
|
+
import { getTlcFingerprint } from "./tlc/fingerprint";
|
|
12
|
+
import type { Value } from "./values";
|
|
13
|
+
|
|
14
|
+
export type CoverageKind = "init" | "next" | "invariant" | "constraint" | "implied";
|
|
15
|
+
|
|
16
|
+
interface CoverageRelation {
|
|
17
|
+
kind: CoverageKind;
|
|
18
|
+
label: string;
|
|
19
|
+
expression: Expression;
|
|
20
|
+
scope: Scope | undefined;
|
|
21
|
+
node: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Coverage {
|
|
25
|
+
current: number;
|
|
26
|
+
expressions: (Expression | undefined)[];
|
|
27
|
+
ranges: (SourceRange | undefined)[];
|
|
28
|
+
firstChild: number[];
|
|
29
|
+
lastChild: number[];
|
|
30
|
+
nextSibling: number[];
|
|
31
|
+
prefixes: number[];
|
|
32
|
+
isPart: boolean[];
|
|
33
|
+
isPrimed: boolean[];
|
|
34
|
+
isUnchanged: boolean[];
|
|
35
|
+
recursive: number[];
|
|
36
|
+
lets: (number[] | undefined)[];
|
|
37
|
+
roots: number[];
|
|
38
|
+
primary: number[];
|
|
39
|
+
secondary: number[];
|
|
40
|
+
covered: WeakSet<Expression>;
|
|
41
|
+
values: WeakMap<object, number>;
|
|
42
|
+
registers: Uint8Array;
|
|
43
|
+
relations: CoverageRelation[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface CoverageReport {
|
|
47
|
+
variables: CoverageVariable[];
|
|
48
|
+
relations: CoverageRelationReport[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface CoverageVariable {
|
|
52
|
+
label: string;
|
|
53
|
+
distinct: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CoverageRelationReport {
|
|
57
|
+
kind: CoverageKind;
|
|
58
|
+
label: string;
|
|
59
|
+
distinct: number;
|
|
60
|
+
generated: number;
|
|
61
|
+
expressions: CoverageExpression[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface CoverageExpression {
|
|
65
|
+
location: string;
|
|
66
|
+
depth: number;
|
|
67
|
+
count: number;
|
|
68
|
+
cost: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface Local {
|
|
72
|
+
name: string;
|
|
73
|
+
definition: Definition | undefined;
|
|
74
|
+
scope: Local | undefined;
|
|
75
|
+
parent: Local | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Resolved {
|
|
79
|
+
definition: Definition;
|
|
80
|
+
scope: Local | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const STANDARD_MODULES = new Set([
|
|
84
|
+
"FiniteSets",
|
|
85
|
+
"Sequences",
|
|
86
|
+
"Bags",
|
|
87
|
+
"Naturals",
|
|
88
|
+
"Integers",
|
|
89
|
+
"Reals",
|
|
90
|
+
"RealTime",
|
|
91
|
+
"Randomization",
|
|
92
|
+
"TLC",
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const REGISTER_BITS = 10;
|
|
96
|
+
const REGISTERS = 1 << REGISTER_BITS;
|
|
97
|
+
|
|
98
|
+
export const createCoverage = (variableCount: number): Coverage => ({
|
|
99
|
+
current: -1,
|
|
100
|
+
expressions: [],
|
|
101
|
+
ranges: [],
|
|
102
|
+
firstChild: [],
|
|
103
|
+
lastChild: [],
|
|
104
|
+
nextSibling: [],
|
|
105
|
+
prefixes: [],
|
|
106
|
+
isPart: [],
|
|
107
|
+
isPrimed: [],
|
|
108
|
+
isUnchanged: [],
|
|
109
|
+
recursive: [],
|
|
110
|
+
lets: [],
|
|
111
|
+
roots: [],
|
|
112
|
+
primary: [],
|
|
113
|
+
secondary: [],
|
|
114
|
+
covered: new WeakSet(),
|
|
115
|
+
values: new WeakMap(),
|
|
116
|
+
registers: new Uint8Array(variableCount * REGISTERS),
|
|
117
|
+
relations: [],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const addNode = (
|
|
121
|
+
coverage: Coverage,
|
|
122
|
+
expression: Expression | undefined,
|
|
123
|
+
range: SourceRange | undefined,
|
|
124
|
+
parent: number,
|
|
125
|
+
isPart: boolean,
|
|
126
|
+
) => {
|
|
127
|
+
const node = coverage.expressions.length;
|
|
128
|
+
coverage.expressions.push(expression);
|
|
129
|
+
coverage.ranges.push(range);
|
|
130
|
+
coverage.firstChild.push(-1);
|
|
131
|
+
coverage.lastChild.push(-1);
|
|
132
|
+
coverage.nextSibling.push(-1);
|
|
133
|
+
coverage.prefixes.push(-1);
|
|
134
|
+
coverage.isPart.push(isPart);
|
|
135
|
+
coverage.isPrimed.push(false);
|
|
136
|
+
coverage.isUnchanged.push(expression?.kind === "Unchanged");
|
|
137
|
+
coverage.recursive.push(-1);
|
|
138
|
+
coverage.lets.push(undefined);
|
|
139
|
+
coverage.roots.push(parent === -1 ? node : coverage.roots[parent]);
|
|
140
|
+
coverage.primary.push(0);
|
|
141
|
+
coverage.secondary.push(0);
|
|
142
|
+
if (expression) coverage.covered.add(expression);
|
|
143
|
+
if (parent === -1) return node;
|
|
144
|
+
const last = coverage.lastChild[parent];
|
|
145
|
+
if (last === -1) coverage.firstChild[parent] = node;
|
|
146
|
+
if (last !== -1) coverage.nextSibling[last] = node;
|
|
147
|
+
coverage.lastChild[parent] = node;
|
|
148
|
+
return node;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// A next-state action that another shares its predicate with shares its counts, as in TLC.
|
|
152
|
+
export const addCoverageRelation = (
|
|
153
|
+
coverage: Coverage,
|
|
154
|
+
kind: CoverageKind,
|
|
155
|
+
label: string,
|
|
156
|
+
expression: Expression,
|
|
157
|
+
scope?: Scope,
|
|
158
|
+
) => {
|
|
159
|
+
const shared =
|
|
160
|
+
kind === "next"
|
|
161
|
+
? coverage.relations.find(
|
|
162
|
+
(relation) => relation.kind === "next" && relation.expression === expression,
|
|
163
|
+
)
|
|
164
|
+
: undefined;
|
|
165
|
+
if (shared) return shared.node;
|
|
166
|
+
const node = addNode(coverage, undefined, expression.location.range, -1, false);
|
|
167
|
+
coverage.relations.push({ kind, label, expression, scope, node });
|
|
168
|
+
return node;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export const findCoverageRelation = (
|
|
172
|
+
coverage: Coverage,
|
|
173
|
+
kind: CoverageKind,
|
|
174
|
+
expression: Expression,
|
|
175
|
+
) =>
|
|
176
|
+
coverage.relations.find(
|
|
177
|
+
(relation) => relation.kind === kind && relation.expression === expression,
|
|
178
|
+
)?.node;
|
|
179
|
+
|
|
180
|
+
const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
|
|
181
|
+
left !== undefined &&
|
|
182
|
+
right !== undefined &&
|
|
183
|
+
left.module === right.module &&
|
|
184
|
+
left.start.line === right.start.line &&
|
|
185
|
+
left.start.column === right.start.column &&
|
|
186
|
+
left.end.line === right.end.line &&
|
|
187
|
+
left.end.column === right.end.column;
|
|
188
|
+
|
|
189
|
+
// TLC names an action after the left-hand side of its definition, and adds the location of the
|
|
190
|
+
// predicate when the action is only part of the definition's body.
|
|
191
|
+
export const getCoverageLabel = (
|
|
192
|
+
name: string | undefined,
|
|
193
|
+
expression: Expression,
|
|
194
|
+
environment: Environment,
|
|
195
|
+
) => {
|
|
196
|
+
const definition = name === undefined ? undefined : environment.definitions.get(name);
|
|
197
|
+
const head = definition?.kind === "Operator" ? definition.head : undefined;
|
|
198
|
+
const range = expression.location.range;
|
|
199
|
+
if (!definition || !head) return `Action ${range ? formatRange(range) : "Unknown location"}`;
|
|
200
|
+
if (isSameRange(definition.body.location.range, range) || !range) {
|
|
201
|
+
return `${definition.name} ${formatRange(head)}`;
|
|
202
|
+
}
|
|
203
|
+
return `${definition.name} ${formatRange(head)} (${range.start.line} ${range.start.column} ${range.end.line} ${range.end.column})`;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const getRangeKey = (range: SourceRange) =>
|
|
207
|
+
`${range.module}:${range.start.line}:${range.start.column}:${range.end.line}:${range.end.column}`;
|
|
208
|
+
|
|
209
|
+
const spanRanges = (first: SourceRange | undefined, last: SourceRange | undefined) =>
|
|
210
|
+
first && last ? { module: first.module, start: first.start, end: last.end } : undefined;
|
|
211
|
+
|
|
212
|
+
const getStartRange = (location: SourceLocation, last: SourceRange | undefined) =>
|
|
213
|
+
last && {
|
|
214
|
+
module: last.module,
|
|
215
|
+
start: location.range?.start ?? { line: location.line, column: location.column },
|
|
216
|
+
end: last.end,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const getJavaHashCode = (text: string) =>
|
|
220
|
+
Array.from({ length: text.length }, (_, index) => text.charCodeAt(index)).reduce(
|
|
221
|
+
(hash, code) => (Math.imul(hash, 31) + code) | 0,
|
|
222
|
+
0,
|
|
223
|
+
) & 0x7fffffff;
|
|
224
|
+
|
|
225
|
+
// SANY walks a LET's definitions in the order of the java.util.Hashtable that holds them.
|
|
226
|
+
const getHashtableOrder = (definitions: Definition[]) => {
|
|
227
|
+
const table = { buckets: Array.from({ length: 11 }, (): Definition[] => []), count: 0 };
|
|
228
|
+
definitions.forEach((definition) => {
|
|
229
|
+
if (table.count >= Math.floor(table.buckets.length * 0.75)) {
|
|
230
|
+
const buckets = Array.from({ length: table.buckets.length * 2 + 1 }, (): Definition[] => []);
|
|
231
|
+
for (let index = table.buckets.length - 1; index >= 0; index--) {
|
|
232
|
+
table.buckets[index].forEach((entry) =>
|
|
233
|
+
buckets[getJavaHashCode(entry.name) % buckets.length].unshift(entry),
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
table.buckets = buckets;
|
|
237
|
+
}
|
|
238
|
+
table.buckets[getJavaHashCode(definition.name) % table.buckets.length].unshift(definition);
|
|
239
|
+
table.count++;
|
|
240
|
+
});
|
|
241
|
+
return table.buckets.toReversed().flat();
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const getBoundSets = (bounds: Bound[]) =>
|
|
245
|
+
bounds.flatMap((bound, index) =>
|
|
246
|
+
index > 0 && bounds[index - 1].set === bound.set ? [] : [bound.set],
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
const bindNames = (names: string[], locals: Local | undefined) =>
|
|
250
|
+
names.reduce<Local | undefined>(
|
|
251
|
+
(parent, name) => ({ name, definition: undefined, scope: undefined, parent }),
|
|
252
|
+
locals,
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
const getBoundNames = (bounds: Bound[]) =>
|
|
256
|
+
bounds.flatMap((bound) =>
|
|
257
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : bound.pattern.names,
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
export const buildCoverage = (
|
|
261
|
+
coverage: Coverage,
|
|
262
|
+
environment: Environment,
|
|
263
|
+
primedRanges: SourceRange[],
|
|
264
|
+
) => {
|
|
265
|
+
const primed = new Set(primedRanges.map(getRangeKey));
|
|
266
|
+
const stack: number[] = [];
|
|
267
|
+
const definitions: (Definition | undefined)[] = [];
|
|
268
|
+
const walking: Definition[] = [];
|
|
269
|
+
const letBodies = new Map<Expression, Expression>();
|
|
270
|
+
|
|
271
|
+
const resolve = (expression: Expression, name: string, locals: Local | undefined) => {
|
|
272
|
+
if (isLocallyResolved(expression)) {
|
|
273
|
+
for (let local = locals; local; local = local.parent) {
|
|
274
|
+
if (local.name !== name) continue;
|
|
275
|
+
return local.definition && { definition: local.definition, scope: local.scope };
|
|
276
|
+
}
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
const target = environment.substitutions.get(name) ?? name;
|
|
280
|
+
if (environment.variableIndex.has(target) || environment.constants.has(target)) {
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
const definition = environment.definitions.get(target);
|
|
284
|
+
const module = definition?.location.range?.module ?? definition?.body.location.range?.module;
|
|
285
|
+
return definition && !STANDARD_MODULES.has(module ?? "")
|
|
286
|
+
? { definition, scope: undefined }
|
|
287
|
+
: undefined;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const enter = (
|
|
291
|
+
expression: Expression | undefined,
|
|
292
|
+
range: SourceRange | undefined,
|
|
293
|
+
walkChildren: () => void,
|
|
294
|
+
isPart = false,
|
|
295
|
+
definition?: Definition,
|
|
296
|
+
) => {
|
|
297
|
+
const node = addNode(coverage, expression, range, stack.at(-1) ?? -1, isPart);
|
|
298
|
+
coverage.isPrimed[node] = range !== undefined && primed.has(getRangeKey(range));
|
|
299
|
+
const body = expression && letBodies.get(expression);
|
|
300
|
+
if (body) {
|
|
301
|
+
stack.forEach((outer) => {
|
|
302
|
+
if (coverage.expressions[outer] === body) (coverage.lets[outer] ??= []).push(node);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
if (definition?.kind === "Operator" && definition.isRecursive) {
|
|
306
|
+
const outer = stack.findLast((item) => definitions[item] === definition);
|
|
307
|
+
if (outer !== undefined) coverage.recursive[node] = outer;
|
|
308
|
+
}
|
|
309
|
+
definitions[node] = definition;
|
|
310
|
+
stack.push(node);
|
|
311
|
+
walkChildren();
|
|
312
|
+
stack.pop();
|
|
313
|
+
return node;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const walkDefinition = (resolved: Resolved) => {
|
|
317
|
+
const definition = resolved.definition;
|
|
318
|
+
if (walking.includes(definition)) return;
|
|
319
|
+
walking.push(definition);
|
|
320
|
+
const names =
|
|
321
|
+
definition.kind === "Function"
|
|
322
|
+
? getBoundNames(definition.bounds)
|
|
323
|
+
: definition.params.map((param) => param.name);
|
|
324
|
+
walk(definition.body, bindNames(names, resolved.scope));
|
|
325
|
+
if (definition.kind === "Function") {
|
|
326
|
+
getBoundSets(definition.bounds).forEach((set) => walk(set, resolved.scope));
|
|
327
|
+
}
|
|
328
|
+
walking.pop();
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const hasParams = (resolved: Resolved | undefined) =>
|
|
332
|
+
resolved?.definition.kind === "Operator" && resolved.definition.params.length > 0;
|
|
333
|
+
|
|
334
|
+
const walkArgument = (
|
|
335
|
+
argument: Expression,
|
|
336
|
+
index: number,
|
|
337
|
+
callee: Definition | undefined,
|
|
338
|
+
locals: Local | undefined,
|
|
339
|
+
) => {
|
|
340
|
+
if (argument.kind === "Lambda") {
|
|
341
|
+
walk(argument.body, bindNames(argument.params, locals));
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (argument.kind === "Identifier") {
|
|
345
|
+
const resolved = resolve(argument, argument.name, locals);
|
|
346
|
+
const isOperator =
|
|
347
|
+
(callee?.kind === "Operator" && (callee.params[index]?.arity ?? 0) > 0) ||
|
|
348
|
+
hasParams(resolved);
|
|
349
|
+
if (isOperator) {
|
|
350
|
+
if (resolved) walkDefinition(resolved);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
walk(argument, locals);
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const walkJunction = (
|
|
358
|
+
expression: Expression & { kind: "And" | "Or" },
|
|
359
|
+
count: number,
|
|
360
|
+
range: SourceRange | undefined,
|
|
361
|
+
locals: Local | undefined,
|
|
362
|
+
): number =>
|
|
363
|
+
enter(
|
|
364
|
+
count === expression.operands.length ? expression : undefined,
|
|
365
|
+
range,
|
|
366
|
+
() => {
|
|
367
|
+
const operands = expression.operands;
|
|
368
|
+
if (count > 2 && !expression.isBulleted) {
|
|
369
|
+
const end = expression.prefixEnds?.[count - 3];
|
|
370
|
+
const prefix = walkJunction(
|
|
371
|
+
expression,
|
|
372
|
+
count - 1,
|
|
373
|
+
range && end ? { ...range, end } : range,
|
|
374
|
+
locals,
|
|
375
|
+
);
|
|
376
|
+
coverage.prefixes[stack.at(-1) ?? -1] = prefix;
|
|
377
|
+
walk(operands[count - 1], locals);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
operands.slice(0, count).forEach((operand) => walk(operand, locals));
|
|
381
|
+
},
|
|
382
|
+
count < expression.operands.length,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
const walk = (expression: Expression, locals: Local | undefined): void => {
|
|
386
|
+
const range = expression.location.range;
|
|
387
|
+
const walkAll = (expressions: Expression[]) =>
|
|
388
|
+
expressions.forEach((item) => walk(item, locals));
|
|
389
|
+
switch (expression.kind) {
|
|
390
|
+
case "Number":
|
|
391
|
+
case "Decimal":
|
|
392
|
+
case "String":
|
|
393
|
+
case "ExceptAt":
|
|
394
|
+
case "Bracket":
|
|
395
|
+
case "Subexpression":
|
|
396
|
+
case "OperatorReference":
|
|
397
|
+
return;
|
|
398
|
+
case "Label":
|
|
399
|
+
walk(expression.body, bindNames(expression.params, locals));
|
|
400
|
+
return;
|
|
401
|
+
case "Lambda":
|
|
402
|
+
walk(expression.body, bindNames(expression.params, locals));
|
|
403
|
+
return;
|
|
404
|
+
case "Let": {
|
|
405
|
+
const inner = expression.definitions.reduce<Local | undefined>(
|
|
406
|
+
(parent, definition) => ({ name: definition.name, definition, scope: undefined, parent }),
|
|
407
|
+
locals,
|
|
408
|
+
);
|
|
409
|
+
// A LET definition sees the others, as RECURSIVE ones may refer to those after them.
|
|
410
|
+
for (let local = inner; local !== locals && local; local = local.parent) {
|
|
411
|
+
local.scope = inner;
|
|
412
|
+
}
|
|
413
|
+
expression.definitions.forEach((definition) => {
|
|
414
|
+
if (definition.kind === "Operator") letBodies.set(definition.body, expression.body);
|
|
415
|
+
});
|
|
416
|
+
getHashtableOrder(expression.definitions).forEach((definition) =>
|
|
417
|
+
walkDefinition({ definition, scope: inner }),
|
|
418
|
+
);
|
|
419
|
+
walk(expression.body, inner);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
case "Identifier": {
|
|
423
|
+
const resolved = resolve(expression, expression.name, locals);
|
|
424
|
+
enter(
|
|
425
|
+
expression,
|
|
426
|
+
range,
|
|
427
|
+
() => {
|
|
428
|
+
if (resolved) walkDefinition(resolved);
|
|
429
|
+
},
|
|
430
|
+
false,
|
|
431
|
+
resolved?.definition,
|
|
432
|
+
);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
case "Apply": {
|
|
436
|
+
const resolved = resolve(expression, expression.operator, locals);
|
|
437
|
+
enter(
|
|
438
|
+
expression,
|
|
439
|
+
range,
|
|
440
|
+
() => {
|
|
441
|
+
if (resolved) walkDefinition(resolved);
|
|
442
|
+
expression.args.forEach((argument, index) =>
|
|
443
|
+
walkArgument(argument, index, resolved?.definition, locals),
|
|
444
|
+
);
|
|
445
|
+
},
|
|
446
|
+
false,
|
|
447
|
+
resolved?.definition,
|
|
448
|
+
);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
case "And":
|
|
452
|
+
case "Or":
|
|
453
|
+
walkJunction(expression, expression.operands.length, range, locals);
|
|
454
|
+
return;
|
|
455
|
+
case "Case":
|
|
456
|
+
enter(expression, range, () => {
|
|
457
|
+
expression.arms.forEach((arm) =>
|
|
458
|
+
enter(
|
|
459
|
+
undefined,
|
|
460
|
+
spanRanges(arm.condition.location.range, arm.body.location.range),
|
|
461
|
+
() => walkAll([arm.condition, arm.body]),
|
|
462
|
+
true,
|
|
463
|
+
),
|
|
464
|
+
);
|
|
465
|
+
const other = expression.other;
|
|
466
|
+
if (other) enter(undefined, expression.otherRange, () => walk(other, locals), true);
|
|
467
|
+
});
|
|
468
|
+
return;
|
|
469
|
+
case "Record":
|
|
470
|
+
case "RecordSet":
|
|
471
|
+
enter(expression, range, () =>
|
|
472
|
+
expression.fields.forEach((field) =>
|
|
473
|
+
enter(
|
|
474
|
+
undefined,
|
|
475
|
+
getStartRange(field.location, field.value.location.range),
|
|
476
|
+
() => walk(field.value, locals),
|
|
477
|
+
true,
|
|
478
|
+
),
|
|
479
|
+
),
|
|
480
|
+
);
|
|
481
|
+
return;
|
|
482
|
+
case "FunctionApplication":
|
|
483
|
+
enter(expression, range, () => {
|
|
484
|
+
walk(expression.function, locals);
|
|
485
|
+
const args = expression.args;
|
|
486
|
+
if (args.length === 1) walk(args[0], locals);
|
|
487
|
+
else {
|
|
488
|
+
enter(
|
|
489
|
+
undefined,
|
|
490
|
+
spanRanges(args[0]?.location.range, args.at(-1)?.location.range),
|
|
491
|
+
() => walkAll(args),
|
|
492
|
+
true,
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
return;
|
|
497
|
+
case "Forall":
|
|
498
|
+
case "Exists":
|
|
499
|
+
case "FunctionConstruction":
|
|
500
|
+
enter(expression, range, () => {
|
|
501
|
+
walk(expression.body, bindNames(getBoundNames(expression.bounds), locals));
|
|
502
|
+
walkAll(getBoundSets(expression.bounds));
|
|
503
|
+
});
|
|
504
|
+
return;
|
|
505
|
+
case "SetMap":
|
|
506
|
+
enter(expression, range, () => {
|
|
507
|
+
walk(expression.expression, bindNames(getBoundNames(expression.bounds), locals));
|
|
508
|
+
walkAll(getBoundSets(expression.bounds));
|
|
509
|
+
});
|
|
510
|
+
return;
|
|
511
|
+
case "SetFilter":
|
|
512
|
+
enter(expression, range, () => {
|
|
513
|
+
walk(expression.predicate, bindNames(getBoundNames([expression.bound]), locals));
|
|
514
|
+
walk(expression.bound.set, locals);
|
|
515
|
+
});
|
|
516
|
+
return;
|
|
517
|
+
case "Choose":
|
|
518
|
+
enter(expression, range, () => {
|
|
519
|
+
const pattern = expression.pattern;
|
|
520
|
+
walk(
|
|
521
|
+
expression.body,
|
|
522
|
+
bindNames(pattern.kind === "Name" ? [pattern.name] : pattern.names, locals),
|
|
523
|
+
);
|
|
524
|
+
if (expression.set) walk(expression.set, locals);
|
|
525
|
+
});
|
|
526
|
+
return;
|
|
527
|
+
case "UnboundedQuantifier":
|
|
528
|
+
enter(expression, range, () => walk(expression.body, bindNames(expression.names, locals)));
|
|
529
|
+
return;
|
|
530
|
+
case "Except":
|
|
531
|
+
enter(expression, range, () => {
|
|
532
|
+
walk(expression.function, locals);
|
|
533
|
+
expression.updates.forEach((update) => {
|
|
534
|
+
update.path.forEach((step) => {
|
|
535
|
+
if (step.kind === "Index") walkAll(step.args);
|
|
536
|
+
});
|
|
537
|
+
walk(update.value, locals);
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
return;
|
|
541
|
+
case "Prime":
|
|
542
|
+
case "Unchanged":
|
|
543
|
+
case "Enabled":
|
|
544
|
+
case "Always":
|
|
545
|
+
case "Eventually":
|
|
546
|
+
enter(expression, range, () => walk(expression.expression, locals));
|
|
547
|
+
return;
|
|
548
|
+
case "If":
|
|
549
|
+
enter(expression, range, () =>
|
|
550
|
+
walkAll([expression.condition, expression.consequent, expression.alternative]),
|
|
551
|
+
);
|
|
552
|
+
return;
|
|
553
|
+
case "SetEnumeration":
|
|
554
|
+
case "Tuple":
|
|
555
|
+
enter(expression, range, () => walkAll(expression.elements));
|
|
556
|
+
return;
|
|
557
|
+
case "Product":
|
|
558
|
+
enter(expression, range, () => walkAll(expression.factors));
|
|
559
|
+
return;
|
|
560
|
+
case "FunctionSet":
|
|
561
|
+
enter(expression, range, () => walkAll([expression.domain, expression.range]));
|
|
562
|
+
return;
|
|
563
|
+
case "FieldAccess":
|
|
564
|
+
enter(expression, range, () => walk(expression.record, locals));
|
|
565
|
+
return;
|
|
566
|
+
case "BoxAction":
|
|
567
|
+
case "AngleAction":
|
|
568
|
+
enter(expression, range, () => walkAll([expression.action, expression.subscript]));
|
|
569
|
+
return;
|
|
570
|
+
case "WeakFairness":
|
|
571
|
+
case "StrongFairness":
|
|
572
|
+
enter(expression, range, () => walkAll([expression.subscript, expression.action]));
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
coverage.relations.forEach((relation) => {
|
|
577
|
+
stack.push(relation.node);
|
|
578
|
+
walk(relation.expression, undefined);
|
|
579
|
+
stack.pop();
|
|
580
|
+
});
|
|
581
|
+
};
|
|
582
|
+
|
|
583
|
+
const findChild = (coverage: Coverage, node: number, expression: Expression): number => {
|
|
584
|
+
for (let child = coverage.firstChild[node]; child !== -1; child = coverage.nextSibling[child]) {
|
|
585
|
+
if (coverage.expressions[child] === expression) return child;
|
|
586
|
+
if (coverage.isPart[child]) {
|
|
587
|
+
const found = findChild(coverage, child, expression);
|
|
588
|
+
if (found !== -1) return found;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return -1;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
// OpApplNodeWrapper.get: a lookup that fails counts under the node it started from.
|
|
595
|
+
export const getCoverageNode = (coverage: Coverage, node: number, expression: Expression) => {
|
|
596
|
+
if (coverage.expressions[node] === expression) return node;
|
|
597
|
+
const child = findChild(coverage, node, expression);
|
|
598
|
+
if (child !== -1) return child;
|
|
599
|
+
const recursive = coverage.recursive[node];
|
|
600
|
+
const inner = recursive === -1 ? -1 : findChild(coverage, recursive, expression);
|
|
601
|
+
if (inner !== -1) return inner;
|
|
602
|
+
return coverage.lets[node]?.find((item) => coverage.expressions[item] === expression) ?? node;
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
export const countCoverage = (coverage: Coverage, node: number) => {
|
|
606
|
+
for (let item = node; item !== -1; item = coverage.prefixes[item]) coverage.primary[item]++;
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
export const runAtCoverageNode = <Result>(
|
|
610
|
+
coverage: Coverage,
|
|
611
|
+
node: number,
|
|
612
|
+
run: () => Result,
|
|
613
|
+
): Result => {
|
|
614
|
+
const outer = coverage.current;
|
|
615
|
+
coverage.current = node;
|
|
616
|
+
try {
|
|
617
|
+
return run();
|
|
618
|
+
} finally {
|
|
619
|
+
coverage.current = outer;
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
export const costCoverage = (coverage: Coverage, value: Value, cost: number) => {
|
|
624
|
+
const node = typeof value === "object" ? coverage.values.get(value) : undefined;
|
|
625
|
+
if (node !== undefined) coverage.secondary[node] += cost;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
const getLeadingZeros = (value: bigint) => {
|
|
629
|
+
const high = Number(value >> 32n);
|
|
630
|
+
return high === 0 ? 32 + Math.clz32(Number(value & 0xffffffffn)) : Math.clz32(high);
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
export const countCoverageVariables = (coverage: Coverage, state: Value[]) =>
|
|
634
|
+
state.forEach((value, variable) => {
|
|
635
|
+
const fingerprint = getTlcFingerprint([value]);
|
|
636
|
+
const register = Number(fingerprint >> BigInt(64 - REGISTER_BITS));
|
|
637
|
+
const rest = BigInt.asUintN(64, (fingerprint << BigInt(REGISTER_BITS)) | (1n << 9n));
|
|
638
|
+
const index = variable * REGISTERS + register;
|
|
639
|
+
coverage.registers[index] = Math.max(coverage.registers[index], getLeadingZeros(rest) + 1);
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
export const getDistinctCount = (coverage: Coverage, variable: number) => {
|
|
643
|
+
const registers = coverage.registers.subarray(variable * REGISTERS, (variable + 1) * REGISTERS);
|
|
644
|
+
const alpha = 0.7213 / (1 + 1.079 / REGISTERS);
|
|
645
|
+
const sum = registers.reduce((total, register) => total + 2 ** -register, 0);
|
|
646
|
+
const estimate = (alpha * REGISTERS * REGISTERS) / sum;
|
|
647
|
+
const zeros = registers.filter((register) => register === 0).length;
|
|
648
|
+
if (estimate <= 2.5 * REGISTERS) {
|
|
649
|
+
return Math.trunc(zeros === 0 ? estimate : REGISTERS * Math.log(REGISTERS / zeros));
|
|
650
|
+
}
|
|
651
|
+
return Math.trunc(
|
|
652
|
+
estimate > 2 ** 64 / 30 ? -(2 ** 64) * Math.log(1 - estimate / 2 ** 64) : estimate,
|
|
653
|
+
);
|
|
654
|
+
};
|
|
655
|
+
|
|
656
|
+
interface Pair {
|
|
657
|
+
primary: number;
|
|
658
|
+
secondary: number;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const getPairHash = (pair: Pair) => {
|
|
662
|
+
const hash = (Math.imul(31, 31 + pair.primary) + pair.secondary) | 0;
|
|
663
|
+
return hash ^ (hash >>> 16);
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// java.util.HashSet iterates its pairs by bucket, and in the order it added those of a bucket.
|
|
667
|
+
const getFirstPair = (pairs: Pair[]) => {
|
|
668
|
+
const capacity = { value: 16 };
|
|
669
|
+
while (pairs.length > capacity.value * 0.75) capacity.value *= 2;
|
|
670
|
+
const getBucket = (pair: Pair) => getPairHash(pair) & (capacity.value - 1);
|
|
671
|
+
return pairs.reduce((first, pair) => (getBucket(pair) < getBucket(first) ? pair : first));
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
const addPair = (pairs: Pair[], pair: Pair) => {
|
|
675
|
+
if (!pairs.some((item) => item.primary === pair.primary && item.secondary === pair.secondary)) {
|
|
676
|
+
pairs.push(pair);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
const isNodePrimed = (coverage: Coverage, node: number) =>
|
|
681
|
+
coverage.isPrimed[node] || coverage.isUnchanged[node];
|
|
682
|
+
|
|
683
|
+
const getChildren = (coverage: Coverage, node: number) => {
|
|
684
|
+
const children: number[] = [];
|
|
685
|
+
for (let child = coverage.firstChild[node]; child !== -1; child = coverage.nextSibling[child]) {
|
|
686
|
+
children.push(child);
|
|
687
|
+
}
|
|
688
|
+
return children;
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
const getRelationCounts = (coverage: Coverage, relation: CoverageRelation) => {
|
|
692
|
+
const primary = coverage.primary[relation.node];
|
|
693
|
+
const secondary = coverage.secondary[relation.node];
|
|
694
|
+
if (relation.kind === "init") return { distinct: primary, generated: primary + secondary };
|
|
695
|
+
if (relation.kind === "next") return { distinct: secondary, generated: primary };
|
|
696
|
+
if (relation.kind === "constraint") {
|
|
697
|
+
return { distinct: secondary, generated: primary + secondary };
|
|
698
|
+
}
|
|
699
|
+
return { distinct: 0, generated: 0 };
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
const getExpressions = (coverage: Coverage, root: number) => {
|
|
703
|
+
const expressions: CoverageExpression[] = [];
|
|
704
|
+
const subtrees = new Map<number, Pair[]>();
|
|
705
|
+
const collect = (node: number): Pair[] => {
|
|
706
|
+
const pairs: Pair[] = [];
|
|
707
|
+
const own = { primary: coverage.primary[node], secondary: coverage.secondary[node] };
|
|
708
|
+
if (own.primary > 0 || own.secondary > 0 || isNodePrimed(coverage, node)) addPair(pairs, own);
|
|
709
|
+
getChildren(coverage, node).forEach((child) =>
|
|
710
|
+
collect(child).forEach((pair) => addPair(pairs, pair)),
|
|
711
|
+
);
|
|
712
|
+
subtrees.set(node, pairs);
|
|
713
|
+
return pairs;
|
|
714
|
+
};
|
|
715
|
+
const add = (node: number, depth: number, count: number, cost: number) => {
|
|
716
|
+
const range = coverage.ranges[node];
|
|
717
|
+
expressions.push({
|
|
718
|
+
location: range ? formatRange(range) : "Unknown location",
|
|
719
|
+
depth,
|
|
720
|
+
count,
|
|
721
|
+
cost,
|
|
722
|
+
});
|
|
723
|
+
};
|
|
724
|
+
const addSelf = (node: number, depth: number) =>
|
|
725
|
+
add(node, depth, coverage.primary[node], coverage.secondary[node]);
|
|
726
|
+
const print = (node: number, depth: number) => {
|
|
727
|
+
const children = getChildren(coverage, node);
|
|
728
|
+
const collected: Pair[] = [];
|
|
729
|
+
children.forEach((child) =>
|
|
730
|
+
(subtrees.get(child) ?? collect(child)).forEach((pair) => addPair(collected, pair)),
|
|
731
|
+
);
|
|
732
|
+
const own = { primary: coverage.primary[node], secondary: coverage.secondary[node] };
|
|
733
|
+
const isPrimed = isNodePrimed(coverage, node);
|
|
734
|
+
const printChildren = (level: number) => children.forEach((child) => print(child, level));
|
|
735
|
+
if (coverage.isUnchanged[node]) {
|
|
736
|
+
const changed = collected.filter((pair) => pair.primary !== 0 || pair.secondary !== 0);
|
|
737
|
+
if (changed.length === 0) addSelf(node, depth);
|
|
738
|
+
else add(node, depth, Math.max(own.primary, getFirstPair(changed).primary), 0);
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
if (collected.length === 0) {
|
|
742
|
+
if (own.primary !== 0 || isPrimed) addSelf(node, depth);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const isZero = own.primary === 0 && own.secondary === 0;
|
|
746
|
+
if (collected.length === 1) {
|
|
747
|
+
const consistent = collected[0];
|
|
748
|
+
const isConsistentZero = consistent.primary === 0 && consistent.secondary === 0;
|
|
749
|
+
if (consistent.primary < own.primary) {
|
|
750
|
+
addSelf(node, depth);
|
|
751
|
+
printChildren(depth + 1);
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
if (!isPrimed && isZero && !isConsistentZero) {
|
|
755
|
+
add(node, depth, consistent.primary, consistent.secondary);
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (isZero && isConsistentZero) {
|
|
759
|
+
if (isPrimed) addSelf(node, depth);
|
|
760
|
+
printChildren(isPrimed ? depth + 1 : depth);
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
if (own.primary === consistent.primary && own.secondary === consistent.secondary) {
|
|
764
|
+
addSelf(node, depth);
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
const isPrinted = !isZero || isPrimed;
|
|
769
|
+
if (isPrinted) addSelf(node, depth);
|
|
770
|
+
printChildren(isPrinted ? depth + 1 : depth);
|
|
771
|
+
};
|
|
772
|
+
getChildren(coverage, root).forEach((child) => {
|
|
773
|
+
collect(child);
|
|
774
|
+
print(child, 0);
|
|
775
|
+
});
|
|
776
|
+
return expressions;
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
const KIND_ORDER: CoverageKind[] = ["init", "next", "invariant", "constraint", "implied"];
|
|
780
|
+
|
|
781
|
+
const compareRanges = (left: SourceRange | undefined, right: SourceRange | undefined) => {
|
|
782
|
+
if (!left || !right) return left ? 1 : right ? -1 : 0;
|
|
783
|
+
if (left.module !== right.module) return left.module < right.module ? -1 : 1;
|
|
784
|
+
return (
|
|
785
|
+
left.start.line - right.start.line ||
|
|
786
|
+
left.start.column - right.start.column ||
|
|
787
|
+
left.end.line - right.end.line ||
|
|
788
|
+
left.end.column - right.end.column
|
|
789
|
+
);
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
// TLC reports the next-state actions in the order of their locations, once for each location.
|
|
793
|
+
const getReportedRelations = (coverage: Coverage) =>
|
|
794
|
+
KIND_ORDER.flatMap((kind) => {
|
|
795
|
+
const relations = coverage.relations.filter((relation) => relation.kind === kind);
|
|
796
|
+
if (kind !== "next") return relations;
|
|
797
|
+
return relations
|
|
798
|
+
.toSorted((left, right) =>
|
|
799
|
+
compareRanges(left.expression.location.range, right.expression.location.range),
|
|
800
|
+
)
|
|
801
|
+
.filter(
|
|
802
|
+
(relation, index, sorted) =>
|
|
803
|
+
index === 0 ||
|
|
804
|
+
compareRanges(
|
|
805
|
+
sorted[index - 1].expression.location.range,
|
|
806
|
+
relation.expression.location.range,
|
|
807
|
+
) !== 0,
|
|
808
|
+
);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
export const getCoverageReport = (
|
|
812
|
+
coverage: Coverage,
|
|
813
|
+
environment: Environment,
|
|
814
|
+
variableLocations: SourceLocation[],
|
|
815
|
+
variableOrder: number[],
|
|
816
|
+
): CoverageReport => ({
|
|
817
|
+
variables: variableOrder.map((index) => {
|
|
818
|
+
const range = variableLocations[index]?.range;
|
|
819
|
+
return {
|
|
820
|
+
label: `${environment.variables[index]} ${range ? formatRange(range) : "Unknown location"}`,
|
|
821
|
+
distinct: getDistinctCount(coverage, index),
|
|
822
|
+
};
|
|
823
|
+
}),
|
|
824
|
+
relations: getReportedRelations(coverage).map((relation) => ({
|
|
825
|
+
kind: relation.kind,
|
|
826
|
+
label: relation.label,
|
|
827
|
+
...getRelationCounts(coverage, relation),
|
|
828
|
+
expressions: getExpressions(coverage, relation.node),
|
|
829
|
+
})),
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
export const getCoverageCounts = (
|
|
833
|
+
coverage: Coverage,
|
|
834
|
+
kind: CoverageKind,
|
|
835
|
+
expression: Expression,
|
|
836
|
+
): [string, number][] | undefined => {
|
|
837
|
+
const node = findCoverageRelation(coverage, kind, expression);
|
|
838
|
+
if (node === undefined) return undefined;
|
|
839
|
+
const primary = coverage.primary[node];
|
|
840
|
+
const secondary = coverage.secondary[node];
|
|
841
|
+
if (kind === "init") {
|
|
842
|
+
return [
|
|
843
|
+
["generated", primary + secondary],
|
|
844
|
+
["distinct", primary],
|
|
845
|
+
];
|
|
846
|
+
}
|
|
847
|
+
if (kind === "next") {
|
|
848
|
+
return [
|
|
849
|
+
["generated", primary],
|
|
850
|
+
["distinct", secondary],
|
|
851
|
+
];
|
|
852
|
+
}
|
|
853
|
+
const child = coverage.firstChild[node];
|
|
854
|
+
return [["count", child === -1 ? primary : coverage.primary[child]]];
|
|
855
|
+
};
|