Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better codeOf by re-creating the original config file #1876

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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/)
Expand Down Expand Up @@ -118,7 +118,7 @@ class IniFileFrontend(language: Language<IniFileFrontend>, 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)
Expand All @@ -141,8 +141,53 @@ class IniFileFrontend(language: Language<IniFileFrontend>, 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} = ${entry.value}"
}

/**
* 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) }
}

/**
Expand Down
Loading