Skip to content

Commit

Permalink
parser: Better naming and handling of duplicate tags
Browse files Browse the repository at this point in the history
Having the same duplicate names of structs and unions was often
confusing.
  • Loading branch information
mefistotelis committed Mar 15, 2022
1 parent a1025b9 commit 8685e8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 19 deletions.
3 changes: 3 additions & 0 deletions csym/c/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ func fakeUnionString(t *UnionType) string {
// IsFakeTag reports whether the tag name is fake (generated by the compiler for
// symbols lacking a tag name).
func IsFakeTag(tag string) bool {
if i := strings.LastIndex(tag, "_duplicate_"); i > 0 {
tag = tag[:i]
}
if strings.HasPrefix(tag, "_") && strings.HasSuffix(tag, "fake") {
s := tag[len("_") : len(tag)-len("fake")]
_, err := strconv.Atoi(s)
Expand Down
10 changes: 5 additions & 5 deletions csym/parse_fixups.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func UniqueName(name string, addr uint32) string {
}

// UniqueTag returns a unique tag based on the given tag and duplicate index.
func UniqueTag(tag string, idx int) string {
return fmt.Sprintf("%s_duplicate_%d", tag, idx)
func UniqueTag(tag string, typ string, idx int) string {
return fmt.Sprintf("%s_duplicate_%s%d", tag, typ, idx)
}

// UniqueVarName returns a unique variable name based on the given variable
Expand Down Expand Up @@ -316,7 +316,7 @@ func UniqueStructTag(structTags map[string][]*c.StructType, t *c.StructType) str
if !ok { break } // the tag is unique - done
k := SliceIndex(len(structs), func(i int) bool { return structs[i] == t })
if k < 0 { k = len(structs) }
newTag = UniqueTag(newTag, k)
newTag = UniqueTag(newTag, "s", k)
}
return newTag
}
Expand All @@ -330,7 +330,7 @@ func UniqueUnionTag(unionTags map[string][]*c.UnionType, t *c.UnionType) string
if !ok { break } // the tag is unique - done
k := SliceIndex(len(unions), func(i int) bool { return unions[i] == t })
if k < 0 { k = len(unions) }
newTag = UniqueTag(newTag, k)
newTag = UniqueTag(newTag, "u", k)
}
return newTag
}
Expand All @@ -344,7 +344,7 @@ func UniqueEnumTag(EnumTags map[string][]*c.EnumType, t *c.EnumType) string {
if !ok { break } // the tag is unique - done
k := SliceIndex(len(enums), func(i int) bool { return enums[i] == t })
if k < 0 { k = len(enums) }
newTag = UniqueTag(newTag, k)
newTag = UniqueTag(newTag, "e", k)
}
return newTag
}
Expand Down
14 changes: 0 additions & 14 deletions csym/parse_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,6 @@ func (p *Parser) parseTypedef(t sym.Type, dims []uint32, tag, name string) {

// ### [ Helper functions ] ####################################################

// Duplicate tag format string.
const duplicateTagFormat = "%s_duplicate_%d"

// uniqueTag returns a unique tag based on the given tag and set of present
// tags.
func uniqueTag(tag string, tags map[string]bool) string {
newTag := tag
for i := 0; tags[newTag]; i++ {
newTag = fmt.Sprintf(duplicateTagFormat, tag, i)
}
tags[newTag] = true
return newTag
}

// Duplicate enum member format string.
const duplicateEnumFormat = "%s_DUPLICATE_%d"

Expand Down

0 comments on commit 8685e8f

Please sign in to comment.