elliotchance / c2go

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

Can't increment a pointer in a struct #811

Closed andybalholm closed 5 years ago

andybalholm commented 5 years ago

This C code:

typedef struct {
    char *a;
} aStruct;

int main() {
    aStruct v;
    v.a = "Hello";
    ++v.a;
}

produces an error:

Error (UnaryOperator):  /Users/andy/desktop/test.c:8 : Cannot transpileToExpr. err = Cannot transpile UnaryOperator: err = Cannot transpileUnaryOperatorInc. err = unsupported type *ast.MemberExpr
elliotchance commented 5 years ago

Hmm, I'm not sure what that is off the top of my head but is definitely a bug.

andybalholm commented 5 years ago

The special handling for incrementing pointers in transpileUnaryOperatorInc is calling getSoleChildDeclRefExpr, which only works if the item being incremented is a regular variable. Anything more complicated returns an error about an unsupported type of AST node. So I suppose that function needs to be replaced with one that can handle more types.