Skip to content

Commit 1e299b1

Browse files
committed
mkgodef: implements rawfunc mode
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent 90a5c8a commit 1e299b1

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

cmd/mkgodef/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var log logr.Logger
4545

4646
func main() {
4747
flag.StringVar(&flagPackage, fnamePackage, "", "package name of godef")
48-
flag.StringSliceVar(&flagMode, fnameMode, []string{"enum, func, type"}, "generate mode")
48+
flag.StringSliceVar(&flagMode, fnameMode, []string{"enum, func, type, rawfunc"}, "generate mode")
4949
flag.StringSliceVar(&flagHeaders, fnameHeader, nil, "C header to analyze")
5050
flag.StringSliceVar(&flagArgs, fnameArg, nil, "arg for analyze")
5151
flag.StringSliceVar(&flagSources, fnameSource, nil, "additional C source to analyze")

cmd/mkgodef/mkgodef.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
EnumMode = 1 << iota
4545
TypeMode
4646
FuncMode
47+
RawFuncMode
4748
)
4849

4950
// Config represents a mkgodef config.
@@ -280,6 +281,8 @@ func run(flags *flag.FlagSet) int {
280281
mode |= TypeMode
281282
case "func":
282283
mode |= FuncMode
284+
case "rawfunc":
285+
mode |= RawFuncMode
283286
}
284287
}
285288

@@ -493,6 +496,48 @@ func run(flags *flag.FlagSet) int {
493496
}
494497
}
495498

499+
if mode&RawFuncMode != 0 {
500+
// sort funcMap by DisplayName
501+
fns := make([]string, len(funcMap))
502+
i := 0
503+
for fn := range funcMap {
504+
fns[i] = fn
505+
i++
506+
}
507+
sort.Strings(fns)
508+
509+
var sb strings.Builder
510+
seenFn := make(map[string]bool)
511+
for _, fn := range fns {
512+
cursor := funcMap[fn]
513+
if seenFn[fn] {
514+
log.V(1).Info("ignore", "s", fn, "kind", cursor.Kind())
515+
continue
516+
}
517+
seenFn[fn] = true
518+
519+
switch cursor.Kind() {
520+
case clang.Cursor_FunctionDecl:
521+
p(&sb, "//sys func %s(", cursor.Spelling())
522+
523+
numArgs := cursor.NumArguments()
524+
for i := int32(0); i < numArgs; i++ {
525+
argName := cursor.Argument(uint32(i)).DisplayName()
526+
argType := cursor.Argument(uint32(i)).Type().CanonicalType().Spelling()
527+
528+
p(&sb, "%s %s", argName, argType)
529+
if i+1 < numArgs {
530+
p(&sb, ", ")
531+
}
532+
}
533+
p(&sb, ") %s\n", cursor.ResultType().Spelling())
534+
535+
bio.WriteString(sb.String())
536+
sb.Reset()
537+
}
538+
}
539+
}
540+
496541
bio.Flush()
497542
bio2.Flush()
498543

0 commit comments

Comments
 (0)