Skip to content

Commit f6d7035

Browse files
fix: record variants of a subscripted variable in removeUnusedVariables (#65)
Fixes #64
1 parent d4bf555 commit f6d7035

4 files changed

Lines changed: 45 additions & 15 deletions

File tree

models/prune/prune.mdl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ With Look1 at t1 = WITH LOOKUP ( 1, ([(0,0)-(2,2)],(0,0),(1,1),(2,2)) )
9797
With Look2 at t1 = WITH LOOKUP ( 1, ([(0,0)-(2,2)],(0,0),(1,1),(2,2)) )
9898
~~|
9999

100+
Constant Partial 1 = 1
101+
~~|
102+
103+
Constant Partial 2 = 2
104+
~~|
105+
106+
Initial Partial[C1] =
107+
INITIAL( Constant Partial 1 )
108+
~~|
109+
110+
Initial Partial[C2] =
111+
INITIAL( Constant Partial 2 )
112+
~~|
113+
114+
Partial[C2] =
115+
Initial Partial[C2]
116+
~~|
117+
100118
********************************************************
101119
.Control
102120
********************************************************~

models/prune/prune_check.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ expect_present "__lookup1"
3939
expect_present "_look1"
4040
expect_present "_look1_value_at_t1"
4141
expect_present "_with_look1_at_t1"
42+
expect_present "_constant_partial_1"
43+
expect_present "_constant_partial_2"
44+
expect_present "_initial_partial"
45+
expect_present "_partial"
4246

4347
# Verify that unreferenced variables do not appear in the generated C file
4448
expect_not_present "_input_3"

models/prune/prune_spec.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
{
22
"name": "prune",
3-
"inputVars": [
4-
"_input_1",
5-
"_input_2"
3+
"inputVarNames": [
4+
"Input 1",
5+
"Input 2"
66
],
7-
"outputVars": [
8-
"_time",
9-
"_input_1_and_2_total",
10-
"_simple_totals",
11-
"_a_totals",
12-
"_b1_totals",
13-
"_look1_value_at_t1",
14-
"_with_look1_at_t1"
7+
"outputVarNames": [
8+
"Time",
9+
"Input 1 and 2 Total",
10+
"Simple Totals",
11+
"A Totals",
12+
"B1 Totals",
13+
"Look1 Value at t1",
14+
"With Look1 at t1",
15+
"Partial[C2]"
1516
],
1617
"externalDatfiles": [
1718
"prune_data.dat"

src/Model.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function removeUnusedVariables(spec) {
245245
// Keep track of all variable names that are referenced somewhere. Note that we
246246
// don't attempt to track specific "ref ids" (e.g. `_some_variable[_subscript]`)
247247
// but instead just track generic variable names (e.g. `_some_variable`). This
248-
// ensure that we include all subscripts for a variable, which might mean we
248+
// ensures that we include all subscripts for a variable, which might mean we
249249
// include some subscripts that aren't needed, but it is safer than trying to
250250
// eliminate those and possibly omit something that is needed.
251251
const referencedVarNames = []
@@ -259,7 +259,7 @@ function removeUnusedVariables(spec) {
259259
}
260260

261261
// Add the given variable to the list of referenced variables, and do the same for
262-
// some special things (i.e., lookups) that it might reference.a
262+
// some special things (i.e., lookups) that it might reference.
263263
const recordUsedVariable = v => {
264264
// Add the variable to the list of referenced variables
265265
recordUsedVarName(v.varName)
@@ -291,8 +291,15 @@ function removeUnusedVariables(spec) {
291291
// that it references) as being "used".
292292
const referencedRefIds = []
293293
const recordRefsOfVariable = v => {
294-
let refs = v.references.concat(v.initReferences)
295-
for (const refId of refs) {
294+
// If this variable is subscripted, we need to record all subscript variants;
295+
// `refIdsWithName` will return those. We also need to record all variables
296+
// that are referenced by this variable, either directly (`v.references`) or
297+
// in an "INITIAL" expression (`v.initReferences`). It's OK if we end up with
298+
// duplicates in this list, because we will examine each reference only once.
299+
let refIds = refIdsWithName(v.varName)
300+
refIds = refIds.concat(v.references)
301+
refIds = refIds.concat(v.initReferences)
302+
for (const refId of refIds) {
296303
if (!referencedRefIds.includes(refId)) {
297304
referencedRefIds.push(refId)
298305
const refVar = varWithRefId(refId)

0 commit comments

Comments
 (0)