Skip to content

Commit

Permalink
Fixed Go symbol resolving with builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 28, 2023
1 parent 15127bf commit a544ba8
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,48 +139,51 @@ class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx), ScopeProvider {

private fun addBuiltIn(): TranslationUnitDeclaration {
val builtin = newTranslationUnitDeclaration("builtin.go")
builtin.language = GoLanguage()
scopeManager.resetToGlobal(builtin)

val len = newFunctionDeclaration("len", localNameOnly = true)
len.parameters = listOf(newParameterDeclaration("v", GoLanguage().autoType()))
len.type =
typeManager.registerType(
FunctionType(funcTypeName(len.signatureTypes, len.returnTypes))
)
scopeManager.addDeclaration(len)

/**
* ```go
* func append(slice []Type, elems ...Type) []Type
* ```
*/
val append = newFunctionDeclaration("append", localNameOnly = true)
append.parameters =
listOf(
newParameterDeclaration("slice", GoLanguage().autoType().array()),
newParameterDeclaration("elems", GoLanguage().autoType(), variadic = true),
)
append.returnTypes = listOf(GoLanguage().autoType().array())
append.type =
typeManager.registerType(
FunctionType(funcTypeName(append.signatureTypes, append.returnTypes))
)
scopeManager.addDeclaration(append)

val error = newRecordDeclaration("error", "interface")
scopeManager.enterScope(error)

val errorFunc = newMethodDeclaration("Error", recordDeclaration = error)
errorFunc.returnTypes = listOf(GoLanguage().primitiveType("string"))
errorFunc.type =
typeManager.registerType(
FunctionType(funcTypeName(errorFunc.signatureTypes, errorFunc.returnTypes))
)
scopeManager.addDeclaration(errorFunc)

scopeManager.leaveScope(error)

return builtin
return with(builtin) {
val len = newFunctionDeclaration("len", localNameOnly = true)
len.parameters = listOf(newParameterDeclaration("v", autoType()))
len.returnTypes = listOf(primitiveType("int"))
len.type =
typeManager.registerType(
FunctionType(funcTypeName(len.signatureTypes, len.returnTypes))
)
scopeManager.addDeclaration(len)

/**
* ```go
* func append(slice []Type, elems ...Type) []Type
* ```
*/
val append = newFunctionDeclaration("append", localNameOnly = true)
append.parameters =
listOf(
newParameterDeclaration("slice", autoType().array()),
newParameterDeclaration("elems", autoType(), variadic = true),
)
append.returnTypes = listOf(autoType().array())
append.type =
typeManager.registerType(
FunctionType(funcTypeName(append.signatureTypes, append.returnTypes))
)
scopeManager.addDeclaration(append)

val error = newRecordDeclaration("error", "interface")
scopeManager.enterScope(error)

val errorFunc = newMethodDeclaration("Error", recordDeclaration = error)
errorFunc.returnTypes = listOf(primitiveType("string"))
errorFunc.type =
typeManager.registerType(
FunctionType(funcTypeName(errorFunc.signatureTypes, errorFunc.returnTypes))
)
scopeManager.addDeclaration(errorFunc)

scopeManager.leaveScope(error)
builtin
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,21 @@ class GoLanguageFrontendTest : BaseTest() {
assertNotNull(app)

val tus = app.translationUnits

// fetch the function declaration from the struct TU
val tu2 = tus[1]

val p2 = tu2.namespaces["p"]
assertNotNull(p2)

val myOtherFunc = p2.methods["myOtherFunc"]
assertNotNull(myOtherFunc)
assertFalse(myOtherFunc.isImplicit)

val newMyStruct = p2.functions["NewMyStruct"]
assertNotNull(newMyStruct)

// and compare it with the call TU
val tu = tus[0]

val p = tu.namespaces["p"]
Expand All @@ -697,23 +712,19 @@ class GoLanguageFrontendTest : BaseTest() {
assertEquals(objectType("p.MyStruct").pointer(), c.type)
}

val newMyStruct = assertIs<CallExpression>(c.firstAssignment)
val newMyStructCall = assertIs<CallExpression>(c.firstAssignment)
assertInvokes(newMyStructCall, newMyStruct)

// fetch the function declaration from the other TU
val tu2 = tus[1]

val p2 = tu2.namespaces["p"]
assertNotNull(p2)

val newMyStructDef = p2.functions["NewMyStruct"]
assertTrue(newMyStruct.invokes.contains(newMyStructDef))

val call = body.statements[1] as? MemberCallExpression
val call = tu.calls["myOtherFunc"] as? MemberCallExpression
assertNotNull(call)

val base = call.base as? Reference
assertNotNull(base)
assertEquals(c, base.refersTo)
assertRefersTo(base, c)

val myOtherFuncCall = tu.calls["myOtherFunc"]
assertNotNull(myOtherFuncCall)
assertInvokes(myOtherFuncCall, myOtherFunc)

val go = main.calls["go"]
assertNotNull(go)
Expand Down
9 changes: 4 additions & 5 deletions cpg-language-go/src/test/resources/golang/call.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package p

import ("http")

func main() {
c := NewMyStruct()
c.myOtherFunc()
c := NewMyStruct()
if i := len("a"); i < 1 {
c.myOtherFunc(i)
}

go c.MyFunc()
go c.MyFunc()
}
12 changes: 5 additions & 7 deletions cpg-language-go/src/test/resources/golang/if.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package p

import ("fmt")

func main() {
var b bool = true
var b bool = true

if b {
b = false
}
}
if b {
b = false
}
}
6 changes: 3 additions & 3 deletions cpg-language-go/src/test/resources/golang/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type MyInterface interface {
}

func (s MyStruct) MyFunc() string {
fmt.Printf(s.myOtherFunc(), s.MyField)
fmt.Printf(s.myOtherFunc(1), s.MyField)

return "s"
}

func (s MyStruct) myOtherFunc() string {
return "%d"
func (s MyStruct) myOtherFunc(i int) string {
return fmt.Sprintf("%d", i)
}

func NewMyStruct() *MyStruct {
Expand Down

0 comments on commit a544ba8

Please sign in to comment.