Skip to content

Commit

Permalink
Fixed additional issue where type was inferred even though it was a t…
Browse files Browse the repository at this point in the history
…ypedef
  • Loading branch information
oxisto committed Dec 12, 2024
1 parent 87bc08f commit b85dcf1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<CPPLanguage>()
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<CLanguage>()
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CPPLanguage>()
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<CLanguage>()
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b85dcf1

Please sign in to comment.