Skip to content

Commit 90a5c8a

Browse files
committed
mkgodef: fix write function logic
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent 738b0ff commit 90a5c8a

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

cmd/mkgodef/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ func main() {
7070
os.Exit(fix(os.Stdin))
7171
}
7272

73-
os.Exit(run(flag.CommandLine, flag.Args()))
73+
os.Exit(run(flag.CommandLine))
7474
}

cmd/mkgodef/mkgodef.go

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func ConfigFromFlags(flags *flag.FlagSet) (config *Config, err error) {
123123
}, nil
124124
}
125125

126-
func run(flags *flag.FlagSet, _ []string) int {
126+
func run(flags *flag.FlagSet) int {
127127
config, err := ConfigFromFlags(flags)
128128
if err != nil {
129129
fmt.Fprintf(os.Stderr, "parse configs: %v\n", err)
@@ -152,11 +152,11 @@ func run(flags *flag.FlagSet, _ []string) int {
152152
clang.TranslationUnit_ForSerialization |
153153
clang.TranslationUnit_CXXChainedPCH |
154154
clang.TranslationUnit_CreatePreambleOnFirstParse |
155-
clang.TranslationUnit_KeepGoing,
155+
clang.TranslationUnit_KeepGoing |
156+
clang.TranslationUnit_IncludeAttributedTypes,
156157
)
157158

158-
var sb strings.Builder
159-
funcMap := make(map[clang.Cursor][]string)
159+
funcMap := make(map[string]clang.Cursor)
160160
typeMap := make(map[clang.CursorKind][]clang.Cursor)
161161
enumMap := make(map[clang.Cursor][]clang.Cursor)
162162

@@ -166,8 +166,8 @@ func run(flags *flag.FlagSet, _ []string) int {
166166
tu := idx.ParseTranslationUnit(header, config.Args, nil, clangFlags)
167167
defer tu.Dispose()
168168

169-
var visit func(cursor, parent clang.Cursor) clang.ChildVisitResult
170-
visit = func(cursor, parent clang.Cursor) clang.ChildVisitResult {
169+
cursor := tu.TranslationUnitCursor()
170+
cursor.Visit(func(cursor, parent clang.Cursor) clang.ChildVisitResult {
171171
if cursor.IsNull() {
172172
return clang.ChildVisit_Continue
173173
}
@@ -181,22 +181,7 @@ func run(flags *flag.FlagSet, _ []string) int {
181181
var skip bool
182182
switch kind := cursor.Kind(); kind {
183183
case clang.Cursor_FunctionDecl: // function
184-
p(&sb, "//sys func %s(", upperCamelCase(cursor.Spelling()))
185-
186-
numArgs := cursor.NumArguments()
187-
for i := int32(0); i < numArgs; i++ {
188-
argName := strings.TrimSpace(lowerCamelCase(cursor.Argument(uint32(i)).DisplayName()))
189-
argType := convertGoType(cursor.Argument(uint32(i)).Type().CanonicalType().Spelling())
190-
191-
p(&sb, "%s %s", argName, argType)
192-
if i+1 < numArgs {
193-
p(&sb, ", ")
194-
}
195-
}
196-
p(&sb, ") %s\n", convertGoType(cursor.ResultType().Spelling()))
197-
198-
funcMap[cursor] = append(funcMap[cursor], sb.String())
199-
sb.Reset()
184+
funcMap[cursor.Spelling()] = cursor
200185

201186
return clang.ChildVisit_Recurse
202187

@@ -265,10 +250,7 @@ func run(flags *flag.FlagSet, _ []string) int {
265250

266251
return clang.ChildVisit_Continue
267252
}
268-
}
269-
270-
cursor := tu.TranslationUnitCursor()
271-
cursor.Visit(visit)
253+
})
272254
}
273255

274256
var buf bytes.Buffer
@@ -471,29 +453,42 @@ func run(flags *flag.FlagSet, _ []string) int {
471453

472454
if mode&FuncMode != 0 {
473455
// sort funcMap by DisplayName
474-
cursors := make([]clang.Cursor, len(funcMap))
456+
fns := make([]string, len(funcMap))
475457
i := 0
476-
for cursor := range funcMap {
477-
cursors[i] = cursor
458+
for fn := range funcMap {
459+
fns[i] = fn
478460
i++
479461
}
480-
sort.Slice(cursors, func(i, j int) bool { return cursors[i].DisplayName() < cursors[j].DisplayName() })
462+
sort.Strings(fns)
481463

464+
var sb strings.Builder
482465
seenFn := make(map[string]bool)
483-
for _, cursor := range cursors {
484-
ss := funcMap[cursor]
466+
for _, fn := range fns {
467+
cursor := funcMap[fn]
468+
if seenFn[fn] {
469+
log.V(1).Info("ignore", "s", fn, "kind", cursor.Kind())
470+
continue
471+
}
472+
seenFn[fn] = true
485473

486474
switch cursor.Kind() {
487475
case clang.Cursor_FunctionDecl:
488-
for _, s := range ss {
489-
if seenFn[s] {
490-
log.V(1).Info("ignore", "s", s, "kind", cursor.Kind())
491-
continue
492-
}
493-
seenFn[s] = true
476+
p(&sb, "//sys func %s(", upperCamelCase(cursor.Spelling()))
494477

495-
bio.WriteString(s)
478+
numArgs := cursor.NumArguments()
479+
for i := int32(0); i < numArgs; i++ {
480+
argName := strings.TrimSpace(lowerCamelCase(cursor.Argument(uint32(i)).DisplayName()))
481+
argType := convertGoType(cursor.Argument(uint32(i)).Type().CanonicalType().Spelling())
482+
483+
p(&sb, "%s %s", argName, argType)
484+
if i+1 < numArgs {
485+
p(&sb, ", ")
486+
}
496487
}
488+
p(&sb, ") %s\n", convertGoType(cursor.ResultType().Spelling()))
489+
490+
bio.WriteString(sb.String())
491+
sb.Reset()
497492
}
498493
}
499494
}

0 commit comments

Comments
 (0)