From b85dcf11478b8a600540eaf74e52ce32d26afd67 Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Thu, 12 Dec 2024 20:31:02 +0100 Subject: [PATCH] Fixed additional issue where type was inferred even though it was a typedef --- .../de/fraunhofer/aisec/cpg/ScopeManager.kt | 2 +- .../cpg/enhancements/types/TypedefTest.kt | 48 ++++++++++++++++++- .../frontends/cxx/CXXLanguageFrontendTest.kt | 36 -------------- .../resources/typedefs/weird_typedefs.cpp | 5 ++ 4 files changed, 53 insertions(+), 38 deletions(-) diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt index 01530f6db2..e20f32ed04 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/ScopeManager.kt @@ -818,7 +818,7 @@ class ScopeManager : ScopeProvider { // This process has several steps: // First, do a quick local lookup, to see if we have a typedef our current scope // (only do this if the name is not qualified) - if (!alias.isQualified() && current == currentScope) { + if (!alias.isQualified() && current == scope) { val decl = current.typedefs[alias] if (decl != null) { return decl.type diff --git a/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/types/TypedefTest.kt b/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/types/TypedefTest.kt index c52198a112..0a85008599 100644 --- a/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/types/TypedefTest.kt +++ b/cpg-language-cxx/src/test/kotlin/de/fraunhofer/aisec/cpg/enhancements/types/TypedefTest.kt @@ -25,6 +25,8 @@ */ package de.fraunhofer.aisec.cpg.enhancements.types +import de.fraunhofer.aisec.cpg.InferenceConfiguration.Companion.builder +import de.fraunhofer.aisec.cpg.frontends.cxx.CLanguage import de.fraunhofer.aisec.cpg.frontends.cxx.CPPLanguage import de.fraunhofer.aisec.cpg.graph.* import de.fraunhofer.aisec.cpg.graph.declarations.ValueDeclaration @@ -34,6 +36,7 @@ import de.fraunhofer.aisec.cpg.graph.types.NumericType import de.fraunhofer.aisec.cpg.graph.types.ObjectType import de.fraunhofer.aisec.cpg.graph.variables import de.fraunhofer.aisec.cpg.test.* +import java.io.File import java.nio.file.Path import kotlin.test.* @@ -195,8 +198,15 @@ internal class TypedefTest : BaseTest() { } val ullong1 = tu.variables["someUllong1"] + assertNotNull(ullong1) + val ullong2 = tu.variables["someUllong2"] - assertEquals(ullong1?.type, ullong2?.type) + assertNotNull(ullong2) + assertEquals(ullong1.type, ullong2.type) + + val records = tu.records + assertEquals(2, records.size) + assertEquals(listOf("bar", "foo"), records.map { it.name.localName }) } @Test @@ -243,4 +253,40 @@ internal class TypedefTest : BaseTest() { assertNotNull(size) assertRefersTo(size, sizeField) } + + @Test + fun testTypedefStructCPP() { + val file = File("src/test/resources/cxx/typedef_struct.cpp") + val tu = + analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) { + it.registerLanguage() + it.inferenceConfiguration(builder().enabled(false).build()) + } + with(tu) { + val me = tu.memberExpressions + me.forEach { assertNotNull(it.refersTo) } + + val test = tu.records.singleOrNull() + assertNotNull(test) + assertLocalName("test", test) + } + } + + @Test + fun testTypedefStructC() { + val file = File("src/test/resources/c/typedef_struct.c") + val tu = + analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) { + it.registerLanguage() + it.inferenceConfiguration(builder().enabled(false).build()) + } + with(tu) { + val me = tu.memberExpressions + me.forEach { assertNotNull(it.refersTo) } + + val test = tu.records.singleOrNull() + assertNotNull(test) + assertLocalName("test", test) + } + } } 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 8db67fc499..8592f8d0f7 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 @@ -1777,40 +1777,4 @@ internal class CXXLanguageFrontendTest : BaseTest() { assertEquals(label, goto.targetLabel) } - - @Test - fun testTypedefStructCPP() { - val file = File("src/test/resources/cxx/typedef_struct.cpp") - val tu = - analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) { - it.registerLanguage() - it.inferenceConfiguration(builder().enabled(false).build()) - } - with(tu) { - val me = tu.memberExpressions - me.forEach { assertNotNull(it.refersTo) } - - val test = tu.records.singleOrNull() - assertNotNull(test) - assertLocalName("test", test) - } - } - - @Test - fun testTypedefStructC() { - val file = File("src/test/resources/c/typedef_struct.c") - val tu = - analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) { - it.registerLanguage() - it.inferenceConfiguration(builder().enabled(false).build()) - } - with(tu) { - val me = tu.memberExpressions - me.forEach { assertNotNull(it.refersTo) } - - val test = tu.records.singleOrNull() - assertNotNull(test) - assertLocalName("test", test) - } - } } diff --git a/cpg-language-cxx/src/test/resources/typedefs/weird_typedefs.cpp b/cpg-language-cxx/src/test/resources/typedefs/weird_typedefs.cpp index 4ad3e9a075..b5826bbb33 100644 --- a/cpg-language-cxx/src/test/resources/typedefs/weird_typedefs.cpp +++ b/cpg-language-cxx/src/test/resources/typedefs/weird_typedefs.cpp @@ -2,6 +2,11 @@ // more conventionally spelled "typedef unsigned long long int ullong;" unsigned long typedef long int ullong; +// usage of type that is identical to typedef +unsigned long long int someUllong1; +// usage of typedef +ullong someUllong2; + // also possible with structs struct bar { int a;