elliotchance / c2go

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

transpile: add support for FullComment AST node with VarDecl parent node #851

Open mewmew opened 4 years ago

mewmew commented 4 years ago

Currently, the FullComment AST node is correct handled if the parent node is FunctionDecl. However, if the parent node of the FullComment node is VarDecl, the comment is dropped during transpilation and a warning comment is emitted.

Example input C source code

not working Example where comment is dropped (not transpiled).

/**
 * foo
 */
int x;

working Example where comment is kept (transpiled).

/**
 * foo
 */
int f(int x) {
   return x;
}

AST

$ c2go ast n.c
TranslationUnitDecl 0x55a284b2a9f8 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55a284b2b290 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55a284b2af90 '__int128'
|-TypedefDecl 0x55a284b2b300 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55a284b2afb0 'unsigned __int128'
|-TypedefDecl 0x55a284b2b5e8 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x55a284b2b3e0 'struct __NSConstantString_tag'
|   `-Record 0x55a284b2b358 '__NSConstantString_tag'
|-TypedefDecl 0x55a284b2b680 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55a284b2b640 'char *'
|   `-BuiltinType 0x55a284b2aa90 'char'
|-TypedefDecl 0x55a284b2b958 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x55a284b2b900 'struct __va_list_tag [1]' 1 
|   `-RecordType 0x55a284b2b760 'struct __va_list_tag'
|     `-Record 0x55a284b2b6d8 '__va_list_tag'
`-VarDecl 0x55a284b896e0 </home/u/Desktop/ccc/n/n.c:4:1, col:5> col:5 x 'int'
  `-FullComment 0x55a284b89830 <line:2:3, col:6>
    `-ParagraphComment 0x55a284b89800 <col:3, col:6>
      `-TextComment 0x55a284b897d0 <col:3, col:6> Text=" foo"

Example output Go source code

/*
    Package main - transpiled by c2go version: v0.25.9 Dubnium 2018-12-30

    If you have found any issues, please raise an issue at:
    https://github.com/elliotchance/c2go/
*/

// Warning (FullComment):  /home/u/Desktop/ccc/n/n.c:2 : cannot transpile to expr
// Warning (VarDecl):  /home/u/Desktop/ccc/n/n.c:4 : Cannot casting { -> int}. err = Cannot resolve type '' : probably an incorrect type translation 1

package main

var // Warning (FullComment):  /home/u/Desktop/ccc/n/n.c:2 : cannot transpile to expr
// Warning (VarDecl):  /home/u/Desktop/ccc/n/n.c:4 : Cannot casting { -> int}. err = Cannot resolve type '' : probably an incorrect type translation 1
x int32

func init() {
}

In particular, note the // Warning (FullComment): /home/u/Desktop/ccc/n/n.c:2 : cannot transpile to expr comment.

Additional information

For reference, this is what the output Go source code looks like for a FullComment AST node with a FunctionDecl parent node:

/*
    Package main - transpiled by c2go version: v0.25.9 Dubnium 2018-12-30

    If you have found any issues, please raise an issue at:
    https://github.com/elliotchance/c2go/
*/

package main

// f - transpiled function from  /home/u/Desktop/ccc/n/n.c:4
/**
 * foo
 */ //
//
func f(x int32) int32 {
    return x
}
func init() {
}