From b3de605d50bfcbeedd6df70931594522f9738fdc Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 8 Aug 2018 21:31:12 -0400 Subject: [PATCH] Started template support, writing the initial C++ template. --- windows/tools/a | 11 --------- windows/tools/cpp.template | 29 +++++++++++++++++++++++ windows/tools/gen.sh | 2 +- windows/tools/hresultwrap.go | 46 +++++++++++++++++++++++++++++++++--- 4 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 windows/tools/cpp.template diff --git a/windows/tools/a b/windows/tools/a index 7f6648df..5be22d75 100644 --- a/windows/tools/a +++ b/windows/tools/a @@ -1,14 +1,3 @@ -static HRESULT lastErrorToHRESULT(DWORD lasterr, const char *funcname) -{ - HRESULT hr; - - hr = E_FAIL; - if (lasterr != 0) - hr = HRESULT_FROM_WIN32(lasterr); - uiprivImplBug("error calling %s: last error %I32d\n", funcname, lasterr); - return hr; -} - @BOOL GetWindowPlacement HWND hwnd uiprivLPWWINDOWPLACEMENT wp == 0 $typedef const WINDOWPLACEMENT *uiprivLPCWINDOWPLACEMENT; @BOOL SetWindowPlacement HWND hwnd uiprivLPCWINDOWPLACEMENT wp == 0 diff --git a/windows/tools/cpp.template b/windows/tools/cpp.template new file mode 100644 index 00000000..ced37e58 --- /dev/null +++ b/windows/tools/cpp.template @@ -0,0 +1,29 @@ +{{/* 8 august 2018 */}}// this file is generated by tools/hresultwrap and should NOT be modified directly +#include "uipriv_windows.hpp" + +static inline HRESULT lastErrorToHRESULT(DWORD lasterr, const char *funcname) +{ + HRESULT hr; + + hr = E_FAIL; + if (lasterr != 0) + hr = HRESULT_FROM_WIN32(lasterr); + uiprivImplBug("error calling %s: last error %I32d\n", funcname, lasterr); + return hr; +}{{range .}} + +{{$narg := len .Arg}}HRESULT {{if .CallingConvention}}{{.CallingConvention}}{{else}}WINAPI{{end}} uiprivHR{{.Name}}({{range $i, $a := .Arg}}{{$a}}{{argcomma $i $narg}}{{end}}{{if .Save}}, _Out_ {{.Ret}} *outRet{{end}}) +{ + {{.Ret}} xyzret; + DWORD xyzlasterr; + +{{if .Save}} if (outRet == NULL) + return E_POINTER; +{{end}} SetLastError(0); + xyzret = {{.Name}}({{range $i, $a := .Arg}}{{argname $a}}{{argcomma $i $narg}}{{end}}); + xyzlasterr = GetLastError(); +{{if .Save}} *outRet = xyzret; +{{end}} if (xyzret != {{.Failval}}) + return S_OK; + return lastErrToHRESULT(xyzlasterr, "{{.Name}}()"); +}{{end}} diff --git a/windows/tools/gen.sh b/windows/tools/gen.sh index 7f39837f..168df8fa 100755 --- a/windows/tools/gen.sh +++ b/windows/tools/gen.sh @@ -1 +1 @@ -./hresultwrap funclist.textpb +./hresultwrap funclist.textpb cpp.template diff --git a/windows/tools/hresultwrap.go b/windows/tools/hresultwrap.go index b806cd9f..edf9e6b2 100644 --- a/windows/tools/hresultwrap.go +++ b/windows/tools/hresultwrap.go @@ -6,13 +6,43 @@ import ( "fmt" "os" "io/ioutil" + "text/template" + "strings" "github.com/golang/protobuf/proto" ) +func argname(arg string) string { + fields := strings.Fields(arg) + last := fields[len(fields) - 1] + start := strings.LastIndexFunc(last, func(r rune) bool { + return !(r >= 'A' && r <= 'Z') && + !(r >= 'a' && r <= 'z') && + !(r >= '0' && r <= '9') && + r != '_' + }) + if start == -1 { + return last + } + // TODO replace + 1 with + len of that last rune + return last[start + 1:] +} + +func argcomma(n, len int) string { + if n == len - 1 { + return "" + } + return ", " +} + +var templateFuncs = template.FuncMap{ + "argname": argname, + "argcomma": argcomma, +} + func main() { - if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "usage: %s funclist\n", os.Args[0]) + if len(os.Args) != 3 { + fmt.Fprintf(os.Stderr, "usage: %s funclist template\n", os.Args[0]) os.Exit(1) } b, err := ioutil.ReadFile(os.Args[1]) @@ -26,5 +56,15 @@ func main() { fmt.Fprintf(os.Stderr, "error parsing %s: %v\n", os.Args[1], err) os.Exit(1) } - fmt.Println(f) + + tmpl, err := template.New(os.Args[2]).Funcs(templateFuncs).ParseFiles(os.Args[2]) + if err != nil { + fmt.Fprintf(os.Stderr, "error parsing %s: %v\n", os.Args[2], err) + os.Exit(1) + } + err = tmpl.Execute(os.Stdout, f.Func) + if err != nil { + fmt.Fprintf(os.Stderr, "error executing template: %v\n", err) + os.Exit(1) + } }