From b33dc3890fba267a3e819fa196673add782c981e Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Mon, 2 Dec 2024 13:44:12 +0100 Subject: [PATCH 1/2] Better codeOf by re-creating the original config file --- .../frontend/configfiles/IniFileFrontend.kt | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt b/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt index 340f839806..5990fcdf7e 100644 --- a/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt +++ b/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt @@ -39,7 +39,7 @@ import java.io.File import java.io.FileInputStream import java.net.URI import org.ini4j.Ini -import org.ini4j.Profile +import org.ini4j.Profile.Section /** * The INI file frontend. This frontend utilizes the [ini4j library](https://ini4j.sourceforge.net/) @@ -116,7 +116,7 @@ class IniFileFrontend(language: Language, ctx: TranslationConte * Translates a `Section` into a [RecordDeclaration] and handles all `entries` using * [handleEntry]. */ - private fun handleSection(section: Profile.Section) { + private fun handleSection(section: Section) { val record = newRecordDeclaration(name = section.name, kind = "section", rawNode = section) scopeManager.addDeclaration(record) scopeManager.enterScope(record) @@ -141,8 +141,53 @@ class IniFileFrontend(language: Language, ctx: TranslationConte return primitiveType("string") } + /** + * Returns an approximation of the original code by re-creating (parts of) the INI file given + * the parsed results provided by ini4j. This is not a perfect representation of the original + * code (comments, order, ...), however re-parsing it should result in the same + * CPG-representation. + */ override fun codeOf(astNode: Any): String? { - return astNode.toString() + return when (astNode) { + is Ini -> codeOfIni(astNode) + is Section -> codeOfSection(astNode) + is Map.Entry<*, *> -> codeOfEntry(astNode) + else -> null + } + } + + /** + * Returns an approximation of the original code by re-creating (parts of) the INI file given + * the parsed results provided by ini4j. This is not a perfect representation of the original + * code (comments, order, ...), however re-parsing it should result in the same + * CPG-representation. + */ + private fun codeOfIni(ini: Ini): String { + return ini.values.joinToString(System.lineSeparator()) { codeOfSection(it) } + } + + /** + * Returns an approximation of the original code by re-creating (parts of) the INI file given + * the parsed results provided by ini4j. This is not a perfect representation of the original + * code (comments, order, ...), however re-parsing it should result in the same + * CPG-representation. + */ + private fun codeOfEntry(entry: Map.Entry<*, *>): String { + return entry.key.toString() + " = " + entry.value.toString() + } + + /** + * Returns an approximation of the original code by re-creating (parts of) the INI file given + * the parsed results provided by ini4j. This is not a perfect representation of the original + * code (comments, order, ...), however re-parsing it should result in the same + * CPG-representation. + */ + private fun codeOfSection(section: Section): String { + return "[" + + section.name + + "]" + + System.lineSeparator() + + section.entries.joinToString(System.lineSeparator()) { codeOfEntry(it) } } /** From 0e2fe673f2eeef50e4a7a335ab05a48d6376f843 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Mon, 2 Dec 2024 13:54:00 +0100 Subject: [PATCH 2/2] Use a string template (where it improves code quality) --- .../aisec/cpg/frontend/configfiles/IniFileFrontend.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt b/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt index 5990fcdf7e..00f2dd26f4 100644 --- a/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt +++ b/cpg-language-ini/src/main/kotlin/de/fraunhofer/aisec/cpg/frontend/configfiles/IniFileFrontend.kt @@ -173,7 +173,7 @@ class IniFileFrontend(language: Language, ctx: TranslationConte * CPG-representation. */ private fun codeOfEntry(entry: Map.Entry<*, *>): String { - return entry.key.toString() + " = " + entry.value.toString() + return "${entry.key} = ${entry.value}" } /**