Skip to content

Commit

Permalink
Handle nested member expression
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Aug 30, 2023
1 parent 41ecf92 commit 446f4a6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ open class VariableUsageResolver(ctx: TranslationContext) : SymbolResolverPass(c
}

protected fun resolveBase(reference: DeclaredReferenceExpression): Declaration? {
// We need to check, whether we first need to resolve our own base
if (reference is MemberExpression) {
val base = reference.base
if (base is DeclaredReferenceExpression && base.refersTo == null) {
base.refersTo = resolveBase(base)
}
}

val declaration = scopeManager.resolveReference(reference)
if (declaration != null) {
return declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,34 @@ class GoLanguageFrontendTest : BaseTest() {
assertNotNull(call)
assertInvokes(call, doInterface)
}

@Test
fun testFuncOptions() {
val topLevel = Path.of("src", "test", "resources", "golang", "options")
val result =
analyze(
listOf(
topLevel.resolve("srv.go").toFile(),
topLevel.resolve("srv_option.go").toFile()
),
topLevel,
true
) {
it.registerLanguage<GoLanguage>()
}
assertNotNull(result)

val inner = result.records["inner"]
assertNotNull(inner)

val field = inner.fields["field"]
assertNotNull(field)

val assign = result.assignments.firstOrNull()
assertNotNull(assign)

val mce = assign.target
assertIs<MemberExpression>(mce)
assertRefersTo(mce, field)
}
}
9 changes: 9 additions & 0 deletions cpg-language-go/src/test/resources/golang/options/srv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package options

type srv struct {
inner inner
}

type inner struct {
field int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package options

type Option func(*srv)

func WithField(a int) Option {
return func(s *srv) {
s.inner.field = a
}
}

0 comments on commit 446f4a6

Please sign in to comment.