Skip to content

Commit

Permalink
Support for <<, >> and |, &, ^ in ValueEvaluator and `Bin…
Browse files Browse the repository at this point in the history
…aryOperation` (#1333)

Support for `<<`, `>>` and `|` in `ValueEvaluator` and `BinaryOperation`
  • Loading branch information
oxisto committed Oct 18, 2023
1 parent b8ef18c commit ee1c7aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,16 @@ class ScopeManager : ScopeProvider {
* Resolves only references to Values in the current scope, static references to other visible
* records are not resolved over the ScopeManager.
*
* @param scope
* @param ref
* @return
*
* TODO: We should merge this function with [.resolveFunction]
*/
@JvmOverloads
fun resolveReference(ref: Reference, startScope: Scope? = currentScope): ValueDeclaration? {
fun resolveReference(ref: Reference): ValueDeclaration? {
val startScope = ref.scope

// Retrieve a unique tag for the particular reference based on the current scope
val tag = ref.buildUniqueTag(startScope)
val tag = ref.uniqueTag

// If we find a match in our symbol table, we can immediately return the declaration
var decl = symbolTable[tag]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import de.fraunhofer.aisec.cpg.graph.declarations.Declaration
import de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration
import de.fraunhofer.aisec.cpg.graph.edge.Properties
import de.fraunhofer.aisec.cpg.graph.scopes.Scope
import de.fraunhofer.aisec.cpg.graph.types.HasType
import de.fraunhofer.aisec.cpg.graph.types.Type
import de.fraunhofer.aisec.cpg.passes.SymbolResolver
Expand Down Expand Up @@ -160,9 +159,10 @@ open class Reference : Expression(), HasType.TypeObserver {
* Its purpose is to cache symbol resolutions, similar to LLVMs system of Unified Symbol
* Resolution (USR).
*/
fun buildUniqueTag(startScope: Scope?): ReferenceTag {
return Objects.hash(this.name, this.resolutionHelper, startScope)
}
val uniqueTag: ReferenceTag
get() {
return Objects.hash(this.name, this.resolutionHelper, this.scope)
}
}

typealias ReferenceTag = Int
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
// Peek into the declaration, and if it is a variable, we can proceed normally, as we
// are running into the special case explained above. Otherwise, we abort here (for
// now).
wouldResolveTo = scopeManager.resolveReference(current, current.scope)
wouldResolveTo = scopeManager.resolveReference(current)
if (wouldResolveTo !is VariableDeclaration && wouldResolveTo !is ParameterDeclaration) {
return
}
Expand All @@ -214,9 +214,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
// Only consider resolving, if the language frontend did not specify a resolution. If we
// already have populated the wouldResolveTo variable, we can re-use this instead of
// resolving again
var refersTo =
current.refersTo
?: wouldResolveTo ?: scopeManager.resolveReference(current, current.scope)
var refersTo = current.refersTo ?: wouldResolveTo ?: scopeManager.resolveReference(current)

var recordDeclType: Type? = null
if (currentClass != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import de.fraunhofer.aisec.cpg.TestUtils.assertRefersTo
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference
import de.fraunhofer.aisec.cpg.graph.statements.expressions.ReferenceTag
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
Expand Down Expand Up @@ -71,4 +73,23 @@ class SymbolResolverTest {
assertNotNull(construct)
assertInvokes(construct, constructor)
}

@Test
fun testUniqueTags() {
val result = GraphExamples.getConditionalExpression()

val map = mutableMapOf<ReferenceTag, MutableList<Reference>>()

val refs = result.refs
refs.forEach {
// Build a unique tag based on the scope of the reference is in (since this is usually
// the start scope)
val list = map.computeIfAbsent(it.uniqueTag) { mutableListOf() }
list += it

// All elements in the list must have the same scope and name
assertEquals(1, list.map { ref -> ref.scope }.toSet().size)
assertEquals(1, list.map { ref -> ref.name }.toSet().size)
}
}
}

0 comments on commit ee1c7aa

Please sign in to comment.