Repository: Konstantin8105/c4go
Pull request #264 (2019-02-09 14:32:13)

transpileSwitchStmt: add defaultStmt without body

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/264?src=pr&el=h1) Report
> Merging [#264](https://codecov.io/gh/Konstantin8105/c4go/pull/264?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/8f82ecfdf337bb1c9d9d5ce5aa0023dd6e630385?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/264/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/264?src=pr&el=tree)

```diff
@@           Coverage Diff            @@
##           master   #264      +/-   ##
========================================
+ Coverage   86.98%    87%   +0.01%     
========================================
  Files         176    176              
  Lines        9653   9663      +10     
========================================
+ Hits         8397   8407      +10     
  Misses        868    868              
  Partials      388    388
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/264?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/switch.go](https://codecov.io/gh/Konstantin8105/c4go/pull/264/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9zd2l0Y2guZ28=) | `90.1% <100%> (+0.57%)` | :arrow_up: |

------

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


Patch:
diff --git a/transpiler/switch.go b/transpiler/switch.go
index b7eab16..cccbfde 100644
--- a/transpiler/switch.go
+++ b/transpiler/switch.go
@@ -58,27 +58,38 @@ func transpileSwitchStmt(n *ast.SwitchStmt, p *program.Program) (
 	// default: ...
 checkAgain:
 	for i := range body.Children() {
-		if v, ok := body.Children()[i].(*ast.CaseStmt); ok {
-			if vv, ok := v.Children()[len(v.Children())-1].(*ast.CaseStmt); ok {
-				if len(body.Children()) > i+1 {
-					body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
-				} else {
-					up := body.ChildNodes
-					body.ChildNodes = append(up, vv)
-				}
-				v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
-				goto checkAgain
+		found := false
+		if _, ok := body.Children()[i].(*ast.CaseStmt); ok {
+			found = true
+		}
+		if _, ok := body.Children()[i].(*ast.DefaultStmt); ok {
+			found = true
+		}
+
+		if !found {
+			continue
+		}
+
+		v := body.Children()[i]
+		if vv, ok := v.Children()[len(v.Children())-1].(*ast.CaseStmt); ok {
+			if len(body.Children()) > i+1 {
+				body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
+			} else {
+				up := body.ChildNodes
+				body.ChildNodes = append(up, vv)
 			}
-			if vv, ok := v.Children()[len(v.Children())-1].(*ast.DefaultStmt); ok {
-				if len(body.Children()) > i+1 {
-					body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
-				} else {
-					up := body.ChildNodes
-					body.ChildNodes = append(up, vv)
-				}
-				v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
-				goto checkAgain
+			v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
+			goto checkAgain
+		}
+		if vv, ok := v.Children()[len(v.Children())-1].(*ast.DefaultStmt); ok {
+			if len(body.Children()) > i+1 {
+				body.ChildNodes = append(body.ChildNodes[:i+1], append([]ast.Node{vv}, body.ChildNodes[i+1:]...)...)
+			} else {
+				up := body.ChildNodes
+				body.ChildNodes = append(up, vv)
 			}
+			v.Children()[len(v.Children())-1] = &ast.CompoundStmt{}
+			goto checkAgain
 		}
 	}
 
@@ -325,7 +336,12 @@ func appendCaseOrDefaultToNormalizedCases(cases []*goast.CaseClause,
 }
 
 func transpileCaseStmt(n *ast.CaseStmt, p *program.Program) (
-	*goast.CaseClause, []goast.Stmt, []goast.Stmt, error) {
+	_ *goast.CaseClause, _ []goast.Stmt, _ []goast.Stmt, err error) {
+	defer func() {
+		if err != nil {
+			err = fmt.Errorf("Cannot transpileCaseStmt: %v", err)
+		}
+	}()
 	preStmts := []goast.Stmt{}
 	postStmts := []goast.Stmt{}
 
@@ -337,7 +353,6 @@ func transpileCaseStmt(n *ast.CaseStmt, p *program.Program) (
 		c, err = types.CastExpr(p, c, cType, "int")
 		p.AddMessage(p.GenerateWarningMessage(err, n))
 	}
-
 	preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
 
 	stmts, err := transpileStmts(n.Children()[1:], p)


Test patch:
diff --git a/tests/switch.c b/tests/switch.c
index ea4e326..6b1155e 100644
--- a/tests/switch.c
+++ b/tests/switch.c
@@ -333,10 +333,26 @@ void switch_bool()
     }
 }
 
+void switch_char()
+{
+	char form = '1';
+	switch (form) {
+	default:
+	case '1':
+		form = 'S';
+		break;
+	case 0:
+		;
+	};
+	is_eq(form,'S');
+}
+
+
 int main()
 {
-    plan(28);
+    plan(29);
 
+	switch_char();
     switch_bool();
     match_a_single_case();
     fallthrough_to_next_case();