WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e094974

Browse files
committed
cxgo: Fix side effects for assign expr. Fixes #77.
1 parent 4a6556d commit e094974

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

c_expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ func (e *CAssignExpr) AsExpr() GoExpr {
11931193
define(pg, x.AsExpr()),
11941194
)
11951195
stmts = append(stmts,
1196-
e.Stmt.g.NewCAssignStmtP(e.Stmt.g.cDeref(x), e.Stmt.Op, y).AsStmt()...,
1196+
e.Stmt.g.NewCAssignStmtP(e.Stmt.g.cDeref(PtrIdent{p}), e.Stmt.Op, y).AsStmt()...,
11971197
)
11981198
stmts = append(stmts,
11991199
returnStmt(deref(pg)),

compile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func goProject(t testing.TB, out, cxgo string) {
105105
require.NoError(t, err)
106106

107107
gomod := fmt.Sprintf(`module main
108-
go 1.18
108+
go 1.19
109109
require (
110110
github.com/gotranspile/cxgo v0.0.0-local
111111
)

parse_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,39 @@ func foo() {
688688
return a
689689
}()
690690
}
691+
`,
692+
},
693+
{
694+
name: "assign expr side effect",
695+
src: `
696+
697+
int global = 0;
698+
int* calc(int v) {
699+
global += v;
700+
return &global;
701+
}
702+
703+
void foo() {
704+
int a;
705+
a = *calc(1) += *calc(2);
706+
}
707+
`,
708+
exp: `
709+
var global int32 = 0
710+
711+
func calc(v int32) *int32 {
712+
global += v
713+
return &global
714+
}
715+
func foo() {
716+
var a int32
717+
_ = a
718+
a = func() int32 {
719+
p := calc(1)
720+
*p += *calc(2)
721+
return *p
722+
}()
723+
}
691724
`,
692725
},
693726
{

run_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cxgo
2+
3+
import "testing"
4+
5+
var casesRun = []struct {
6+
name string
7+
src string
8+
}{
9+
{
10+
name: "issue 77",
11+
src: `
12+
#include <stdio.h>
13+
14+
int globe = 35;
15+
16+
17+
int* fn1(int strd) {
18+
static int result = 0;
19+
result = globe;
20+
if (strd > 2) {
21+
result += 4;
22+
}
23+
result += strd;
24+
return &result;
25+
}
26+
27+
int main() {
28+
char q_q = 34;
29+
int p_p = 110;
30+
p_p += q_q ^ p_p || globe;
31+
q_q = globe ^ p_p ^ 25;
32+
printf("%d\n", *fn1(q_q));
33+
printf("%d\n", *fn1(p_p));
34+
int t1 = *fn1(q_q);
35+
int t2 = *fn1(p_p);
36+
int final=t1 += t2;
37+
printf("Result: %d\n", final );
38+
return 0;
39+
}
40+
`,
41+
},
42+
}
43+
44+
func TestRunTranslated(t *testing.T) {
45+
for _, c := range casesRun {
46+
c := c
47+
t.Run(c.name, func(t *testing.T) {
48+
testTranspileOut(t, c.src)
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)