Repository: elliotchance/c2go (go)

Issue:
Empty init functions still get generated
These do nothing and only add bloat to generated output.

Pull request description:
Only generate init when there are startup lines
Fixes #864

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/c2go/865)
<!-- Reviewable:end -->


Patch:
diff --git a/ast/asm_label_attr.go b/ast/asm_label_attr.go
index 81566d5..55e2420 100644
--- a/ast/asm_label_attr.go
+++ b/ast/asm_label_attr.go
@@ -2,27 +2,30 @@ package ast
 
 // AsmLabelAttr is a type of attribute for assembler label
 type AsmLabelAttr struct {
-	Addr         Address
-	Pos          Position
-	Inherited    bool
-	FunctionName string
-	ChildNodes   []Node
+	Addr           Address
+	Pos            Position
+	Inherited      bool
+	FunctionName   string
+	ChildNodes     []Node
+	IsLiteralLabel bool
 }
 
 func parseAsmLabelAttr(line string) *AsmLabelAttr {
 	groups := groupsFromRegex(
 		`<(?P<position>.*)>
 		(?P<inherited> Inherited)?
-		 "(?P<function>.+)"`,
+		 "(?P<function>.+)"
+		(?P<literal> IsLiteralLabel)?`,
 		line,
 	)
 
 	return &AsmLabelAttr{
-		Addr:         ParseAddress(groups["address"]),
-		Pos:          NewPositionFromString(groups["position"]),
-		Inherited:    len(groups["inherited"]) > 0,
-		FunctionName: groups["function"],
-		ChildNodes:   []Node{},
+		Addr:           ParseAddress(groups["address"]),
+		Pos:            NewPositionFromString(groups["position"]),
+		Inherited:      len(groups["inherited"]) > 0,
+		FunctionName:   groups["function"],
+		ChildNodes:     []Node{},
+		IsLiteralLabel: len(groups["literal"]) > 0,
 	}
 }
 
diff --git a/ast/warn_unused_result_attr.go b/ast/warn_unused_result_attr.go
index 7a46b29..a64c6df 100644
--- a/ast/warn_unused_result_attr.go
+++ b/ast/warn_unused_result_attr.go
@@ -10,7 +10,7 @@ type WarnUnusedResultAttr struct {
 }
 
 func parseWarnUnusedResultAttr(line string) *WarnUnusedResultAttr {
-	groups := groupsFromRegex(`<(?P<position>.*)>(?P<inherited> Inherited)?( warn_unused_result)?`, line)
+	groups := groupsFromRegex(`<(?P<position>.*)>(?P<inherited> Inherited)?( warn_unused_result)?( "")?`, line)
 
 	return &WarnUnusedResultAttr{
 		Addr:       ParseAddress(groups["address"]),
diff --git a/transpiler/transpiler.go b/transpiler/transpiler.go
index 8c71248..28a4a1e 100644
--- a/transpiler/transpiler.go
+++ b/transpiler/transpiler.go
@@ -80,15 +80,17 @@ func TranspileAST(fileName, packageName string, p *program.Program, root ast.Nod
 		})
 	}
 
-	// Now we need to build the __init() function. This sets up certain state
-	// and variables that the runtime expects to be ready.
-	p.File.Decls = append(p.File.Decls, &goast.FuncDecl{
-		Name: goast.NewIdent("init"),
-		Type: util.NewFuncType(&goast.FieldList{}, "", false),
-		Body: &goast.BlockStmt{
-			List: p.StartupStatements(),
-		},
-	})
+	if list := p.StartupStatements(); len(list) > 0 {
+		// Now we need to build the init() function. This sets up certain state
+		// and variables that the runtime expects to be ready.
+		p.File.Decls = append(p.File.Decls, &goast.FuncDecl{
+			Name: goast.NewIdent("init"),
+			Type: util.NewFuncType(&goast.FieldList{}, "", false),
+			Body: &goast.BlockStmt{
+				List: list,
+			},
+		})
+	}
 
 	// only for "stdbool.h"
 	if p.IncludeHeaderIsExists("stdbool.h") {


Test patch:
diff --git a/ast/asm_label_attr_test.go b/ast/asm_label_attr_test.go
index 83775c9..3b6dc23 100644
--- a/ast/asm_label_attr_test.go
+++ b/ast/asm_label_attr_test.go
@@ -7,18 +7,28 @@ import (
 func TestAsmLabelAttr(t *testing.T) {
 	nodes := map[string]Node{
 		`0x7ff26d8224e8 </usr/include/sys/cdefs.h:569:36> "_fopen"`: &AsmLabelAttr{
-			Addr:         0x7ff26d8224e8,
-			Pos:          NewPositionFromString("/usr/include/sys/cdefs.h:569:36"),
-			Inherited:    false,
-			FunctionName: "_fopen",
-			ChildNodes:   []Node{},
+			Addr:           0x7ff26d8224e8,
+			Pos:            NewPositionFromString("/usr/include/sys/cdefs.h:569:36"),
+			Inherited:      false,
+			FunctionName:   "_fopen",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: false,
 		},
 		`0x7fd55a169318 </usr/include/stdio.h:325:47> Inherited "_popen"`: &AsmLabelAttr{
-			Addr:         0x7fd55a169318,
-			Pos:          NewPositionFromString("/usr/include/stdio.h:325:47"),
-			Inherited:    true,
-			FunctionName: "_popen",
-			ChildNodes:   []Node{},
+			Addr:           0x7fd55a169318,
+			Pos:            NewPositionFromString("/usr/include/stdio.h:325:47"),
+			Inherited:      true,
+			FunctionName:   "_popen",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: false,
+		},
+		`0x559fea32f5f0 <line:407:94> "__isoc99_fscanf" IsLiteralLabel`: &AsmLabelAttr{
+			Addr:           0x559fea32f5f0,
+			Pos:            NewPositionFromString("line:407:94"),
+			Inherited:      false,
+			FunctionName:   "__isoc99_fscanf",
+			ChildNodes:     []Node{},
+			IsLiteralLabel: true,
 		},
 	}
 
diff --git a/ast/warn_unused_result_attr_test.go b/ast/warn_unused_result_attr_test.go
index 30f8a8a..95fb662 100644
--- a/ast/warn_unused_result_attr_test.go
+++ b/ast/warn_unused_result_attr_test.go
@@ -24,6 +24,12 @@ func TestWarnUnusedResultAttr(t *testing.T) {
 			Inherited:  true,
 			ChildNodes: []Node{},
 		},
+		`0x55c2df4f93b8 <col:53> warn_unused_result ""`: &WarnUnusedResultAttr{
+			Addr:       0x55c2df4f93b8,
+			Pos:        NewPositionFromString("col:53"),
+			Inherited:  false,
+			ChildNodes: []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)