Repository: Konstantin8105/c4go
Pull request #355 (2019-03-24 13:11:02)

Scripts: sqlite: fix problems in transpilation

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=h1) Report
> Merging [#355](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/b24cd234231a7abfd0d6ae5462c0a44f8b40043d?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `71.42%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/355/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #355      +/-   ##
==========================================
- Coverage   86.33%   86.32%   -0.02%     
==========================================
  Files         186      186              
  Lines       10682    10688       +6     
==========================================
+ Hits         9222     9226       +4     
- Misses       1043     1044       +1     
- Partials      417      418       +1
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [util/util.go](https://codecov.io/gh/Konstantin8105/c4go/pull/355/diff?src=pr&el=tree#diff-dXRpbC91dGlsLmdv) | `83.92% <100%> (+0.21%)` | :arrow_up: |
| [types/resolve.go](https://codecov.io/gh/Konstantin8105/c4go/pull/355/diff?src=pr&el=tree#diff-dHlwZXMvcmVzb2x2ZS5nbw==) | `76.32% <100%> (ø)` | :arrow_up: |
| [transpiler/transpiler.go](https://codecov.io/gh/Konstantin8105/c4go/pull/355/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci90cmFuc3BpbGVyLmdv) | `86.35% <33.33%> (-0.4%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=footer). Last update [b24cd23...055a835](https://codecov.io/gh/Konstantin8105/c4go/pull/355?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/transpiler/cast.go b/transpiler/cast.go
index 6397cef..0f63315 100644
--- a/transpiler/cast.go
+++ b/transpiler/cast.go
@@ -221,8 +221,8 @@ func transpileCStyleCastExpr(n *ast.CStyleCastExpr, p *program.Program, exprIsSt
 		return
 	}
 
-	expr, exprType, preStmts, postStmts, err = transpileToExpr(
-		n.Children()[0], p, exprIsStmt)
+	expr, exprType, preStmts, postStmts, err = atomicOperation(
+		n.Children()[0], p)
 	if err != nil {
 		return nil, "", nil, nil, err
 	}
diff --git a/transpiler/functions.go b/transpiler/functions.go
index d6fc35f..f5b55e9 100644
--- a/transpiler/functions.go
+++ b/transpiler/functions.go
@@ -290,7 +290,7 @@ func getFieldList(p *program.Program, f *ast.FunctionDecl, fieldTypes []string)
 			if len(t) > 0 {
 				r = append(r, &goast.Field{
 					Names: []*goast.Ident{util.NewIdent(v.Name)},
-					Type:  util.NewTypeIdent(t),
+					Type:  goast.NewIdent(t),
 				})
 			}
 		}
diff --git a/transpiler/operators.go b/transpiler/operators.go
index f71b2c9..d261323 100644
--- a/transpiler/operators.go
+++ b/transpiler/operators.go
@@ -495,6 +495,7 @@ func atomicOperation(n ast.Node, p *program.Program) (
 	if err != nil {
 		return
 	}
+
 	defer func() {
 		if err != nil {
 			err = fmt.Errorf("Cannot create atomicOperation |%T|. err = %v", n, err)
@@ -1097,15 +1098,20 @@ func atomicOperation(n ast.Node, p *program.Program) (
 			preStmts = nil
 			postStmts = nil
 
-			returnValue, _, _, _, _ := transpileToExpr(decl, p, false)
-			if d, ok := decl.(*ast.DeclRefExpr); ok &&
-				types.IsPointer(d.Type, p) && !types.IsPointer(v.Type, p) {
-				returnValue = &goast.IndexExpr{
-					X: returnValue,
-					Index: &goast.BasicLit{
-						Kind:  token.INT,
-						Value: "0",
-					},
+			var returnValue goast.Expr
+			if bin, ok := e.(*goast.BinaryExpr); ok {
+				returnValue = bin.X
+			} else {
+				returnValue, _, _, _, _ = transpileToExpr(decl, p, false)
+				if d, ok := decl.(*ast.DeclRefExpr); ok &&
+					types.IsPointer(d.Type, p) && !types.IsPointer(v.Type, p) {
+					returnValue = &goast.IndexExpr{
+						X: returnValue,
+						Index: &goast.BasicLit{
+							Kind:  token.INT,
+							Value: "0",
+						},
+					}
 				}
 			}
 
@@ -1135,12 +1141,20 @@ func getDeclRefExprOrArraySub(n ast.Node) (ast.Node, bool) {
 	switch v := n.(type) {
 	case *ast.DeclRefExpr:
 		return v, true
+	case *ast.ParenExpr:
+		return getDeclRefExprOrArraySub(n.Children()[0])
 	case *ast.ImplicitCastExpr:
 		return getDeclRefExprOrArraySub(n.Children()[0])
 	case *ast.UnaryOperator:
 		return getDeclRefExprOrArraySub(n.Children()[0])
 	case *ast.ArraySubscriptExpr:
 		return v, true
+	case *ast.BinaryOperator:
+		for i := range v.Children() {
+			if v, ok := getDeclRefExprOrArraySub(v.Children()[i]); ok {
+				return v, true
+			}
+		}
 	case *ast.MemberExpr:
 		return v, true
 	}
diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go
index 0b0720c..d13c7fb 100644
--- a/transpiler/transpiler.go
+++ b/transpiler/transpiler.go
@@ -477,6 +477,12 @@ func transpileToNode(node ast.Node, p *program.Program) (
 		decls = nilFilterDecl(decls)
 	}()
 
+	defer func() {
+		if r := recover(); r != nil {
+			err = fmt.Errorf("error - panic : %#v", r)
+		}
+	}()
+
 	switch n := node.(type) {
 	case *ast.TranslationUnitDecl:
 		return transpileTranslationUnitDecl(p, n)
diff --git a/types/resolve.go b/types/resolve.go
index 5a56db5..181e6d4 100644
--- a/types/resolve.go
+++ b/types/resolve.go
@@ -392,7 +392,7 @@ func SeparateFunction(p *program.Program, s string) (
 		}
 		returns = append(returns, t)
 	}
-	prefix = pr
+	prefix = util.CleanCType(pr)
 	return
 }
 
diff --git a/util/util.go b/util/util.go
index b265df8..a27f16e 100644
--- a/util/util.go
+++ b/util/util.go
@@ -327,6 +327,13 @@ func CleanCType(s string) (out string) {
 	out = strings.Replace(out, "\r", "", -1)
 	list := []string{"const", "volatile", "__restrict", "restrict", "_Nullable"}
 	for _, word := range list {
+		// example :
+		// `const`
+		if out == word {
+			out = ""
+			continue
+		}
+
 		// examples :
 		// `const char  * *`
 		// `const struct parg_option`


Test patch:
diff --git a/tests/code_quality/ap.go.expected b/tests/code_quality/ap.go.expected
index e2e8f4f..d53ec63 100644
--- a/tests/code_quality/ap.go.expected
+++ b/tests/code_quality/ap.go.expected
@@ -28,7 +28,7 @@ func b(v1 []int32, size int32) {
 
 // get - transpiled function from  C4GO/tests/code_quality/ap.c:14
 func get() int32 {
-	return (0)
+	return 0
 }
 
 // main - transpiled function from  C4GO/tests/code_quality/ap.c:19
diff --git a/tests/code_quality/if.go.expected b/tests/code_quality/if.go.expected
index c4f3e9c..3f99d38 100644
--- a/tests/code_quality/if.go.expected
+++ b/tests/code_quality/if.go.expected
@@ -17,9 +17,9 @@ func if_1() {
 	} else if c <= a {
 		a = 0
 	}
-	_ = (a)
-	_ = (b)
-	_ = (c)
+	_ = a
+	_ = b
+	_ = c
 	var w int32 = func() int32 {
 		if 2 > 1 {
 			return -1
diff --git a/tests/code_quality/operators.go.expected b/tests/code_quality/operators.go.expected
index c7fc205..ca36e9e 100644
--- a/tests/code_quality/operators.go.expected
+++ b/tests/code_quality/operators.go.expected
@@ -9,7 +9,7 @@ package code_quality
 
 // f - transpiled function from  C4GO/tests/code_quality/operators.c:1
 func f(d uint32) {
-	_ = (d)
+	_ = d
 }
 
 // operators_equals - transpiled function from  C4GO/tests/code_quality/operators.c:8
@@ -27,11 +27,11 @@ func operators_equals() {
 	var q3 int32 = 42
 	var q4 int64 = 42
 	var q5 int16 = 42
-	var w1 int32 = int32(int64((42)))
-	var w2 uint32 = uint32(int64((42)))
-	var w3 int32 = int32(int64((42)))
-	var w4 int64 = int64((42))
-	var w5 int16 = int16(int64((42)))
+	var w1 int32 = int32(int64(42))
+	var w2 uint32 = uint32(int64(42))
+	var w3 int32 = int32(int64(42))
+	var w4 int64 = int64(42)
+	var w5 int16 = int16(int64(42))
 	var s1 int32
 	var s2 uint32
 	var s3 int32
diff --git a/tests/code_quality/paren.go.expected b/tests/code_quality/paren.go.expected
index 25389c6..57998e1 100644
--- a/tests/code_quality/paren.go.expected
+++ b/tests/code_quality/paren.go.expected
@@ -10,10 +10,10 @@ package code_quality
 // function - transpiled function from  C4GO/tests/code_quality/paren.c:1
 func function() {
 	var t int32
-	t = (0)
+	t = 0
 	var a int32
 	a = t
-	_ = (t)
-	_ = (a)
+	_ = t
+	_ = a
 	(function())
 }
diff --git a/tests/code_quality/return.go.expected b/tests/code_quality/return.go.expected
index d707858..d3211ab 100644
--- a/tests/code_quality/return.go.expected
+++ b/tests/code_quality/return.go.expected
@@ -13,7 +13,7 @@ func a() {
 
 // r - transpiled function from  C4GO/tests/code_quality/return.c:6
 func r(y int32) {
-	_ = (y)
+	_ = y
 }
 
 // b - transpiled function from  C4GO/tests/code_quality/return.c:12
diff --git a/tests/code_quality/switch.go.expected b/tests/code_quality/switch.go.expected
index 15a978e..319d5d4 100644
--- a/tests/code_quality/switch.go.expected
+++ b/tests/code_quality/switch.go.expected
@@ -16,7 +16,7 @@ func switch_function() {
 	case (1):
 		return
 	case (2):
-		_ = (i)
+		_ = i
 		return
 	case 3:
 		var c int32
diff --git a/tests/code_quality/unsafe.go.expected b/tests/code_quality/unsafe.go.expected
index 069f5a7..ff20359 100644
--- a/tests/code_quality/unsafe.go.expected
+++ b/tests/code_quality/unsafe.go.expected
@@ -49,56 +49,56 @@ func test_char() {
 	var t byte
 	var pt []byte = c4goUnsafeConvert_byte(&t)
 	// integers
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_short - transpiled function from  C4GO/tests/code_quality/unsafe.c:22
 func test_short() {
 	var t int16
 	var pt []int16 = c4goUnsafeConvert_int16(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_int - transpiled function from  C4GO/tests/code_quality/unsafe.c:23
 func test_int() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_long - transpiled function from  C4GO/tests/code_quality/unsafe.c:24
 func test_long() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_li - transpiled function from  C4GO/tests/code_quality/unsafe.c:25
 func test_li() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_ll - transpiled function from  C4GO/tests/code_quality/unsafe.c:26
 func test_ll() {
 	var t int64
 	var pt []int64 = c4goUnsafeConvert_int64(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_lli - transpiled function from  C4GO/tests/code_quality/unsafe.c:27
 func test_lli() {
 	var t int64
 	var pt []int64 = c4goUnsafeConvert_int64(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_f - transpiled function from  C4GO/tests/code_quality/unsafe.c:30
@@ -106,24 +106,24 @@ func test_f() {
 	var t float32
 	var pt []float32 = c4goUnsafeConvert_float32(&t)
 	// floats
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_d - transpiled function from  C4GO/tests/code_quality/unsafe.c:31
 func test_d() {
 	var t float64
 	var pt []float64 = c4goUnsafeConvert_float64(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_ld - transpiled function from  C4GO/tests/code_quality/unsafe.c:32
 func test_ld() {
 	var t float64
 	var pt []float64 = c4goUnsafeConvert_float64(&t)
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_struct - transpiled function from  C4GO/tests/code_quality/unsafe.c:35
@@ -131,8 +131,8 @@ func test_struct() {
 	var t str
 	var pt []str = c4goUnsafeConvert_str(&t)
 	// struct
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_un - transpiled function from  C4GO/tests/code_quality/unsafe.c:38
@@ -140,8 +140,8 @@ func test_un() {
 	var t un
 	var pt []un = c4goUnsafeConvert_un(&t)
 	// union
-	_ = (t)
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // test_typedef - transpiled function from  C4GO/tests/code_quality/unsafe.c:41
@@ -149,8 +149,8 @@ func test_typedef() {
 	var t db
 	var pt []db = c4goUnsafeConvert_float64(&t)
 	// typedef
-	_ = db((t))
-	_ = (pt)
+	_ = t
+	_ = pt
 }
 
 // c4goUnsafeConvert_byte : created by c4go
diff --git a/tests/operators.c b/tests/operators.c
index 5456d36..e7b6385 100644
--- a/tests/operators.c
+++ b/tests/operators.c
@@ -69,9 +69,14 @@ static int valGlobInt       = 42;
 static double valGlobDouble = 45.0;
 static bool restricted      = true;
 
+typedef unsigned int u00;
+struct UUU000{
+	u00 u[2];
+};
+
 int main()
 {
-    plan(123);
+    plan(153);
 
 	is_eq(valGlobInt, 42);
 	is_eq(valGlobDouble, 45);
@@ -538,6 +543,150 @@ int main()
         sflags |= SGR;
         is_eq(sflags, 5);
     }
+	diag("equal paren");
+	{
+		int a,b;
+		a = b = 42;
+		is_eq(a,42);
+		is_eq(b,42);
+		a = (b = 42);
+		is_eq(a,42);
+		is_eq(b,42);
+	}
+	diag("equal paren pointer");
+	{
+		int * a;
+		int * b;
+		int val = 45;
+		a = b = &val;
+		is_eq(*a,val);
+		is_eq(*b,val);
+		val = 42;
+		a = (b = &val);
+		is_eq(*a,val);
+		is_eq(*b,val);
+	}
+	diag("equal paren u00");
+	{
+		u00 a,b;
+		a = b = 44;
+		is_eq(a,44);
+		is_eq(b,44);
+		a = (b = 42);
+		is_eq(a,42);
+		is_eq(b,42);
+	}
+	diag("equal paren UUU000");
+	{
+		u00 a = 150;
+		struct UUU000 bs;
+		bs.u[0] = 100;
+		a = bs.u[0] = 44;
+		is_eq(a,44);
+		is_eq(bs.u[0],44);
+		a = (bs.u[0] = 42);
+		is_eq(a,42);
+		is_eq(bs.u[0],42);
+	}
+	diag("equal paren UUU000 pointer");
+	{
+		u00 a = 150;
+		u00 b = 100;
+		u00 *pb = &b;
+		a = (*pb = 42);
+		is_eq(a,42);
+		is_eq(b,42);
+
+		// check more complex
+		a = (*(pb) = (45));
+		is_eq(a,45);
+		is_eq(b,45);
+	}
+	diag("equal paren member");
+	{
+		struct sd1 {
+			int * mark1;
+		};
+		int a1 = 900;
+		struct sd1 c1  ;
+		int F = 76;
+		c1.mark1 = &F;
+		struct sd1 * b1 = &c1;
+		a1 = (*(b1->mark1) = (43));
+		is_eq(a1,43);
+		is_eq(*(b1->mark1),43);
+	}
+	diag("equal paren member 2");
+	{
+		struct sd2 {
+			int * mark;
+		};
+		int a = 900;
+		struct sd2 c  ;
+		int F = 98;
+		c.mark = &F;
+		struct sd2 * b = &c;
+		a = (*(b->mark + 0) = (43));
+		is_eq(a,43);
+		is_eq(*(b->mark),43);
+	}
+
+
+	diag("+= paren member");
+	{
+		struct sd10 {
+			int * mark10;
+		};
+		int a10 = 900;
+		struct sd10 c10  ;
+		int F = 76;
+		c10.mark10 = &F;
+		struct sd10 * b10 = &c10;
+		a10 = (*(b10->mark10) += (43));
+		is_eq(a10,43+76);
+		is_eq(*(b10->mark10),43+76);
+	}
+	diag("+= paren member 2");
+	{
+		struct sd20 {
+			int * mark;
+		};
+		int a = 900;
+		struct sd20 c  ;
+		int F = 98;
+		c.mark = &F;
+		struct sd20 * b = &c;
+		a = (*(b->mark + 0) += (43));
+		is_eq(a,43+98);
+		is_eq(*(b->mark),43+98);
+	}
+	diag("++ conv");
+	{
+		int a = 90;
+		long b = (long)++a;
+		is_eq(a,91);
+		is_eq(b,91);
+	}
 
     done_testing();
 }
+
+struct wordStr {
+	char * w;
+};
+
+static const struct wordStr * wordQImpl(const char * ch, int y) {
+	return NULL;
+}
+
+static const struct wordStr * ( * const wordQ)(const char *, int) = wordQImpl;
+
+
+int v2(
+  int *db,
+  unsigned mTrace,
+  int(*xTrace)(unsigned,void*,void*,void*),
+  void *pArg 
+){
+	return 0;
+}