From 6067d07e5b13a82367274850e864f03e1f3aba89 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Tue, 27 Aug 2024 11:20:48 +0200 Subject: [PATCH 1/9] TODO -> Problem --- .../cpg/frontends/python/StatementHandler.kt | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index 00acbbe7c6..ac5ff206d9 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -139,7 +139,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : val ret = newWhileStatement(rawNode = node) ret.condition = frontend.expressionHandler.handle(node.test) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { TODO("Not supported") } + node.orelse.firstOrNull()?.let { + frontend.currentTU?.addDeclaration( + newProblemDeclaration("Cannot handle \"orelse\" in while loops.") + ) + } return ret } @@ -148,7 +152,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.iterable = frontend.expressionHandler.handle(node.iter) ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { TODO("Not supported") } + node.orelse.firstOrNull()?.let { + frontend.currentTU?.addDeclaration( + newProblemDeclaration("Cannot handle \"orelse\" in for loops.") + ) + } return ret } @@ -157,7 +165,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.iterable = frontend.expressionHandler.handle(node.iter) ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { TODO("Not supported") } + node.orelse.firstOrNull()?.let { + frontend.currentTU?.addDeclaration( + newProblemDeclaration("Cannot handle \"orelse\" in async for loops.") + ) + } return ret } @@ -391,7 +403,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ) { // We can merge posonlyargs and args because both are positional arguments. We do not // enforce that posonlyargs can ONLY be used in a positional style, whereas args can be used - // both in positional as well as keyword style. + // both in positional and keyword style. var positionalArguments = args.posonlyargs + args.args // Handle arguments @@ -419,12 +431,19 @@ class StatementHandler(frontend: PythonLanguageFrontend) : when (result) { is ConstructorDeclaration -> result.receiver = recvNode is MethodDeclaration -> result.receiver = recvNode - else -> TODO() + else -> + frontend.currentTU?.addDeclaration( + newProblemDeclaration( + "Expected a constructor or method declaration. Got something else." + ) + ) } } } - if (recordDeclaration != null) { + if ( + recordDeclaration != null && positionalArguments.size > 1 + ) { // TODO the second check fixes a bug but is this correct...??? // first argument is the receiver for (arg in positionalArguments.subList(1, positionalArguments.size)) { handleArgument(arg, arg in args.posonlyargs) From d71b0009be32174d60d617c2f3c279515b702d38 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Thu, 5 Sep 2024 14:31:39 +0200 Subject: [PATCH 2/9] only handle args in methods when there is more than 1 --- .../cpg/frontends/python/StatementHandler.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index 36140e3789..cfa3f24445 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -113,7 +113,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : // Wildcards luckily do not have aliases val decl = if (imp.name == "*") { - // In the wildcard case, our "import" is the module name and we set "wildcard" + // In the wildcard case, our "import" is the module name, and we set "wildcard" // to true newImportDeclaration(module, true, rawNode = imp) } else { @@ -440,12 +440,13 @@ class StatementHandler(frontend: PythonLanguageFrontend) : } } - if ( - recordDeclaration != null && positionalArguments.size > 1 - ) { // TODO the second check fixes a bug but is this correct...??? - // first argument is the receiver - for (arg in positionalArguments.subList(1, positionalArguments.size)) { - handleArgument(arg, arg in args.posonlyargs) + if (recordDeclaration != null) { + if ( + positionalArguments.size > 1 // more arguments than only a receiver + ) { // first argument is the receiver + for (arg in positionalArguments.subList(1, positionalArguments.size)) { + handleArgument(arg, arg in args.posonlyargs) + } } } else { for (arg in positionalArguments) { From d0782fceeaf0e67825482168f93eaab57355acb4 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Thu, 5 Sep 2024 15:40:27 +0200 Subject: [PATCH 3/9] first draft problem nodes --- .../aisec/cpg/graph/DeclarationBuilder.kt | 21 ++++++++++++ .../de/fraunhofer/aisec/cpg/graph/Node.kt | 6 ++++ .../de/fraunhofer/aisec/cpg/graph/Problem.kt | 32 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt index e24ce7ff18..630aab87f9 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt @@ -403,6 +403,27 @@ fun MetadataProvider.newProblemDeclaration( return node } +/** + * Creates a new [Problem]. The [MetadataProvider] receiver will be used to fill different meta-data + * using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires an + * appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended argument. + */ +@JvmOverloads +fun MetadataProvider.newProblem( + problem: String = "", + problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.PARSING, + rawNode: Any? = null +): Problem { + val node = Problem() + node.applyMetadata(this, EMPTY_NAME, rawNode, true) + + node.problem = problem + node.problemType = problemType + + log(node) + return node +} + /** * Creates a new [IncludeDeclaration]. The [MetadataProvider] receiver will be used to fill * different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt index 95a222b500..a9b65a315b 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Node.kt @@ -257,6 +257,12 @@ abstract class Node : @Relationship("ANNOTATIONS") var annotationEdges = astEdgesOf() var annotations by unwrapping(Node::annotationEdges) + /** + * Additional problem nodes. These nodes represent problems which occurred during processing of + * a node (i.e. only partially processed). + */ + val additionalProblems: MutableSet = mutableSetOf() + /** * If a node should be removed from the graph, just removing it from the AST is not enough (see * issue #60). It will most probably be referenced somewhere via DFG or EOG edges. Thus, if it diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt new file mode 100644 index 0000000000..04e364fd06 --- /dev/null +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + * $$$$$$\ $$$$$$$\ $$$$$$\ + * $$ __$$\ $$ __$$\ $$ __$$\ + * $$ / \__|$$ | $$ |$$ / \__| + * $$ | $$$$$$$ |$$ |$$$$\ + * $$ | $$ ____/ $$ |\_$$ | + * $$ | $$\ $$ | $$ | $$ | + * \$$$$$ |$$ | \$$$$$ | + * \______/ \__| \______/ + * + */ +package de.fraunhofer.aisec.cpg.graph + +/** A problem node documenting that something failed. */ +data class Problem( + override var problem: String = "", + override var problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.TRANSLATION +) : ProblemNode, MetadataProvider, Node() From f6149cb1aae9ea7daf8e059065e8efbfac5bb713 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Thu, 5 Sep 2024 15:50:22 +0200 Subject: [PATCH 4/9] Making use of the new problem storage. --- .../cpg/frontends/python/StatementHandler.kt | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index cfa3f24445..fba376d3c7 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -140,9 +140,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.condition = frontend.expressionHandler.handle(node.test) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) node.orelse.firstOrNull()?.let { - frontend.currentTU?.addDeclaration( - newProblemDeclaration("Cannot handle \"orelse\" in while loops.") - ) + ret.additionalProblems += + newProblem( + problem = "Cannot handle \"orelse\" in while loops.", + rawNode = node.orelse + ) } return ret } @@ -153,9 +155,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) node.orelse.firstOrNull()?.let { - frontend.currentTU?.addDeclaration( - newProblemDeclaration("Cannot handle \"orelse\" in for loops.") - ) + ret.additionalProblems += + newProblem( + problem = "Cannot handle \"orelse\" in for loops.", + rawNode = node.orelse + ) } return ret } @@ -166,9 +170,11 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) node.orelse.firstOrNull()?.let { - frontend.currentTU?.addDeclaration( - newProblemDeclaration("Cannot handle \"orelse\" in async for loops.") - ) + ret.additionalProblems += + newProblem( + problem = "Cannot handle \"orelse\" in async for loops.", + rawNode = node.orelse + ) } return ret } @@ -253,9 +259,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : frontend.scopeManager.enterScope(cls) stmt.keywords.forEach { - frontend.currentTU?.addDeclaration( - newProblemDeclaration("could not parse keyword $it in class") - ) + cls.addDeclaration(newProblemDeclaration("could not parse keyword $it in class")) } for (s in stmt.body) { @@ -431,11 +435,12 @@ class StatementHandler(frontend: PythonLanguageFrontend) : is ConstructorDeclaration -> result.receiver = recvNode is MethodDeclaration -> result.receiver = recvNode else -> - frontend.currentTU?.addDeclaration( - newProblemDeclaration( - "Expected a constructor or method declaration. Got something else." + result.additionalProblems += + newProblem( + problem = + "Expected a constructor or method declaration. Got something else.", + rawNode = result ) - ) } } } From e90892d856ac9d670ecf6b8321a7d5416b131ef1 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Thu, 5 Sep 2024 15:52:40 +0200 Subject: [PATCH 5/9] cleanup --- .../aisec/cpg/frontends/python/StatementHandler.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index fba376d3c7..67b31889cf 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -139,7 +139,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : val ret = newWhileStatement(rawNode = node) ret.condition = frontend.expressionHandler.handle(node.test) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { + if (node.orelse.isNotEmpty()) { ret.additionalProblems += newProblem( problem = "Cannot handle \"orelse\" in while loops.", @@ -154,7 +154,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.iterable = frontend.expressionHandler.handle(node.iter) ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { + if (node.orelse.isNotEmpty()) { ret.additionalProblems += newProblem( problem = "Cannot handle \"orelse\" in for loops.", @@ -169,7 +169,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.iterable = frontend.expressionHandler.handle(node.iter) ret.variable = frontend.expressionHandler.handle(node.target) ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) - node.orelse.firstOrNull()?.let { + if (node.orelse.isNotEmpty()) { ret.additionalProblems += newProblem( problem = "Cannot handle \"orelse\" in async for loops.", From da04417b89eb5cad55881662134a2e89742bc222 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Thu, 5 Sep 2024 15:57:26 +0200 Subject: [PATCH 6/9] cleanup --- .../cpg/frontends/python/StatementHandler.kt | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index 67b31889cf..7b42348611 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -259,7 +259,8 @@ class StatementHandler(frontend: PythonLanguageFrontend) : frontend.scopeManager.enterScope(cls) stmt.keywords.forEach { - cls.addDeclaration(newProblemDeclaration("could not parse keyword $it in class")) + cls.additionalProblems += + newProblem(problem = "could not parse keyword $it in class", rawNode = it) } for (s in stmt.body) { @@ -414,13 +415,12 @@ class StatementHandler(frontend: PythonLanguageFrontend) : // first argument is the `receiver` val recvPythonNode = positionalArguments.firstOrNull() if (recvPythonNode == null) { - val problem = - newProblemDeclaration( + result.additionalProblems += + newProblem( "Expected a receiver", problemType = ProblemNode.ProblemType.TRANSLATION, rawNode = args ) - frontend.scopeManager.addDeclaration(problem) } else { val tpe = recordDeclaration.toType() val recvNode = @@ -462,43 +462,39 @@ class StatementHandler(frontend: PythonLanguageFrontend) : args.vararg?.let { handleArgument(it, isPosOnly = false, isVariadic = true) } if (args.kwonlyargs.isNotEmpty()) { - val problem = + result.additionalProblems += newProblemDeclaration( "`kwonlyargs` are not yet supported", problemType = ProblemNode.ProblemType.TRANSLATION, rawNode = args ) - frontend.scopeManager.addDeclaration(problem) } if (args.kw_defaults.isNotEmpty()) { - val problem = + result.additionalProblems += newProblemDeclaration( "`kw_defaults` are not yet supported", problemType = ProblemNode.ProblemType.TRANSLATION, rawNode = args ) - frontend.scopeManager.addDeclaration(problem) } args.kwarg?.let { - val problem = + result.additionalProblems += newProblemDeclaration( "`kwarg` is not yet supported", problemType = ProblemNode.ProblemType.TRANSLATION, rawNode = it ) - frontend.scopeManager.addDeclaration(problem) } if (args.defaults.isNotEmpty()) { - val problem = + result.additionalProblems += newProblemDeclaration( "`defaults` are not yet supported", problemType = ProblemNode.ProblemType.TRANSLATION, rawNode = args ) - frontend.scopeManager.addDeclaration(problem) } } From af5e3d0f3c9fb26ae95b25ae91d92e22670468f0 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Fri, 6 Sep 2024 09:50:43 +0200 Subject: [PATCH 7/9] remove `Problem` and use `ProblemExpression` instead --- .../aisec/cpg/graph/DeclarationBuilder.kt | 21 ------------ .../de/fraunhofer/aisec/cpg/graph/Problem.kt | 32 ------------------- .../cpg/frontends/python/StatementHandler.kt | 16 ++++------ 3 files changed, 6 insertions(+), 63 deletions(-) delete mode 100644 cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt index 630aab87f9..e24ce7ff18 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/DeclarationBuilder.kt @@ -403,27 +403,6 @@ fun MetadataProvider.newProblemDeclaration( return node } -/** - * Creates a new [Problem]. The [MetadataProvider] receiver will be used to fill different meta-data - * using [Node.applyMetadata]. Calling this extension function outside of Kotlin requires an - * appropriate [MetadataProvider], such as a [LanguageFrontend] as an additional prepended argument. - */ -@JvmOverloads -fun MetadataProvider.newProblem( - problem: String = "", - problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.PARSING, - rawNode: Any? = null -): Problem { - val node = Problem() - node.applyMetadata(this, EMPTY_NAME, rawNode, true) - - node.problem = problem - node.problemType = problemType - - log(node) - return node -} - /** * Creates a new [IncludeDeclaration]. The [MetadataProvider] receiver will be used to fill * different meta-data using [Node.applyMetadata]. Calling this extension function outside of Kotlin diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt deleted file mode 100644 index 04e364fd06..0000000000 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Problem.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - * $$$$$$\ $$$$$$$\ $$$$$$\ - * $$ __$$\ $$ __$$\ $$ __$$\ - * $$ / \__|$$ | $$ |$$ / \__| - * $$ | $$$$$$$ |$$ |$$$$\ - * $$ | $$ ____/ $$ |\_$$ | - * $$ | $$\ $$ | $$ | $$ | - * \$$$$$ |$$ | \$$$$$ | - * \______/ \__| \______/ - * - */ -package de.fraunhofer.aisec.cpg.graph - -/** A problem node documenting that something failed. */ -data class Problem( - override var problem: String = "", - override var problemType: ProblemNode.ProblemType = ProblemNode.ProblemType.TRANSLATION -) : ProblemNode, MetadataProvider, Node() diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index 7b42348611..808d088cf5 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -141,7 +141,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) if (node.orelse.isNotEmpty()) { ret.additionalProblems += - newProblem( + newProblemExpression( problem = "Cannot handle \"orelse\" in while loops.", rawNode = node.orelse ) @@ -156,7 +156,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) if (node.orelse.isNotEmpty()) { ret.additionalProblems += - newProblem( + newProblemExpression( problem = "Cannot handle \"orelse\" in for loops.", rawNode = node.orelse ) @@ -171,7 +171,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.statement = makeBlock(node.body).codeAndLocationFromChildren(node) if (node.orelse.isNotEmpty()) { ret.additionalProblems += - newProblem( + newProblemExpression( problem = "Cannot handle \"orelse\" in async for loops.", rawNode = node.orelse ) @@ -260,7 +260,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : stmt.keywords.forEach { cls.additionalProblems += - newProblem(problem = "could not parse keyword $it in class", rawNode = it) + newProblemExpression(problem = "could not parse keyword $it in class", rawNode = it) } for (s in stmt.body) { @@ -416,11 +416,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : val recvPythonNode = positionalArguments.firstOrNull() if (recvPythonNode == null) { result.additionalProblems += - newProblem( - "Expected a receiver", - problemType = ProblemNode.ProblemType.TRANSLATION, - rawNode = args - ) + newProblemExpression("Expected a receiver", rawNode = args) } else { val tpe = recordDeclaration.toType() val recvNode = @@ -436,7 +432,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : is MethodDeclaration -> result.receiver = recvNode else -> result.additionalProblems += - newProblem( + newProblemExpression( problem = "Expected a constructor or method declaration. Got something else.", rawNode = result From 4db41d44dbac8428a60e57345f005df7c087f726 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Fri, 6 Sep 2024 14:07:04 +0200 Subject: [PATCH 8/9] orelse is a list -> don't use it as rawNode --- .../fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index fbe1d4f221..64c0459841 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -144,7 +144,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.additionalProblems += newProblemExpression( problem = "Cannot handle \"orelse\" in while loops.", - rawNode = node.orelse + rawNode = node ) } return ret @@ -168,7 +168,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ret.additionalProblems += newProblemExpression( problem = "Cannot handle \"orelse\" in for loops.", - rawNode = node.orelse + rawNode = node ) } return ret From 4aa244f45f9cffc14ec16897eff975db94d835e6 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Fri, 6 Sep 2024 14:23:20 +0200 Subject: [PATCH 9/9] fix doc string --- .../fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt index 64c0459841..d4d56396f7 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/StatementHandler.kt @@ -497,7 +497,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : } /** - * This function "wraps" a list of [Python.ASTBASEstmt] nodes into a [Block]. Since the list + * This function "wraps" a list of [Python.AST.BaseStmt] nodes into a [Block]. Since the list * itself does not have a code/location, we need to employ [codeAndLocationFromChildren] on the * [parentNode]. */