Skip to content

Commit

Permalink
Recognize setwd as a side effect (#933)
Browse files Browse the repository at this point in the history
* feat: support `setwd`

* lint-fix: handle linter errors
  • Loading branch information
EagleoutIce committed Aug 30, 2024
1 parent 429eef3 commit 4f9af13
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 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'], defaultBuiltInProcessor, { hasUnknownSideEffects: true } )
registerBuiltInFunctions(true, ['load', 'load_all', 'setwd'], 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,5 +201,6 @@ registerBuiltInFunctions(true, ['quote', 'substitute', 'bquote'],
registerBuiltInFunctions(true, ['for'], processForLoop, {} )
registerBuiltInFunctions(true, ['repeat'], processRepeatLoop, {} )
registerBuiltInFunctions(true, ['while'], processWhileLoop, {} )

/* they are all mapped to `<-` but we separate super assignments */
registerReplacementFunctions({ makeMaybe: true }, ['<-', '<<-'], '[', '[[', '$', '@', 'names', 'dimnames', 'attributes', 'attr', 'class', 'levels', 'rownames', 'colnames')
6 changes: 6 additions & 0 deletions src/r-bridge/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ export const flowrCapabilities = {
supported: 'partially',
description: '_Handle cases like `print <- function(x) x`, `` `for` <- function(a,b,c) a``, ..._ Currently, we can not handle all of them there are no tests. Still wip as part of desugaring'
},
{
name: 'Functions with global side effects',
id: 'functions-with-global-side-effects',
supported: 'partially',
description: '_Support functions like `setwd` which have an impact on the subsequent program._'
},
{
name: 'Index Access',
id: 'index-access',
Expand Down
25 changes: 19 additions & 6 deletions test/functionality/slicing/static-program-slices/calls-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,17 @@ if(x == 3) {
x`)
})
describe('Lapply Forcing the Map Function Body', () => {
assertSliced(label('Forcing Second Argument', []), shell,
'res <- lapply(1:3, function(x) x + 1)', ['1@res'],
'res <- lapply(1:3, function(x) x + 1)'
assertSliced(label('Forcing Second Argument', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'res <- lapply(1:3, function(x) x + 1)', ['1@res'],
'res <- lapply(1:3, function(x) x + 1)'
)
assertSliced(label('Forcing Including Reference', []), shell,
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)', ['2@res'],
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)'
assertSliced(label('Forcing Including Reference', [
'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'normal-definition', 'newlines', 'unnamed-arguments', 'call-normal', 'implicit-return'
]), shell,
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)', ['2@res'],
'foo <- bar()\nres <- lapply(1:3, function(x) foo * 2)'
)
})
})
Expand Down Expand Up @@ -653,4 +657,13 @@ x <- 2
f()()
x`)
})
describe('Calls with potential side effects', () => {
assertSliced(label('Changing the working directory', [
'functions-with-global-side-effects', 'name-normal', 'strings', 'call-normal', 'unnamed-arguments', 'newlines'
]), shell,
'setwd("f/")\nx', ['2@x'],
'setwd("f/")\nx'
)

})
}))

2 comments on commit 4f9af13

@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: 4f9af13 Previous: 4fb2496 Ratio
Retrieve AST from R code 234.62973959090908 ms (97.9492173096981) 238.1723235909091 ms (96.71402866840735) 0.99
Normalize R AST 19.720898727272726 ms (34.78191537020925) 19.4989345 ms (33.57276767882534) 1.01
Produce dataflow information 36.71146372727273 ms (77.1495909201379) 38.792485772727275 ms (84.2875165952748) 0.95
Total per-file 774.37977 ms (1471.4749660968093) 799.2110945 ms (1544.5301502222158) 0.97
Static slicing 1.1751748371040667 ms (1.0316689386323894) 1.1726790800999163 ms (1.0086901141796194) 1.00
Reconstruct code 0.2625680857123076 ms (0.2341397044957091) 0.24335608198030453 ms (0.18573862888815007) 1.08
Total per-slice 1.4547523754130707 ms (1.0866731978817743) 1.4331761674751269 ms (1.0536924338527542) 1.02
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: 4f9af13 Previous: 4fb2496 Ratio
Retrieve AST from R code 240.21458002 ms (43.140344783597385) 240.9999736 ms (45.52546037334287) 1.00
Normalize R AST 22.008781719999998 ms (16.80889070064074) 22.044827100000003 ms (17.4420619617221) 1.00
Produce dataflow information 68.9622755 ms (82.82845609621393) 68.41181266 ms (83.25046712198441) 1.01
Total per-file 3650.10450604 ms (7944.775795709758) 3603.0243807399997 ms (7958.676569737224) 1.01
Static slicing 7.334293855374718 ms (20.25455425862405) 7.403007833130669 ms (20.923205633042343) 0.99
Reconstruct code 0.2402430711374398 ms (0.14805123856913055) 0.24681178006363166 ms (0.15169934993997963) 0.97
Total per-slice 7.582108364330436 ms (20.28314031976789) 7.657637977886095 ms (20.95063234618626) 0.99
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.911529865537238 # 0.9214445180065712 # 0.99
reduction (normalized tokens) 0.8812244738794927 # 0.88847659105633 # 0.99
memory (df-graph) 142.46974609375 KiB (146.61529550780887) 142.5463671875 KiB (146.6995040110581) 1.00

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

Please sign in to comment.