Skip to content

Commit

Permalink
Resolve chained calls
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 13, 2023
1 parent a96e175 commit 40d9be8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ open class CallResolver(ctx: TranslationContext) : SymbolResolverPass(ctx) {
curClass: RecordDeclaration?,
call: CallExpression
): List<FunctionDeclaration> {
// We need to resolve the base calls first. This might be done duplicate now
if (callee is MemberExpression && callee.base is CallExpression) {
handleCallExpression(curClass, callee.base as CallExpression)
}

// We need to adjust certain types of the base in case of a super call and we delegate this.
// If that is successful, we can continue with regular resolving
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,29 @@ class GoLanguageFrontendTest : BaseTest() {
assertIs<MemberExpression>(mce)
assertRefersTo(mce, field)
}

@Test
fun testChainedCall() {
val topLevel = Path.of("src", "test", "resources", "golang", "chained")
val tu =
analyze(
listOf(
topLevel.resolve("chained.go").toFile(),
),
topLevel,
true
) {
it.registerLanguage<GoLanguage>()
}
assertNotNull(tu)

val type = tu.records["Type"]
assertNotNull(type)

val elem = type.methods["Elem"]
assertNotNull(elem)

val call = tu.calls["Elem"]
assertInvokes(call, elem)
}
}
16 changes: 16 additions & 0 deletions cpg-language-go/src/test/resources/golang/chained/chained.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package chained

type Type interface {
Elem() Type
}

func TypeOf(i any) Type {
return nil
}

type MyStruct interface{}

var (
structType = TypeOf((*MyStruct)(nil)).Elem()
_ = structType
)

0 comments on commit 40d9be8

Please sign in to comment.