From d609e2b0a22006a25775923a3eaf0b01a53f87c9 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 18 Jul 2024 22:03:28 +0200 Subject: [PATCH] More LLVM --- .../cpg/frontends/llvm/StatementHandler.kt | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/cpg-language-llvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/llvm/StatementHandler.kt b/cpg-language-llvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/llvm/StatementHandler.kt index ad71b554214..a6689adf4b5 100644 --- a/cpg-language-llvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/llvm/StatementHandler.kt +++ b/cpg-language-llvm/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/llvm/StatementHandler.kt @@ -236,6 +236,7 @@ class StatementHandler(lang: LLVMIRLanguageFrontend) : var currentIfStatement: IfStatement? = null var idx = 1 while (idx < numOps) { + // TODO: somehow change the AST stack here if (currentIfStatement == null) { currentIfStatement = ifStatement } else { @@ -254,21 +255,22 @@ class StatementHandler(lang: LLVMIRLanguageFrontend) : val matchesCatchpad = newCallExpression( - llvmInternalRef("llvm.matchesCatchpad"), - "llvm.matchesCatchpad", - false, - rawNode = instr - ) - - val parentCatchSwitch = LLVMGetParentCatchSwitch(catchpad) - val catchswitch = frontend.expressionHandler.handle(parentCatchSwitch) as Expression - matchesCatchpad.addArgument(catchswitch, "parentCatchswitch") - - for (i in 0 until catchOps) { - val arg = frontend.getOperandValueAtIndex(catchpad, i) - matchesCatchpad.addArgument(arg, "args_$i") - } - + llvmInternalRef("llvm.matchesCatchpad"), + "llvm.matchesCatchpad", + false, + rawNode = instr + ) + .withChildren { + val parentCatchSwitch = LLVMGetParentCatchSwitch(catchpad) + val catchswitch = + frontend.expressionHandler.handle(parentCatchSwitch) as Expression + it.addArgument(catchswitch, "parentCatchswitch") + + for (i in 0 until catchOps) { + val arg = frontend.getOperandValueAtIndex(catchpad, i) + it.addArgument(arg, "args_$i") + } + } currentIfStatement.condition = matchesCatchpad // Get the label of the goto statement. @@ -311,17 +313,19 @@ class StatementHandler(lang: LLVMIRLanguageFrontend) : val dummyCall = newCallExpression( - llvmInternalRef("llvm.cleanuppad"), - "llvm.cleanuppad", - false, - rawNode = instr - ) - dummyCall.addArgument(catchswitch, "parentCatchswitch") + llvmInternalRef("llvm.cleanuppad"), + "llvm.cleanuppad", + false, + rawNode = instr + ) + .withChildren { + it.addArgument(catchswitch, "parentCatchswitch") - for (i in 1 until numOps) { - val arg = frontend.getOperandValueAtIndex(instr, i) - dummyCall.addArgument(arg, "args_${i-1}") - } + for (i in 1 until numOps) { + val arg = frontend.getOperandValueAtIndex(instr, i) + it.addArgument(arg, "args_${i - 1}") + } + } return declarationOrNot(dummyCall, instr) } @@ -1417,21 +1421,26 @@ class StatementHandler(lang: LLVMIRLanguageFrontend) : // if it is still empty, we probably do not have a left side return if (lhs != "") { - val decl = - newVariableDeclaration(lhs, frontend.typeOf(valueRef), false, rawNode = valueRef) - decl.initializer = rhs - - // add the declaration to the current scope - frontend.scopeManager.addDeclaration(decl) - - // add it to our bindings cache - frontend.bindingsCache[symbolName] = decl - // Since the declaration statement only contains the single declaration, we can use the // same raw node, so we end up with the same code and location - val declStatement = newDeclarationStatement(rawNode = valueRef) - declStatement.singleDeclaration = decl - declStatement + newDeclarationStatement(rawNode = valueRef).withChildren { + it.singleDeclaration = + newVariableDeclaration( + lhs, + frontend.typeOf(valueRef), + false, + rawNode = valueRef + ) + .withChildren { decl -> + decl.initializer = rhs + + // add the declaration to the current scope + frontend.scopeManager.addDeclaration(decl) + + // add it to our bindings cache + frontend.bindingsCache[symbolName] = decl + } + } } else { rhs }