diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/GotoStatement.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/GotoStatement.kt index 813e59742a..0d0a653146 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/GotoStatement.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/statements/GotoStatement.kt @@ -25,7 +25,8 @@ */ package de.fraunhofer.aisec.cpg.graph.statements -import java.util.Objects +import java.util.* +import org.apache.commons.lang3.builder.ToStringBuilder class GotoStatement : Statement() { var labelName: String = "" @@ -42,4 +43,12 @@ class GotoStatement : Statement() { } override fun hashCode() = Objects.hash(super.hashCode(), labelName, targetLabel) + + override fun toString(): String { + return ToStringBuilder(this, TO_STRING_STYLE) + .append("labelName", labelName) + .append("targetName", targetLabel) + .append("location", location) + .toString() + } } diff --git a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/StatementHandler.kt b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/StatementHandler.kt index 2dfc0f60b9..3c507437de 100644 --- a/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/StatementHandler.kt +++ b/cpg-language-cxx/src/main/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/StatementHandler.kt @@ -160,6 +160,7 @@ class StatementHandler(lang: CXXLanguageFrontend) : val statement = newLabelStatement(rawNode = ctx) statement.subStatement = handle(ctx.nestedStatement) statement.label = ctx.name.toString() + statement.name = newName(name = ctx.name.toString()) return statement } @@ -167,12 +168,15 @@ class StatementHandler(lang: CXXLanguageFrontend) : val statement = newGotoStatement(rawNode = ctx) val assigneeTargetLabel = BiConsumer { _: Any, to: Node -> statement.targetLabel = to as LabelStatement + to.label?.let { + statement.labelName = it + statement.name = newName(it) + } } val b: IBinding? try { b = ctx.name.resolveBinding() if (b is ILabel) { - b.labelStatement // If the bound AST node is/or was transformed into a CPG node the cpg node is bound // to the CPG goto statement frontend.registerObjectListener(b.labelStatement, assigneeTargetLabel) diff --git a/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CXXLanguageFrontendTest.kt b/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CXXLanguageFrontendTest.kt index 3e1b661e95..4e6243fd57 100644 --- a/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CXXLanguageFrontendTest.kt +++ b/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/cxx/CXXLanguageFrontendTest.kt @@ -25,8 +25,8 @@ */ package de.fraunhofer.aisec.cpg.frontends.cxx -import de.fraunhofer.aisec.cpg.* import de.fraunhofer.aisec.cpg.InferenceConfiguration.Companion.builder +import de.fraunhofer.aisec.cpg.TranslationConfiguration import de.fraunhofer.aisec.cpg.graph.* import de.fraunhofer.aisec.cpg.graph.declarations.* import de.fraunhofer.aisec.cpg.graph.statements.* @@ -41,9 +41,8 @@ import de.fraunhofer.aisec.cpg.sarif.Region import de.fraunhofer.aisec.cpg.test.* import java.io.File import java.nio.file.Path -import java.util.* import java.util.function.Consumer -import kotlin.collections.set +import kotlin.Throws import kotlin.test.* internal class CXXLanguageFrontendTest : BaseTest() { @@ -1758,4 +1757,27 @@ internal class CXXLanguageFrontendTest : BaseTest() { assertIs(cast) assertLocalName("mytype", cast.castType) } + + @Test + fun testGoto() { + val file = File("src/test/resources/c/goto.c") + val tu = + analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) { + it.registerLanguage() + } + assertNotNull(tu) + + val labelCName = "LAB_123" + + val goto = tu.allChildren().firstOrNull() + assertIs(goto) + assertEquals(labelCName, goto.labelName) + assertLocalName(labelCName, goto) + + val label = tu.labels[labelCName] + assertIs(label) + assertLocalName(labelCName, label) + + assertEquals(label, goto.targetLabel) + } } diff --git a/cpg-language-cxx/src/test/resources/c/goto.c b/cpg-language-cxx/src/test/resources/c/goto.c new file mode 100644 index 0000000000..61c41e0bfb --- /dev/null +++ b/cpg-language-cxx/src/test/resources/c/goto.c @@ -0,0 +1,5 @@ +void foo() { + goto LAB_123; +LAB_123: + return; +}