Repository: Konstantin8105/c4go
Pull request #348 (2019-03-20 16:11:58)

add typedef case for convert value to slice

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=h1) Report
> Merging [#348](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/74c2e3f58221090df86247a23953d48b26c08bd1?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master     #348      +/-   ##
==========================================
+ Coverage   86.44%   86.45%   +<.01%     
==========================================
  Files         186      186              
  Lines       10611    10617       +6     
==========================================
+ Hits         9173     9179       +6     
  Misses       1026     1026              
  Partials      412      412
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/value\_to\_pointer.go](https://codecov.io/gh/Konstantin8105/c4go/pull/348/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YWx1ZV90b19wb2ludGVyLmdv) | `93.33% <100%> (+0.57%)` | :arrow_up: |

------

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

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=h1) Report
> Merging [#348](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/74c2e3f58221090df86247a23953d48b26c08bd1?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master     #348      +/-   ##
==========================================
+ Coverage   86.44%   86.45%   +<.01%     
==========================================
  Files         186      186              
  Lines       10611    10617       +6     
==========================================
+ Hits         9173     9179       +6     
  Misses       1026     1026              
  Partials      412      412
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/value\_to\_pointer.go](https://codecov.io/gh/Konstantin8105/c4go/pull/348/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YWx1ZV90b19wb2ludGVyLmdv) | `93.33% <100%> (+0.57%)` | :arrow_up: |

------

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

codecov-io: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=h1) Report
> Merging [#348](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/74c2e3f58221090df86247a23953d48b26c08bd1?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master     #348      +/-   ##
==========================================
+ Coverage   86.44%   86.45%   +<.01%     
==========================================
  Files         186      186              
  Lines       10611    10615       +4     
==========================================
+ Hits         9173     9177       +4     
  Misses       1026     1026              
  Partials      412      412
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/348?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/value\_to\_pointer.go](https://codecov.io/gh/Konstantin8105/c4go/pull/348/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YWx1ZV90b19wb2ludGVyLmdv) | `93.15% <100%> (+0.39%)` | :arrow_up: |

------

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


Patch:
diff --git a/transpiler/value_to_pointer.go b/transpiler/value_to_pointer.go
index 9e94224..22a230d 100644
--- a/transpiler/value_to_pointer.go
+++ b/transpiler/value_to_pointer.go
@@ -29,7 +29,17 @@ func ConvertValueToPointer(nodes []ast.Node, p *program.Program) (expr goast.Exp
 		return nil, false
 	}
 
-	resolvedType, err := types.ResolveType(p, decl.Type)
+	// get base type if it typedef
+	var td string = decl.Type
+	for {
+		if t, ok := p.TypedefType[td]; ok {
+			td = t
+			continue
+		}
+		break
+	}
+
+	resolvedType, err := types.ResolveType(p, td)
 	if err != nil {
 		p.AddMessage(p.GenerateWarningMessage(err, decl))
 		return
@@ -57,7 +67,7 @@ func GetUnsafeConvertDecls(p *program.Program) {
 	p.AddImport("unsafe")
 
 	var names []string
-	for t, _ := range p.UnsafeConvertValueToPointer {
+	for t := range p.UnsafeConvertValueToPointer {
 		names = append(names, t)
 	}
 	sort.Sort(sort.StringSlice(names))
@@ -70,7 +80,7 @@ func GetUnsafeConvertDecls(p *program.Program) {
 			Type: &goast.FuncType{
 				Params: &goast.FieldList{
 					List: []*goast.Field{
-						&goast.Field{
+						{
 							Names: []*goast.Ident{goast.NewIdent(varName)},
 							Type:  goast.NewIdent("*" + t),
 						},
@@ -78,7 +88,7 @@ func GetUnsafeConvertDecls(p *program.Program) {
 				},
 				Results: &goast.FieldList{
 					List: []*goast.Field{
-						&goast.Field{
+						{
 							Type: &goast.ArrayType{
 								Lbrack: 1,
 								Elt:    goast.NewIdent(t),


Test patch:
diff --git a/tests/code_quality/unsafe.c b/tests/code_quality/unsafe.c
index 4b2e033..2935a4e 100644
--- a/tests/code_quality/unsafe.c
+++ b/tests/code_quality/unsafe.c
@@ -15,6 +15,8 @@ union un{
 	double d;
 };
 
+typedef double db;
+
 // integers
 unsafetype(char,test_char)
 unsafetype(short,test_short)
@@ -34,3 +36,6 @@ unsafetype(struct str, test_struct)
 
 // union
 unsafetype(union un, test_un)
+
+// typedef
+unsafetype(db, test_typedef)
diff --git a/tests/code_quality/unsafe.go.expected b/tests/code_quality/unsafe.go.expected
index 24e8cca..f5fefa9 100644
--- a/tests/code_quality/unsafe.go.expected
+++ b/tests/code_quality/unsafe.go.expected
@@ -41,7 +41,10 @@ func (unionVar *un) d() *float64 {
 	return (*float64)(unionVar.memory)
 }
 
-// test_char - transpiled function from  C4GO/tests/code_quality/unsafe.c:19
+// db - transpiled function from  C4GO/tests/code_quality/unsafe.c:18
+type db = float64
+
+// test_char - transpiled function from  C4GO/tests/code_quality/unsafe.c:21
 func test_char() {
 	var t byte
 	var pt []byte = c4goUnsafeConvert_byte(&t)
@@ -50,7 +53,7 @@ func test_char() {
 	_ = (pt)
 }
 
-// test_short - transpiled function from  C4GO/tests/code_quality/unsafe.c:20
+// test_short - transpiled function from  C4GO/tests/code_quality/unsafe.c:22
 func test_short() {
 	var t int16
 	var pt []int16 = c4goUnsafeConvert_int16(&t)
@@ -58,7 +61,7 @@ func test_short() {
 	_ = (pt)
 }
 
-// test_int - transpiled function from  C4GO/tests/code_quality/unsafe.c:21
+// test_int - transpiled function from  C4GO/tests/code_quality/unsafe.c:23
 func test_int() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
@@ -66,7 +69,7 @@ func test_int() {
 	_ = (pt)
 }
 
-// test_long - transpiled function from  C4GO/tests/code_quality/unsafe.c:22
+// test_long - transpiled function from  C4GO/tests/code_quality/unsafe.c:24
 func test_long() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
@@ -74,7 +77,7 @@ func test_long() {
 	_ = (pt)
 }
 
-// test_li - transpiled function from  C4GO/tests/code_quality/unsafe.c:23
+// test_li - transpiled function from  C4GO/tests/code_quality/unsafe.c:25
 func test_li() {
 	var t int32
 	var pt []int32 = c4goUnsafeConvert_int32(&t)
@@ -82,7 +85,7 @@ func test_li() {
 	_ = (pt)
 }
 
-// test_ll - transpiled function from  C4GO/tests/code_quality/unsafe.c:24
+// test_ll - transpiled function from  C4GO/tests/code_quality/unsafe.c:26
 func test_ll() {
 	var t int64
 	var pt []int64 = c4goUnsafeConvert_int64(&t)
@@ -90,7 +93,7 @@ func test_ll() {
 	_ = (pt)
 }
 
-// test_lli - transpiled function from  C4GO/tests/code_quality/unsafe.c:25
+// test_lli - transpiled function from  C4GO/tests/code_quality/unsafe.c:27
 func test_lli() {
 	var t int64
 	var pt []int64 = c4goUnsafeConvert_int64(&t)
@@ -98,7 +101,7 @@ func test_lli() {
 	_ = (pt)
 }
 
-// test_f - transpiled function from  C4GO/tests/code_quality/unsafe.c:28
+// test_f - transpiled function from  C4GO/tests/code_quality/unsafe.c:30
 func test_f() {
 	var t float32
 	var pt []float32 = c4goUnsafeConvert_float32(&t)
@@ -107,7 +110,7 @@ func test_f() {
 	_ = (pt)
 }
 
-// test_d - transpiled function from  C4GO/tests/code_quality/unsafe.c:29
+// test_d - transpiled function from  C4GO/tests/code_quality/unsafe.c:31
 func test_d() {
 	var t float64
 	var pt []float64 = c4goUnsafeConvert_float64(&t)
@@ -115,7 +118,7 @@ func test_d() {
 	_ = (pt)
 }
 
-// test_ld - transpiled function from  C4GO/tests/code_quality/unsafe.c:30
+// test_ld - transpiled function from  C4GO/tests/code_quality/unsafe.c:32
 func test_ld() {
 	var t float64
 	var pt []float64 = c4goUnsafeConvert_float64(&t)
@@ -123,7 +126,7 @@ func test_ld() {
 	_ = (pt)
 }
 
-// test_struct - transpiled function from  C4GO/tests/code_quality/unsafe.c:33
+// test_struct - transpiled function from  C4GO/tests/code_quality/unsafe.c:35
 func test_struct() {
 	var t str
 	var pt []str = (*[100000000]str)(unsafe.Pointer(&t))[:]
@@ -132,7 +135,7 @@ func test_struct() {
 	_ = (pt)
 }
 
-// test_un - transpiled function from  C4GO/tests/code_quality/unsafe.c:36
+// test_un - transpiled function from  C4GO/tests/code_quality/unsafe.c:38
 func test_un() {
 	var t un
 	var pt []un = (*[100000000]un)(unsafe.Pointer(&t))[:]
@@ -140,6 +143,15 @@ func test_un() {
 	_ = (t)
 	_ = (pt)
 }
+
+// test_typedef - transpiled function from  C4GO/tests/code_quality/unsafe.c:41
+func test_typedef() {
+	var t db
+	var pt []db = c4goUnsafeConvert_float64(&t)
+	// typedef
+	_ = db((t))
+	_ = (pt)
+}
 func c4goUnsafeConvert_byte(c4go_name *byte) []byte {
 	return (*[1000000]byte)(unsafe.Pointer(c4go_name))[:]
 }