Skip to content

Commit

Permalink
Calculating multi language in component and translation result
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Dec 21, 2024
1 parent 6e601b6 commit 9e6e14a
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ class ScopeManager : ScopeProvider {
helper?.type is FunctionPointerType && it is FunctionDeclaration -> {
val fptrType = helper.type as FunctionPointerType
// TODO(oxisto): Support multiple return values
val returnType = it.returnTypes.firstOrNull() ?: IncompleteType()
val returnType =
it.returnTypes.firstOrNull() ?: IncompleteType(ref.language)
returnType == fptrType.returnType &&
it.matchesSignature(fptrType.parameters) !=
IncompatibleSignature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/
package de.fraunhofer.aisec.cpg

import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import de.fraunhofer.aisec.cpg.frontends.multiLanguage
import de.fraunhofer.aisec.cpg.graph.Component
import de.fraunhofer.aisec.cpg.graph.Name
import de.fraunhofer.aisec.cpg.graph.Node
Expand Down Expand Up @@ -173,6 +175,12 @@ class TranslationResult(
override val config: TranslationConfiguration
get() = finalCtx.config

override var language: Language<*>
get() {
return multiLanguage()
}
set(_) {}

companion object {
const val SOURCE_LOCATIONS_TO_FRONTEND = "sourceLocationsToFrontend"
const val APPLICATION_LOCAL_NAME = "application"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
package de.fraunhofer.aisec.cpg.frontends

import de.fraunhofer.aisec.cpg.graph.Node
import de.fraunhofer.aisec.cpg.graph.types.Type
import kotlin.reflect.KClass

Expand Down Expand Up @@ -56,3 +57,14 @@ class MultipleLanguages(val languages: Set<Language<*>>) : Language<Nothing>() {
override val builtInTypes: Map<String, Type> = mapOf()
override val compoundAssignmentOperators: Set<String> = setOf()
}

fun Node.multiLanguage(): Language<*> {
val languages = astChildren.map { it.language }.toSet()
return if (languages.size == 1) {
languages.singleOrNull() ?: UnknownLanguage
} else if (languages.size > 1) {
MultipleLanguages(languages = languages)
} else {
UnknownLanguage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
package de.fraunhofer.aisec.cpg.graph

import de.fraunhofer.aisec.cpg.PopulatedByPass
import de.fraunhofer.aisec.cpg.frontends.MultipleLanguages
import de.fraunhofer.aisec.cpg.frontends.UnknownLanguage
import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.multiLanguage
import de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.edges.ast.astEdgesOf
import de.fraunhofer.aisec.cpg.graph.edges.unwrapping
Expand Down Expand Up @@ -56,14 +56,6 @@ open class Component : Node() {
@Synchronized
fun addTranslationUnit(tu: TranslationUnitDeclaration) {
translationUnits.add(tu)

// Check, if this component is of a particular single language or multi-language
val languages = translationUnitEdges.map { it.end.language }.toSet()
if (languages.size == 1) {
this.language = languages.singleOrNull() ?: UnknownLanguage
} else if (languages.size > 1) {
this.language = MultipleLanguages(languages = languages)
}
}

/**
Expand All @@ -75,4 +67,10 @@ open class Component : Node() {

/** All outgoing interactions such as sending data to the network or some kind of IPC. */
val outgoingInteractions: MutableList<Node> = mutableListOf()

override var language: Language<*>
get() {
return multiLanguage()
}
set(_) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ fun LanguageProvider.autoType(): Type {
return AutoType(this.language)
}

fun MetadataProvider?.incompleteType(): Type {
return IncompleteType()
fun LanguageProvider.incompleteType(): Type {
return IncompleteType(this.language)
}

/** Returns a [PointerType] that describes an array reference to the current type. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ constructor(
typeName: String = "",
var parameters: List<Type> = listOf(),
var returnTypes: List<Type> = listOf(),
language: Language<*>? = null
language: Language<*>
) : Type(typeName, language) {

override fun reference(pointer: PointerType.PointerOrigin?): Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import de.fraunhofer.aisec.cpg.graph.types.PointerType.PointerOrigin
* unknown size apart from void. Therefore, this Type is not called VoidType
*/
class IncompleteType : Type {
@JvmOverloads constructor(language: Language<*>? = null) : super("void", language)
constructor(language: Language<*>) : super("void", language)

/** @return PointerType to a IncompleteType, e.g. void* */
override fun reference(pointer: PointerOrigin?): Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Inference internal constructor(val start: Node, override val ctx: Translat
ContextProvider,
RawNodeTypeProvider<Nothing> {

override val language: Language<*>?
override val language: Language<*>
get() = start.language

override val isInferred: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@
*/
package de.fraunhofer.aisec.cpg.frontends

import de.fraunhofer.aisec.cpg.ScopeManager
import de.fraunhofer.aisec.cpg.TranslationConfiguration
import de.fraunhofer.aisec.cpg.TranslationContext
import de.fraunhofer.aisec.cpg.TranslationManager
import de.fraunhofer.aisec.cpg.TranslationResult
import de.fraunhofer.aisec.cpg.TypeManager
import de.fraunhofer.aisec.cpg.graph.Component
import de.fraunhofer.aisec.cpg.graph.newRecordDeclaration
import de.fraunhofer.aisec.cpg.graph.newTranslationUnitDeclaration
import de.fraunhofer.aisec.cpg.graph.objectType
import de.fraunhofer.aisec.cpg.graph.pointer
import de.fraunhofer.aisec.cpg.tryCast
Expand Down Expand Up @@ -63,4 +71,46 @@ class LanguageTest {
assertIs<ImplicitCast>(matches)
}
}

@Test
fun testMultiLanguage() {
val otherLanguage = object : TestLanguage() {}
val testLanguage = TestLanguage()

val result =
TranslationResult(
translationManager = TranslationManager.builder().build(),
finalCtx =
TranslationContext(
config = TranslationConfiguration.builder().build(),
scopeManager = ScopeManager(),
typeManager = TypeManager(),
),
)

val comp1 =
with(TestLanguageFrontend("::", language = otherLanguage)) {
val tu = newTranslationUnitDeclaration("tu-language-other")
val comp = Component()
comp.addTranslationUnit(tu)
comp
}
result.components += comp1

val comp2 =
with(TestLanguageFrontend("::", language = testLanguage)) {
val tu = newTranslationUnitDeclaration("tu-language-test")
val comp = Component()
comp.addTranslationUnit(tu)
comp
}
result.components += comp2

val language = result.language
assertIs<MultipleLanguages>(language)
assertEquals(setOf(otherLanguage, testLanguage), language.languages)

assertEquals(otherLanguage, result.components.getOrNull(0)?.language)
assertEquals(testLanguage, result.components.getOrNull(1)?.language)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ open class CPPLanguage :
IntegerType("unsigned long long int", 64, this, NumericType.Modifier.UNSIGNED),

// Boolean type
"bool" to BooleanType("bool"),
"bool" to BooleanType("bool", language = this),

// Character types
"signed char" to IntegerType("signed char", 8, this, NumericType.Modifier.SIGNED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ open class CXXLanguageFrontend(language: Language<CXXLanguageFrontend>, ctx: Tra
}
// void type
specifier.type == IASTSimpleDeclSpecifier.t_void -> {
IncompleteType()
incompleteType()
}
// __typeof__ type
specifier.type == IASTSimpleDeclSpecifier.t_typeof -> {
Expand Down

0 comments on commit 9e6e14a

Please sign in to comment.