From 2076f442c0181f7f1d15317d25e3bd4f0dd8ee53 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Fri, 26 Jul 2024 09:53:05 +0200 Subject: [PATCH] Populating `codeOf` of `ASTModule` (#1634) * 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? * make sonarcube happy --------- Co-authored-by: Maximilian Kaul --- .../python/PythonLanguageFrontend.kt | 19 ++++++++++++------- 1 file changed, 12 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..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,15 +181,20 @@ 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) + return if (astNode is Python.ASTModule) { + 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()) + lines.joinToString(separator = lineSeparator.toString()) + } else { + null + } } - return null } private fun getRelevantLines(location: PhysicalLocation): MutableList {