-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not resolve unqualified symbols in current record without `HasImpl…
…icitReceiver` (#1984) * Introduce `HasExplicitMemberAccess` trait Fixes #1983 * Added trait to GoLanguage as well to be safe * Added more documentation * Spotless * Addressed code review * Using existing trait * added more tests * Fixed the tests as good as they can be fixed in this PR * a -> globalA * Fixed another bug where field declarations were declared twice * Update cpg-language-python/src/main/kotlin/de/fraunhofer/aisec/cpg/passes/PythonAddDeclarationsPass.kt Co-authored-by: Maximilian Kaul <[email protected]> * Better documentation --------- Co-authored-by: Maximilian Kaul <[email protected]>
- Loading branch information
1 parent
167a54d
commit c72becd
Showing
10 changed files
with
214 additions
and
34 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
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
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
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
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
91 changes: 91 additions & 0 deletions
91
...age-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/SymbolResolverTest.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,91 @@ | ||
/* | ||
* Copyright (c) 2025, 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.python | ||
|
||
import de.fraunhofer.aisec.cpg.graph.* | ||
import de.fraunhofer.aisec.cpg.graph.declarations.FieldDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.declarations.MethodDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression | ||
import de.fraunhofer.aisec.cpg.graph.statements.expressions.MemberExpression | ||
import de.fraunhofer.aisec.cpg.test.analyze | ||
import de.fraunhofer.aisec.cpg.test.assertNotRefersTo | ||
import de.fraunhofer.aisec.cpg.test.assertRefersTo | ||
import java.io.File | ||
import kotlin.test.* | ||
|
||
class SymbolResolverTest { | ||
@Test | ||
fun testFields() { | ||
val topLevel = File("src/test/resources/python/fields.py") | ||
val result = | ||
analyze(listOf(topLevel), topLevel.toPath(), true) { | ||
it.registerLanguage<PythonLanguage>() | ||
} | ||
|
||
val globalA = | ||
result.namespaces["fields"] | ||
.variables[{ it.name.localName == "a" && it !is FieldDeclaration }] | ||
assertNotNull(globalA) | ||
|
||
// Make sure, we only have one (!) field a | ||
val fieldsA = result.records["MyClass"]?.fields("a") | ||
val fieldA = fieldsA?.singleOrNull() | ||
assertNotNull(fieldA) | ||
|
||
val aRefs = result.refs("a") | ||
aRefs.filterIsInstance<MemberExpression>().forEach { assertRefersTo(it, fieldA) } | ||
aRefs.filter { it !is MemberExpression }.forEach { assertRefersTo(it, globalA) } | ||
|
||
// We should only have one reference to "os" -> the member expression "self.os" | ||
val osRefs = result.refs("os") | ||
assertEquals(1, osRefs.size) | ||
assertIs<MemberExpression>(osRefs.singleOrNull()) | ||
|
||
// "os.name" is not a member expression but a reference to the field "name" of the "os" | ||
// module, therefore it is a reference | ||
val osNameRefs = result.refs("os.name") | ||
assertEquals(1, osNameRefs.size) | ||
assertIsNot<MemberExpression>(osNameRefs.singleOrNull()) | ||
|
||
// Same tests but for fields declared at the record level. | ||
// A variable "declared" inside a class is considered a field in Python. | ||
val fieldCopyA = result.records["MyClass"]?.fields["copyA"] | ||
assertIs<FieldDeclaration>(fieldCopyA) | ||
val baz = result.records["MyClass"]?.methods["baz"] | ||
assertIs<MethodDeclaration>(baz) | ||
val bazPrint = baz.calls("print").singleOrNull() | ||
assertIs<CallExpression>(bazPrint) | ||
val bazPrintArgument = bazPrint.arguments.firstOrNull() | ||
assertRefersTo(bazPrintArgument, fieldCopyA) | ||
|
||
// make sure, that this does not work without the receiver | ||
val bazDoesNotWork = baz.calls("doesNotWork").singleOrNull() | ||
assertIs<CallExpression>(bazDoesNotWork) | ||
val bazDoesNotWorkArgument = bazDoesNotWork.arguments.firstOrNull() | ||
assertNotNull(bazDoesNotWorkArgument) | ||
assertNotRefersTo(bazDoesNotWorkArgument, fieldCopyA) | ||
} | ||
} |
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,23 @@ | ||
import os | ||
|
||
a = "Hello" | ||
|
||
class MyClass: | ||
copyA = 1 | ||
def foo(self): | ||
self.a = 1 | ||
print(a) | ||
|
||
def bar(self): | ||
self.os = 1 | ||
print(os.name) | ||
self.a = 1 | ||
|
||
def baz(self): | ||
print(self.copyA) | ||
doesNotWork(copyA) # not defined | ||
|
||
m = MyClass() | ||
m.foo() | ||
m.bar() | ||
m.baz() |