elliotchance / c2go

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

i met a panic when parse c code #794

Open carolove opened 6 years ago

carolove commented 6 years ago

when i build c code at my macbook, the code is fine, while when i execute c2go transpile main.c the c2go panic,here is the log

panic: could not match regexp with string
^(?P<address>[0-9a-fx]+) (?:prev (?P<prev>0x[0-9a-f]+) )?(?:parent (?P<parent>0x[0-9a-f]+) )?<(?P<position>.*)>(?P<position2> .+:\d+)?(?P<used>used)?(?P<referenced> referenced)?(?P<name> \w+)? '(?P<type>.+?)'(?P<type2>:'.*?')?(?P<extern> extern)?(?P<static> static)?(?P<cinit> cinit)?(?P<register> register)?[\s]*$
0x7f9d279ba938 <col:13, col:39> col:35 used c 'unsigned int' register cinit

goroutine 35 [running]:
github.com/elliotchance/c2go/ast.groupsFromRegex(0xc0009dd040, 0x13b, 0xc0005e09c0, 0x4b, 0xc0005e0990)
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/ast/ast.go:286 +0x379
github.com/elliotchance/c2go/ast.parseVarDecl(0xc0005e09c0, 0x4b, 0x1295391)
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/ast/var_decl.go:26 +0x65
github.com/elliotchance/c2go/ast.Parse(0xc0005e09b8, 0x53, 0x1294334, 0x5)
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/ast/ast.go:252 +0x46c
main.convertLinesToNodes(0xc0015d1cb0, 0x29cb, 0x7d61, 0x0, 0x0, 0x0)
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/main.go:89 +0x1cf
main.convertLinesToNodesParallel.func1.2(0xc00010e060, 0xc000080018, 0xc0015d1cb0, 0x29cb, 0x7d61, 0x0)
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/main.go:121 +0x59
created by main.convertLinesToNodesParallel.func1
        /Users/chendan/project/gopath/src/github.com/elliotchance/c2go/main.go:119 +0x19d
elliotchance commented 6 years ago

This is because c2go does not understand one of the output lines from the clang AST. It can be fixed by adding a test case to var_decl_test.go, just like this: https://github.com/elliotchance/c2go/blob/master/ast/var_decl_test.go#L137-L152

The test should fail, then you can adjust the regexp here to satisfy the test: https://github.com/elliotchance/c2go/blob/master/ast/var_decl.go#L26-L41