-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved stability of
isDerivedFrom
decisions (#1488)
This PR adds the way `Type.isDerivedFrom` works. More concretly, we are once again taking the "wrap state" of the type into account. This means that pointer types and non-pointer types will not match even though their root types derive from each other. This was the way this function behaved in the past and it seems this was changed at some point, which led to some weird over-approximations in call resolving, basically ignoring wether a type was a pointer or not.
- Loading branch information
1 parent
a102bff
commit f737e86
Showing
4 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
cpg-core/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/LanguageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2024, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* $$$$$$\ $$$$$$$\ $$$$$$\ | ||
* $$ __$$\ $$ __$$\ $$ __$$\ | ||
* $$ / \__|$$ | $$ |$$ / \__| | ||
* $$ | $$$$$$$ |$$ |$$$$\ | ||
* $$ | $$ ____/ $$ |\_$$ | | ||
* $$ | $$\ $$ | $$ | $$ | | ||
* \$$$$$ |$$ | \$$$$$ | | ||
* \______/ \__| \______/ | ||
* | ||
*/ | ||
package de.fraunhofer.aisec.cpg.frontends | ||
|
||
import de.fraunhofer.aisec.cpg.graph.newRecordDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.objectType | ||
import de.fraunhofer.aisec.cpg.graph.pointer | ||
import de.fraunhofer.aisec.cpg.isDerivedFrom | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class LanguageTest { | ||
|
||
@Test | ||
fun testLanguageDerived() { | ||
with(TestLanguageFrontend()) { | ||
val baseType = objectType("baseType") | ||
|
||
val myTypeRecord = newRecordDeclaration("myType", "class") | ||
myTypeRecord.superClasses = mutableListOf(baseType) | ||
val myType = myTypeRecord.toType() | ||
|
||
val pointerBaseType = baseType.pointer() | ||
val pointerMyType = myType.pointer() | ||
|
||
// pointer-type and non-pointer types -> will not match in any case | ||
var matches = pointerMyType.isDerivedFrom(myType) | ||
assertFalse(matches) | ||
|
||
// the same type will always match | ||
matches = pointerMyType.isDerivedFrom(pointerMyType) | ||
assertTrue(matches) | ||
|
||
// a pointer to the derived type will match a pointer to its base type | ||
matches = pointerMyType.isDerivedFrom(pointerBaseType) | ||
assertTrue(matches) | ||
|
||
// non-pointer types as well | ||
matches = myType.isDerivedFrom(baseType) | ||
assertTrue(matches) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters