Repository: Konstantin8105/c4go
Pull request #368 (2019-03-30 12:45:23)

initialization of variables

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

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

```diff
@@            Coverage Diff             @@
##           master     #368      +/-   ##
==========================================
+ Coverage   85.91%   85.96%   +0.05%     
==========================================
  Files         188      188              
  Lines       11030    11027       -3     
==========================================
+ Hits         9476     9479       +3     
+ Misses       1112     1106       -6     
  Partials      442      442
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/368?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/variables.go](https://codecov.io/gh/Konstantin8105/c4go/pull/368/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YXJpYWJsZXMuZ28=) | `86.25% <95%> (+1.72%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/368?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/368?src=pr&el=footer). Last update [e1d8270...28ae2ab](https://codecov.io/gh/Konstantin8105/c4go/pull/368?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 f9b9798..793b727 100644
--- a/scripts/scripts.txt
+++ b/scripts/scripts.txt
@@ -11,7 +11,7 @@ Calculate warnings : ./testdata/ed
 
 Calculate warnings in file: ./testdata/neateqn/neateqn.go
 		After transpiling : 0 warnings.
-		Go build : 5 warnings
+		Go build : 0 warnings
 		Unsafe   : 22
 
 Calculate warnings in file: ./testdata/neatmkfn/neatmkfn.go
@@ -31,7 +31,7 @@ Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
 
 Calculate warnings in file: ./testdata/neatroff/neatroff.go
 		After transpiling : 6 warnings.
-		Go build : 25 warnings
+		Go build : 21 warnings
 		Unsafe   : 202
 
 Calculate warnings in file: ./testdata/neatvi/neatvi.go
diff --git a/transpiler/variables.go b/transpiler/variables.go
index dfc6503..8cc06f0 100644
--- a/transpiler/variables.go
+++ b/transpiler/variables.go
@@ -2,16 +2,16 @@ package transpiler
 
 import (
 	"fmt"
+	goast "go/ast"
+	"go/parser"
+	"go/token"
+	"strconv"
 	"strings"
 
 	"github.com/Konstantin8105/c4go/ast"
 	"github.com/Konstantin8105/c4go/program"
 	"github.com/Konstantin8105/c4go/types"
 	"github.com/Konstantin8105/c4go/util"
-
-	goast "go/ast"
-	"go/parser"
-	"go/token"
 )
 
 func transpileDeclRefExpr(n *ast.DeclRefExpr, p *program.Program) (
@@ -208,48 +208,52 @@ func transpileInitListExpr(e *ast.InitListExpr, p *program.Program) (
 	e.Type2 = util.GenerateCorrectType(e.Type2)
 	exprType = e.Type1
 
-	structType, isStruct := p.Structs[e.Type1]
-	if !isStruct {
-		if tt, ok := p.GetBaseTypeOfTypedef(e.Type1); ok {
-			structType, isStruct = p.Structs[tt]
-		}
-	}
-
-	for fieldPos, node := range e.Children() {
+	for _, node := range e.Children() {
 		// Skip ArrayFiller
 		if _, ok := node.(*ast.ArrayFiller); ok {
 			continue
 		}
 
-		var expr goast.Expr
-		var eType string
-		var err error
+		// var expr goast.Expr
+		// var eType string
+		// var err error
 
-		var isStringLiteral bool
-		var sl *ast.StringLiteral
+		expr, _, _, _, err := transpileToExpr(node, p, true)
+		p.AddMessage(p.GenerateWarningMessage(err, node))
 
-		sl, isStringLiteral = node.(*ast.StringLiteral)
-		if !isStringLiteral {
-			if impl, ok := node.(*ast.ImplicitCastExpr); ok && len(impl.Children()) > 0 {
-				sl, isStringLiteral = impl.Children()[0].(*ast.StringLiteral)
-			}
-		}
+		resp = append(resp, expr)
+	}
 
-		expr, eType, _, _, err = transpileToExpr(node, p, true)
-		p.AddMessage(p.GenerateWarningMessage(err, node))
+	goType, err := types.ResolveType(p, e.Type1)
+	if err != nil {
+		return nil, "", err
+	}
+
+	arrayType, arraySize := types.GetArrayTypeAndSize(e.Type1)
+	if arraySize > 0 {
+		for i := len(resp); i < arraySize; i++ {
+			zero, _ := zeroValue(p, arrayType)
+			resp = append(resp, zero)
+		}
+		exprType = arrayType + "[]"
+	}
 
-		if isStruct {
+	structType, isStruct := p.Structs[e.Type1]
+	if !isStruct {
+		if tt, ok := p.GetBaseTypeOfTypedef(e.Type1); ok {
+			structType, isStruct = p.Structs[tt]
+		}
+	}
+	if isStruct {
+		for fieldPos, node := range resp {
 			if fieldType, ok := structType.Fields[structType.FieldNames[fieldPos]]; ok {
 				if ft, ok := fieldType.(string); ok {
 
 					arr, arrFieldSize := types.GetArrayTypeAndSize(ft)
-					_, arrExprSize := types.GetArrayTypeAndSize(eType)
+					if arrFieldSize > 0 {
 
-					if isStringLiteral && arrFieldSize > 0 {
-						expr, eType, err = transpileStringLiteral(p, sl, true)
-					} else if arrFieldSize > 0 && arrExprSize < 0 {
 						var fixed bool
-						switch v := expr.(type) {
+						switch v := node.(type) {
 						case *goast.CompositeLit:
 							if id, ok := v.Type.(*goast.Ident); ok {
 								goType, err := types.ResolveType(p, arr)
@@ -258,11 +262,32 @@ func transpileInitListExpr(e *ast.InitListExpr, p *program.Program) (
 								fixed = true
 							}
 						case *goast.CallExpr:
-							if id, ok := v.Fun.(*goast.Ident); ok {
-								goType, err := types.ResolveType(p, arr)
-								p.AddMessage(p.GenerateWarningMessage(err, nil))
-								id.Name = fmt.Sprintf("[%d]%s", arrFieldSize, goType)
-								fixed = true
+							// From:
+							// 0  *ast.CallExpr {
+							// 1  .  Fun: *ast.Ident {
+							// 3  .  .  Name: "[]byte"
+							// 4  .  }
+							// 6  .  Args: []ast.Expr (len = 1) {
+							// 7  .  .  0: *ast.BasicLit {
+							// 9  .  .  .  Kind: STRING
+							// 10  .  .  .  Value: "\"dream\\x00\""
+							// 11  .  .  }
+							// 12  .  }
+							// 15  }
+							if id, ok := v.Fun.(*goast.Ident); ok && id.Name == "[]byte" {
+								if len(v.Args) == 1 {
+									if bl, ok := v.Args[0].(*goast.BasicLit); ok && bl.Kind == token.STRING {
+										var sl ast.StringLiteral
+										sl.Type = ft
+										sl.Value, err = strconv.Unquote(bl.Value)
+										p.AddMessage(p.GenerateWarningMessage(err, e))
+										var ex goast.Expr
+										ex, _, err = transpileStringLiteral(p, &sl, true)
+										p.AddMessage(p.GenerateWarningMessage(err, e))
+										resp[fieldPos] = ex
+										fixed = true
+									}
+								}
 							}
 						}
 						if !fixed {
@@ -272,24 +297,6 @@ func transpileInitListExpr(e *ast.InitListExpr, p *program.Program) (
 				}
 			}
 		}
-
-		p.AddMessage(p.GenerateWarningMessage(err, node))
-
-		resp = append(resp, expr)
-	}
-
-	goType, err := types.ResolveType(p, e.Type1)
-	if err != nil {
-		return nil, "", err
-	}
-
-	arrayType, arraySize := types.GetArrayTypeAndSize(e.Type1)
-	if arraySize > 0 {
-		for i := len(resp); i < arraySize; i++ {
-			zero, _ := zeroValue(p, arrayType)
-			resp = append(resp, zero)
-		}
-		exprType = arrayType + "[]"
 	}
 
 	if len(resp) == 1 && goType == "[]byte" {
@@ -297,8 +304,9 @@ func transpileInitListExpr(e *ast.InitListExpr, p *program.Program) (
 	}
 
 	return &goast.CompositeLit{
-		Type: goast.NewIdent(goType),
-		Elts: resp,
+		Lbrace: 1,
+		Type:   goast.NewIdent(goType),
+		Elts:   resp,
 	}, exprType, nil
 }
 
@@ -325,10 +333,12 @@ func zeroValue(p *program.Program, cType string) (zero goast.Expr, zeroType stri
 	case types.IsCPointer(cType, p):
 		zero = goast.NewIdent("nil")
 	case types.IsCArray(cType, p):
-		arr, arrFieldSize := types.GetArrayTypeAndSize(cType)
-		goType, err := types.ResolveType(p, arr)
+		goType, err := types.ResolveType(p, cType)
 		p.AddMessage(p.GenerateWarningMessage(err, nil))
-		zero = goast.NewIdent(fmt.Sprintf("[%d]%s{}", arrFieldSize, goType))
+		zero = &goast.CompositeLit{
+			Lbrace: 1,
+			Type:   goast.NewIdent(goType),
+		}
 	default:
 		zero = goast.NewIdent("0")
 	}


Test patch:
diff --git a/tests/init.c b/tests/init.c
index 3c455f2..be5f588 100644
--- a/tests/init.c
+++ b/tests/init.c
@@ -301,9 +301,15 @@ void test_vti()
 	is_eq(aqq[0][0], 5);
 }
 
+void test_bm()
+{
+	char brac[4][3][2] = { {"1", "2"} };
+	is_streq(brac[0][1], "2");
+}
+
 int main()
 {
-    plan(147);
+    plan(148);
 
 	START_TEST(partly);
 
@@ -326,6 +332,7 @@ int main()
 	START_TEST(poz);
 	START_TEST(ab);
 	START_TEST(vti);
+	START_TEST(bm);
 	
     done_testing();
 }