Skip to content

Commit

Permalink
Fixed another bug where field declarations were declared twice
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jan 29, 2025
1 parent e4cff2b commit 5bdffd9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,18 @@ class PythonAddDeclarationsPass(ctx: TranslationContext) : ComponentPass(ctx), L

// There are a couple of things to consider now
var symbol =
// Since this is a WRITE access, we need
// - to look for a local symbol, unless
// Since this is a WRITE access, we need to look for a local symbol, unless
// - a global keyword is present for this symbol and scope
// - the name is qualified
if (targetScope != null) {
// When a target scope is set, then we have a "global" or "non local" keyword for
// this symbol, and we need to start looking in this scope
scopeManager.lookupSymbolByNodeName(ref, targetScope)
} else {
scopeManager.lookupSymbolByNodeName(ref) { it.scope == scopeManager.currentScope }
scopeManager.lookupSymbolByNodeName(ref) {
// Otherwise, we need to stick to the current scope unless the name is qualified
it.scope == scopeManager.currentScope || ref.name.isQualified()
}
}

// Nothing to create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class SymbolResolverTest {
.variables[{ it.name.localName == "a" && it !is FieldDeclaration }]
assertNotNull(globalA)

val fieldA = result.records["MyClass"]?.fields["a"]
// 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")
Expand Down
1 change: 1 addition & 0 deletions cpg-language-python/src/test/resources/python/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def foo(self):
def bar(self):
self.os = 1
print(os.name)
self.a = 1

def baz(self):
print(self.copyA)
Expand Down

0 comments on commit 5bdffd9

Please sign in to comment.