Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall: mksyscall_windows.go generate unused unsafe package #9900

Closed
mattn opened this issue Feb 17, 2015 · 2 comments
Closed

syscall: mksyscall_windows.go generate unused unsafe package #9900

mattn opened this issue Feb 17, 2015 · 2 comments

Comments

@mattn
Copy link
Member

@mattn mattn commented Feb 17, 2015

package main

//sys GetForegroundWindow() (hwnd syscall.Handle) = user32.GetForegroundWindow

func main() {
}
C:\temp>go run c:/go/src/syscall/mksyscall_windows.go -output bar.go foo.go
C:\temp>go build
.\bar.go:5: imported and not used: "unsafe"

Since mksyscall_windows.go uses template, we should handle whether parameters has pointer before execute template.
Most easy ways to fix this:

diff --git a/src/syscall/mksyscall_windows.go b/src/syscall/mksyscall_windows.go
index cb540d3..3e29901 100644
--- a/src/syscall/mksyscall_windows.go
+++ b/src/syscall/mksyscall_windows.go
@@ -65,6 +65,7 @@ import (
 var (
    filename       = flag.String("output", "", "output file name (standard output if omitted)")
    printTraceFlag = flag.Bool("trace", false, "generate print statement after every syscall")
+   hasUnsafe      = false
 )

 func trim(s string) string {
@@ -171,10 +172,12 @@ func (p *Param) SyscallArgList() []string {
    var s string
    switch {
    case t[0] == '*':
+       hasUnsafe = true
        s = fmt.Sprintf("unsafe.Pointer(%s)", p.Name)
    case t == "bool":
        s = p.tmpVar()
    case strings.HasPrefix(t, "[]"):
+       hasUnsafe = true
        return []string{
            fmt.Sprintf("uintptr(unsafe.Pointer(%s))", p.tmpVar()),
            fmt.Sprintf("uintptr(len(%s))", p.Name),
@@ -306,6 +309,7 @@ func (r *Rets) SetErrorCode() string {
    s := ""
    switch {
    case r.Type[0] == '*':
+       hasUnsafe = true
        s = fmt.Sprintf("%s = (%s)(unsafe.Pointer(r0))", r.Name, r.Type)
    case r.Type == "bool":
        s = fmt.Sprintf("%s = r0 != 0", r.Name)
@@ -716,6 +720,15 @@ func main() {
    if err != nil {
        log.Fatal(err)
    }
+
+   // TODO: When there is pointer, unsafe package is needless.
+   if !hasUnsafe {
+       s := string(data)
+       if pos := strings.Index(s, "import \"unsafe\"\n"); pos >= 0 {
+           data = []byte(s[:pos] + s[pos+16:])
+       }
+   }
+
    if *filename == "" {
        _, err = os.Stdout.Write(data)
    } else {
@mattn
Copy link
Member Author

@mattn mattn commented Feb 17, 2015

Or put dummy function which use unsafe.

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Feb 17, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.