From b27fdb5d87dbd5f644aa71be1ff4028db25faecc Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Sat, 5 Aug 2023 20:05:48 +0200 Subject: [PATCH] Trying to improve test coverage slightly --- .../expressions/InitializerListExpression.kt | 10 +-- .../de/fraunhofer/aisec/cpg/passes/DFGPass.kt | 4 +- .../cpg/frontends/cxx/ExpressionHandler.kt | 76 +++++-------------- 3 files changed, 24 insertions(+), 66 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/InitializerListExpression.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/InitializerListExpression.kt index c2e3bfb43f..c937cc65bb 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/InitializerListExpression.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/expressions/InitializerListExpression.kt @@ -48,15 +48,9 @@ class InitializerListExpression : Expression(), ArgumentHolder, HasType.TypeObse @AST var initializerEdges = mutableListOf>() set(value) { - field.forEach { - it.end.unregisterTypeObserver(this) - removePrevDFG(it.end) - } + field.forEach { it.end.unregisterTypeObserver(this) } field = value - value.forEach { - it.end.registerTypeObserver(this) - addPrevDFG(it.end) - } + value.forEach { it.end.registerTypeObserver(this) } } /** Virtual property to access [initializerEdges] without property edges. */ diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt index 6bd7e88187..580ebe69ed 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/DFGPass.kt @@ -71,7 +71,7 @@ class DFGPass(ctx: TranslationContext) : ComponentPass(ctx) { is NewExpression -> handleNewExpression(node) // We keep the logic for the InitializerListExpression in that class because the // performance would decrease too much. - // is InitializerListExpression -> handleInitializerListExpression(node) + is InitializerListExpression -> handleInitializerListExpression(node) is KeyValueExpression -> handleKeyValueExpression(node) is LambdaExpression -> handleLambdaExpression(node) is UnaryOperator -> handleUnaryOperator(node) @@ -272,8 +272,6 @@ class DFGPass(ctx: TranslationContext) : ComponentPass(ctx) { /** * Adds the DFG edges for an [InitializerListExpression]. All values in the initializer flow to * this expression. - * - * TODO: This change seems to have performance issues! */ protected fun handleInitializerListExpression(node: InitializerListExpression) { node.initializers.forEach { node.addPrevDFG(it) } diff --git a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/ExpressionHandler.kt b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/ExpressionHandler.kt index e383378927..0811bdf02b 100644 --- a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/ExpressionHandler.kt +++ b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/ExpressionHandler.kt @@ -42,6 +42,7 @@ import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression.* import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression import org.eclipse.cdt.internal.core.dom.parser.c.CASTDesignatedInitializer import org.eclipse.cdt.internal.core.dom.parser.cpp.* +import org.eclipse.cdt.internal.core.model.ASTStringUtil /** * Note: CDT expresses hierarchies in Interfaces to allow to have multi-inheritance in java. Because @@ -476,47 +477,26 @@ class ExpressionHandler(lang: CXXLanguageFrontend) : } private fun handleBinaryExpression(ctx: IASTBinaryExpression): Expression { - var operatorCode = "" - when (ctx.operator) { - op_multiply -> operatorCode = "*" - op_divide -> operatorCode = "/" - op_modulo -> operatorCode = "%" - op_plus -> operatorCode = "+" - op_minus -> operatorCode = "-" - op_shiftLeft -> operatorCode = "<<" - op_shiftRight -> operatorCode = ">>" - op_lessThan -> operatorCode = "<" - op_greaterThan -> operatorCode = ">" - op_lessEqual -> operatorCode = "<=" - op_greaterEqual -> operatorCode = ">=" - op_binaryAnd -> operatorCode = "&" - op_binaryXor -> operatorCode = "^" - op_binaryOr -> operatorCode = "|" - op_logicalAnd -> operatorCode = "&&" - op_logicalOr -> operatorCode = "||" - op_assign, - op_multiplyAssign, - op_divideAssign, - op_moduloAssign, - op_plusAssign, - op_minusAssign, - op_shiftLeftAssign, - op_shiftRightAssign, - op_binaryAndAssign, - op_binaryXorAssign, - op_binaryOrAssign -> { - return handleAssignment(ctx) + var operatorCode = + when (ctx.operator) { + op_assign, + op_multiplyAssign, + op_divideAssign, + op_moduloAssign, + op_plusAssign, + op_minusAssign, + op_shiftLeftAssign, + op_shiftRightAssign, + op_binaryAndAssign, + op_binaryXorAssign, + op_binaryOrAssign -> { + return handleAssignment(ctx) + } + op_pmdot -> ".*" + op_pmarrow -> "->*" + else -> String(ASTStringUtil.getBinaryOperatorString(ctx)) } - op_equals -> operatorCode = "==" - op_notequals -> operatorCode = "!=" - op_pmdot -> operatorCode = ".*" - op_pmarrow -> operatorCode = "->*" - op_max -> operatorCode = ">?" - op_min -> operatorCode = "?<" - op_ellipses -> operatorCode = "..." - else -> - Util.errorWithFileLocation(frontend, ctx, log, "unknown operator {}", ctx.operator) - } + val binaryOperator = newBinaryOperator(operatorCode, ctx.rawSignature) val lhs = handle(ctx.operand1) ?: newProblemExpression("could not parse lhs") val rhs = @@ -541,22 +521,8 @@ class ExpressionHandler(lang: CXXLanguageFrontend) : handle(ctx.initOperand2) } ?: newProblemExpression("missing RHS") - val operatorCode = - when (ctx.operator) { - op_assign -> "=" - op_multiplyAssign -> "*=" - op_divideAssign -> "/=" - op_moduloAssign -> "%=" - op_plusAssign -> "+=" - op_minusAssign -> "-=" - op_shiftLeftAssign -> "<<=" - op_shiftRightAssign -> ">>=" - op_binaryAndAssign -> "&=" - op_binaryXorAssign -> "^=" - op_binaryOrAssign -> "|=" - else -> "" - } + val operatorCode = String(ASTStringUtil.getBinaryOperatorString(ctx)) val assign = newAssignExpression(operatorCode, listOf(lhs), listOf(rhs), rawNode = ctx) if (rhs is UnaryOperator && rhs.input is DeclaredReferenceExpression) { (rhs.input as DeclaredReferenceExpression).resolutionHelper = lhs