elliotchance / c2go

⚖️ A tool for transpiling C to Go.
MIT License
2.09k stars 155 forks source link

++p translated weirdly #646

Open kjk opened 6 years ago

kjk commented 6 years ago

This example is extracted and simplified from https://github.com/commonmark/cmark/blob/master/src/scanners.c

int _scan_scheme(const unsigned char *p) {
  {
    unsigned char yych;
    yych = *p;
    if (yych <= '@')
      goto yy2;
    if (yych <= 'Z')
      goto yy3;

    yy2:
      ++p;
    yy3 : { return 0; }
    return 3;
  }
}

Is translated to:

// _scan_scheme - transpiled function from  /Users/kjk/src/cmark/test.c:1
func _scan_scheme(p []uint8) (c2goDefaultReturn int) {
    {
        var yych uint8
        yych = p[0]
        if int(yych) <= '@' {
            goto yy2
        }
        if int(yych) <= 'Z' {
            goto yy3
        }
    yy2:
        ;
        p = (*(*[1000000000]uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&p[0])) + (uintptr)(1)*unsafe.Sizeof(p[0]))))[:]
    yy3:
        ;
        {
            return 0
        }
        return 3
    }
    return
}
func init() {
}

++p; was translated to: p = (*(*[1000000000]uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(&p[0])) + (uintptr)(1)*unsafe.Sizeof(p[0]))))[:]

It seems like the right translation should be p = p[1:].

Konstantin8105 commented 6 years ago

Dear @kjk ++p is pointer ariphmetic in C, but in Go we haven't simple access to pointer ariphmetic(we have to use package unsafe), but it is look ugly. Thank you for your solution. Feel free for implemented in c2go.