From d57e87db39b383891a5852420347fbcba7f04a31 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 25 Jul 2024 16:44:58 +0200 Subject: [PATCH 1/2] Populating `codeOf` of `ASTModule` This error message was annoying me, so here is a fix. I am not sure if it is easy to find the "location" of the module. The region should be able to be computed out of the lengt of `fileContents` and its actual content. I actually thought we had a function like that somewhere @konradweiss? --- .../frontends/python/PythonLanguageFrontend.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt index 22a9d80fe7..fafc416d81 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt @@ -181,15 +181,19 @@ class PythonLanguageFrontend(language: Language, ctx: Tr * 3) Remove trailing whitespaces / tabs */ override fun codeOf(astNode: Python.AST): String? { - val location = locationOf(astNode) - if (location != null) { - var lines = getRelevantLines(location) - lines = removeExtraAtEnd(location, lines) - lines = fixStartColumn(location, lines) + if (astNode is Python.ASTModule) { + return fileContent + } else { + val location = locationOf(astNode) + if (location != null) { + var lines = getRelevantLines(location) + lines = removeExtraAtEnd(location, lines) + lines = fixStartColumn(location, lines) - return lines.joinToString(separator = lineSeparator.toString()) + return lines.joinToString(separator = lineSeparator.toString()) + } + return null } - return null } private fun getRelevantLines(location: PhysicalLocation): MutableList { From c14cf12fe67ffa8691ae494e78c25363f5c46700 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Fri, 26 Jul 2024 08:57:45 +0200 Subject: [PATCH 2/2] make sonarcube happy --- .../aisec/cpg/frontends/python/PythonLanguageFrontend.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt index fafc416d81..bc53dc070d 100644 --- a/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt +++ b/cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonLanguageFrontend.kt @@ -181,8 +181,8 @@ class PythonLanguageFrontend(language: Language, ctx: Tr * 3) Remove trailing whitespaces / tabs */ override fun codeOf(astNode: Python.AST): String? { - if (astNode is Python.ASTModule) { - return fileContent + return if (astNode is Python.ASTModule) { + fileContent } else { val location = locationOf(astNode) if (location != null) { @@ -190,9 +190,10 @@ class PythonLanguageFrontend(language: Language, ctx: Tr lines = removeExtraAtEnd(location, lines) lines = fixStartColumn(location, lines) - return lines.joinToString(separator = lineSeparator.toString()) + lines.joinToString(separator = lineSeparator.toString()) + } else { + null } - return null } }