Skip to content

Commit

Permalink
options, set.seed, and on.exit may have global side effects! (#934
Browse files Browse the repository at this point in the history
)

* feat: support `set.seed`

* feat: recognize options as side effect as well

* feat: support `on.exit` (closes #931)

* feat: supporting `sys.on.exit`

* lint-fix: handle linter errors
  • Loading branch information
EagleoutIce authored Aug 30, 2024
1 parent 4f9af13 commit ee27161
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ registerSimpleFunctions(
registerBuiltInFunctions(true, ['apply', 'lapply', 'sapply', 'tapply', 'mapply'], defaultBuiltInProcessor, { forceArgs: [false, true] } )
registerBuiltInFunctions(true, ['print'], defaultBuiltInProcessor, { returnsNthArgument: 0, forceArgs: 'all' as const } )
registerBuiltInFunctions(true, ['('], defaultBuiltInProcessor, { returnsNthArgument: 0 } )
registerBuiltInFunctions(true, ['load', 'load_all', 'setwd'], defaultBuiltInProcessor, { hasUnknownSideEffects: true, forceArgs: [true] } )
registerBuiltInFunctions(true, ['load', 'load_all', 'setwd', 'set.seed'], defaultBuiltInProcessor, { hasUnknownSideEffects: true, forceArgs: [true] } )
registerBuiltInFunctions(false, ['cat'], defaultBuiltInProcessor, { forceArgs: 'all' as const } ) /* returns null */
registerBuiltInFunctions(false, ['switch'], defaultBuiltInProcessor, {} ) /* returns null */
registerBuiltInFunctions(true, ['return'], defaultBuiltInProcessor, { returnsNthArgument: 0, cfg: ExitPointType.Return } )
Expand All @@ -201,6 +201,8 @@ registerBuiltInFunctions(true, ['quote', 'substitute', 'bquote'],
registerBuiltInFunctions(true, ['for'], processForLoop, {} )
registerBuiltInFunctions(true, ['repeat'], processRepeatLoop, {} )
registerBuiltInFunctions(true, ['while'], processWhileLoop, {} )
registerBuiltInFunctions(true, ['options'], defaultBuiltInProcessor, { hasUnknownSideEffects: true, forceArgs: 'all' as const } )
registerBuiltInFunctions(true, ['on.exit', 'sys.on.exit'], defaultBuiltInProcessor, { hasUnknownSideEffects: true } )

/* they are all mapped to `<-` but we separate super assignments */
registerReplacementFunctions({ makeMaybe: true }, ['<-', '<<-'], '[', '[[', '$', '@', 'names', 'dimnames', 'attributes', 'attr', 'class', 'levels', 'rownames', 'colnames')
19 changes: 18 additions & 1 deletion test/functionality/slicing/static-program-slices/calls-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,23 @@ x`)
'setwd("f/")\nx', ['2@x'],
'setwd("f/")\nx'
)

assertSliced(label('Setting a fixed seed', [
'functions-with-global-side-effects', 'name-normal', 'numbers', 'call-normal', 'unnamed-arguments', 'newlines'
]), shell,
'seed <- 1234\nset.seed(seed)\nx', ['3@x'],
'seed <- 1234\nset.seed(seed)\nx'
)
assertSliced(label('Configuring options', [
'functions-with-global-side-effects', 'name-normal', 'numbers', 'call-normal', 'unnamed-arguments', 'newlines', 'named-arguments'
]), shell,
'options(y=2)\nx', ['2@x'],
'options(y=2)\nx'
)
assertSliced(label('Exit hooks', [
'functions-with-global-side-effects', 'name-normal', 'numbers', 'call-normal', 'unnamed-arguments', 'newlines', 'named-arguments', 'implicit-return', 'function-definitions'
]), shell,
'x\non.exit(function() 3)', ['1@x'],
'x\non.exit(function() 3)'
)
})
}))

2 comments on commit ee27161

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"artificial" Benchmark Suite

Benchmark suite Current: ee27161 Previous: 4fb2496 Ratio
Retrieve AST from R code 246.05185213636364 ms (108.71295828974641) 238.1723235909091 ms (96.71402866840735) 1.03
Normalize R AST 20.84174 ms (37.18186921613879) 19.4989345 ms (33.57276767882534) 1.07
Produce dataflow information 37.047965727272725 ms (78.553118199089) 38.792485772727275 ms (84.2875165952748) 0.96
Total per-file 805.1761586818182 ms (1533.6493105880927) 799.2110945 ms (1544.5301502222158) 1.01
Static slicing 1.1617918195597892 ms (1.1260740698850225) 1.1726790800999163 ms (1.0086901141796194) 0.99
Reconstruct code 0.2468166996028351 ms (0.19930707941744) 0.24335608198030453 ms (0.18573862888815007) 1.01
Total per-slice 1.4248328496973997 ms (1.1760621423053028) 1.4331761674751269 ms (1.0536924338527542) 0.99
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7869724682442361 # 0.786663222057468 # 1.00
reduction (normalized tokens) 0.7640044233283717 # 0.763664433957929 # 1.00
memory (df-graph) 147.58589311079547 KiB (359.2574768951678) 147.66770241477272 KiB (359.55136525995476) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"social-science" Benchmark Suite

Benchmark suite Current: ee27161 Previous: 4fb2496 Ratio
Retrieve AST from R code 241.77226738 ms (45.01399758561522) 240.9999736 ms (45.52546037334287) 1.00
Normalize R AST 21.79906732 ms (16.778494750766374) 22.044827100000003 ms (17.4420619617221) 0.99
Produce dataflow information 69.24065474 ms (83.64458224031215) 68.41181266 ms (83.25046712198441) 1.01
Total per-file 3717.81688036 ms (7912.866466619969) 3603.0243807399997 ms (7958.676569737224) 1.03
Static slicing 7.6484400334826965 ms (20.07257857764472) 7.403007833130669 ms (20.923205633042343) 1.03
Reconstruct code 0.24656202820542572 ms (0.1567464393258898) 0.24681178006363166 ms (0.15169934993997963) 1.00
Total per-slice 7.902844162878471 ms (20.102495506358835) 7.657637977886095 ms (20.95063234618626) 1.03
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.9060203516109888 # 0.9214445180065712 # 0.98
reduction (normalized tokens) 0.8749621691839767 # 0.88847659105633 # 0.98
memory (df-graph) 142.4557421875 KiB (146.62238285216054) 142.5463671875 KiB (146.6995040110581) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.