From 965bfdf47010fde8f9a0e5efbe05bc2b25899eb3 Mon Sep 17 00:00:00 2001 From: KuechA <31155350+KuechA@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:49:16 +0200 Subject: [PATCH 1/2] Support async with in python (#1795) Support async with --- .../de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt | 4 ++-- .../aisec/cpg/frontends/python/StatementHandler.kt | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt index ffa70363dfe..6e323eb7c59 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/NodeBuilder.kt @@ -284,8 +284,8 @@ fun T.codeAndLocationFrom(other: Node): T { * expression handler. */ context(CodeAndLocationProvider) -fun T.codeAndLocationFromOtherRawNode(rawNode: AstNode): T { - setCodeAndLocation(this@CodeAndLocationProvider, rawNode) +fun T.codeAndLocationFromOtherRawNode(rawNode: AstNode?): T { + rawNode?.let { setCodeAndLocation(this@CodeAndLocationProvider, it) } return this } 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 521dfc7858f..709bde56a96 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 @@ -72,13 +72,13 @@ class StatementHandler(frontend: PythonLanguageFrontend) : is Python.AST.Assert -> handleAssert(node) is Python.AST.Try -> handleTryStatement(node) is Python.AST.Delete -> handleDelete(node) - is Python.AST.With -> handleWithStatement(node) + is Python.AST.With, + is Python.AST.AsyncWith -> handleWithStatement(node) is Python.AST.Global -> handleGlobal(node) is Python.AST.Nonlocal -> handleNonLocal(node) is Python.AST.Raise -> handleRaise(node) is Python.AST.Match, - is Python.AST.TryStar, - is Python.AST.AsyncWith -> + is Python.AST.TryStar -> newProblemExpression( "The statement of class ${node.javaClass} is not supported yet", rawNode = node @@ -133,7 +133,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : * manager.__exit__(None, None, None) * ``` */ - private fun handleWithStatement(node: Python.AST.With): Block { + private fun handleWithStatement(node: Python.AST.NormalOrAsyncWith): Block { /** * Prepares the `manager = ContextManager()` and returns the random name for the "manager" * as well as the assignment. @@ -246,8 +246,8 @@ class StatementHandler(frontend: PythonLanguageFrontend) : tmpValName ) } - - val result = newBlock().codeAndLocationFromOtherRawNode(node).implicit() + val result = + newBlock().codeAndLocationFromOtherRawNode(node as? Python.AST.BaseStmt).implicit() // If there are multiple elements in node.items, we have to nest the try statements. // We start with a generic block for the outer context manager. @@ -293,7 +293,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : ) } } - .codeAndLocationFromOtherRawNode(node) + .codeAndLocationFromOtherRawNode(node as? Python.AST.BaseStmt) .implicit() // Add the catch block this.catchClauses.add( From 6c0682a7bc51b0f0412cd68fd2330cb451727dd5 Mon Sep 17 00:00:00 2001 From: KuechA <31155350+KuechA@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:05:45 +0200 Subject: [PATCH 2/2] Fix bugs in docs page (#1796) --- docs/docs/CPG/specs/dfg.md | 2 +- docs/docs/CPG/specs/eog.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/CPG/specs/dfg.md b/docs/docs/CPG/specs/dfg.md index cf0f786e04a..d5db7894a75 100755 --- a/docs/docs/CPG/specs/dfg.md +++ b/docs/docs/CPG/specs/dfg.md @@ -403,7 +403,7 @@ The return value flows to the whole statement. Scheme: ```mermaid flowchart LR - exception -- DFG --> node([ReturnStatement]); + exception -- DFG --> node([ThrowStatement]); parentException -- DFG --> node; exception -.- node; parentException -.- node; diff --git a/docs/docs/CPG/specs/eog.md b/docs/docs/CPG/specs/eog.md index 12ec6b46e71..d50463781e3 100644 --- a/docs/docs/CPG/specs/eog.md +++ b/docs/docs/CPG/specs/eog.md @@ -394,7 +394,7 @@ flowchart LR prev:::outer --EOG--> child1["exception"] child1 --EOG--> child2["parentException"] child2 --EOG-->parent - parent(["throw"]) --EOG--> catchingContext:::outer + parent(["ThrowStatement"]) --EOG--> catchingContext:::outer parent -.-> child1 parent -.-> child2