Skip to content

Commit 2a14e68

Browse files
authored
Apply two upstream CL to mkwinsyscall (#278)
* Apply CL463216: write source to temp file if formatting fails This change writes the unformatted Go source code to a temp file if "format.Source" fails. Print the temp file path to the console to make it easy to find. The source code is what causes formatting errors, and it can be difficult to diagnose them without this context. CL link: https://go-review.googlesource.com/c/sys/+/463216 commit: 4112509618ee88519f899be20efc6882496b57c8 Signed-off-by: Hamza El-Saawy <[email protected]> * Apply CL463215: support "." and "-" in DLL name This change adds "." and "-" support for DLL filenames in "//sys". Supporting "." requires a change in how mkwinsyscall handles the "= <filename>.<function>" syntax. Instead of assuming that only one "." can appear in this string, now mkwinsyscall assumes that any additional "." belongs to the filename. Supporting "." also requires changing how Go identifiers are created for each DLL. This change also allows mkwinsyscall to support "-". When creating a Go identifier, "." and "-" in the DLL filename are replaced with "_". Otherwise, mkwinsyscall would produce invalid Go code, causing "format.Source" to fail. CL link: https://go-review.googlesource.com/c/sys/+/463215 commit: 71da6904945ac440253cb5c132d64712f80ca497 Signed-off-by: Hamza El-Saawy <[email protected]> --------- Signed-off-by: Hamza El-Saawy <[email protected]>
1 parent dd5de69 commit 2a14e68

1 file changed

Lines changed: 65 additions & 20 deletions

File tree

tools/mkwinsyscall/mkwinsyscall.go

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,14 @@ func newFn(s string) (*Fn, error) {
477477
return nil, errors.New("Could not extract dll name from \"" + f.src + "\"")
478478
}
479479
s = trim(s[1:])
480-
a := strings.Split(s, ".")
481-
switch len(a) {
482-
case 1:
483-
f.dllfuncname = a[0]
484-
case 2:
485-
f.dllname = a[0]
486-
f.dllfuncname = a[1]
487-
default:
488-
return nil, errors.New("Could not extract dll name from \"" + f.src + "\"")
480+
if i := strings.LastIndex(s, "."); i >= 0 {
481+
f.dllname = s[:i]
482+
f.dllfuncname = s[i+1:]
483+
} else {
484+
f.dllfuncname = s
485+
}
486+
if f.dllfuncname == "" {
487+
return nil, fmt.Errorf("function name is not specified in %q", s)
489488
}
490489
if n := f.dllfuncname; endsIn(n, '?') {
491490
f.dllfuncname = n[:len(n)-1]
@@ -502,7 +501,23 @@ func (f *Fn) DLLName() string {
502501
return f.dllname
503502
}
504503

505-
// DLLName returns DLL function name for function f.
504+
// DLLVar returns a valid Go identifier that represents DLLName.
505+
func (f *Fn) DLLVar() string {
506+
id := strings.Map(func(r rune) rune {
507+
switch r {
508+
case '.', '-':
509+
return '_'
510+
default:
511+
return r
512+
}
513+
}, f.DLLName())
514+
if !token.IsIdentifier(id) {
515+
panic(fmt.Errorf("could not create Go identifier for DLLName %q", f.DLLName()))
516+
}
517+
return id
518+
}
519+
520+
// DLLFuncName returns DLL function name for function f.
506521
func (f *Fn) DLLFuncName() string {
507522
if f.dllfuncname == "" {
508523
return f.Name
@@ -648,6 +663,13 @@ func (f *Fn) HelperName() string {
648663
return "_" + f.Name
649664
}
650665

666+
// DLL is a DLL's filename and a string that is valid in a Go identifier that should be used when
667+
// naming a variable that refers to the DLL.
668+
type DLL struct {
669+
Name string
670+
Var string
671+
}
672+
651673
// Source files and functions.
652674
type Source struct {
653675
Funcs []*Fn
@@ -697,18 +719,20 @@ func ParseFiles(fs []string) (*Source, error) {
697719
}
698720

699721
// DLLs return dll names for a source set src.
700-
func (src *Source) DLLs() []string {
722+
func (src *Source) DLLs() []DLL {
701723
uniq := make(map[string]bool)
702-
r := make([]string, 0)
724+
r := make([]DLL, 0)
703725
for _, f := range src.Funcs {
704-
name := f.DLLName()
705-
if _, found := uniq[name]; !found {
706-
uniq[name] = true
707-
r = append(r, name)
726+
id := f.DLLVar()
727+
if _, found := uniq[id]; !found {
728+
uniq[id] = true
729+
r = append(r, DLL{f.DLLName(), id})
708730
}
709731
}
710732
if *sortdecls {
711-
sort.Strings(r)
733+
sort.Slice(r, func(i, j int) bool {
734+
return r[i].Var < r[j].Var
735+
})
712736
}
713737
return r
714738
}
@@ -878,6 +902,22 @@ func (src *Source) Generate(w io.Writer) error {
878902
return nil
879903
}
880904

905+
func writeTempSourceFile(data []byte) (string, error) {
906+
f, err := os.CreateTemp("", "mkwinsyscall-generated-*.go")
907+
if err != nil {
908+
return "", err
909+
}
910+
_, err = f.Write(data)
911+
if closeErr := f.Close(); err == nil {
912+
err = closeErr
913+
}
914+
if err != nil {
915+
os.Remove(f.Name()) // best effort
916+
return "", err
917+
}
918+
return f.Name(), nil
919+
}
920+
881921
func usage() {
882922
fmt.Fprintf(os.Stderr, "usage: mkwinsyscall [flags] [path ...]\n")
883923
flag.PrintDefaults()
@@ -904,7 +944,12 @@ func main() {
904944

905945
data, err := format.Source(buf.Bytes())
906946
if err != nil {
907-
log.Fatal(err)
947+
log.Printf("failed to format source: %v", err)
948+
f, err := writeTempSourceFile(buf.Bytes())
949+
if err != nil {
950+
log.Fatalf("failed to write unformatted source to file: %v", err)
951+
}
952+
log.Fatalf("for diagnosis, wrote unformatted source to %v", f)
908953
}
909954
if *filename == "" {
910955
_, err = os.Stdout.Write(data)
@@ -970,10 +1015,10 @@ var (
9701015
9711016
{{/* help functions */}}
9721017
973-
{{define "dlls"}}{{range .DLLs}} mod{{.}} = {{newlazydll .}}
1018+
{{define "dlls"}}{{range .DLLs}} mod{{.Var}} = {{newlazydll .Name}}
9741019
{{end}}{{end}}
9751020
976-
{{define "funcnames"}}{{range .DLLFuncNames}} proc{{.DLLFuncName}} = mod{{.DLLName}}.NewProc("{{.DLLFuncName}}")
1021+
{{define "funcnames"}}{{range .DLLFuncNames}} proc{{.DLLFuncName}} = mod{{.DLLVar}}.NewProc("{{.DLLFuncName}}")
9771022
{{end}}{{end}}
9781023
9791024
{{define "helperbody"}}

0 commit comments

Comments
 (0)