From 6e2b5834a511c4127febd66c0b36aec8d06be8eb Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Mon, 30 Sep 2024 15:13:11 +0200 Subject: [PATCH] Setting correct block location for `Python.AST.ExceptHandler` (#1750) --- .../cpg/frontends/python/StatementHandler.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 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 3d29d4a9fa7..244f5fc8694 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 @@ -527,8 +527,7 @@ class StatementHandler(frontend: PythonLanguageFrontend) : // Make sure we open a new (block) scope for the function body. This is not a 1:1 // mapping to python scopes, since python only has a "function scope", but in the CPG // the function scope only comprises the function arguments, and we need a block scope - // to - // hold all local variables within the function body. + // to hold all local variables within the function body. result.body = makeBlock(s.body, parentNode = s, enterScope = true) } @@ -772,16 +771,17 @@ class StatementHandler(frontend: PythonLanguageFrontend) : } // Try to retrieve the code and location from the parent node, if it is a base stmt - var baseStmt = parentNode as? Python.AST.BaseStmt - return if (baseStmt != null) { - result.codeAndLocationFromChildren(baseStmt) - } else { - // Otherwise, continue without setting the location - log.warn( - "Could not set location on wrapped block because the parent node is not a python statement" - ) - result + val ast = parentNode as? Python.AST.AST + if (ast != null) { + // We need to scope the call to codeAndLocationFromChildren to our frontend, so that + // all Python.AST.AST nodes are accepted, otherwise it would be scoped to the handler + // and only Python.AST.BaseStmt nodes would be accepted. This would cause issues with + // other nodes that are not "statements", but also handled as part of this handler, + // e.g., the Python.AST.ExceptHandler. + with(frontend) { result.codeAndLocationFromChildren(ast) } } + + return result } /**