Skip to content
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
54 changes: 54 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/Collector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 UnitTestBot contributors (utbot.org)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("PropertyName")

package org.jacodb.ets.utils

import org.jacodb.ets.base.EtsEntity
import org.jacodb.ets.base.EtsStmt

class EntityCollector<R : Any, C : MutableCollection<R>>(
val result: C,
val block: (EtsEntity) -> R?,
) : AbstractHandler() {
override fun handle(value: EtsEntity) {
val item = block(value)
if (item != null) {
result += item
}
}

override fun handle(stmt: EtsStmt) {
// Do nothing.
}
}

fun <R : Any, C : MutableCollection<R>> EtsEntity.collectEntitiesTo(
destination: C,
block: (EtsEntity) -> R?,
): C {
accept(EntityCollector(destination, block))
return destination

Check warning on line 45 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/Collector.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/Collector.kt#L44-L45

Added lines #L44 - L45 were not covered by tests
}

fun <R : Any, C : MutableCollection<R>> EtsStmt.collectEntitiesTo(
destination: C,
block: (EtsEntity) -> R?,
): C {
accept(EntityCollector(destination, block))
return destination

Check warning on line 53 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/Collector.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/Collector.kt#L52-L53

Added lines #L52 - L53 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,31 @@

package org.jacodb.ets.utils

import org.jacodb.ets.base.EtsAssignStmt
import org.jacodb.ets.base.EtsEntity
import org.jacodb.ets.base.EtsLocal
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.model.EtsMethod

fun EtsStmt.getUses(): Sequence<EtsEntity> =
getOperands().flatMap { sequenceOf(it) + it.getUses() }
fun EtsMethod.getDeclaredLocals(): Set<EtsLocal> =
cfg.stmts.mapNotNullTo(mutableSetOf()) {

Check warning on line 26 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt#L26

Added line #L26 was not covered by tests
if (it is EtsAssignStmt && it.lhv is EtsLocal) {
it.lhv

Check warning on line 28 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt#L28

Added line #L28 was not covered by tests
} else {
null
}
}

Check warning on line 32 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt#L30-L32

Added lines #L30 - L32 were not covered by tests

fun EtsEntity.getUses(): Sequence<EtsEntity> =
getOperands().flatMap { sequenceOf(it) + it.getUses() }
fun EtsMethod.getLocals(): Set<EtsLocal> {
val result = mutableSetOf<EtsLocal>()

Check warning on line 35 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt#L35

Added line #L35 was not covered by tests
cfg.stmts.forEach { it.collectEntitiesTo(result) { it as? EtsLocal } }
return result

Check warning on line 37 in jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetLocals.kt#L37

Added line #L37 was not covered by tests
}

fun EtsStmt.getLocals(): Set<EtsLocal> {
return collectEntitiesTo(mutableSetOf()) { it as? EtsLocal }
}

fun EtsEntity.getLocals(): Set<EtsLocal> {
return collectEntitiesTo(mutableSetOf()) { it as? EtsLocal }
}
10 changes: 6 additions & 4 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/GetValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import org.jacodb.ets.base.EtsEntity
import org.jacodb.ets.base.EtsStmt
import org.jacodb.ets.base.EtsValue

fun EtsStmt.getValues(): Sequence<EtsValue> =
getOperands().filterIsInstance<EtsValue>()
fun EtsStmt.getValues(): Set<EtsValue> {
return collectEntitiesTo(mutableSetOf()) { it as? EtsValue }
}

fun EtsEntity.getValues(): Sequence<EtsValue> =
getOperands().filterIsInstance<EtsValue>()
fun EtsEntity.getValues(): Set<EtsValue> {
return collectEntitiesTo(mutableSetOf()) { it as? EtsValue }
}
Loading