Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle library #793

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { RFunctionArgument } from '../../r-bridge/lang-4.x/ast/model/nodes/
import type { RSymbol } from '../../r-bridge/lang-4.x/ast/model/nodes/r-symbol'
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { EdgeType } from '../graph/edge'
import { processLibrary } from '../internal/process/functions/call/built-in/built-in-library'

export const BuiltIn = 'built-in'

Expand Down Expand Up @@ -158,6 +159,7 @@ registerBuiltInFunctions(['[', '[['], processAccess,
registerBuiltInFunctions(['$', '@'], processAccess, { treatIndicesAsString: true }, )
registerBuiltInFunctions(['if'], processIfThenElse, {}, )
registerBuiltInFunctions(['get'], processGet, {}, )
registerBuiltInFunctions(['library'], processLibrary, {}, )
registerBuiltInFunctions(['<-', '='], processAssignment, { canBeReplacement: true }, )
registerBuiltInFunctions([':=', 'assign'], processAssignment, {}, )
registerBuiltInFunctions(['delayedAssign'], processAssignment, { quoteSource: true }, )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { type DataflowProcessorInformation } from '../../../../../processor'
import type { DataflowInformation } from '../../../../../info'
import { processKnownFunctionCall } from '../known-call-handling'
import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate'
import type { RFunctionArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call'
import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol'
import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { dataflowLogger } from '../../../../../logger'
import { unpackArgument } from '../argument/unpack-argument'
import type { RString } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string'
import { RType } from '../../../../../../r-bridge/lang-4.x/ast/model/type'
import { wrapArgumentsUnnamed } from '../argument/make-argument'


export function processLibrary<OtherInfo>(
name: RSymbol<OtherInfo & ParentInformation>,
args: readonly RFunctionArgument<OtherInfo & ParentInformation>[],
rootId: NodeId,
data: DataflowProcessorInformation<OtherInfo & ParentInformation>
): DataflowInformation {
if(args.length !== 1) {
dataflowLogger.warn(`Currently only one-arg library-likes are allows (for ${name.content}), skipping`)
return processKnownFunctionCall({ name, args, rootId, data }).information

Check warning on line 23 in src/dataflow/internal/process/functions/call/built-in/built-in-library.ts

View check run for this annotation

Codecov / codecov/patch

src/dataflow/internal/process/functions/call/built-in/built-in-library.ts#L22-L23

Added lines #L22 - L23 were not covered by tests
}
const nameToLoad = unpackArgument(args[0])
if(nameToLoad === undefined || nameToLoad.type !== RType.Symbol) {
dataflowLogger.warn('No library name provided, skipping')
return processKnownFunctionCall({ name, args, rootId, data }).information

Check warning on line 28 in src/dataflow/internal/process/functions/call/built-in/built-in-library.ts

View check run for this annotation

Codecov / codecov/patch

src/dataflow/internal/process/functions/call/built-in/built-in-library.ts#L27-L28

Added lines #L27 - L28 were not covered by tests
}

// treat as a function call but convert the first argument to a string
const newArg: RString<OtherInfo & ParentInformation> = {
type: RType.String,
info: nameToLoad.info,
lexeme: nameToLoad.lexeme,
location: nameToLoad.location,
content: {
quotes: 'none',
str: nameToLoad.content
}
}

return processKnownFunctionCall({ name, args: wrapArgumentsUnnamed([newArg], data.completeAst.idMap), rootId, data }).information
}
6 changes: 5 additions & 1 deletion src/slicing/criterion/collect-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { guard } from '../../util/assert'
import { getUniqueCombinationsOfSize } from '../../util/arrays'
import type { RNodeWithParent } from '../../r-bridge/lang-4.x/ast/model/processing/decorate'
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { EmptyArgument } from '../../r-bridge/lang-4.x/ast/model/nodes/r-function-call'

/**
* Defines the filter for collecting all possible slicing criteria.
Expand Down Expand Up @@ -49,6 +50,9 @@ export function* collectAllSlicingCriteria<OtherInfo>(ast: RNodeWithParent<Other
}

for(const combination of getUniqueCombinationsOfSize(potentialSlicingNodes, filter.minimumSize, filter.maximumSize)) {
yield combination.map(n => `$${n}` as SingleSlicingCriterion)
const c = combination.filter(n => n !== undefined && n !== EmptyArgument)
if(c.length > 0) {
yield c.map(n => `$${n}` as SingleSlicingCriterion)
}
}
}
4 changes: 1 addition & 3 deletions src/slicing/static/visiting-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ export class VisitingQueue {
*/
public add(target: NodeId, env: REnvironmentInformation, envFingerprint: string, onlyForSideEffects: boolean): void {
const idCounter = this.idThreshold.get(target) ?? 0

if(idCounter > this.threshold) {
slicerLogger.warn(`id: ${target} has been visited ${idCounter} times, skipping`)
this.timesHitThreshold++
return
} else {
this.idThreshold.set(target, idCounter + 1)
}

/* we do not include the in call part in the fingerprint as it is 'deterministic' from the source position */
const print = fingerprint(target, envFingerprint, onlyForSideEffects)

if(!this.seen.has(print)) {
this.idThreshold.set(target, idCounter + 1)
this.seen.set(print, target)
this.queue.push({ id: target, baseEnvironment: env, onlyForSideEffects })
}
Expand Down
2 changes: 1 addition & 1 deletion test/functionality/benchmark/slicer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cat(d)`
}, statInfo)
assert.deepStrictEqual(stats.dataflow, {
numberOfNodes: 23,
numberOfEdges: 37,
numberOfEdges: 38,
numberOfCalls: 9,
numberOfFunctionDefinitions: 0
}, statInfo)
Expand Down
Loading