Repository: Konstantin8105/c4go
Pull request #393 (2019-04-05 19:10:50)

enum: anonymous enum in struct

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

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

```diff
@@            Coverage Diff             @@
##           master     #393      +/-   ##
==========================================
+ Coverage   85.51%   85.52%   +0.01%     
==========================================
  Files         188      188              
  Lines       11037    11093      +56     
==========================================
+ Hits         9438     9487      +49     
- Misses       1140     1146       +6     
- Partials      459      460       +1
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/393?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [util/util.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dXRpbC91dGlsLmdv) | `81.77% <100%> (ø)` | :arrow_up: |
| [types/cast.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHlwZXMvY2FzdC5nbw==) | `69.4% <100%> (ø)` | :arrow_up: |
| [transpiler/binary.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9iaW5hcnkuZ28=) | `68.53% <100%> (+0.19%)` | :arrow_up: |
| [transpiler/operators.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9vcGVyYXRvcnMuZ28=) | `80.92% <100%> (+0.06%)` | :arrow_up: |
| [transpiler/declarations.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9kZWNsYXJhdGlvbnMuZ28=) | `71.73% <100%> (ø)` | :arrow_up: |
| [types/sizeof.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHlwZXMvc2l6ZW9mLmdv) | `71.31% <100%> (ø)` | :arrow_up: |
| [noarch/stdlib.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-bm9hcmNoL3N0ZGxpYi5nbw==) | `90.6% <0%> (-0.51%)` | :arrow_down: |
| [transpiler/bind.go](https://codecov.io/gh/Konstantin8105/c4go/pull/393/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9iaW5kLmdv) | `74.9% <0%> (+3.14%)` | :arrow_up: |

------

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


Patch:
diff --git a/.travis.yml b/.travis.yml
index e1bbc23..3ce1106 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -54,7 +54,7 @@ before_install:
         sudo apt-get install lzip ## Added for scripts/ed.sh
         sudo apt-get update
         sudo apt-cache search clang
-        sudo apt-get install -f -y --force-yes clang-$CLANG lldb-$CLANG
+        sudo apt-get install -f -y --force-yes clang-$CLANG lldb-$CLANG libmad0-dev
       # else
       #   brew install llvm
       fi
diff --git a/scripts/scripts.txt b/scripts/scripts.txt
index d8fd38f..92e6591 100644
--- a/scripts/scripts.txt
+++ b/scripts/scripts.txt
@@ -6,13 +6,13 @@ Calculate warnings : ./testdata/9wm
 
 Calculate warnings : ./testdata/ed
 		After transpiling : 0 warnings.
-		Go build : 2 warnings
+		Go build : 17 warnings
 		Unsafe   : 170
 
 Calculate warnings in file: ./testdata/lang/lang.go
-		After transpiling : 310 warnings.
-		Go build : 2 warnings
-		Unsafe   : 1081
+		After transpiling : 154 warnings.
+		Go build : 1367 warnings
+		Unsafe   : 1179
 
 Calculate warnings in file: ./testdata/kilo/kilo.go
 		After transpiling : 0 warnings.
@@ -31,7 +31,7 @@ Calculate warnings in file: ./testdata/neateqn/neateqn.go
 
 Calculate warnings in file: ./testdata/neatmkfn/neatmkfn.go
 		After transpiling : 6 warnings.
-		Go build : 275 warnings
+		Go build : 271 warnings
 		Unsafe   : 135
 
 Calculate warnings in file: ./testdata/neatmail/neatmail.go
@@ -51,7 +51,7 @@ Calculate warnings in file: ./testdata/neatrefer/neatrefer.go
 
 Calculate warnings in file: ./testdata/neatroff/neatroff.go
 		After transpiling : 0 warnings.
-		Go build : 26 warnings
+		Go build : 23 warnings
 		Unsafe   : 254
 
 Calculate warnings in file: ./testdata/neatvi/neatvi.go
@@ -61,7 +61,7 @@ Calculate warnings in file: ./testdata/neatvi/neatvi.go
 
 Calculate warnings in file: ./testdata/triangle/triangle.go
 		After transpiling : 2 warnings.
-		Go build : 589 warnings
+		Go build : 585 warnings
 		Unsafe   : 1411
 
 Calculate warnings in file: ./testdata/SQLITE/sqlite.go
@@ -71,6 +71,6 @@ Calculate warnings in file: ./testdata/SQLITE/sqlite.go
 
 Calculate warnings in file: testdata/vorbis/vorbis.go
 		After transpiling : 4 warnings.
-		Go build : 25 warnings
+		Go build : 21 warnings
 		Unsafe   : 255
 
diff --git a/transpiler/binary.go b/transpiler/binary.go
index 48dfca1..e3b7ffc 100644
--- a/transpiler/binary.go
+++ b/transpiler/binary.go
@@ -71,6 +71,8 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program, exprIsSt
 	}()
 
 	operator := getTokenForOperator(n.Operator)
+	n.Type = util.GenerateCorrectType(n.Type)
+	n.Type2 = util.GenerateCorrectType(n.Type2)
 
 	defer func() {
 		if err != nil {
@@ -557,7 +559,7 @@ func transpileBinaryOperator(n *ast.BinaryOperator, p *program.Program, exprIsSt
 	}
 
 	// Enum casting
-	if operator != token.ASSIGN && strings.Contains(rightType, "enum") {
+	if operator != token.ASSIGN && strings.Contains(rightType, "enum ") {
 		right, err = types.CastExpr(p, right, rightType, "int")
 		if err != nil {
 			p.AddMessage(p.GenerateWarningMessage(err, n))
diff --git a/transpiler/declarations.go b/transpiler/declarations.go
index 41a58f0..0405752 100644
--- a/transpiler/declarations.go
+++ b/transpiler/declarations.go
@@ -477,7 +477,7 @@ func transpileTypedefDecl(p *program.Program, n *ast.TypedefDecl) (
 
 	// added for support "typedef enum {...} dd" with empty name of struct
 	// Result in Go: "type dd int"
-	if strings.Contains(n.Type, "enum") {
+	if strings.Contains(n.Type, "enum ") {
 		// Registration new type in program.Program
 		if !p.IsTypeAlreadyDefined(n.Name) {
 			p.DefineType(n.Name)
diff --git a/transpiler/operators.go b/transpiler/operators.go
index 7c1201f..549c088 100644
--- a/transpiler/operators.go
+++ b/transpiler/operators.go
@@ -748,6 +748,9 @@ func atomicOperation(n ast.Node, p *program.Program) (
 			return
 		}
 
+		v.Type = util.GenerateCorrectType(v.Type)
+		v.Type2 = util.GenerateCorrectType(v.Type2)
+
 		// avoid problem :
 		//
 		// constant -1331 overflows uint32
diff --git a/types/cast.go b/types/cast.go
index e4e3503..fadee3f 100644
--- a/types/cast.go
+++ b/types/cast.go
@@ -235,7 +235,7 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
 	}
 
 	// convert enum to int and recursive
-	if strings.Contains(fromType, "enum") && !strings.Contains(toType, "enum") {
+	if strings.Contains(fromType, "enum ") && !strings.Contains(toType, "enum ") {
 		in := goast.CallExpr{
 			Fun: &goast.Ident{
 				Name: "int32",
@@ -253,10 +253,10 @@ func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
 		return CastExpr(p, &in, "int", toType)
 	}
 	// convert int to enum and recursive
-	if !strings.Contains(fromType, "enum") && strings.Contains(toType, "enum") {
+	if !strings.Contains(fromType, "enum ") && strings.Contains(toType, "enum ") {
 		in := goast.CallExpr{
 			Fun: &goast.Ident{
-				Name: strings.TrimSpace(strings.Replace(toType, "enum", "", -1)),
+				Name: strings.TrimSpace(strings.Replace(toType, "enum ", "", -1)),
 			},
 			Lparen: 1,
 			Args: []goast.Expr{
diff --git a/types/sizeof.go b/types/sizeof.go
index cf94587..7c42e10 100644
--- a/types/sizeof.go
+++ b/types/sizeof.go
@@ -50,7 +50,7 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 	}
 
 	// Enum with name
-	if strings.HasPrefix(cType, "enum") {
+	if strings.HasPrefix(cType, "enum ") {
 		return SizeOf(p, "int")
 	}
 
diff --git a/util/util.go b/util/util.go
index c326082..87cf1f1 100644
--- a/util/util.go
+++ b/util/util.go
@@ -383,7 +383,7 @@ func CleanCType(s string) (out string) {
 
 // GenerateCorrectType - generate correct type
 // Example: 'union (anonymous union at tests/union.c:46:3)'
-func GenerateCorrectType(name string) string {
+func GenerateCorrectType(name string) (result string) {
 	if !strings.Contains(name, "anonymous") {
 		return CleanCType(name)
 	}


Test patch:
diff --git a/tests/struct.c b/tests/struct.c
index 2762344..ab5fc8f 100644
--- a/tests/struct.c
+++ b/tests/struct.c
@@ -637,6 +637,7 @@ undo;
 
 void typedef_with_union()
 {
+	diag("typedef with union");
 	undo   u ;
 	(void)(u);
 	u.typet = UMOV;
@@ -645,11 +646,29 @@ void typedef_with_union()
 	int y = 53;
 	u.head = &y;
 	is_eq(*u.head, y);
+
+	undo u2;
+	u2.typet = VMOV;
+	int y2 = 42;
+	u2.head = &y2;
+	is_eq(u2.typet, VMOV);
+	is_eq(*u2.head, y2  );
+
+	undo * u3 = &u2;
+	is_eq((*u3).typet, VMOV);
+	is_eq(*(*u3).head, y2  );
+
+	(*u3).typet = u.typet;
+	is_eq((*u3).typet, UMOV);
+
+	int bd = UDEL;
+	(*u3).typet = bd;
+	is_eq((*u3).typet, UDEL);
 }
 
 int main()
 {
-    plan(102);
+    plan(108);
 
     pointer_arithm_in_struct();
     test_extern_vec();