Codebase list golang-golang-x-sys / 9a0d5db
windows/mkwinsyscall: account for non-"err" return values when processing "?" The "?" code assumed that the error value was always called "err", when in reality it might be called something different (like "ret") or even entirely absent. This commit makes the templating robust to that. At the same time, we move a lot of the complexity out of the actual templates and into helper functions, so that this remains easy to read. Change-Id: I939d56413a24f0e3e1bbf13da5adf13e9401747a Reviewed-on: https://go-review.googlesource.com/c/sys/+/275472 Trust: Jason A. Donenfeld <Jason@zx2c4.com> Trust: Alex Brainman <alex.brainman@gmail.com> Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Jason A. Donenfeld 3 years ago
1 changed file(s) with 25 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
238238
239239 // Rets describes function return parameters.
240240 type Rets struct {
241 Name string
242 Type string
243 ReturnsError bool
244 FailCond string
241 Name string
242 Type string
243 ReturnsError bool
244 FailCond string
245 fnMaybeAbsent bool
245246 }
246247
247248 // ErrorVarName returns error variable name for r.
272273 s := join(r.ToParams(), func(p *Param) string { return p.Name + " " + p.Type }, ", ")
273274 if len(s) > 0 {
274275 s = "(" + s + ")"
276 } else if r.fnMaybeAbsent {
277 s = "(err error)"
275278 }
276279 return s
277280 }
344347 Params []*Param
345348 Rets *Rets
346349 PrintTrace bool
347 MaybeAbsent bool
348350 dllname string
349351 dllfuncname string
350352 src string
474476 }
475477 if n := f.dllfuncname; strings.HasSuffix(n, "?") {
476478 f.dllfuncname = n[:len(n)-1]
477 f.MaybeAbsent = true
479 f.Rets.fnMaybeAbsent = true
478480 }
479481 return f, nil
480482 }
574576 return strings.Join(a, ", ")
575577 }
576578
579 // MaybeAbsent returns source code for handling functions that are possibly unavailable.
580 func (p *Fn) MaybeAbsent() string {
581 if !p.Rets.fnMaybeAbsent {
582 return ""
583 }
584 const code = `%[1]s = proc%[2]s.Find()
585 if %[1]s != nil {
586 return
587 }`
588 errorVar := p.Rets.ErrorVarName()
589 if errorVar == "" {
590 errorVar = "err"
591 }
592 return fmt.Sprintf(code, errorVar, p.DLLFuncName())
593 }
594
577595 // IsUTF16 is true, if f is W (utf16) function. It is false
578596 // for all A (ascii) functions.
579597 func (f *Fn) IsUTF16() bool {
914932 {{define "helpertmpvars"}}{{range .Params}}{{if .TmpVarHelperCode}} {{.TmpVarHelperCode}}
915933 {{end}}{{end}}{{end}}
916934
917 {{define "maybeabsent"}}{{if .MaybeAbsent}}err = proc{{.DLLFuncName}}.Find()
918 if err != nil { return }
935 {{define "maybeabsent"}}{{if .MaybeAbsent}}{{.MaybeAbsent}}
919936 {{end}}{{end}}
920937
921938 {{define "tmpvars"}}{{range .Params}}{{if .TmpVarCode}} {{.TmpVarCode}}