Skip to content

Commit

Permalink
Adding new operator call expression
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 1, 2024
1 parent a52bc60 commit 3924bb5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ fun MetadataProvider.newOperatorCallExpression(
rawNode: Any? = null
): OperatorCallExpression {
val node = OperatorCallExpression()
node.applyMetadata(
this,
operatorCode,
rawNode
)
node.applyMetadata(this, operatorCode, rawNode)

node.operatorCode = operatorCode
node.callee = callee

log(node)
return node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.graph.statements.expressions

import de.fraunhofer.aisec.cpg.graph.HasBase
import de.fraunhofer.aisec.cpg.graph.HasOperatorCode
import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.declarations.OperatorDeclaration
Expand All @@ -34,11 +35,21 @@ import de.fraunhofer.aisec.cpg.graph.declarations.OperatorDeclaration
* In this case, we replace the original [BinaryOperator] with an [OperatorCallExpression], which
* points to its respective [OperatorDeclaration].
*/
class OperatorCallExpression : CallExpression(), HasOperatorCode {
class OperatorCallExpression : CallExpression(), HasOperatorCode, HasBase {

override val operatorCode: String? = null
override var operatorCode: String? = null

override var name: Name
get() = Name(operatorCode ?: "")
set(_) {}

/**
* The base object. This is basically a shortcut to accessing the base of the [callee], if it
* has one (i.e., if it implements [HasBase]). This is the case for example, if it is a
* [MemberExpression].
*/
override val base: Expression?
get() {
return (callee as? HasBase)?.base
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,6 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
.filterIsInstance<OperatorDeclaration>()
.singleOrNull()

// We unfortunately, have no direct access to the AST parent, but this is a very good
// heuristic to get it
var parent = reference.base.prevEOG.singleOrNull()

if (op != null) {
type = op.returnTypes.singleOrNull()?.root ?: unknownType()

Expand All @@ -460,7 +456,8 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
newMemberExpression(op.name, reference.base, operatorCode = ".")
.implicit(op.name.localName, location = reference.location)
ref.refersTo = op
var call = newOperatorCallExpression(operatorCode = ".", ref).codeAndLocationFrom(ref)
var call =
newOperatorCallExpression(operatorCode = "->", ref).codeAndLocationFrom(ref)
call.invokes = listOf(op)

// Make the call our new base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration
import de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement
import de.fraunhofer.aisec.cpg.graph.statements.expressions.BinaryOperator
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Block
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.OperatorCallExpression
import de.fraunhofer.aisec.cpg.graph.statements.expressions.UnaryOperator
import de.fraunhofer.aisec.cpg.graph.types.FunctionPointerType
import de.fraunhofer.aisec.cpg.test.*
Expand Down Expand Up @@ -280,7 +280,7 @@ class CXXDeclarationTest {
// we should now have an implicit call to our operator in-between "p" and "size"
val opCall = sizeRef.base
assertNotNull(opCall)
assertIs<MemberCallExpression>(opCall)
assertIs<OperatorCallExpression>(opCall)
assertEquals(p, opCall.base)
assertInvokes(opCall, op)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ class Proxy {
int main() {
Proxy p;
int size = p->size;

// int another_size = p.operator->()->size;
}

0 comments on commit 3924bb5

Please sign in to comment.