Repository: Konstantin8105/c4go
Pull request #304 (2019-03-06 14:37:36)

added comments for transpiled structs, types


Patch:
diff --git a/transpiler/switch.go b/transpiler/switch.go
index cccbfde..69abe57 100644
--- a/transpiler/switch.go
+++ b/transpiler/switch.go
@@ -39,7 +39,8 @@ func transpileSwitchStmt(n *ast.SwitchStmt, p *program.Program) (
 
 	// The condition is the expression to be evaulated against each of the
 	// cases.
-	condition, conditionType, newPre, newPost, err := transpileToExpr(n.Children()[len(n.Children())-2], p, false)
+	condition, conditionType, newPre, newPost, err := atomicOperation(
+		n.Children()[len(n.Children())-2], p)
 	if err != nil {
 		return nil, nil, nil, err
 	}
diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go
index 8714811..b0cc70f 100644
--- a/transpiler/transpiler.go
+++ b/transpiler/transpiler.go
@@ -504,28 +504,95 @@ func transpileToNode(node ast.Node, p *program.Program) (
 		}
 	}
 
-	switch n := node.(type) {
-	case *ast.FunctionDecl:
-		com := p.GetComments(node.Position())
-		decls, err = transpileFunctionDecl(n, p)
-		if len(decls) > 0 {
-			if _, ok := decls[0].(*goast.FuncDecl); ok {
-				decls[0].(*goast.FuncDecl).Doc = p.GetMessageComments()
-				decls[0].(*goast.FuncDecl).Doc.List =
-					append(decls[0].(*goast.FuncDecl).Doc.List,
-						com...)
+	defer func() {
+		if len(decls) > 0 && err == nil {
+			for i := range decls {
+				if decls[i] == nil {
+					continue
+				}
+
+				var (
+					doc   *goast.CommentGroup
+					name  string
+					found bool
+				)
+
+				if p.Function != nil {
+					continue
+				}
+
+				switch decls[i].(type) {
+				case *goast.GenDecl:
+					if decls[i].(*goast.GenDecl).Doc == nil {
+						decls[i].(*goast.GenDecl).Doc = &goast.CommentGroup{}
+					}
+					doc = decls[i].(*goast.GenDecl).Doc
+					found = true
+
+					// try to find name
+					name = "c4go_name_is_not_found"
+					specs := decls[i].(*goast.GenDecl).Specs
+					if len(specs) > 0 {
+						switch v := specs[0].(type) {
+						case *goast.TypeSpec:
+							if v.Name != nil {
+								name = v.Name.Name
+							}
+
+						case *goast.ValueSpec:
+							if len(v.Names) > 0 {
+								if v.Names[0] != nil {
+									name = v.Names[0].Name
+								}
+							}
+
+						default:
+							// ignored
+						}
+					}
+
+				case *goast.FuncDecl:
+					if decls[i].(*goast.FuncDecl).Doc == nil {
+						decls[i].(*goast.FuncDecl).Doc = &goast.CommentGroup{}
+					}
+					if decls[i].(*goast.FuncDecl).Name == nil {
+						decls[i].(*goast.FuncDecl).Name = goast.NewIdent("c4go_noname")
+					}
+					doc = decls[i].(*goast.FuncDecl).Doc
+					name = decls[i].(*goast.FuncDecl).Name.Name
+					found = true
+
+				default:
+					// ignore that goast.Decl
+					found = false
+					continue
+				}
+
+				if !found {
+					continue
+				}
+
+				com := p.GetComments(node.Position())
+				msg := p.GetMessageComments().List
+				doc.List = append(doc.List, com...)
+				doc.List = append(doc.List, msg...)
 
 				// location of file
 				location := node.Position().GetSimpleLocation()
 				location = strings.Replace(location, os.Getenv("GOPATH"), "$GOPATH", -1)
-				decls[0].(*goast.FuncDecl).Doc.List =
-					append([]*goast.Comment{{
-						Text: fmt.Sprintf("// %s - transpiled function from %s",
-							decls[0].(*goast.FuncDecl).Name.Name,
-							location),
-					}}, decls[0].(*goast.FuncDecl).Doc.List...)
+				doc.List = append([]*goast.Comment{{
+					Text: fmt.Sprintf("// %s - transpiled function from %s",
+						name, location),
+				}}, doc.List...)
+
+				break
 			}
 		}
+	}()
+
+	switch n := node.(type) {
+	case *ast.FunctionDecl:
+		decls, err = transpileFunctionDecl(n, p)
 
 	case *ast.CXXRecordDecl:
 		if !strings.Contains(n.RecordDecl.Kind, "class") {


Test patch:
diff --git a/tests/switch.c b/tests/switch.c
index 2540308..cee5712 100644
--- a/tests/switch.c
+++ b/tests/switch.c
@@ -346,9 +346,27 @@ void switch_char()
     is_eq(form, 'S');
 }
 
+int value()
+{
+	return 42;
+}
+
+void switch_stat()
+{
+	int v = 15;
+	switch(v = value()) {
+	case 15:
+		fail("wrong");
+		break;
+	case 42: 
+		is_true(v == 42);
+		break;
+	}
+}
+
 int main()
 {
-    plan(29);
+    plan(30);
 
     switch_char();
     switch_bool();
@@ -371,6 +389,7 @@ int main()
     empty_switch();
     default_only_switch();
     switch_without_input();
+	switch_stat();
 
     done_testing();
 }