gotranspile / cxgo

Tool for transpiling C to Go.
https://gotranspile.dev
MIT License
337 stars 22 forks source link

block statement inside if gets removed #42

Closed TotallyGamerJet closed 2 years ago

TotallyGamerJet commented 2 years ago

Using a define that contains a block scope inside of an if statement removes the brackets. Given:

#define send_bits(s) \
{ int t = s;\
}

void main() {
  int s;
  if (0) {
    send_bits(s)
    send_bits(s)
    send_bits(s)
  }
}

The generated code:

package main

func main() {
    var s int
    if false {
        var t int = s
        _ = t
        var t int = s
        _ = t
        var t int = s
        _ = t
    }
}

I would expect there to be an inner bracket for each time the define is used. This might be an issue with the preprocessor but I didn't know if CXGO does some "optimization" that would remove these brackets unintentionally. If there isn't an if statement the brackets are generated.

dennwc commented 2 years ago

Yes, it's an optimization that cxgo does. It should only do that if there's no declarations in that scope.

TotallyGamerJet commented 2 years ago

So in testing some other cases I have come to the conclusion that it has nothing to do with the define. Just putting the blocks directly inside the if statement gets them removed. It also doesn't matter if there are many or few lines in the block it always gets removed while inside an if statement. Do you know where this optimization code is so I can look for the bug?