Repository: Konstantin8105/c4go
Pull request #398 (2019-04-07 20:48:04)

indexExpr for pointer of struct field

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

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

```diff
@@            Coverage Diff             @@
##           master     #398      +/-   ##
==========================================
- Coverage   85.47%   85.43%   -0.05%     
==========================================
  Files         188      188              
  Lines       11076    11071       -5     
==========================================
- Hits         9467     9458       -9     
- Misses       1145     1153       +8     
+ Partials      464      460       -4
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/398?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/variables.go](https://codecov.io/gh/Konstantin8105/c4go/pull/398/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YXJpYWJsZXMuZ28=) | `87.72% <100%> (+0.04%)` | :arrow_up: |
| [transpiler/value\_to\_pointer.go](https://codecov.io/gh/Konstantin8105/c4go/pull/398/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YWx1ZV90b19wb2ludGVyLmdv) | `83.2% <50%> (-0.42%)` | :arrow_down: |
| [transpiler/call.go](https://codecov.io/gh/Konstantin8105/c4go/pull/398/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9jYWxsLmdv) | `78.02% <71.42%> (-0.82%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/398?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/398?src=pr&el=footer). Last update [b2de9b4...8d24f0e](https://codecov.io/gh/Konstantin8105/c4go/pull/398?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/scripts/scripts.txt b/scripts/scripts.txt
index d06f90f..bd3be9e 100644
--- a/scripts/scripts.txt
+++ b/scripts/scripts.txt
@@ -46,7 +46,7 @@ Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
 
 Calculate warnings in file: ./testdata/neatroff/neatroff.go
 		After transpiling : 0 warnings.
-		Go build : 20 warnings
+		Go build : 17 warnings
 		Unsafe   : 254
 
 Calculate warnings in file: ./testdata/neatvi/neatvi.go
diff --git a/transpiler/call.go b/transpiler/call.go
index 42c55b6..bb1a66a 100644
--- a/transpiler/call.go
+++ b/transpiler/call.go
@@ -43,28 +43,17 @@ func getName(p *program.Program, firstChild ast.Node) (name string, err error) {
 		return fc.Name, nil
 
 	case *ast.MemberExpr:
-		if isUnionMemberExpr(p, fc) {
-			var expr goast.Expr
-			expr, _, _, _, err = transpileToExpr(fc, p, false)
-			if err != nil {
-				return
-			}
-			var buf bytes.Buffer
-			err = printer.Fprint(&buf, token.NewFileSet(), expr)
-			if err != nil {
-				return
-			}
-			return buf.String(), nil
-		}
-		if len(fc.Children()) == 0 {
-			return fc.Name, nil
+		var expr goast.Expr
+		expr, _, _, _, err = transpileToExpr(fc, p, false)
+		if err != nil {
+			return
 		}
-		var n string
-		n, err = getName(p, fc.Children()[0])
+		var buf bytes.Buffer
+		err = printer.Fprint(&buf, token.NewFileSet(), expr)
 		if err != nil {
 			return
 		}
-		return n + "." + fc.Name, nil
+		return buf.String(), nil
 
 	case *ast.CallExpr:
 		if len(fc.Children()) == 0 {
diff --git a/transpiler/value_to_pointer.go b/transpiler/value_to_pointer.go
index e1913fb..1377e79 100644
--- a/transpiler/value_to_pointer.go
+++ b/transpiler/value_to_pointer.go
@@ -645,11 +645,6 @@ func pointerArithmetic(p *program.Program,
 		break
 	}
 
-	resolvedLeftType, err := types.ResolveType(p, leftType)
-	if err != nil {
-		return
-	}
-
 	type pA struct {
 		Name      string // name of variable: 'ptr'
 		Type      string // type of variable: 'int','double'
@@ -659,6 +654,17 @@ func pointerArithmetic(p *program.Program,
 
 	var s pA
 
+	resolvedLeftType, err := types.ResolveType(p, leftType)
+	if err != nil {
+		return
+	}
+	switch resolvedLeftType {
+	case "interface{}":
+		s.Type = "byte"
+	default:
+		s.Type = resolvedLeftType[2:]
+	}
+
 	{
 		var buf bytes.Buffer
 		_ = printer.Fprint(&buf, token.NewFileSet(), left)
@@ -670,11 +676,6 @@ func pointerArithmetic(p *program.Program,
 		s.Condition = buf.String()
 	}
 
-	s.Type = resolvedLeftType
-	if resolvedLeftType != "interface{}" {
-		s.Type = resolvedLeftType[2:]
-	}
-
 	s.Operator = "+"
 	if operator == token.SUB {
 		s.Operator = "-"
diff --git a/transpiler/variables.go b/transpiler/variables.go
index 9cb2cb7..387136f 100644
--- a/transpiler/variables.go
+++ b/transpiler/variables.go
@@ -44,8 +44,9 @@ func transpileDeclRefExpr(n *ast.DeclRefExpr, p *program.Program) (
 	}
 
 	theType := n.Type
+	expr = util.NewIdent(n.Name)
 
-	return util.NewIdent(n.Name), theType, nil
+	return expr, theType, nil
 }
 
 func getDefaultValueForVar(p *program.Program, a *ast.VarDecl) (


Test patch:
diff --git a/ast/member_expr_test.go b/ast/member_expr_test.go
index fb6dad9..fede742 100644
--- a/ast/member_expr_test.go
+++ b/ast/member_expr_test.go
@@ -90,6 +90,30 @@ func TestMemberExpr(t *testing.T) {
 			IsPointer:  false,
 			ChildNodes: []Node{},
 		},
+		`0x2a0dbd0 <col:15, col:18> 'int (*)(int, int)' lvalue .f 0x2a0d7c0`: &MemberExpr{
+			Addr:       0x2a0dbd0,
+			Pos:        NewPositionFromString("col:15, col:18"),
+			Type:       "int (*)(int, int)",
+			Type2:      "",
+			IsLvalue:   true,
+			IsBitfield: false,
+			Name:       "f",
+			Address2:   "0x2a0d7c0",
+			IsPointer:  false,
+			ChildNodes: []Node{},
+		},
+		`0x2a0fdf8 <col:15, col:19> 'int (*)(int, int)' lvalue ->f 0x2a0d7c0`: &MemberExpr{
+			Addr:       0x2a0fdf8,
+			Pos:        NewPositionFromString("col:15, col:19"),
+			Type:       "int (*)(int, int)",
+			Type2:      "",
+			IsLvalue:   true,
+			IsBitfield: false,
+			Name:       "f",
+			Address2:   "0x2a0d7c0",
+			IsPointer:  true,
+			ChildNodes: []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)
diff --git a/tests/stdlib.c b/tests/stdlib.c
index 379b31e..4f5bced 100644
--- a/tests/stdlib.c
+++ b/tests/stdlib.c
@@ -285,6 +285,25 @@ void test_atoi_post()
     is_eq(n, 23);
 }
 
+// TODO:
+// static void * my_memrchr(void *m, int c, long n)
+// {
+	// int i;
+	// for (i = 0; i < n; i++)
+		// if (*(unsigned char *) (m + n - 1 - i) == c)
+			// return m + n - 1 - i;
+	// return NULL;
+// }
+// 
+// void test_my_memrchr()
+// {
+	// unsigned char * word = "Hello my dear world";
+	// void * p = my_memrchr(word, (int)('d'), 18);
+	// is_not_null(p);
+	// p = my_memrchr(word, (int)('z'), 18);
+	// is_null(p);
+// }
+
 int main()
 {
     plan(763);
@@ -577,5 +596,9 @@ int main()
     diag("q_sort");
     q_sort();
 
+	// TODO:
+	// diag("my_memrchr");
+	// test_my_memrchr();
+
     done_testing();
 }
diff --git a/tests/struct.c b/tests/struct.c
index 682dfeb..de87c4c 100644
--- a/tests/struct.c
+++ b/tests/struct.c
@@ -705,9 +705,38 @@ void typedef_struct_with_typedef_union()
 	is_eq(*((double*)((&tv)->value.p)), d);
 }
 
+int add(int a, int b)
+{
+    return a + b;
+}
+
+struct simon {
+	int (*f)(int, int);
+	long double g;
+};
+
+void test_struct_with_func()
+{
+	struct simon s1;
+	s1.f = add;
+	s1.g = 46.;
+	is_eq(s1.f(12,23), 12+23);
+	is_eq(s1.g       , 46.  );
+	struct simon * ps = &s1;
+	is_eq(ps->f(12,23), 12+23);
+	is_eq(ps->g       , 46.  );
+	struct simon as[2];
+	as[0] = s1;
+	as[1] = *ps;
+	is_eq(as[0].f(12,23), 12+23);
+	is_eq(as[0].g       , 46.  );
+	is_eq(as[1].f(12,23), 12+23);
+	is_eq(as[1].g       , 46.  );
+}
+
 int main()
 {
-    plan(117);
+    plan(125);
 
     pointer_arithm_in_struct();
     test_extern_vec();
@@ -1111,6 +1140,7 @@ int main()
 
 	typedef_with_union();
 	typedef_struct_with_typedef_union();
+	test_struct_with_func();
 
     done_testing();
 }