elliotchance / c2go

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

very simple highlighter.c does not transpile #817

Open zx2c4 opened 5 years ago

zx2c4 commented 5 years ago

See these two files, which are mostly pure and simple C operating on string slices: https://git.zx2c4.com/WireGuard/plain/contrib/examples/highlighter/highlighter.c https://git.zx2c4.com/WireGuard/plain/contrib/examples/highlighter/highlighter.h

c2go becomes upset:

$ ~/.go/bin/c2go transpile highlighter.c 
panic: could not match regexp with string
^(?P<address>[0-9a-fx]+) <(?P<position>.*)> '(?P<type>.*?)'(:'(?P<type2>.*)')?(?P<lvalue> lvalue)?(?P<prefix> prefix)?(?P<postfix> postfix)? '(?P<operator>.*?)'[\s]*$
0x556f22e8c470 <col:28, col:29> 'struct highlight_span_array *' prefix '&' cannot overflow

goroutine 60 [running]:
github.com/elliotchance/c2go/ast.groupsFromRegex(0xc00026ec60, 0xa6, 0xc0003d7cd2, 0x5a, 0x6981f8)
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/ast/ast.go:286 +0x379
github.com/elliotchance/c2go/ast.parseUnaryOperator(0xc0003d7cd2, 0x5a, 0x69dd5a)
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/ast/unary_operator.go:16 +0x62
github.com/elliotchance/c2go/ast.Parse(0xc0003d7cc4, 0x68, 0x699766, 0x5)
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/ast/ast.go:246 +0x2350
main.convertLinesToNodes(0xc00031ed70, 0x186, 0x186, 0x0, 0x0, 0x0)
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/main.go:89 +0x1d8
main.convertLinesToNodesParallel.func1.2(0xc0001ca300, 0xc00019c000, 0xc00031ed70, 0x186, 0x186, 0x0)
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/main.go:121 +0x59
created by main.convertLinesToNodesParallel.func1
        /home/zx2c4/.go/src/github.com/elliotchance/c2go/main.go:119 +0x1a6
elliotchance commented 5 years ago

You will need to modify the regex that handles the parsing of a unary operator: https://github.com/elliotchance/c2go/blob/master/ast/unary_operator.go#L15-L36

You must use your panic as a test case, like this: https://github.com/elliotchance/c2go/blob/master/ast/unary_operator_test.go#L39-L48

The panic is caused by the fact that the clang AST is interpreted as text. So every so often we come across a formatted AST node that doesn't fit the regex.

Konstantin8105 commented 5 years ago

Please try https://github.com/Konstantin8105/c4go