Skip to content

Commit

Permalink
cxgo: do not flatten blocks with decls; fixes #42
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jan 7, 2022
1 parent 031dc58 commit 5359b0e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
12 changes: 11 additions & 1 deletion c_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,21 @@ func (g *translator) cBlockCast(s CStmt) *BlockStmt {
return g.newBlockStmt(s)
}

func stmtHasDecl(stmts ...CStmt) bool {
for _, st := range stmts {
switch st.(type) {
case *CDeclStmt:
return true
}
}
return false
}

func flattenBlocks(stmts []CStmt) ([]CStmt, bool) {
mod := false
for i := 0; i < len(stmts); i++ {
s, ok := stmts[i].(*BlockStmt)
if !ok {
if !ok || stmtHasDecl(s.Stmts...) {
continue
}
arr := append([]CStmt{}, stmts[:i]...)
Expand Down
67 changes: 67 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,73 @@ func foo(a *int32) int32 {
foo(a)
return 1
}
`,
},
{
name: "blocks with vars",
src: `
#define set(s) \
{ int t = s;\
}
void main() {
int s;
if (0) {
set(s)
set(s)
set(s)
}
}
`,
exp: `
func main() {
var s int32
if false {
{
var t int32 = s
_ = t
}
{
var t int32 = s
_ = t
}
{
var t int32 = s
_ = t
}
}
}
`,
},
{
name: "blocks no vars",
src: `
#define set(s) \
{ t = s;\
}
void main() {
int s, t;
if (0) {
set(s)
set(s)
set(s)
}
}
`,
exp: `
func main() {
var (
s int32
t int32
)
_ = t
if false {
t = s
t = s
t = s
}
}
`,
},
{
Expand Down

0 comments on commit 5359b0e

Please sign in to comment.