Skip to content

Commit

Permalink
C: add names to gotos and labels (#1871)
Browse files Browse the repository at this point in the history
* fix goto / label missing names

* test++

* test++
  • Loading branch information
maximiliankaul authored Nov 29, 2024
1 parent 07c1dd5 commit 5155fff
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,23 @@ 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
}

private fun handleGotoStatement(ctx: IASTGotoStatement): GotoStatement {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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() {
Expand Down Expand Up @@ -1758,4 +1757,27 @@ internal class CXXLanguageFrontendTest : BaseTest() {
assertIs<CastExpression>(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<CLanguage>()
}
assertNotNull(tu)

val labelCName = "LAB_123"

val goto = tu.allChildren<GotoStatement>().firstOrNull()
assertIs<GotoStatement>(goto)
assertEquals(labelCName, goto.labelName)
assertLocalName(labelCName, goto)

val label = tu.labels[labelCName]
assertIs<LabelStatement>(label)
assertLocalName(labelCName, label)

assertEquals(label, goto.targetLabel)
}
}
5 changes: 5 additions & 0 deletions cpg-language-cxx/src/test/resources/c/goto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void foo() {
goto LAB_123;
LAB_123:
return;
}

0 comments on commit 5155fff

Please sign in to comment.