Skip to content

Commit

Permalink
Resolving composite literal key values in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Oct 14, 2023
1 parent 2d4b38a commit 1c60ceb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,25 @@ class GoExtraPass(ctx: TranslationContext) : ComponentPass(ctx), ScopeProvider {
init.type = type.elementType
} else if (init is KeyValueExpression && init.value is InitializerListExpression) {
init.value?.type = type.elementType
} else if (init is KeyValueExpression && init.key is InitializerListExpression) {
init.key?.type = type.elementType
}
}
} else if (type?.isMap == true) {
for (init in node.initializers) {
if (init is KeyValueExpression) {
if (init.key is InitializerListExpression) {
init.key?.type = (type as ObjectType).generics.getOrNull(0) ?: unknownType()
} else if (init.value is InitializerListExpression) {
init.value?.type =
(type as ObjectType).generics.getOrNull(1) ?: unknownType()
}
}
}
}

// We are not interested in arrays and maps, but only the "inner" single-object expressions
// Afterwards, we are not interested in arrays and maps, but only the "inner" single-object
// expressions
if (
type is UnknownType ||
(type is PointerType && type.isArray) ||
Expand Down
12 changes: 12 additions & 0 deletions cpg-language-go/src/test/resources/golang/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ var mapr = map[int][]byte{
var structr = []MyStruct{
0: {Field: 10},
}

type pairNameValue struct {
name, value string
}

var structKey = map[pairNameValue]uint64{
{name: "this", value: "that"}: 1,
}

var structValue = map[uint64]pairNameValue{
1: {name: "this", value: "that"},
}

0 comments on commit 1c60ceb

Please sign in to comment.